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