1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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 /***
35 * @author Steve Viens (sviens@apache.org)
36 */
37 public class GetAuthTokenFunction extends AbstractFunction
38 {
39
40 private static Log log = LogFactory.getLog(GetAuthTokenFunction.class);
41
42 /***
43 *
44 */
45 public GetAuthTokenFunction(RegistryEngine registry)
46 {
47 super(registry);
48 }
49
50 /***
51 *
52 */
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
62 DataStore dataStore = DataStoreFactory.getDataStore();
63
64
65 Authenticator authenticator = AuthenticatorFactory.getAuthenticator();
66
67 try
68 {
69
70 dataStore.beginTrans();
71
72
73 String publisherID = authenticator.authenticate(userID,cred);
74 if (publisherID == null)
75 throw new UnknownUserException("get_authToken: "+
76 "userID="+userID);
77
78
79 Publisher publisher = dataStore.getPublisher(publisherID);
80 if (publisher == null)
81 throw new UnknownUserException("get_authToken: "+
82 "userID="+userID);
83
84
85 String token = dataStore.generateToken(publisher);
86
87
88 dataStore.storeAuthToken(token,publisher);
89
90
91 dataStore.commit();
92
93
94 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 /****************************** TEST DRIVER *********************************/
128 /****************************************************************************/
129
130
131 public static void main(String[] args)
132 {
133
134 RegistryEngine reg = new RegistryEngine();
135 reg.init();
136
137 try
138 {
139
140 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
146 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
152 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
160 ex.printStackTrace();
161 }
162 finally
163 {
164
165 reg.dispose();
166 }
167 }
168 }