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.openejb.server.axis2;
018:
019: import org.apache.openejb.core.webservices.PortData;
020: import org.apache.openejb.server.axis2.pojo.PojoWsContainer;
021: import org.apache.openejb.server.axis2.testdata.simple.HelloWorld;
022: import org.apache.openejb.server.httpd.HttpRequest;
023: import org.w3c.dom.Element;
024: import org.w3c.dom.NamedNodeMap;
025: import org.w3c.dom.NodeList;
026:
027: import java.io.ByteArrayOutputStream;
028: import java.net.URI;
029: import java.util.HashMap;
030:
031: public class Axis2WsContainerTest extends Axis2AbstractTestCase {
032: private final String RESOURCE_PATH = testDir + "/resources/";
033:
034: public Axis2WsContainerTest(String testName) {
035: super (testName);
036: }
037:
038: /*
039: public void testDocInvokeWithWSDL() throws Exception {
040: invokeWithWSDL("BareDocLitService", "org.apache.openejb.server.axis2.testdata.doclitbare.BareDocLitService",
041: "test_service_doclitbare.wsdl", "test_service_doclitbare_request.xml");
042: }
043: */
044:
045: public void testRPCInvokeWithWSDL() throws Exception {
046: invokeWithWSDL(
047: "RpcLitService",
048: "org.apache.openejb.server.axis2.testdata.rpclit.RpcLitService",
049: "test_service_rpclit.wsdl",
050: "test_service_rpclit_request.xml");
051: }
052:
053: public void XtestGetWSDL() throws Exception {
054: ClassLoader cl = Thread.currentThread().getContextClassLoader();
055:
056: PortData port = new PortData();
057: port.setLocation("/axis2/HelloWorld");
058:
059: Axis2Request req = new Axis2Request(504,
060: "text/xml; charset=utf-8", null,
061: HttpRequest.Method.GET, new HashMap<String, String>(),
062: new URI("/axis2/HelloWorld?wsdl"),
063: new HashMap<String, String>(), "127.0.0.1");
064: ByteArrayOutputStream out = new ByteArrayOutputStream();
065: Axis2Response res = new Axis2Response(
066: "text/xml; charset=utf-8", "127.0.0.1", null, null,
067: 8080, out);
068:
069: PojoWsContainer container = new PojoWsContainer(port,
070: HelloWorld.class, null, "/axis2");
071: container.start();
072: container.onMessage(req, res);
073: out.flush();
074:
075: }
076:
077: private void invokeWithWSDL(String serviceName,
078: String endPointClassName, String wsdl, String requestFile)
079: throws Exception {
080: // ClassLoader cl = Thread.currentThread().getContextClassLoader();
081: // InputStream in = cl.getResourceAsStream(requestFile);
082: //
083: // //This will reduce number of requests files
084: // DocumentBuilder documentBuilder = XmlUtil.newDocumentBuilderFactory().newDocumentBuilder();
085: // Document doc = documentBuilder.parse(in);
086: //
087: // Element root = doc.getDocumentElement();
088: // NodeList nodeList = root.getElementsByTagName("soap:Envelope");
089: //
090: // InputStream request;
091: //
092: // for(int i = 0; i < nodeList.getLength(); i++){
093: // StringBuffer envelope = new StringBuffer("<soap:Envelope");
094: // Element element = (Element)nodeList.item(i);
095: // NamedNodeMap attributes = element.getAttributes();
096: // if(attributes != null){
097: // for(int k=0; k < attributes.getLength(); k++){
098: // envelope.append(" "+attributes.item(k).getNodeName().trim());
099: // envelope.append("=\""+attributes.item(k).getNodeValue().trim()+"\"");
100: // }
101: // String content = element.getTextContent();
102: //
103: // if(content != null && !content.equals("")){
104: // envelope.append(">");
105: //
106: // NodeList children = element.getChildNodes();
107: // if(children != null){
108: // for(int j=0; j < children.getLength(); j++){
109: // if(children.item(j).getNodeType() == org.w3c.dom.Node.ELEMENT_NODE){
110: // Element child = (Element)children.item(j);
111: // envelope.append(getElementContent(child).trim());
112: // }else if(children.item(j).getNodeType() == org.w3c.dom.Node.TEXT_NODE){
113: // envelope.append(children.item(j).getNodeValue().trim());
114: // }
115: // }
116: // }
117: // envelope.append("</soap:Envelope>");
118: // }else {
119: // envelope.append("/>");
120: // }
121: // }
122: //
123: // System.out.println(envelope.toString());
124: //
125: // request = new ByteArrayInputStream(envelope.toString().getBytes("UTF-8"));
126: //
127: // PortInfo portInfo = new PortInfo();
128: // portInfo.location = "/axis2/" + serviceName;
129: //
130: // File wsdlFile = new File(RESOURCE_PATH + wsdl);
131: // portInfo.wsdlFile = wsdlFile.toURL().toString();
132: //
133: // try {
134: // Axis2Request req = new Axis2Request(504,
135: // "text/xml; charset=utf-8",
136: // request,
137: // HttpRequest.POST,
138: // new HashMap<String,String>(),
139: // new URI("/axis2/"+serviceName),
140: // new HashMap<String,String>(),
141: // "127.0.0.1");
142: //
143: // ByteArrayOutputStream out = new ByteArrayOutputStream();
144: // Axis2Response res = new Axis2Response("text/xml; charset=utf-8", "127.0.0.1", null, null, 8080, out);
145: //
146: // POJOWebServiceContainer container = new POJOWebServiceContainer(portInfo, endPointClassName, cl, null, null, Holder.EMPTY, "/axis2");
147: // container.init();
148: // container.onMessage(req, res);
149: // System.out.println("Response "+out);
150: // out.flush();
151: //
152: // } catch (Throwable ex) {
153: // ex.printStackTrace();
154: // throw new Exception(ex.toString());
155: // } finally {
156: // if (request != null) {
157: // try {
158: // request.close();
159: // } catch (IOException ignore) {
160: // // ignore
161: // }
162: // }
163: // }
164: // }
165: }
166:
167: private String getElementContent(Element e) {
168: StringBuffer content = new StringBuffer("<" + e.getNodeName());
169: NamedNodeMap attributes = e.getAttributes();
170:
171: if (attributes != null) {
172: for (int k = 0; k < attributes.getLength(); k++) {
173: content.append(" " + attributes.item(k).getNodeName());
174: content.append("=\""
175: + attributes.item(k).getNodeValue() + "\"");
176: }
177: }
178:
179: String value = e.getTextContent();
180:
181: if (value != null && !value.equals("")) {
182: content.append(">");
183:
184: NodeList children = e.getChildNodes();
185: if (children != null) {
186: for (int j = 0; j < children.getLength(); j++) {
187: if (children.item(j).getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) {
188: Element child = (Element) children.item(j);
189: content.append(getElementContent(child).trim());
190: } else if (children.item(j).getNodeType() == org.w3c.dom.Node.TEXT_NODE) {
191: content.append(children.item(j).getNodeValue()
192: .trim());
193: }
194: }
195: }
196: content.append("</" + e.getNodeName() + ">");
197: } else {
198: content.append("/>");
199: }
200:
201: return content.toString();
202: }
203:
204: protected void setUp() throws Exception {
205: }
206:
207: protected void tearDown() throws Exception {
208: }
209:
210: }
|