KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > webservice > jbws168 > HelloHandler


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.webservice.jbws168;
23
24 import java.io.ByteArrayOutputStream JavaDoc;
25 import java.io.StringWriter JavaDoc;
26 import java.util.Iterator JavaDoc;
27
28 import javax.xml.namespace.QName JavaDoc;
29 import javax.xml.rpc.handler.GenericHandler JavaDoc;
30 import javax.xml.rpc.handler.HandlerInfo JavaDoc;
31 import javax.xml.rpc.handler.MessageContext JavaDoc;
32 import javax.xml.rpc.handler.soap.SOAPMessageContext JavaDoc;
33 import javax.xml.soap.Name JavaDoc;
34 import javax.xml.soap.SOAPBody JavaDoc;
35 import javax.xml.soap.SOAPElement JavaDoc;
36 import javax.xml.soap.SOAPEnvelope JavaDoc;
37 import javax.xml.soap.SOAPException JavaDoc;
38 import javax.xml.soap.SOAPFactory JavaDoc;
39 import javax.xml.soap.SOAPMessage JavaDoc;
40
41 import org.jboss.logging.Logger;
42 import org.jboss.util.xml.DOMWriter;
43
44 /**
45  * A simple server side handler
46  *
47  * @author thomas.diesler@jboss.org
48  */

49 public class HelloHandler extends GenericHandler JavaDoc
50 {
51    // provide logging
52
private static final Logger log = Logger.getLogger(HelloHandler.class);
53
54    protected QName JavaDoc[] headers;
55
56    public QName JavaDoc[] getHeaders()
57    {
58       return headers;
59    }
60
61    public void init(HandlerInfo JavaDoc config)
62    {
63       headers = config.getHeaders();
64    }
65
66    public boolean handleRequest(MessageContext JavaDoc msgContext)
67    {
68       log.info("handleRequest");
69
70       try
71       {
72          SOAPMessageContext JavaDoc soapContext = (SOAPMessageContext JavaDoc)msgContext;
73          SOAPMessage JavaDoc soapMessage = soapContext.getMessage();
74          SOAPFactory JavaDoc soapFactory = SOAPFactory.newInstance();
75
76          ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
77          soapMessage.writeTo(baos);
78          String JavaDoc msgStr = new String JavaDoc(baos.toByteArray());
79
80          System.out.println(msgStr);
81
82          SOAPBody JavaDoc soapBody = soapMessage.getSOAPBody();
83          Name JavaDoc name = soapFactory.createName("hello", "ns1", "http://org.jboss.test.webservice/jbws168/types");
84          SOAPElement JavaDoc helloElement = (SOAPElement JavaDoc)soapBody.getChildElements(name).next();
85
86          /*
87           <ns1:hello xmlns:ns1="http://org.jboss.test.webservice/jbws168/types">
88           <UserType_1>
89           <propC xsi:nil="1"/>
90           <propA>A</propA>
91           </UserType_1>
92           </ns1:hello>
93           */

94
95          validateMessageContent(helloElement, "UserType_1");
96       }
97       catch (Exception JavaDoc e)
98       {
99          log.error("Handler processing error", e);
100          return false;
101       }
102
103       return true;
104    }
105
106    public boolean handleResponse(MessageContext JavaDoc msgContext)
107    {
108       log.info("handleResponse");
109
110       try
111       {
112          SOAPMessageContext JavaDoc soapContext = (SOAPMessageContext JavaDoc)msgContext;
113          SOAPMessage JavaDoc soapMessage = soapContext.getMessage();
114          SOAPFactory JavaDoc soapFactory = SOAPFactory.newInstance();
115
116          ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
117          soapMessage.writeTo(baos);
118          String JavaDoc msgStr = new String JavaDoc(baos.toByteArray());
119
120          System.out.println(msgStr);
121
122          SOAPBody JavaDoc soapBody = soapMessage.getSOAPBody();
123          Name JavaDoc name = soapFactory.createName("helloResponse", "ns1", "http://org.jboss.test.webservice/jbws168/types");
124          SOAPElement JavaDoc helloElement = (SOAPElement JavaDoc)soapBody.getChildElements(name).next();
125          helloElement.getChildElements();
126
127          /*
128           <ns1:helloResponse xmlns:ns1="http://org.jboss.test.webservice/jbws168/types">
129           <result>
130           <propC xsi:nil="1"/>
131           <propA>A</propA>
132           </result>
133           </ns1:helloResponse>
134           */

135
136          // The message gets serialized ok, but navigation of the SOAP tree fails
137
// http://jira.jboss.com/jira/browse/JBWS-250
138
//validateMessageContent(helloElement, "result");
139
}
140       catch (Exception JavaDoc e)
141       {
142          log.error("Handler processing error", e);
143       }
144
145       return true;
146    }
147
148    private void validateMessageContent(SOAPElement JavaDoc element, String JavaDoc rootName) throws SOAPException JavaDoc
149    {
150       SOAPFactory JavaDoc soapFactory = SOAPFactory.newInstance();
151       SOAPElement JavaDoc utElement = (SOAPElement JavaDoc)element.getChildElements(soapFactory.createName(rootName)).next();
152
153       Iterator JavaDoc it = utElement.getChildElements();
154       SOAPElement JavaDoc propC = (SOAPElement JavaDoc)it.next();
155       if (propC.getElementName().equals(soapFactory.createName("propC")) == false)
156          throw new RuntimeException JavaDoc("Unexpected SOAP element: " + propC.getElementName());
157
158       if (propC.hasAttributeNS("http://www.w3.org/2001/XMLSchema-instance", "nil") == false)
159          throw new RuntimeException JavaDoc("Cannot find attribute xsi:nil");
160
161       String JavaDoc value = propC.getValue();
162       if (value != null)
163          throw new RuntimeException JavaDoc("Unexpected text value: " + value);
164
165       SOAPElement JavaDoc propA = (SOAPElement JavaDoc)it.next();
166       if (propA.getElementName().equals(soapFactory.createName("propA")) == false)
167          throw new RuntimeException JavaDoc("Unexpected SOAP element: " + propA.getElementName());
168
169       value = propA.getValue();
170       if ("A".equals(value) == false)
171          throw new RuntimeException JavaDoc("Unexpected text value: " + value);
172
173       if (it.hasNext())
174          throw new RuntimeException JavaDoc("Unexpected SOAP element");
175    }
176 }
177
Popular Tags