001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.geronimo.axis2;
018:
019: import java.io.ByteArrayInputStream;
020: import java.io.ByteArrayOutputStream;
021: import java.io.File;
022: import java.io.IOException;
023: import java.io.InputStream;
024: import java.net.URI;
025: import java.util.HashMap;
026:
027: import javax.xml.parsers.DocumentBuilder;
028:
029: import org.apache.geronimo.axis2.pojo.POJOWebServiceContainer;
030: import org.apache.geronimo.jaxws.PortInfo;
031: import org.apache.geronimo.jaxws.annotations.AnnotationHolder;
032: import org.apache.geronimo.kernel.util.XmlUtil;
033: import org.apache.geronimo.webservices.WebServiceContainer.Request;
034: import org.w3c.dom.Document;
035: import org.w3c.dom.Element;
036: import org.w3c.dom.NamedNodeMap;
037: import org.w3c.dom.NodeList;
038:
039: public class Axis2WebServiceContainerTest extends Axis2AbstractTestCase {
040: private final String RESOURCE_PATH = testDir + "/resources/";
041:
042: public Axis2WebServiceContainerTest(String testName) {
043: super (testName);
044: }
045:
046: /*
047: public void testDocInvokeWithWSDL() throws Exception {
048: invokeWithWSDL("BareDocLitService", "org.apache.geronimo.axis2.testdata.doclitbare.BareDocLitService",
049: "test_service_doclitbare.wsdl", "test_service_doclitbare_request.xml");
050: }
051: */
052:
053: public void testRPCInvokeWithWSDL() throws Exception {
054: invokeWithWSDL(
055: "RPCLitService",
056: "org.apache.geronimo.axis2.testdata.rpclit.RPCLitService",
057: "test_service_rpclit.wsdl",
058: "test_service_rpclit_request.xml");
059: }
060:
061: public void testGetWSDL() throws Exception {
062: ClassLoader cl = Thread.currentThread().getContextClassLoader();
063:
064: PortInfo portInfo = new PortInfo();
065: portInfo.setLocation("/axis2/HelloWorld");
066: portInfo
067: .setServiceEndpointInterfaceName("org.apache.geronimo.axis2.testdata.simple.HelloWorld");
068:
069: Axis2Request req = new Axis2Request(504,
070: "text/xml; charset=utf-8", null, Request.GET,
071: new HashMap(), new URI("/axis2/HelloWorld?wsdl"),
072: new HashMap(), "127.0.0.1");
073: ByteArrayOutputStream out = new ByteArrayOutputStream();
074: Axis2Response res = new Axis2Response(
075: "text/xml; charset=utf-8", "127.0.0.1", null, null,
076: 8080, out);
077:
078: String endpointClassName = "org.apache.geronimo.axis2.testdata.simple.HelloWorld";
079: POJOWebServiceContainer container = new POJOWebServiceContainer(
080: portInfo, endpointClassName, cl, null, null,
081: AnnotationHolder.EMPTY, "/axis2");
082: container.init();
083: container.invoke(req, res);
084: out.flush();
085:
086: }
087:
088: private void invokeWithWSDL(String serviceName,
089: String endPointClassName, String wsdl, String requestFile)
090: throws Exception {
091: ClassLoader cl = Thread.currentThread().getContextClassLoader();
092: InputStream in = cl.getResourceAsStream(requestFile);
093:
094: //This will reduce number of requests files
095: DocumentBuilder documentBuilder = XmlUtil
096: .newDocumentBuilderFactory().newDocumentBuilder();
097: Document doc = documentBuilder.parse(in);
098:
099: Element root = doc.getDocumentElement();
100: NodeList nodeList = root.getElementsByTagName("soap:Envelope");
101:
102: InputStream request;
103:
104: for (int i = 0; i < nodeList.getLength(); i++) {
105: StringBuffer envelope = new StringBuffer("<soap:Envelope");
106: Element element = (Element) nodeList.item(i);
107: NamedNodeMap attributes = element.getAttributes();
108: if (attributes != null) {
109: for (int k = 0; k < attributes.getLength(); k++) {
110: envelope.append(" "
111: + attributes.item(k).getNodeName().trim());
112: envelope.append("=\""
113: + attributes.item(k).getNodeValue().trim()
114: + "\"");
115: }
116: String content = element.getTextContent();
117:
118: if (content != null && !content.equals("")) {
119: envelope.append(">");
120:
121: NodeList children = element.getChildNodes();
122: if (children != null) {
123: for (int j = 0; j < children.getLength(); j++) {
124: if (children.item(j).getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) {
125: Element child = (Element) children
126: .item(j);
127: envelope
128: .append(getElementContent(child)
129: .trim());
130: } else if (children.item(j).getNodeType() == org.w3c.dom.Node.TEXT_NODE) {
131: envelope.append(children.item(j)
132: .getNodeValue().trim());
133: }
134: }
135: }
136: envelope.append("</soap:Envelope>");
137: } else {
138: envelope.append("/>");
139: }
140: }
141:
142: System.out.println(envelope.toString());
143:
144: request = new ByteArrayInputStream(envelope.toString()
145: .getBytes("UTF-8"));
146:
147: PortInfo portInfo = new PortInfo();
148: portInfo.setLocation("/axis2/" + serviceName);
149:
150: File wsdlFile = new File(RESOURCE_PATH + wsdl);
151: portInfo.setWsdlFile(wsdlFile.toURL().toString());
152:
153: try {
154: Axis2Request req = new Axis2Request(504,
155: "text/xml; charset=utf-8", request,
156: Request.POST, new HashMap(), new URI("/axis2/"
157: + serviceName), new HashMap(),
158: "127.0.0.1");
159:
160: ByteArrayOutputStream out = new ByteArrayOutputStream();
161: Axis2Response res = new Axis2Response(
162: "text/xml; charset=utf-8", "127.0.0.1", null,
163: null, 8080, out);
164:
165: POJOWebServiceContainer container = new POJOWebServiceContainer(
166: portInfo, endPointClassName, cl, null, null,
167: AnnotationHolder.EMPTY, "/axis2");
168: container.init();
169: container.invoke(req, res);
170: System.out.println("Response " + out);
171: out.flush();
172:
173: } catch (Throwable ex) {
174: ex.printStackTrace();
175: throw new Exception(ex.toString());
176: } finally {
177: if (request != null) {
178: try {
179: request.close();
180: } catch (IOException ignore) {
181: // ignore
182: }
183: }
184: }
185: }
186: }
187:
188: private String getElementContent(Element e) {
189: StringBuffer content = new StringBuffer("<" + e.getNodeName());
190: NamedNodeMap attributes = e.getAttributes();
191:
192: if (attributes != null) {
193: for (int k = 0; k < attributes.getLength(); k++) {
194: content.append(" " + attributes.item(k).getNodeName());
195: content.append("=\""
196: + attributes.item(k).getNodeValue() + "\"");
197: }
198: }
199:
200: String value = e.getTextContent();
201:
202: if (value != null && !value.equals("")) {
203: content.append(">");
204:
205: NodeList children = e.getChildNodes();
206: if (children != null) {
207: for (int j = 0; j < children.getLength(); j++) {
208: if (children.item(j).getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) {
209: Element child = (Element) children.item(j);
210: content.append(getElementContent(child).trim());
211: } else if (children.item(j).getNodeType() == org.w3c.dom.Node.TEXT_NODE) {
212: content.append(children.item(j).getNodeValue()
213: .trim());
214: }
215: }
216: }
217: content.append("</" + e.getNodeName() + ">");
218: } else {
219: content.append("/>");
220: }
221:
222: return content.toString();
223: }
224:
225: protected void setUp() throws Exception {
226: }
227:
228: protected void tearDown() throws Exception {
229: }
230:
231: }
|