KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > webservice > handler > ServerLoginHandler


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7
8 // $Id: ServerLoginHandler.java,v 1.1.2.3 2005/04/12 16:07:45 starksm Exp $
9
package org.jboss.webservice.handler;
10
11 // $Id: ServerLoginHandler.java,v 1.1.2.3 2005/04/12 16:07:45 starksm Exp $
12

13 import org.jboss.axis.AxisFault;
14 import org.jboss.axis.MessageContext;
15 import org.jboss.axis.encoding.Base64;
16 import org.jboss.axis.handlers.BasicHandler;
17 import org.jboss.logging.Logger;
18 import org.jboss.security.SecurityAssociation;
19 import org.jboss.security.SimplePrincipal;
20 import org.jboss.webservice.Constants;
21
22 import javax.xml.soap.SOAPException JavaDoc;
23 import javax.xml.soap.SOAPHeader JavaDoc;
24 import javax.xml.soap.SOAPHeaderElement JavaDoc;
25 import javax.xml.soap.SOAPMessage JavaDoc;
26 import java.util.Iterator JavaDoc;
27
28 /**
29  * An Axis style handler that gets username/password from SOAP header elements.
30  *
31  * <soapenv:Header>
32  * <jbws:username actor="http://webservice.jboss.com/ws4ee/login" xmlns:jbws="http://webservice.jboss.com/ws4ee">kermit</jbws:username>
33  * <jbws:password actor="http://webservice.jboss.com/ws4ee/login" xmlns:jbws="http://webservice.jboss.com/ws4ee">thefrog</jbws:password>
34  * </soapenv:Header>
35  *
36  *
37  * @author Thomas.Diesler@jboss.org
38  * @since 27-April-2004
39  */

40 public class ServerLoginHandler extends BasicHandler
41 {
42    /** @since 4.0.2 */
43    static final long serialVersionUID = 8239981936517243635L;
44
45    // provide logging
46
private static final Logger log = Logger.getLogger(ServerLoginHandler.class);
47
48    /** Extract the username/password and set principal/credential in the SecurityAssociation.
49     */

50    public void invoke(MessageContext msgContext) throws AxisFault
51    {
52       try
53       {
54          SOAPMessage JavaDoc soapMessage = msgContext.getMessage();
55          SOAPHeader JavaDoc soapHeader = soapMessage.getSOAPPart().getEnvelope().getHeader();
56          Iterator JavaDoc it = soapHeader.extractHeaderElements(Constants.NAMESPACE + "/login");
57          while (it.hasNext())
58          {
59             SOAPHeaderElement JavaDoc element = (SOAPHeaderElement JavaDoc)it.next();
60             String JavaDoc localName = element.getElementName().getLocalName();
61             String JavaDoc value = element.getValue();
62             if (localName.equals("username"))
63             {
64                SecurityAssociation.setPrincipal(new SimplePrincipal(value));
65             }
66             if (localName.equals("password"))
67             {
68                String JavaDoc password = new String JavaDoc(Base64.decode(value));
69                SecurityAssociation.setCredential(password);
70             }
71          }
72       }
73       catch (SOAPException JavaDoc e)
74       {
75          log.error("Server login failed: " + e.toString());
76          throw new AxisFault("Server login failed", e);
77       }
78    }
79 }
80
Popular Tags