1 22 package org.jboss.test.webservice.header; 23 24 import org.jboss.logging.Logger; 25 import org.jboss.util.id.UID; 26 27 import javax.xml.namespace.QName ; 28 import javax.xml.rpc.JAXRPCException ; 29 import javax.xml.rpc.handler.GenericHandler ; 30 import javax.xml.rpc.handler.MessageContext ; 31 import javax.xml.rpc.handler.soap.SOAPMessageContext ; 32 import javax.xml.soap.Name ; 33 import javax.xml.soap.SOAPElement ; 34 import javax.xml.soap.SOAPException ; 35 import javax.xml.soap.SOAPFactory ; 36 import javax.xml.soap.SOAPHeader ; 37 import javax.xml.soap.SOAPHeaderElement ; 38 import javax.xml.soap.SOAPMessage ; 39 import java.util.Iterator ; 40 41 public class ServerHandler extends GenericHandler 42 { 43 private final Logger log = Logger.getLogger(ServerHandler.class); 45 46 private static final String NAMESPACE_URI = ImplicitHeaderEndpoint.NAMESPACE_URI; 47 private static final String LOCAL_PART = ImplicitHeaderEndpoint.LOCAL_PART; 48 private static final String PREFIX = ImplicitHeaderEndpoint.PREFIX; 49 50 private String username; 51 private String sessionID; 52 53 public QName [] getHeaders() 54 { 55 QName qname = new QName (NAMESPACE_URI, LOCAL_PART); 56 return new QName []{qname}; 57 } 58 59 public boolean handleRequest(MessageContext msgContext) 60 { 61 try 62 { 63 SOAPFactory factory = SOAPFactory.newInstance(); 64 65 SOAPMessage soapMessage = ((SOAPMessageContext )msgContext).getMessage(); 66 SOAPHeader soapHeader = soapMessage.getSOAPHeader(); 67 68 Iterator it = soapHeader.examineAllHeaderElements(); 70 71 if (it.hasNext() == false) 72 throw new SOAPException ("No header element"); 73 74 SOAPHeaderElement headerElement = (SOAPHeaderElement )it.next(); 75 76 Name usernameName = factory.createName("username", PREFIX, NAMESPACE_URI); 77 SOAPElement usrElement = (SOAPElement )headerElement.getChildElements(usernameName).next(); 78 username = usrElement.getValue(); 79 80 Name sessionIDName = factory.createName("sessionID", PREFIX, NAMESPACE_URI); 81 SOAPElement seElement = (SOAPElement )headerElement.getChildElements(sessionIDName).next(); 82 sessionID = seElement.getValue(); 83 84 log.info("username: " + username); 85 log.info("sessionID: " + sessionID); 86 87 if (sessionID == null) 88 sessionID = UID.asString(); 89 90 return true; 91 } 92 catch (SOAPException e) 93 { 94 throw new JAXRPCException (e.toString(), e); 95 } 96 } 97 98 public boolean handleResponse(MessageContext msgContext) 99 { 100 try 101 { 102 SOAPMessage soapMessage = ((SOAPMessageContext )msgContext).getMessage(); 103 SOAPFactory factory = SOAPFactory.newInstance(); 104 105 SOAPHeader soapHeader = soapMessage.getSOAPHeader(); 106 Name name = factory.createName(LOCAL_PART, PREFIX, NAMESPACE_URI); 107 SOAPHeaderElement headerElement = soapHeader.addHeaderElement(name); 108 109 SOAPElement usrElement = headerElement.addChildElement("username", PREFIX, NAMESPACE_URI); 110 usrElement.setValue(username); 111 112 SOAPElement seElement = headerElement.addChildElement("sessionID", PREFIX, NAMESPACE_URI); 113 seElement.setValue(sessionID); 114 115 log.info("username: " + username); 116 log.info("sessionID: " + sessionID); 117 return true; 118 119 } 120 catch (SOAPException e) 121 { 122 throw new JAXRPCException (e.toString(), e); 123 } 124 } 125 } 126 | Popular Tags |