View Javadoc

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.datastore.DataStore;
21  import org.apache.juddi.datastore.DataStoreFactory;
22  import org.apache.juddi.datatype.RegistryObject;
23  import org.apache.juddi.datatype.publisher.Publisher;
24  import org.apache.juddi.datatype.request.AuthInfo;
25  import org.apache.juddi.datatype.request.DiscardAuthToken;
26  import org.apache.juddi.datatype.request.GetAuthToken;
27  import org.apache.juddi.datatype.response.AuthToken;
28  import org.apache.juddi.datatype.response.DispositionReport;
29  import org.apache.juddi.datatype.response.Result;
30  import org.apache.juddi.error.AuthTokenRequiredException;
31  import org.apache.juddi.error.RegistryException;
32  import org.apache.juddi.registry.RegistryEngine;
33  import org.apache.juddi.util.Config;
34  
35  /***
36   * @author Steve Viens (sviens@apache.org)
37   */
38  public class DiscardAuthTokenFunction extends AbstractFunction
39  {
40    // private reference to jUDDI logger
41    private static Log log = LogFactory.getLog(DiscardAuthTokenFunction.class);
42  
43    /***
44     *
45     */
46    public DiscardAuthTokenFunction(RegistryEngine registry)
47    {
48      super(registry);
49    }
50  
51    /***
52     *
53     */
54    public RegistryObject execute(RegistryObject regObject)
55      throws RegistryException
56    {
57      // extract individual parameters
58      DiscardAuthToken request = (DiscardAuthToken)regObject;
59      String generic = request.getGeneric();
60      AuthInfo authInfo = request.getAuthInfo();
61  
62      // aquire a jUDDI datastore instance
63      DataStore dataStore = DataStoreFactory.getDataStore();
64  
65      try
66      {
67        dataStore.beginTrans();
68  
69        // validates authentication parameters
70        Publisher publisher = getPublisher(authInfo,dataStore);
71        String publisherID = publisher.getPublisherID();
72  
73        String authToken = authInfo.getValue();
74        if ((authInfo == null) || (authInfo.getValue() == null))
75          throw new AuthTokenRequiredException("discard_authToken: "+
76              "authInfo="+authInfo);
77  
78        dataStore.retireAuthToken(authToken);
79        dataStore.commit();
80  
81        log.info("Publisher '"+publisherID+"' has discarded AuthToken: "+authToken);
82      }
83      catch(AuthTokenRequiredException authex)
84      {
85        try { dataStore.rollback(); } catch(Exception e) { }
86        log.info(authex.getMessage());
87        throw (RegistryException)authex;
88      }
89      catch(RegistryException regex)
90      {
91        try { dataStore.rollback(); } catch(Exception e) { }
92        log.error(regex);
93        throw (RegistryException)regex;
94      }
95      catch(Exception ex)
96      {
97        try { dataStore.rollback(); } catch(Exception e) { }
98        log.error(ex);
99        throw new RegistryException(ex);
100     }
101     finally
102     {
103       if (dataStore != null)
104         dataStore.release();
105     }
106 
107     // We didn't encounter any problems so let's create an
108     // E_SUCCESS Result, embed it in a DispositionReport
109     // and return it.
110     Result result = new Result(Result.E_SUCCESS);
111     result.setErrCode(Result.lookupErrCode(Result.E_SUCCESS));    
112     DispositionReport dispRpt = new DispositionReport();
113     dispRpt.setGeneric(generic);
114     dispRpt.setOperator(Config.getOperator());
115     dispRpt.addResult(result);
116     
117     return dispRpt;
118   }
119 
120 
121   /****************************************************************************/
122   /****************************** TEST DRIVER *********************************/
123   /****************************************************************************/
124 
125 
126   public static void main(String[] args)
127   {
128     // initialize the registry
129     RegistryEngine reg = new RegistryEngine();
130     reg.init();
131 
132     try
133     {
134       // generate the request object
135       GetAuthToken getRequest = new GetAuthToken("sviens","password");
136 
137       // invoke the server
138       AuthToken getResponse = (AuthToken)(new GetAuthTokenFunction(reg).execute(getRequest));
139 
140       // create a request
141       DiscardAuthToken discardRequest1 = new DiscardAuthToken(getResponse.getAuthInfo());
142       // invoke the server with a valid AuthToken value
143       DispositionReport discardResponse = (DispositionReport)(new DiscardAuthTokenFunction(reg).execute(discardRequest1));
144       System.out.println("errno: "+discardResponse.toString());
145 
146       // create a request
147       DiscardAuthToken discardRequest2 = new DiscardAuthToken();
148       discardRequest2.setAuthInfo(new AuthInfo("**-BadAuthToken-**"));
149       // invoke the server with an invalid AuthToken value
150       DispositionReport discardResponse2 = (DispositionReport)(new DiscardAuthTokenFunction(reg).execute(discardRequest2));
151       System.out.println("errno: "+discardResponse2.toString());
152     }
153     catch (Exception ex)
154     {
155       // write execption to the console
156       ex.printStackTrace();
157     }
158     finally
159     {
160       // destroy the registry
161       reg.dispose();
162     }
163   }
164 }