KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > webservice > util > AuthenticationUtils


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.webservice.util;
18
19 import java.io.ByteArrayInputStream JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.rmi.RemoteException JavaDoc;
22
23 import javax.security.auth.callback.Callback JavaDoc;
24 import javax.security.auth.callback.CallbackHandler JavaDoc;
25 import javax.security.auth.callback.UnsupportedCallbackException JavaDoc;
26
27 import org.alfresco.webservice.authentication.AuthenticationFault;
28 import org.alfresco.webservice.authentication.AuthenticationResult;
29 import org.apache.axis.EngineConfiguration;
30 import org.apache.axis.configuration.FileProvider;
31 import org.apache.ws.security.WSPasswordCallback;
32
33 /**
34  * @author Roy Wetherall
35  */

36 public class AuthenticationUtils implements CallbackHandler JavaDoc
37 {
38     /** WS security information */
39     private static final String JavaDoc WS_SECURITY_INFO =
40          "<deployment xmlns='http://xml.apache.org/axis/wsdd/' xmlns:java='http://xml.apache.org/axis/wsdd/providers/java'>" +
41          " <transport name='http' pivot='java:org.apache.axis.transport.http.HTTPSender'/>" +
42          " <globalConfiguration >" +
43          " <requestFlow >" +
44          " <handler type='java:org.apache.ws.axis.security.WSDoAllSender' >" +
45          " <parameter name='action' value='UsernameToken Timestamp'/>" +
46          " <parameter name='user' value='ticket'/>" +
47          " <parameter name='passwordCallbackClass' value='org.alfresco.webservice.util.AuthenticationUtils'/>" +
48          " <parameter name='passwordType' value='PasswordText'/>" +
49          " </handler>" +
50          " </requestFlow >" +
51          " </globalConfiguration>" +
52          "</deployment>";
53     
54     /** Thread local containing the current ticket */
55     private static ThreadLocal JavaDoc<String JavaDoc> currentTicket = new ThreadLocal JavaDoc<String JavaDoc>();
56     
57     /**
58      * Start a session
59      *
60      * @param username
61      * @param password
62      * @throws AuthenticationFault
63      */

64     public static void startSession(String JavaDoc username, String JavaDoc password)
65         throws AuthenticationFault
66     {
67         try
68         {
69             // Start the session
70
AuthenticationResult result = WebServiceFactory.getAuthenticationService().startSession(username, password);
71             
72             // Store the ticket for use later
73
currentTicket.set(result.getTicket());
74         }
75         catch (RemoteException JavaDoc exception)
76         {
77             if (exception instanceof AuthenticationFault)
78             {
79                 // Rethrow the authentication exception
80
throw (AuthenticationFault)exception;
81             }
82             else
83             {
84                 // Throw the exception as a wrapped runtime exception
85
throw new WebServiceException("Error starting session.", exception);
86             }
87         }
88     }
89     
90     /**
91      * Ends the current session
92      */

93     public static void endSession()
94     {
95         String JavaDoc ticket = currentTicket.get();
96         if (ticket != null)
97         {
98             try
99             {
100                 WebServiceFactory.getAuthenticationService().endSession(ticket);
101                 currentTicket.remove();
102             }
103             catch (RemoteException JavaDoc exception)
104             {
105                 exception.printStackTrace();
106                 throw new WebServiceException("Error ending session.", exception);
107             }
108         }
109     }
110     
111     public static String JavaDoc getCurrentTicket()
112     {
113         return currentTicket.get();
114     }
115     
116     /**
117      * The implementation of the passwrod call back used by the WS Security
118      *
119      * @see javax.security.auth.callback.CallbackHandler#handle(javax.security.auth.callback.Callback[])
120      */

121     public void handle(Callback JavaDoc[] callbacks) throws IOException JavaDoc, UnsupportedCallbackException JavaDoc
122     {
123        for (int i = 0; i < callbacks.length; i++)
124        {
125           if (callbacks[i] instanceof WSPasswordCallback)
126           {
127              WSPasswordCallback pc = (WSPasswordCallback)callbacks[i];
128              pc.setPassword(currentTicket.get());
129           }
130           else
131           {
132              throw new UnsupportedCallbackException JavaDoc(callbacks[i], "Unrecognized Callback");
133           }
134        }
135     }
136     
137     /**
138      * Gets the engine configuration used to create the web service references
139      *
140      * @return
141      */

142     public static EngineConfiguration getEngineConfiguration()
143     {
144         return new FileProvider(new ByteArrayInputStream JavaDoc(WS_SECURITY_INFO.getBytes()));
145     }
146 }
147
Popular Tags