KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > webservice > jbws84 > MessageJavaBean


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.jbws84;
23
24 import com.ibm.wsdl.util.xml.DOM2Writer;
25 import org.jboss.logging.Logger;
26 import org.w3c.dom.Document JavaDoc;
27
28 import javax.xml.parsers.DocumentBuilder JavaDoc;
29 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
30 import javax.xml.parsers.ParserConfigurationException JavaDoc;
31 import javax.xml.soap.MessageFactory JavaDoc;
32 import javax.xml.soap.Name JavaDoc;
33 import javax.xml.soap.SOAPBody JavaDoc;
34 import javax.xml.soap.SOAPElement JavaDoc;
35 import javax.xml.soap.SOAPFactory JavaDoc;
36 import javax.xml.soap.SOAPMessage JavaDoc;
37 import java.io.ByteArrayInputStream JavaDoc;
38 import java.io.StringWriter JavaDoc;
39 import java.rmi.RemoteException JavaDoc;
40
41 /**
42  * @author Thomas.Diesler@jboss.org
43  * @since 26-Nov-2004
44  */

45 public class MessageJavaBean implements Message
46 {
47    // provide logging
48
private final Logger log = Logger.getLogger(MessageJavaBean.class);
49
50    /** javax.xml.soap.SOAPElement
51     */

52    public SOAPElement JavaDoc processSOAPElement(SOAPElement JavaDoc reqElement) throws RemoteException JavaDoc
53    {
54       StringWriter JavaDoc swr = new StringWriter JavaDoc();
55       DOM2Writer.serializeAsXML(reqElement, swr);
56       log.info("processSOAPElement: " + swr);
57
58       try
59       {
60          SOAPFactory JavaDoc soapFactory = SOAPFactory.newInstance();
61
62          Name JavaDoc name = soapFactory.createName("Order", PREFIX, NAMESPACE_URI);
63          Name JavaDoc elementName = reqElement.getElementName();
64          if (name.equals(elementName) == false)
65             throw new IllegalArgumentException JavaDoc("Unexpected element: " + elementName);
66
67          name = soapFactory.createName("Customer");
68          SOAPElement JavaDoc custElement = (SOAPElement JavaDoc)reqElement.getChildElements(name).next();
69          String JavaDoc elementValue = custElement.getValue();
70          if ("Customer".equals(custElement.getLocalName()) && "Kermit".equals(elementValue) == false)
71             throw new IllegalArgumentException JavaDoc("Unexpected element value: " + elementValue);
72
73          name = soapFactory.createName("Item");
74          SOAPElement JavaDoc itemElement = (SOAPElement JavaDoc)reqElement.getChildElements(name).next();
75          elementValue = itemElement.getValue();
76          if ("Item".equals(itemElement.getLocalName()) && "Ferrari".equals(elementValue) == false)
77             throw new IllegalArgumentException JavaDoc("Unexpected element value: " + elementValue);
78
79          MessageFactory JavaDoc msgFactory = MessageFactory.newInstance();
80          SOAPMessage JavaDoc resMessage = msgFactory.createMessage();
81          SOAPBody JavaDoc soapBody = resMessage.getSOAPBody();
82
83          DocumentBuilder JavaDoc builder = getDocumentBuilder();
84          Document JavaDoc doc = builder.parse(new ByteArrayInputStream JavaDoc(Message.response.getBytes()));
85          soapBody.addDocument(doc);
86
87          SOAPElement JavaDoc resElement = (SOAPElement JavaDoc)soapBody.getChildElements().next();
88          return resElement;
89       }
90       catch (RuntimeException JavaDoc e)
91       {
92          throw e;
93       }
94       catch (Exception JavaDoc e)
95       {
96          throw new RemoteException JavaDoc(e.toString(), e);
97       }
98    }
99
100    private DocumentBuilder JavaDoc getDocumentBuilder() throws ParserConfigurationException JavaDoc
101    {
102       // Setup document builder
103
DocumentBuilderFactory JavaDoc docBuilderFactory = DocumentBuilderFactory.newInstance();
104       docBuilderFactory.setNamespaceAware(true);
105
106       DocumentBuilder JavaDoc builder = docBuilderFactory.newDocumentBuilder();
107       return builder;
108    }
109 }
110
Popular Tags