KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis > handlers > http > HTTPAuthHandler


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
17 package org.apache.axis.handlers.http;
18
19 import org.apache.axis.AxisFault;
20 import org.apache.axis.MessageContext;
21 import org.apache.axis.components.logger.LogFactory;
22 import org.apache.axis.encoding.Base64;
23 import org.apache.axis.handlers.BasicHandler;
24 import org.apache.axis.transport.http.HTTPConstants;
25 import org.apache.axis.utils.Messages;
26 import org.apache.commons.logging.Log;
27
28
29 /** An <code>HTTPAuthHandler</code> simply sets the context's username
30  * and password properties from the HTTP auth headers.
31  *
32  * @author Glen Daniels (gdaniels@allaire.com)
33  * @author Doug Davis (dug@us.ibm.com)
34  */

35 public class HTTPAuthHandler extends BasicHandler
36 {
37     protected static Log log =
38         LogFactory.getLog(HTTPAuthHandler.class.getName());
39
40     public void invoke(MessageContext msgContext) throws AxisFault
41     {
42         log.debug("Enter: HTTPAuthHandler::invoke");
43         
44         /* Process the Basic Auth stuff in the headers */
45         /***********************************************/
46         String JavaDoc tmp = (String JavaDoc)msgContext.getProperty(HTTPConstants.HEADER_AUTHORIZATION);
47         if ( tmp != null ) tmp = tmp.trim();
48         if ( tmp != null && tmp.startsWith("Basic ") ) {
49             String JavaDoc user=null ;
50             int i ;
51
52             tmp = new String JavaDoc( Base64.decode( tmp.substring(6) ) );
53             i = tmp.indexOf( ':' );
54             if ( i == -1 ) user = tmp ;
55             else user = tmp.substring( 0, i);
56             msgContext.setUsername( user );
57             log.debug( Messages.getMessage("httpUser00", user) );
58             if ( i != -1 ) {
59                 String JavaDoc pwd = tmp.substring(i+1);
60                 if ( pwd != null && pwd.equals("") ) pwd = null ;
61                 if ( pwd != null ) {
62                     msgContext.setPassword( pwd );
63                     log.debug( Messages.getMessage("httpPassword00", pwd) );
64                 }
65             }
66         }
67
68         log.debug("Exit: HTTPAuthHandler::invoke");
69     }
70 }
71
Popular Tags