KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > webservice > message > 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.message;
23
24 import java.io.ByteArrayInputStream JavaDoc;
25 import java.io.StringWriter JavaDoc;
26 import java.rmi.RemoteException JavaDoc;
27
28 import javax.xml.namespace.QName JavaDoc;
29 import javax.xml.parsers.DocumentBuilder JavaDoc;
30 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
31 import javax.xml.rpc.ServiceException JavaDoc;
32 import javax.xml.rpc.ServiceFactory JavaDoc;
33
34 import org.jboss.logging.Logger;
35 import org.jboss.util.xml.DOMUtils;
36 import org.w3c.dom.Document JavaDoc;
37 import org.w3c.dom.Element JavaDoc;
38 import org.w3c.dom.NodeList JavaDoc;
39
40 import com.ibm.wsdl.util.xml.DOM2Writer;
41
42 /**
43  * @author Thomas.Diesler@jboss.org
44  * @since 26-Nov-2004
45  */

46 public class MessageJavaBean implements Message
47 {
48    // provide logging
49
private final Logger log = Logger.getLogger(MessageJavaBean.class);
50
51    /** org.w3c.dom.Element
52     */

53    public Element JavaDoc processElement(Element JavaDoc msg) throws RemoteException JavaDoc
54    {
55       StringWriter JavaDoc swr = new StringWriter JavaDoc();
56       DOM2Writer.serializeAsXML(msg, swr);
57       log.info("processElement: " + swr);
58
59       try
60       {
61          Element JavaDoc reqEl = (Element JavaDoc)msg;
62
63          // verify order element
64
QName JavaDoc qname = new QName JavaDoc(NSURI_1, "Order", PREFIX_1);
65          QName JavaDoc elementName = new QName JavaDoc(reqEl.getNamespaceURI(), reqEl.getLocalName(), reqEl.getPrefix());
66          if (qname.equals(elementName) == false)
67             throw new IllegalArgumentException JavaDoc("Unexpected element: " + elementName);
68
69          // Verify the custom attribute
70
String JavaDoc attrVal = reqEl.getAttribute("attrval");
71          if ("somevalue".equals(attrVal) == false)
72             throw new IllegalArgumentException JavaDoc("Unexpected attribute value: " + attrVal);
73
74          // Namespace attribute handling seems to be broken
75
if (isWS4EEAvailable() == false)
76          {
77             // [TODO] The reqEl should not contain the xmlns:env declaration
78
//NamedNodeMap attributes = reqEl.getAttributes();
79
//if (attributes.getLength() != 3)
80
// throw new IllegalArgumentException("Unexpected number of attributes: " + attributes.getLength());
81

82             // Verify NS declarations
83
String JavaDoc nsURI_1 = reqEl.getAttribute("xmlns:" + PREFIX_1);
84             if (NSURI_1.equals(nsURI_1) == false)
85                throw new IllegalArgumentException JavaDoc("Unexpected namespace URI: " + nsURI_1);
86
87             String JavaDoc nsURI_2 = reqEl.getAttribute("xmlns:" + PREFIX_2);
88             if (NSURI_2.equals(nsURI_2) == false)
89                throw new IllegalArgumentException JavaDoc("Unexpected namespace URI: " + nsURI_2);
90          }
91
92          // Test getElementsByTagNameNS
93
// http://jira.jboss.com/jira/browse/JBWS-99
94
NodeList JavaDoc nodeList1 = reqEl.getElementsByTagNameNS(NSURI_2, "Customer");
95          if (nodeList1.getLength() != 1)
96             throw new IllegalArgumentException JavaDoc("Cannot getElementsByTagNameNS");
97
98          // Test getElementsByTagName
99
// http://jira.jboss.com/jira/browse/JBWS-99
100
NodeList JavaDoc nodeList2 = reqEl.getElementsByTagName("Item");
101          if (nodeList2.getLength() != 1)
102             throw new IllegalArgumentException JavaDoc("Cannot getElementsByTagName");
103
104          // verify customer element
105
qname = new QName JavaDoc(NSURI_2, "Customer", PREFIX_2);
106          Element JavaDoc custEl = DOMUtils.getFirstChildElement(reqEl, qname);
107          String JavaDoc elementValue = DOMUtils.getTextContent(custEl);
108          if ("Customer".equals(custEl.getLocalName()) == false || "Kermit".equals(elementValue) == false)
109             throw new IllegalArgumentException JavaDoc("Unexpected element value: " + elementValue);
110
111          // verify item element
112
qname = new QName JavaDoc("Item");
113          Element JavaDoc itemEl = DOMUtils.getFirstChildElement(reqEl, qname);
114          elementValue = DOMUtils.getTextContent(itemEl);
115          if ("Item".equals(itemEl.getLocalName()) == false || "Ferrari".equals(elementValue) == false)
116             throw new IllegalArgumentException JavaDoc("Unexpected element value: " + elementValue);
117
118          // Setup document builder
119
DocumentBuilderFactory JavaDoc docBuilderFactory = DocumentBuilderFactory.newInstance();
120          docBuilderFactory.setNamespaceAware(true);
121
122          // Prepare response
123
DocumentBuilder JavaDoc builder = docBuilderFactory.newDocumentBuilder();
124          Document JavaDoc doc = builder.parse(new ByteArrayInputStream JavaDoc(response.getBytes()));
125          Element JavaDoc resElement = doc.getDocumentElement();
126
127          return resElement;
128       }
129       catch (RuntimeException JavaDoc e)
130       {
131          throw e;
132       }
133       catch (Exception JavaDoc e)
134       {
135          throw new RemoteException JavaDoc(e.toString(), e);
136       }
137    }
138
139    private boolean isWS4EEAvailable()
140    {
141       try
142       {
143          ServiceFactory JavaDoc factory = ServiceFactory.newInstance();
144          if ("org.jboss.webservice.client.ServiceFactoryImpl".equals(factory.getClass().getName()))
145             return true;
146       }
147       catch (ServiceException JavaDoc e)
148       {
149          // ignore
150
}
151       return false;
152    }
153 }
154
Popular Tags