1 17 package org.apache.servicemix.components.http; 18 19 import java.io.ByteArrayOutputStream ; 20 import java.io.InputStream ; 21 import java.io.OutputStream ; 22 import java.net.URL ; 23 import java.net.URLConnection ; 24 25 import javax.jbi.messaging.InOnly; 26 import javax.jbi.messaging.NormalizedMessage; 27 import javax.xml.namespace.QName ; 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 ; 49 import org.w3c.dom.Node ; 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 { 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 { 68 if (container != null) { 69 container.shutDown(); 70 } 71 } 72 73 public void testInOut() throws Exception { 74 int PORT = 7012; 75 ActivationSpec as = new ActivationSpec(); 76 as.setId("echo"); 77 as.setComponent(new EchoComponent()); 78 as.setService(new QName ("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 ("echo")); 84 container.activateComponent(as); 85 86 URLConnection connection = new URL ("http://localhost:" + PORT).openConnection(); 87 connection.setDoOutput(true); 88 connection.setDoInput(true); 89 OutputStream os = connection.getOutputStream(); 90 InputStream fis = getClass().getResourceAsStream("soap-request.xml"); 92 FileUtil.copyInputStream(fis, os); 93 InputStream is = connection.getInputStream(); 95 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 96 FileUtil.copyInputStream(is, baos); 97 System.out.println(baos.toString()); 98 } 99 100 public void testInOnly() throws Exception { 101 int PORT = 7013; 102 ActivationSpec as = new ActivationSpec(); 103 as.setId("trace"); 104 as.setComponent(new TraceComponent()); 105 as.setService(new QName ("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 ("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 { 120 String 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 { 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 ("mock")); 152 container.activateComponent(as); 153 154 as = new ActivationSpec(); 155 as.setId("http"); 156 as.setDestinationService(new QName ("mock")); 157 HttpConnector http = new HttpConnector(); 158 http.setPort(8100); 159 as.setComponent(http); 160 container.activateComponent(as); 161 162 String 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 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 root = (Element ) iterator.nextNode(); 191 QName 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 { 197 ActivationSpec as = new ActivationSpec(); 198 as.setId("receiver"); 199 ReceiverComponent receiver = new ReceiverComponent(); 200 as.setComponent(receiver); 201 as.setService(new QName ("receiver")); 202 container.activateComponent(as); 203 204 as = new ActivationSpec(); 205 as.setId("http"); 206 as.setDestinationService(new QName ("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 connection = new URL ("http://localhost:8100").openConnection(); 214 connection.setDoOutput(true); 215 connection.setDoInput(true); 216 OutputStream os = connection.getOutputStream(); 217 InputStream 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 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 root = (Element ) iterator.nextNode(); 231 QName 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 { 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 |