1 22 package org.jboss.test.webservice.jbws168; 23 24 import java.io.ByteArrayOutputStream ; 25 import java.io.StringWriter ; 26 import java.util.Iterator ; 27 28 import javax.xml.namespace.QName ; 29 import javax.xml.rpc.handler.GenericHandler ; 30 import javax.xml.rpc.handler.HandlerInfo ; 31 import javax.xml.rpc.handler.MessageContext ; 32 import javax.xml.rpc.handler.soap.SOAPMessageContext ; 33 import javax.xml.soap.Name ; 34 import javax.xml.soap.SOAPBody ; 35 import javax.xml.soap.SOAPElement ; 36 import javax.xml.soap.SOAPEnvelope ; 37 import javax.xml.soap.SOAPException ; 38 import javax.xml.soap.SOAPFactory ; 39 import javax.xml.soap.SOAPMessage ; 40 41 import org.jboss.logging.Logger; 42 import org.jboss.util.xml.DOMWriter; 43 44 49 public class HelloHandler extends GenericHandler 50 { 51 private static final Logger log = Logger.getLogger(HelloHandler.class); 53 54 protected QName [] headers; 55 56 public QName [] getHeaders() 57 { 58 return headers; 59 } 60 61 public void init(HandlerInfo config) 62 { 63 headers = config.getHeaders(); 64 } 65 66 public boolean handleRequest(MessageContext msgContext) 67 { 68 log.info("handleRequest"); 69 70 try 71 { 72 SOAPMessageContext soapContext = (SOAPMessageContext )msgContext; 73 SOAPMessage soapMessage = soapContext.getMessage(); 74 SOAPFactory soapFactory = SOAPFactory.newInstance(); 75 76 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 77 soapMessage.writeTo(baos); 78 String msgStr = new String (baos.toByteArray()); 79 80 System.out.println(msgStr); 81 82 SOAPBody soapBody = soapMessage.getSOAPBody(); 83 Name name = soapFactory.createName("hello", "ns1", "http://org.jboss.test.webservice/jbws168/types"); 84 SOAPElement helloElement = (SOAPElement )soapBody.getChildElements(name).next(); 85 86 94 95 validateMessageContent(helloElement, "UserType_1"); 96 } 97 catch (Exception e) 98 { 99 log.error("Handler processing error", e); 100 return false; 101 } 102 103 return true; 104 } 105 106 public boolean handleResponse(MessageContext msgContext) 107 { 108 log.info("handleResponse"); 109 110 try 111 { 112 SOAPMessageContext soapContext = (SOAPMessageContext )msgContext; 113 SOAPMessage soapMessage = soapContext.getMessage(); 114 SOAPFactory soapFactory = SOAPFactory.newInstance(); 115 116 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 117 soapMessage.writeTo(baos); 118 String msgStr = new String (baos.toByteArray()); 119 120 System.out.println(msgStr); 121 122 SOAPBody soapBody = soapMessage.getSOAPBody(); 123 Name name = soapFactory.createName("helloResponse", "ns1", "http://org.jboss.test.webservice/jbws168/types"); 124 SOAPElement helloElement = (SOAPElement )soapBody.getChildElements(name).next(); 125 helloElement.getChildElements(); 126 127 135 136 } 140 catch (Exception e) 141 { 142 log.error("Handler processing error", e); 143 } 144 145 return true; 146 } 147 148 private void validateMessageContent(SOAPElement element, String rootName) throws SOAPException 149 { 150 SOAPFactory soapFactory = SOAPFactory.newInstance(); 151 SOAPElement utElement = (SOAPElement )element.getChildElements(soapFactory.createName(rootName)).next(); 152 153 Iterator it = utElement.getChildElements(); 154 SOAPElement propC = (SOAPElement )it.next(); 155 if (propC.getElementName().equals(soapFactory.createName("propC")) == false) 156 throw new RuntimeException ("Unexpected SOAP element: " + propC.getElementName()); 157 158 if (propC.hasAttributeNS("http://www.w3.org/2001/XMLSchema-instance", "nil") == false) 159 throw new RuntimeException ("Cannot find attribute xsi:nil"); 160 161 String value = propC.getValue(); 162 if (value != null) 163 throw new RuntimeException ("Unexpected text value: " + value); 164 165 SOAPElement propA = (SOAPElement )it.next(); 166 if (propA.getElementName().equals(soapFactory.createName("propA")) == false) 167 throw new RuntimeException ("Unexpected SOAP element: " + propA.getElementName()); 168 169 value = propA.getValue(); 170 if ("A".equals(value) == false) 171 throw new RuntimeException ("Unexpected text value: " + value); 172 173 if (it.hasNext()) 174 throw new RuntimeException ("Unexpected SOAP element"); 175 } 176 } 177 | Popular Tags |