1 16 package org.apache.juddi.function; 17 18 import org.apache.commons.logging.Log; 19 import org.apache.commons.logging.LogFactory; 20 import org.apache.juddi.auth.Authenticator; 21 import org.apache.juddi.auth.AuthenticatorFactory; 22 import org.apache.juddi.datastore.DataStore; 23 import org.apache.juddi.datastore.DataStoreFactory; 24 import org.apache.juddi.datatype.RegistryObject; 25 import org.apache.juddi.datatype.publisher.Publisher; 26 import org.apache.juddi.datatype.request.AuthInfo; 27 import org.apache.juddi.datatype.request.GetAuthToken; 28 import org.apache.juddi.datatype.response.AuthToken; 29 import org.apache.juddi.error.RegistryException; 30 import org.apache.juddi.error.UnknownUserException; 31 import org.apache.juddi.registry.RegistryEngine; 32 import org.apache.juddi.util.Config; 33 34 37 public class GetAuthTokenFunction extends AbstractFunction 38 { 39 private static Log log = LogFactory.getLog(GetAuthTokenFunction.class); 41 42 45 public GetAuthTokenFunction(RegistryEngine registry) 46 { 47 super(registry); 48 } 49 50 53 public RegistryObject execute(RegistryObject regObject) 54 throws RegistryException 55 { 56 GetAuthToken request = (GetAuthToken)regObject; 57 String generic = request.getGeneric(); 58 String userID = request.getUserID(); 59 String cred = request.getCredential(); 60 61 DataStore dataStore = DataStoreFactory.getDataStore(); 63 64 Authenticator authenticator = AuthenticatorFactory.getAuthenticator(); 66 67 try 68 { 69 dataStore.beginTrans(); 71 72 String publisherID = authenticator.authenticate(userID,cred); 74 if (publisherID == null) 75 throw new UnknownUserException("get_authToken: "+ 76 "userID="+userID); 77 78 Publisher publisher = dataStore.getPublisher(publisherID); 80 if (publisher == null) 81 throw new UnknownUserException("get_authToken: "+ 82 "userID="+userID); 83 84 String token = dataStore.generateToken(publisher); 86 87 dataStore.storeAuthToken(token,publisher); 89 90 dataStore.commit(); 92 93 AuthToken authToken = new AuthToken(); 95 authToken.setGeneric(generic); 96 authToken.setOperator(Config.getOperator()); 97 authToken.setAuthInfo(new AuthInfo(token)); 98 return authToken; 99 } 100 catch(UnknownUserException ukuex) 101 { 102 try { dataStore.rollback(); } catch(Exception e) { } 103 log.info(ukuex.getMessage()); 104 throw (RegistryException)ukuex; 105 } 106 catch(RegistryException regex) 107 { 108 try { dataStore.rollback(); } catch(Exception e) { } 109 log.error(regex); 110 throw (RegistryException)regex; 111 } 112 catch(Exception ex) 113 { 114 try { dataStore.rollback(); } catch(Exception e) { } 115 log.error(ex); 116 throw new RegistryException(ex); 117 } 118 finally 119 { 120 if (dataStore != null) 121 dataStore.release(); 122 } 123 } 124 125 126 127 128 129 130 131 public static void main(String [] args) 132 { 133 RegistryEngine reg = new RegistryEngine(); 135 reg.init(); 136 137 try 138 { 139 GetAuthToken request = new GetAuthToken("sviens","password"); 141 AuthToken response = (AuthToken)(new GetAuthTokenFunction(reg).execute(request)); 142 System.out.println("Function: getAuthToken(sviens/password)"); 143 System.out.println(" AuthInfo: "+response.getAuthInfo()); 144 145 request = new GetAuthToken("jdoe","password"); 147 System.out.println("Function: getAuthToken(jdoe/password)"); 148 response = (AuthToken)(new GetAuthTokenFunction(reg).execute(request)); 149 System.out.println(" AuthInfo: "+response.getAuthInfo()); 150 151 request = new GetAuthToken("guest","password"); 153 System.out.println("Function: getAuthToken(guest/password)"); 154 response = (AuthToken)(new GetAuthTokenFunction(reg).execute(request)); 155 System.out.println(" AuthInfo: "+response.getAuthInfo()); 156 } 157 catch (Exception ex) 158 { 159 ex.printStackTrace(); 161 } 162 finally 163 { 164 reg.dispose(); 166 } 167 } 168 } | Popular Tags |