KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > juddi > function > GetAuthTokenFunction


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
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   // private reference to jUDDI Logger
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 JavaDoc generic = request.getGeneric();
58     String JavaDoc userID = request.getUserID();
59     String JavaDoc cred = request.getCredential();
60
61     // aquire a jUDDI datastore instance
62
DataStore dataStore = DataStoreFactory.getDataStore();
63
64     // aquire a jUDDI Authenticator instance
65
Authenticator authenticator = AuthenticatorFactory.getAuthenticator();
66
67     try
68     {
69       // begin this transaction
70
dataStore.beginTrans();
71
72       // authenticate the requestor's credentials
73
String JavaDoc publisherID = authenticator.authenticate(userID,cred);
74       if (publisherID == null)
75         throw new UnknownUserException("get_authToken: "+
76             "userID="+userID);
77
78       // ensure the user has the authority to publish
79
Publisher publisher = dataStore.getPublisher(publisherID);
80       if (publisher == null)
81         throw new UnknownUserException("get_authToken: "+
82             "userID="+userID);
83
84       // generate a new token (optionally using publisher info)
85
String JavaDoc token = dataStore.generateToken(publisher);
86
87       // save auth token value to persistent storage
88
dataStore.storeAuthToken(token,publisher);
89
90       // commit this transaction
91
dataStore.commit();
92
93       // create, populate and return an AuthToken object
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 JavaDoc e) { }
103       log.info(ukuex.getMessage());
104       throw (RegistryException)ukuex;
105     }
106     catch(RegistryException regex)
107     {
108       try { dataStore.rollback(); } catch(Exception JavaDoc e) { }
109       log.error(regex);
110       throw (RegistryException)regex;
111     }
112     catch(Exception JavaDoc ex)
113     {
114       try { dataStore.rollback(); } catch(Exception JavaDoc 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 JavaDoc[] args)
132   {
133     // initialize the registry
134
RegistryEngine reg = new RegistryEngine();
135     reg.init();
136
137     try
138     {
139       // valid request
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       // invalid (unknown user) request
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       // invalid (invalid credential) request
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 JavaDoc ex)
158     {
159       // write execption to the console
160
ex.printStackTrace();
161     }
162     finally
163     {
164       // destroy the registry
165
reg.dispose();
166     }
167   }
168 }
Popular Tags