1 7 8 package org.jboss.webservice.handler; 10 11 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 ; 22 import javax.xml.soap.SOAPException ; 23 import javax.xml.soap.SOAPFactory ; 24 import javax.xml.soap.SOAPHeader ; 25 import javax.xml.soap.SOAPHeaderElement ; 26 import javax.xml.soap.SOAPMessage ; 27 import java.security.Principal ; 28 29 40 public class ClientLoginHandler extends BasicHandler 41 { 42 43 static final long serialVersionUID = -5105554242454628283L; 44 45 private static final Logger log = Logger.getLogger(ClientLoginHandler.class); 47 48 50 public void invoke(MessageContext msgContext) throws AxisFault 51 { 52 String username = msgContext.getUsername(); 53 String password = msgContext.getPassword(); 54 55 if (username == null && password == null) 56 { 57 Principal principal = SecurityAssociation.getPrincipal(); 58 Object credential = SecurityAssociation.getCredential(); 59 60 if (principal != null) 61 username = principal.getName(); 62 if (credential instanceof char[]) 63 password = new String ((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 soapMessage = msgContext.getMessage(); 75 SOAPHeader soapHeader = soapMessage.getSOAPPart().getEnvelope().getHeader(); 76 SOAPFactory soapFactory = SOAPFactory.newInstance(); 77 78 Name usrName = soapFactory.createName("username", "jbws", Constants.NAMESPACE); 79 SOAPHeaderElement usrElement = soapHeader.addHeaderElement(usrName); 80 usrElement.setActor(Constants.NAMESPACE + "/login"); 81 usrElement.addTextNode(username); 82 83 password = Base64.encode(password.getBytes()); 84 85 Name pwdName = soapFactory.createName("password", "jbws", Constants.NAMESPACE); 86 SOAPHeaderElement pwdElement = soapHeader.addHeaderElement(pwdName); 87 pwdElement.setActor(Constants.NAMESPACE + "/login"); 88 pwdElement.addTextNode(password); 89 } 90 catch (SOAPException 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 |