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.servicemix.components.http;
018:
019: import java.io.ByteArrayOutputStream;
020: import java.io.InputStream;
021: import java.io.OutputStream;
022: import java.net.URL;
023: import java.net.URLConnection;
024:
025: import javax.jbi.messaging.InOnly;
026: import javax.jbi.messaging.NormalizedMessage;
027: import javax.xml.namespace.QName;
028:
029: import org.w3c.dom.Element;
030: import org.w3c.dom.Node;
031: import org.w3c.dom.traversal.NodeIterator;
032:
033: import junit.framework.TestCase;
034:
035: import org.apache.commons.httpclient.HttpClient;
036: import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
037: import org.apache.commons.httpclient.methods.PostMethod;
038: import org.apache.commons.httpclient.methods.StringRequestEntity;
039: import org.apache.commons.logging.Log;
040: import org.apache.commons.logging.LogFactory;
041: import org.apache.servicemix.components.util.EchoComponent;
042: import org.apache.servicemix.components.util.MockServiceComponent;
043: import org.apache.servicemix.components.util.TraceComponent;
044: import org.apache.servicemix.jbi.container.ActivationSpec;
045: import org.apache.servicemix.jbi.container.JBIContainer;
046: import org.apache.servicemix.jbi.jaxp.SourceTransformer;
047: import org.apache.servicemix.jbi.jaxp.StringSource;
048: import org.apache.servicemix.jbi.messaging.InOnlyImpl;
049: import org.apache.servicemix.jbi.util.DOMUtil;
050: import org.apache.servicemix.jbi.util.FileUtil;
051: import org.apache.servicemix.tck.ReceiverComponent;
052: import org.apache.xpath.CachedXPathAPI;
053: import org.springframework.core.io.ClassPathResource;
054:
055: public class HttpSoapTest extends TestCase {
056: private static transient Log log = LogFactory
057: .getLog(HttpSoapTest.class);
058:
059: protected JBIContainer container;
060:
061: protected void setUp() throws Exception {
062: System.setProperty("DEBUG", "true");
063: container = new JBIContainer();
064: container.setMonitorInstallationDirectory(false);
065: container.setUseMBeanServer(false);
066: container.setCreateMBeanServer(false);
067: container.setEmbedded(true);
068: container.init();
069: container.start();
070: }
071:
072: protected void tearDown() throws Exception {
073: if (container != null) {
074: container.shutDown();
075: }
076: }
077:
078: public void testInOut() throws Exception {
079: int PORT = 7012;
080: ActivationSpec as = new ActivationSpec();
081: as.setId("echo");
082: as.setComponent(new EchoComponent());
083: as.setService(new QName("echo"));
084: container.activateComponent(as);
085: as = new ActivationSpec();
086: as.setId("xfireBinding");
087: as.setComponent(new HttpSoapConnector(null, PORT, true));
088: as.setDestinationService(new QName("echo"));
089: container.activateComponent(as);
090:
091: URLConnection connection = new URL("http://localhost:" + PORT)
092: .openConnection();
093: connection.setDoOutput(true);
094: connection.setDoInput(true);
095: OutputStream os = connection.getOutputStream();
096: // Post the request file.
097: InputStream fis = getClass().getResourceAsStream(
098: "soap-request.xml");
099: FileUtil.copyInputStream(fis, os);
100: // Read the response.
101: InputStream is = connection.getInputStream();
102: ByteArrayOutputStream baos = new ByteArrayOutputStream();
103: FileUtil.copyInputStream(is, baos);
104: log.info(baos.toString());
105: }
106:
107: public void testInOnly() throws Exception {
108: int PORT = 7013;
109: ActivationSpec as = new ActivationSpec();
110: as.setId("trace");
111: as.setComponent(new TraceComponent());
112: as.setService(new QName("trace"));
113: container.activateComponent(as);
114: as = new ActivationSpec();
115: as.setId("xfireBinding");
116: as.setComponent(new HttpSoapConnector(null, PORT, false));
117: as.setDestinationService(new QName("trace"));
118: container.activateComponent(as);
119:
120: PostMethod method = new PostMethod("http://localhost:" + PORT
121: + "/?name=Guillaume");
122: method.setRequestEntity(new InputStreamRequestEntity(getClass()
123: .getResourceAsStream("soap-request.xml")));
124: new HttpClient().executeMethod(method);
125: log.info(method.getResponseBodyAsString());
126: }
127:
128: public void testMarhaler() throws Exception {
129: String url = "http://64.124.140.30:9090/soap";
130: HttpSoapClientMarshaler marshaler = new HttpSoapClientMarshaler();
131: PostMethod method = new PostMethod(url);
132: method.addRequestHeader("Content-Type", "text/xml");
133: method.addRequestHeader("SOAPAction",
134: "urn:xmethods-delayed-quotes#getQuote");
135:
136: InOnly exchange = new InOnlyImpl("id");
137: NormalizedMessage in = exchange.createMessage();
138: exchange.setInMessage(in);
139: in
140: .setContent(new StringSource(
141: "<?xml version='1.0'?><ns1:getQuote xmlns:ns1='urn:xmethods-delayed-quotes' xmlns:xsi='http://www.w3.org/1999/XMLSchema-instance' xmlns:se='http://schemas.xmlsoap.org/soap/envelope/' se:encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'><symbol xsi:type='xsd:string'>SUNW</symbol></ns1:getQuote>"));
142: marshaler.fromNMS(method, exchange, in);
143: log.info(((StringRequestEntity) method.getRequestEntity())
144: .getContent());
145:
146: HttpClient httpClient = new HttpClient();
147: httpClient.executeMethod(method);
148: log.info(method.getResponseBodyAsString());
149:
150: exchange = new InOnlyImpl("id");
151: in = exchange.createMessage();
152: exchange.setInMessage(in);
153: marshaler.toNMS(in, method);
154:
155: log.info(new SourceTransformer().toString(in.getContent()));
156: }
157:
158: public void testMarshalerNamespaces() throws Exception {
159: ActivationSpec as = new ActivationSpec();
160: as.setId("mock");
161: MockServiceComponent mock = new MockServiceComponent();
162: mock
163: .setResponseResource(new ClassPathResource(
164: "org/apache/servicemix/components/http/soap-response.xml"));
165: as.setComponent(mock);
166: as.setService(new QName("mock"));
167: container.activateComponent(as);
168:
169: as = new ActivationSpec();
170: as.setId("http");
171: as.setDestinationService(new QName("mock"));
172: HttpConnector http = new HttpConnector();
173: http.setPort(8100);
174: as.setComponent(http);
175: container.activateComponent(as);
176:
177: String url = "http://localhost:8100";
178: HttpSoapClientMarshaler marshaler = new HttpSoapClientMarshaler();
179: PostMethod method = new PostMethod(url);
180: method.addRequestHeader("Content-Type", "text/xml");
181: method.addRequestHeader("SOAPAction",
182: "urn:xmethods-delayed-quotes#getQuote");
183:
184: InOnly exchange = new InOnlyImpl("id");
185: NormalizedMessage in = exchange.createMessage();
186: exchange.setInMessage(in);
187: in
188: .setContent(new StringSource(
189: "<?xml version='1.0'?><ns1:getQuote xmlns:ns1='urn:xmethods-delayed-quotes' xmlns:xsi='http://www.w3.org/1999/XMLSchema-instance' xmlns:se='http://schemas.xmlsoap.org/soap/envelope/' se:encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'><symbol xsi:type='xsd:string'>SUNW</symbol></ns1:getQuote>"));
190: marshaler.fromNMS(method, exchange, in);
191: log.info(((StringRequestEntity) method.getRequestEntity())
192: .getContent());
193:
194: HttpClient httpClient = new HttpClient();
195: httpClient.executeMethod(method);
196: log.info(method.getResponseBodyAsString());
197:
198: exchange = new InOnlyImpl("id");
199: in = exchange.createMessage();
200: exchange.setInMessage(in);
201: marshaler.toNMS(in, method);
202:
203: Node node = new SourceTransformer()
204: .toDOMNode(new SourceTransformer().toStreamSource(in
205: .getContent()));
206: log.info(new SourceTransformer().toString(node));
207:
208: CachedXPathAPI cachedXPathAPI = new CachedXPathAPI();
209: NodeIterator iterator = cachedXPathAPI.selectNodeIterator(node,
210: "//*[local-name() = 'userId']");
211: Element root = (Element) iterator.nextNode();
212: QName qname = DOMUtil.createQName(root, root.getAttributeNS(
213: "http://www.w3.org/2001/XMLSchema-instance", "type"));
214: assertEquals("http://www.w3.org/2001/XMLSchema", qname
215: .getNamespaceURI());
216: assertEquals("string", qname.getLocalPart());
217: }
218:
219: public void testNamespaces() throws Exception {
220: ActivationSpec as = new ActivationSpec();
221: as.setId("receiver");
222: ReceiverComponent receiver = new ReceiverComponent();
223: as.setComponent(receiver);
224: as.setService(new QName("receiver"));
225: container.activateComponent(as);
226:
227: as = new ActivationSpec();
228: as.setId("http");
229: as.setDestinationService(new QName("receiver"));
230: HttpSoapConnector http = new HttpSoapConnector();
231: http.setDefaultInOut(false);
232: http.setPort(8100);
233: as.setComponent(http);
234: container.activateComponent(as);
235:
236: URLConnection connection = new URL("http://localhost:8100")
237: .openConnection();
238: connection.setDoOutput(true);
239: connection.setDoInput(true);
240: OutputStream os = connection.getOutputStream();
241: // Post the request file.
242: InputStream fis = getClass().getResourceAsStream(
243: "soap-response.xml");
244: FileUtil.copyInputStream(fis, os);
245: connection.getInputStream();
246:
247: receiver.getMessageList().assertMessagesReceived(1);
248: NormalizedMessage msg = (NormalizedMessage) receiver
249: .getMessageList().flushMessages().get(0);
250:
251: Node node = new SourceTransformer()
252: .toDOMNode(new SourceTransformer().toStreamSource(msg
253: .getContent()));
254: log.info(new SourceTransformer().toString(node));
255:
256: CachedXPathAPI cachedXPathAPI = new CachedXPathAPI();
257: NodeIterator iterator = cachedXPathAPI.selectNodeIterator(node,
258: "//*[local-name() = 'userId']");
259: Element root = (Element) iterator.nextNode();
260: QName qname = DOMUtil.createQName(root, root.getAttributeNS(
261: "http://www.w3.org/2001/XMLSchema-instance", "type"));
262: assertEquals("http://www.w3.org/2001/XMLSchema", qname
263: .getNamespaceURI());
264: assertEquals("string", qname.getLocalPart());
265: }
266:
267: public void testMaxThreadsForJettySocketListener() throws Exception {
268: int maxThreads = 512;
269:
270: HttpSoapConnector httpSoap = new HttpSoapConnector();
271: httpSoap.setMaxThreads(maxThreads);
272: assertEquals(
273: "The maxThreads value on the HttpSoapConnector is not correct!",
274: maxThreads, httpSoap.getMaxThreads());
275:
276: HttpConnector http = new HttpConnector();
277: http.setMaxThreads(maxThreads);
278: assertEquals(
279: "The maxThreads value on the HttpConnector is not correct!",
280: maxThreads, http.getMaxThreads());
281: }
282:
283: }
|