KickJava   Java API By Example, From Geeks To Geeks.

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


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: ClientLoginHandler.java,v 1.1.2.3 2005/04/12 16:07:45 starksm Exp $
9
package org.jboss.webservice.handler;
10
11 // $Id: ClientLoginHandler.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.webservice.Constants;
20
21 import javax.xml.soap.Name JavaDoc;
22 import javax.xml.soap.SOAPException JavaDoc;
23 import javax.xml.soap.SOAPFactory JavaDoc;
24 import javax.xml.soap.SOAPHeader JavaDoc;
25 import javax.xml.soap.SOAPHeaderElement JavaDoc;
26 import javax.xml.soap.SOAPMessage JavaDoc;
27 import java.security.Principal JavaDoc;
28
29 /**
30  * An Axis style handler that adds username/password as SOAP header elements.
31  *
32  * <soapenv:Header>
33  * <jbws:username actor="http://webservice.jboss.com/ws4ee/login" xmlns:jbws="http://webservice.jboss.com/ws4ee">kermit</jbws:username>
34  * <jbws:password actor="http://webservice.jboss.com/ws4ee/login" xmlns:jbws="http://webservice.jboss.com/ws4ee">thefrog</jbws:password>
35  * </soapenv:Header>
36  *
37  * @author Thomas.Diesler@jboss.org
38  * @since 27-April-2004
39  */

40 public class ClientLoginHandler extends BasicHandler
41 {
42    /** @since 4.0.2 */
43    static final long serialVersionUID = -5105554242454628283L;
44
45    // provide logging
46
private static final Logger log = Logger.getLogger(ClientLoginHandler.class);
47
48    /** Add the principal/credential from the MessageContext, if not given fall back to the SecurityAssociation.
49     */

50    public void invoke(MessageContext msgContext) throws AxisFault
51    {
52       String JavaDoc username = msgContext.getUsername();
53       String JavaDoc password = msgContext.getPassword();
54
55       if (username == null && password == null)
56       {
57          Principal JavaDoc principal = SecurityAssociation.getPrincipal();
58          Object JavaDoc credential = SecurityAssociation.getCredential();
59
60          if (principal != null)
61             username = principal.getName();
62          if (credential instanceof char[])
63             password = new String JavaDoc((char[])credential);
64          else if (credential != null)
65             password = credential.toString();
66       }
67
68       if (username != null && password != null)
69       {
70          try
71          {
72             msgContext.setUsername(username);
73             msgContext.setPassword(password);
74             SOAPMessage JavaDoc soapMessage = msgContext.getMessage();
75             SOAPHeader JavaDoc soapHeader = soapMessage.getSOAPPart().getEnvelope().getHeader();
76             SOAPFactory JavaDoc soapFactory = SOAPFactory.newInstance();
77
78             Name JavaDoc usrName = soapFactory.createName("username", "jbws", Constants.NAMESPACE);
79             SOAPHeaderElement JavaDoc usrElement = soapHeader.addHeaderElement(usrName);
80             usrElement.setActor(Constants.NAMESPACE + "/login");
81             usrElement.addTextNode(username);
82
83             password = Base64.encode(password.getBytes());
84
85             Name JavaDoc pwdName = soapFactory.createName("password", "jbws", Constants.NAMESPACE);
86             SOAPHeaderElement JavaDoc pwdElement = soapHeader.addHeaderElement(pwdName);
87             pwdElement.setActor(Constants.NAMESPACE + "/login");
88             pwdElement.addTextNode(password);
89          }
90          catch (SOAPException JavaDoc e)
91          {
92             log.error("Client login failed: " + e.toString());
93             throw new AxisFault("Client login failed", e);
94          }
95       }
96    }
97 }
98
Popular Tags