KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > components > http > HttpSoapTest


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.servicemix.components.http;
18
19 import java.io.ByteArrayOutputStream JavaDoc;
20 import java.io.InputStream JavaDoc;
21 import java.io.OutputStream JavaDoc;
22 import java.net.URL JavaDoc;
23 import java.net.URLConnection JavaDoc;
24
25 import javax.jbi.messaging.InOnly;
26 import javax.jbi.messaging.NormalizedMessage;
27 import javax.xml.namespace.QName JavaDoc;
28
29 import junit.framework.TestCase;
30
31 import org.apache.commons.httpclient.HttpClient;
32 import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
33 import org.apache.commons.httpclient.methods.PostMethod;
34 import org.apache.commons.httpclient.methods.StringRequestEntity;
35 import org.apache.servicemix.components.util.EchoComponent;
36 import org.apache.servicemix.components.util.MockServiceComponent;
37 import org.apache.servicemix.components.util.TraceComponent;
38 import org.apache.servicemix.jbi.container.ActivationSpec;
39 import org.apache.servicemix.jbi.container.JBIContainer;
40 import org.apache.servicemix.jbi.jaxp.SourceTransformer;
41 import org.apache.servicemix.jbi.jaxp.StringSource;
42 import org.apache.servicemix.jbi.messaging.InOnlyImpl;
43 import org.apache.servicemix.jbi.util.DOMUtil;
44 import org.apache.servicemix.jbi.util.FileUtil;
45 import org.apache.servicemix.tck.ReceiverComponent;
46 import org.apache.xpath.CachedXPathAPI;
47 import org.springframework.core.io.ClassPathResource;
48 import org.w3c.dom.Element JavaDoc;
49 import org.w3c.dom.Node JavaDoc;
50 import org.w3c.dom.traversal.NodeIterator;
51
52 public class HttpSoapTest extends TestCase {
53     
54     protected JBIContainer container;
55     
56     protected void setUp() throws Exception JavaDoc {
57         System.setProperty("DEBUG", "true");
58         container = new JBIContainer();
59         container.setMonitorInstallationDirectory(false);
60         container.setUseMBeanServer(false);
61         container.setCreateMBeanServer(false);
62         container.setEmbedded(true);
63         container.init();
64         container.start();
65     }
66     
67     protected void tearDown() throws Exception JavaDoc {
68         if (container != null) {
69             container.shutDown();
70         }
71     }
72     
73     public void testInOut() throws Exception JavaDoc {
74         int PORT = 7012;
75         ActivationSpec as = new ActivationSpec();
76         as.setId("echo");
77         as.setComponent(new EchoComponent());
78         as.setService(new QName JavaDoc("echo"));
79         container.activateComponent(as);
80         as = new ActivationSpec();
81         as.setId("xfireBinding");
82         as.setComponent(new HttpSoapConnector(null, PORT, true));
83         as.setDestinationService(new QName JavaDoc("echo"));
84         container.activateComponent(as);
85         
86         URLConnection JavaDoc connection = new URL JavaDoc("http://localhost:" + PORT).openConnection();
87         connection.setDoOutput(true);
88         connection.setDoInput(true);
89         OutputStream JavaDoc os = connection.getOutputStream();
90         // Post the request file.
91
InputStream JavaDoc fis = getClass().getResourceAsStream("soap-request.xml");
92         FileUtil.copyInputStream(fis, os);
93         // Read the response.
94
InputStream JavaDoc is = connection.getInputStream();
95         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
96         FileUtil.copyInputStream(is, baos);
97         System.out.println(baos.toString());
98     }
99
100     public void testInOnly() throws Exception JavaDoc {
101         int PORT = 7013;
102         ActivationSpec as = new ActivationSpec();
103         as.setId("trace");
104         as.setComponent(new TraceComponent());
105         as.setService(new QName JavaDoc("trace"));
106         container.activateComponent(as);
107         as = new ActivationSpec();
108         as.setId("xfireBinding");
109         as.setComponent(new HttpSoapConnector(null, PORT, false));
110         as.setDestinationService(new QName JavaDoc("trace"));
111         container.activateComponent(as);
112
113         PostMethod method = new PostMethod("http://localhost:" + PORT + "/?name=Guillaume");
114         method.setRequestEntity(new InputStreamRequestEntity(getClass().getResourceAsStream("soap-request.xml")));
115         new HttpClient().executeMethod(method);
116         System.out.println(method.getResponseBodyAsString());
117     }
118
119     public void testMarhaler() throws Exception JavaDoc {
120         String JavaDoc url = "http://64.124.140.30:9090/soap";
121         HttpSoapClientMarshaler marshaler = new HttpSoapClientMarshaler();
122         PostMethod method = new PostMethod(url);
123         method.addRequestHeader("Content-Type", "text/xml");
124         method.addRequestHeader("SOAPAction", "urn:xmethods-delayed-quotes#getQuote");
125         
126         InOnly exchange = new InOnlyImpl("id");
127         NormalizedMessage in = exchange.createMessage();
128         exchange.setInMessage(in);
129         in.setContent(new StringSource("<?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>"));
130         marshaler.fromNMS(method, exchange, in);
131         System.out.println(((StringRequestEntity) method.getRequestEntity()).getContent());
132
133         HttpClient httpClient = new HttpClient();
134         httpClient.executeMethod(method);
135         System.out.println(method.getResponseBodyAsString());
136         
137         exchange = new InOnlyImpl("id");
138         in = exchange.createMessage();
139         exchange.setInMessage(in);
140         marshaler.toNMS(in, method);
141         
142         System.out.println(new SourceTransformer().toString(in.getContent()));
143     }
144     
145     public void testMarshalerNamespaces() throws Exception JavaDoc {
146         ActivationSpec as = new ActivationSpec();
147         as.setId("mock");
148         MockServiceComponent mock = new MockServiceComponent();
149         mock.setResponseResource(new ClassPathResource("org/apache/servicemix/components/http/soap-response.xml"));
150         as.setComponent(mock);
151         as.setService(new QName JavaDoc("mock"));
152         container.activateComponent(as);
153         
154         as = new ActivationSpec();
155         as.setId("http");
156         as.setDestinationService(new QName JavaDoc("mock"));
157         HttpConnector http = new HttpConnector();
158         http.setPort(8100);
159         as.setComponent(http);
160         container.activateComponent(as);
161
162         String JavaDoc url = "http://localhost:8100";
163         HttpSoapClientMarshaler marshaler = new HttpSoapClientMarshaler();
164         PostMethod method = new PostMethod(url);
165         method.addRequestHeader("Content-Type", "text/xml");
166         method.addRequestHeader("SOAPAction", "urn:xmethods-delayed-quotes#getQuote");
167         
168         InOnly exchange = new InOnlyImpl("id");
169         NormalizedMessage in = exchange.createMessage();
170         exchange.setInMessage(in);
171         in.setContent(new StringSource("<?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>"));
172         marshaler.fromNMS(method, exchange, in);
173         System.out.println(((StringRequestEntity) method.getRequestEntity()).getContent());
174
175         HttpClient httpClient = new HttpClient();
176         httpClient.executeMethod(method);
177         System.out.println(method.getResponseBodyAsString());
178         
179         exchange = new InOnlyImpl("id");
180         in = exchange.createMessage();
181         exchange.setInMessage(in);
182         marshaler.toNMS(in, method);
183         
184
185         Node JavaDoc node = new SourceTransformer().toDOMNode(new SourceTransformer().toStreamSource(in.getContent()));
186         System.out.println(new SourceTransformer().toString(node));
187         
188         CachedXPathAPI cachedXPathAPI = new CachedXPathAPI();
189         NodeIterator iterator = cachedXPathAPI.selectNodeIterator(node, "//*[local-name() = 'userId']");
190         Element JavaDoc root = (Element JavaDoc) iterator.nextNode();
191         QName JavaDoc qname = DOMUtil.createQName(root, root.getAttributeNS("http://www.w3.org/2001/XMLSchema-instance", "type"));
192         assertEquals("http://www.w3.org/2001/XMLSchema", qname.getNamespaceURI());
193         assertEquals("string", qname.getLocalPart());
194     }
195     
196     public void testNamespaces() throws Exception JavaDoc {
197         ActivationSpec as = new ActivationSpec();
198         as.setId("receiver");
199         ReceiverComponent receiver = new ReceiverComponent();
200         as.setComponent(receiver);
201         as.setService(new QName JavaDoc("receiver"));
202         container.activateComponent(as);
203         
204         as = new ActivationSpec();
205         as.setId("http");
206         as.setDestinationService(new QName JavaDoc("receiver"));
207         HttpSoapConnector http = new HttpSoapConnector();
208         http.setDefaultInOut(false);
209         http.setPort(8100);
210         as.setComponent(http);
211         container.activateComponent(as);
212
213         URLConnection JavaDoc connection = new URL JavaDoc("http://localhost:8100").openConnection();
214         connection.setDoOutput(true);
215         connection.setDoInput(true);
216         OutputStream JavaDoc os = connection.getOutputStream();
217         // Post the request file.
218
InputStream JavaDoc fis = getClass().getResourceAsStream("soap-response.xml");
219         FileUtil.copyInputStream(fis, os);
220         connection.getInputStream();
221         
222         receiver.getMessageList().assertMessagesReceived(1);
223         NormalizedMessage msg = (NormalizedMessage) receiver.getMessageList().flushMessages().get(0);
224
225         Node JavaDoc node = new SourceTransformer().toDOMNode(new SourceTransformer().toStreamSource(msg.getContent()));
226         System.out.println(new SourceTransformer().toString(node));
227         
228         CachedXPathAPI cachedXPathAPI = new CachedXPathAPI();
229         NodeIterator iterator = cachedXPathAPI.selectNodeIterator(node, "//*[local-name() = 'userId']");
230         Element JavaDoc root = (Element JavaDoc) iterator.nextNode();
231         QName JavaDoc qname = DOMUtil.createQName(root, root.getAttributeNS("http://www.w3.org/2001/XMLSchema-instance", "type"));
232         assertEquals("http://www.w3.org/2001/XMLSchema", qname.getNamespaceURI());
233         assertEquals("string", qname.getLocalPart());
234     }
235     
236     public void testMaxThreadsForJettySocketListener()
237         throws Exception JavaDoc {
238             int maxThreads = 512;
239             
240             HttpSoapConnector httpSoap = new HttpSoapConnector();
241             httpSoap.setMaxThreads(maxThreads);
242             assertEquals("The maxThreads value on the HttpSoapConnector is not correct!", maxThreads, httpSoap.getMaxThreads());
243             
244             HttpConnector http = new HttpConnector();
245             http.setMaxThreads(maxThreads);
246             assertEquals("The maxThreads value on the HttpConnector is not correct!", maxThreads, http.getMaxThreads());
247     }
248
249 }
250
Popular Tags