1 17 package org.apache.servicemix.http; 18 19 import java.io.ByteArrayOutputStream ; 20 import java.io.File ; 21 import java.io.FileInputStream ; 22 import java.io.IOException ; 23 import java.net.URI ; 24 import java.util.List ; 25 26 import javax.activation.DataHandler ; 27 import javax.activation.DataSource ; 28 import javax.jbi.messaging.ExchangeStatus; 29 import javax.jbi.messaging.Fault; 30 import javax.jbi.messaging.InOut; 31 import javax.jbi.messaging.MessageExchange; 32 import javax.jbi.messaging.MessagingException; 33 import javax.jbi.messaging.NormalizedMessage; 34 import javax.servlet.http.HttpServletResponse ; 35 import javax.xml.namespace.QName ; 36 import javax.xml.transform.stream.StreamSource ; 37 38 import junit.framework.TestCase; 39 40 import org.apache.commons.httpclient.HttpClient; 41 import org.apache.commons.httpclient.HttpMethod; 42 import org.apache.commons.httpclient.HttpMethodRetryHandler; 43 import org.apache.commons.httpclient.methods.InputStreamRequestEntity; 44 import org.apache.commons.httpclient.methods.PostMethod; 45 import org.apache.commons.httpclient.methods.StringRequestEntity; 46 import org.apache.commons.httpclient.params.HttpMethodParams; 47 import org.apache.servicemix.client.DefaultServiceMixClient; 48 import org.apache.servicemix.client.Destination; 49 import org.apache.servicemix.client.ServiceMixClient; 50 import org.apache.servicemix.components.util.EchoComponent; 51 import org.apache.servicemix.components.util.TransformComponentSupport; 52 import org.apache.servicemix.jbi.FaultException; 53 import org.apache.servicemix.jbi.container.JBIContainer; 54 import org.apache.servicemix.jbi.jaxp.SourceTransformer; 55 import org.apache.servicemix.jbi.jaxp.StringSource; 56 import org.apache.servicemix.jbi.resolver.ServiceNameEndpointResolver; 57 import org.apache.servicemix.jbi.util.ByteArrayDataSource; 58 import org.apache.servicemix.jbi.util.DOMUtil; 59 import org.apache.servicemix.jbi.util.FileUtil; 60 import org.apache.servicemix.soap.marshalers.JBIMarshaler; 61 import org.apache.servicemix.soap.marshalers.SoapMarshaler; 62 import org.apache.servicemix.tck.ReceiverComponent; 63 import org.w3c.dom.Document ; 64 import org.w3c.dom.Element ; 65 import org.w3c.dom.Node ; 66 67 import com.ibm.wsdl.util.xml.DOMUtils; 68 69 public class HttpSoapTest extends TestCase { 70 71 protected JBIContainer container; 72 73 protected void setUp() throws Exception { 74 container = new JBIContainer(); 75 container.setUseMBeanServer(false); 76 container.setCreateMBeanServer(false); 77 container.setEmbedded(true); 78 container.init(); 79 } 80 81 protected void tearDown() throws Exception { 82 if (container != null) { 83 container.shutDown(); 84 } 85 } 86 87 public void testFaultOnParse() throws Exception { 88 HttpSpringComponent http = new HttpSpringComponent(); 89 HttpEndpoint ep = new HttpEndpoint(); 90 ep.setService(new QName ("urn:test", "echo")); 91 ep.setEndpoint("echo"); 92 ep.setLocationURI("http://localhost:8192/ep1/"); 93 ep.setRoleAsString("consumer"); 94 ep.setDefaultMep(URI.create("http://www.w3.org/2004/08/wsdl/in-out")); 95 ep.setSoap(true); 96 http.setEndpoints(new HttpEndpoint[] { ep }); 97 container.activateComponent(http, "http"); 98 container.start(); 99 100 PostMethod method = new PostMethod("http://localhost:8192/ep1/"); 101 method.setRequestEntity(new StringRequestEntity("<hello>world</hello>")); 102 int state = new HttpClient().executeMethod(method); 103 assertEquals(HttpServletResponse.SC_BAD_REQUEST, state); 104 FileUtil.copyInputStream(method.getResponseBodyAsStream(), System.out); 105 } 106 107 public void testSoap() throws Exception { 108 EchoComponent echo = new EchoComponent(); 109 echo.setService(new QName ("urn:test", "echo")); 110 echo.setEndpoint("echo"); 111 container.activateComponent(echo, "echo"); 112 113 HttpSpringComponent http = new HttpSpringComponent(); 114 115 HttpEndpoint ep1 = new HttpEndpoint(); 116 ep1.setService(new QName ("urn:test", "echo")); 117 ep1.setEndpoint("echo"); 118 ep1.setLocationURI("http://localhost:8192/ep1/"); 119 ep1.setRoleAsString("consumer"); 120 ep1.setDefaultMep(URI.create("http://www.w3.org/2004/08/wsdl/in-out")); 121 ep1.setSoap(true); 122 123 HttpEndpoint ep2 = new HttpEndpoint(); 124 ep2.setService(new QName ("urn:test", "s2")); 125 ep2.setEndpoint("ep2"); 126 ep2.setLocationURI("http://localhost:8192/ep1/"); 127 ep2.setRoleAsString("provider"); 128 ep2.setSoap(true); 129 130 http.setEndpoints(new HttpEndpoint[] { ep1, ep2 }); 131 132 container.activateComponent(http, "http"); 133 134 container.start(); 135 136 ServiceMixClient client = new DefaultServiceMixClient(container); 137 client.request(new ServiceNameEndpointResolver(new QName ("urn:test", "s2")), null, null, 138 new StreamSource (getClass().getResourceAsStream("soap-request.xml"))); 139 140 } 141 142 public void testSoapRoundtripConsumerProvider() throws Exception { 143 EchoComponent echo = new EchoComponent(); 144 echo.setService(new QName ("urn:test", "echo")); 145 echo.setEndpoint("echo"); 146 container.activateComponent(echo, "echo"); 147 148 HttpSpringComponent http = new HttpSpringComponent(); 149 150 HttpEndpoint ep1 = new HttpEndpoint(); 151 ep1.setService(new QName ("urn:test", "s1")); 152 ep1.setEndpoint("ep1"); 153 ep1.setTargetService(new QName ("urn:test", "echo")); 154 ep1.setLocationURI("http://localhost:8192/ep1/"); 155 ep1.setRoleAsString("consumer"); 156 ep1.setDefaultMep(URI.create("http://www.w3.org/2004/08/wsdl/in-out")); 157 ep1.setSoap(true); 158 159 HttpEndpoint ep2 = new HttpEndpoint(); 160 ep2.setService(new QName ("urn:test", "s2")); 161 ep2.setEndpoint("ep2"); 162 ep2.setLocationURI("http://localhost:8192/ep1/"); 163 ep2.setRoleAsString("provider"); 164 ep2.setSoap(true); 165 166 http.setEndpoints(new HttpEndpoint[] { ep1, ep2 }); 167 168 container.activateComponent(http, "http"); 169 170 container.start(); 171 172 ServiceMixClient client = new DefaultServiceMixClient(container); 173 Destination dest = client.createDestination("service:urn:test:s2"); 174 InOut me = dest.createInOutExchange(); 175 me.getInMessage().setContent(new StringSource("<hello>world</hello>")); 176 client.sendSync(me); 177 assertEquals(ExchangeStatus.ACTIVE, me.getStatus()); 178 String str = new SourceTransformer().contentToString(me.getOutMessage()); 179 client.done(me); 180 System.err.println(str); 181 } 182 183 public void testSoapRoundtripProviderConsumerProvider() throws Exception { 184 EchoComponent echo = new EchoComponent(); 185 echo.setService(new QName ("urn:test", "echo")); 186 echo.setEndpoint("echo"); 187 container.activateComponent(echo, "echo"); 188 189 HttpSpringComponent http = new HttpSpringComponent(); 190 191 HttpEndpoint ep1 = new HttpEndpoint(); 192 ep1.setService(new QName ("urn:test", "s1")); 193 ep1.setEndpoint("ep1"); 194 ep1.setTargetService(new QName ("urn:test", "s2")); 195 ep1.setLocationURI("http://localhost:8192/ep1/"); 196 ep1.setRoleAsString("consumer"); 197 ep1.setDefaultMep(URI.create("http://www.w3.org/2004/08/wsdl/in-out")); 198 ep1.setSoap(true); 199 200 HttpEndpoint ep2 = new HttpEndpoint(); 201 ep2.setService(new QName ("urn:test", "s2")); 202 ep2.setEndpoint("ep2"); 203 ep2.setLocationURI("http://localhost:8192/ep3/"); 204 ep2.setRoleAsString("provider"); 205 ep2.setSoap(true); 206 207 HttpEndpoint ep3 = new HttpEndpoint(); 208 ep3.setService(new QName ("urn:test", "s3")); 209 ep3.setEndpoint("ep3"); 210 ep3.setTargetService(new QName ("urn:test", "echo")); 211 ep3.setLocationURI("http://localhost:8192/ep3/"); 212 ep3.setRoleAsString("consumer"); 213 ep3.setDefaultMep(URI.create("http://www.w3.org/2004/08/wsdl/in-out")); 214 ep3.setSoap(true); 215 216 http.setEndpoints(new HttpEndpoint[] { ep1, ep2, ep3 }); 217 218 container.activateComponent(http, "http"); 219 220 container.start(); 221 222 PostMethod method = new PostMethod("http://localhost:8192/ep1/"); 223 method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new HttpMethodRetryHandler() { 224 public boolean retryMethod(HttpMethod method, IOException exception, int executionCount) { 225 return false; 226 } 227 }); 228 method.setRequestEntity(new StringRequestEntity("<env:Envelope xmlns:env='http://www.w3.org/2003/05/soap-envelope'><env:Body><hello>world</hello></env:body</env:Envelope>")); 229 int state = new HttpClient().executeMethod(method); 230 assertEquals(HttpServletResponse.SC_OK, state); 231 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 232 FileUtil.copyInputStream(method.getResponseBodyAsStream(), baos); 233 System.err.println(baos.toString()); 234 } 235 236 public void testSoapFault12() throws Exception { 237 TransformComponentSupport echo = new TransformComponentSupport() { 238 protected boolean transform(MessageExchange exchange, NormalizedMessage in, NormalizedMessage out) throws MessagingException { 239 Fault f = exchange.createFault(); 240 f.setContent(new StringSource("<hello xmlns='myuri'>this is a fault</hello>")); 241 f.setProperty(JBIMarshaler.SOAP_FAULT_REASON, "My reason"); 242 throw new FaultException(null, exchange, f); 243 } 244 }; 245 echo.setService(new QName ("urn:test", "echo")); 246 echo.setEndpoint("echo"); 247 container.activateComponent(echo, "echo"); 248 249 HttpEndpoint ep1 = new HttpEndpoint(); 250 ep1.setService(new QName ("urn:test", "http")); 251 ep1.setEndpoint("ep1"); 252 ep1.setTargetService(new QName ("urn:test", "echo")); 253 ep1.setTargetEndpoint("echo"); 254 ep1.setLocationURI("http://localhost:8193/ep1/"); 255 ep1.setRoleAsString("consumer"); 256 ep1.setDefaultMep(URI.create("http://www.w3.org/2004/08/wsdl/in-out")); 257 ep1.setSoap(true); 258 259 HttpEndpoint ep2 = new HttpEndpoint(); 260 ep2.setService(new QName ("urn:test", "http")); 261 ep2.setEndpoint("ep2"); 262 ep2.setTargetService(new QName ("urn:test", "http")); 263 ep2.setTargetEndpoint("ep3"); 264 ep2.setLocationURI("http://localhost:8193/ep2/"); 265 ep2.setRoleAsString("consumer"); 266 ep2.setDefaultMep(URI.create("http://www.w3.org/2004/08/wsdl/in-out")); 267 ep2.setSoap(true); 268 269 HttpEndpoint ep3 = new HttpEndpoint(); 270 ep3.setService(new QName ("urn:test", "http")); 271 ep3.setEndpoint("ep3"); 272 ep3.setLocationURI("http://localhost:8193/ep1/"); 273 ep3.setRoleAsString("provider"); 274 ep3.setDefaultMep(URI.create("http://www.w3.org/2004/08/wsdl/in-out")); 275 ep3.setSoap(true); 276 277 HttpSpringComponent http = new HttpSpringComponent(); 278 http.setEndpoints(new HttpEndpoint[] { ep1, ep2, ep3 }); 279 container.activateComponent(http, "http1"); 280 281 container.start(); 282 283 PostMethod method = new PostMethod("http://localhost:8193/ep2/"); 284 method.setRequestEntity(new InputStreamRequestEntity(getClass().getResourceAsStream("soap-request-12.xml"))); 285 int state = new HttpClient().executeMethod(method); 286 assertEquals(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, state); 287 SourceTransformer st = new SourceTransformer(); 288 Node node = st.toDOMNode(new StreamSource (method.getResponseBodyAsStream())); 289 System.err.println(st.toString(node)); 290 291 Element e = ((Document ) node).getDocumentElement(); 292 assertEquals(new QName (SoapMarshaler.SOAP_12_URI, SoapMarshaler.ENVELOPE), DOMUtil.getQName(e)); 293 e = DOMUtil.getFirstChildElement(e); 294 assertEquals(new QName (SoapMarshaler.SOAP_12_URI, SoapMarshaler.BODY), DOMUtil.getQName(e)); 295 e = DOMUtil.getFirstChildElement(e); 296 assertEquals(new QName (SoapMarshaler.SOAP_12_URI, SoapMarshaler.FAULT), DOMUtil.getQName(e)); 297 } 298 299 public void testSoapFault11() throws Exception { 300 TransformComponentSupport echo = new TransformComponentSupport() { 301 protected boolean transform(MessageExchange exchange, NormalizedMessage in, NormalizedMessage out) throws MessagingException { 302 Fault f = exchange.createFault(); 303 f.setContent(new StringSource("<hello xmlns='myuri'>this is a fault</hello>")); 304 f.setProperty(JBIMarshaler.SOAP_FAULT_REASON, "My reason"); 305 throw new FaultException(null, exchange, f); 306 } 307 }; 308 echo.setService(new QName ("urn:test", "echo")); 309 echo.setEndpoint("echo"); 310 container.activateComponent(echo, "echo"); 311 312 HttpEndpoint ep1 = new HttpEndpoint(); 313 ep1.setService(new QName ("urn:test", "http")); 314 ep1.setEndpoint("ep1"); 315 ep1.setTargetService(new QName ("urn:test", "echo")); 316 ep1.setTargetEndpoint("echo"); 317 ep1.setLocationURI("http://localhost:8194/ep1/"); 318 ep1.setRoleAsString("consumer"); 319 ep1.setDefaultMep(URI.create("http://www.w3.org/2004/08/wsdl/in-out")); 320 ep1.setSoap(true); 321 322 HttpEndpoint ep2 = new HttpEndpoint(); 323 ep2.setService(new QName ("urn:test", "http")); 324 ep2.setEndpoint("ep2"); 325 ep2.setTargetService(new QName ("urn:test", "http")); 326 ep2.setTargetEndpoint("ep3"); 327 ep2.setLocationURI("http://localhost:8194/ep2/"); 328 ep2.setRoleAsString("consumer"); 329 ep2.setDefaultMep(URI.create("http://www.w3.org/2004/08/wsdl/in-out")); 330 ep2.setSoap(true); 331 332 HttpEndpoint ep3 = new HttpEndpoint(); 333 ep3.setService(new QName ("urn:test", "http")); 334 ep3.setEndpoint("ep3"); 335 ep3.setLocationURI("http://localhost:8194/ep1/"); 336 ep3.setRoleAsString("provider"); 337 ep3.setDefaultMep(URI.create("http://www.w3.org/2004/08/wsdl/in-out")); 338 ep3.setSoap(true); 339 340 HttpSpringComponent http = new HttpSpringComponent(); 341 http.setEndpoints(new HttpEndpoint[] { ep1, ep2, ep3 }); 342 container.activateComponent(http, "http1"); 343 344 container.start(); 345 346 PostMethod method = new PostMethod("http://localhost:8194/ep2/"); 347 method.setRequestEntity(new InputStreamRequestEntity(getClass().getResourceAsStream("soap-request.xml"))); 348 int state = new HttpClient().executeMethod(method); 349 String str = method.getResponseBodyAsString(); 350 System.err.println(str); 351 assertEquals(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, state); 352 SourceTransformer st = new SourceTransformer(); 353 Node node = st.toDOMNode(new StringSource(str)); 354 355 Element e = ((Document ) node).getDocumentElement(); 356 assertEquals(new QName (SoapMarshaler.SOAP_11_URI, SoapMarshaler.ENVELOPE), DOMUtil.getQName(e)); 357 e = DOMUtils.getFirstChildElement(e); 358 assertEquals(new QName (SoapMarshaler.SOAP_11_URI, SoapMarshaler.BODY), DOMUtil.getQName(e)); 359 e = DOMUtils.getFirstChildElement(e); 360 assertEquals(new QName (SoapMarshaler.SOAP_11_URI, SoapMarshaler.FAULT), DOMUtil.getQName(e)); 361 } 362 363 public void testSoapXml() throws Exception { 364 ReceiverComponent echo = new ReceiverComponent(); 365 echo.setService(new QName ("urn:test", "echo")); 366 echo.setEndpoint("echo"); 367 container.activateComponent(echo, "echo"); 368 369 HttpEndpoint ep1 = new HttpEndpoint(); 370 ep1.setService(new QName ("urn:test", "http")); 371 ep1.setEndpoint("ep1"); 372 ep1.setTargetService(new QName ("urn:test", "echo")); 373 ep1.setTargetEndpoint("echo"); 374 ep1.setLocationURI("http://localhost:8193/ep1/"); 375 ep1.setRoleAsString("consumer"); 376 ep1.setDefaultMep(URI.create("http://www.w3.org/2004/08/wsdl/in-only")); 377 ep1.setSoap(false); 378 379 HttpEndpoint ep2 = new HttpEndpoint(); 380 ep2.setService(new QName ("urn:test", "http")); 381 ep2.setEndpoint("ep2"); 382 ep2.setTargetService(new QName ("urn:test", "http")); 383 ep2.setTargetEndpoint("ep3"); 384 ep2.setLocationURI("http://localhost:8193/ep2/"); 385 ep2.setRoleAsString("consumer"); 386 ep2.setDefaultMep(URI.create("http://www.w3.org/2004/08/wsdl/in-only")); 387 ep2.setSoap(true); 388 389 HttpEndpoint ep3 = new HttpEndpoint(); 390 ep3.setService(new QName ("urn:test", "http")); 391 ep3.setEndpoint("ep3"); 392 ep3.setLocationURI("http://localhost:8193/ep1/"); 393 ep3.setRoleAsString("provider"); 394 ep3.setDefaultMep(URI.create("http://www.w3.org/2004/08/wsdl/in-only")); 395 ep3.setSoap(true); 396 397 HttpSpringComponent http = new HttpSpringComponent(); 398 http.setEndpoints(new HttpEndpoint[] { ep1, ep2, ep3 }); 399 container.activateComponent(http, "http1"); 400 401 container.start(); 402 403 PostMethod method = new PostMethod("http://localhost:8193/ep2/"); 404 method.setRequestEntity(new InputStreamRequestEntity(getClass().getResourceAsStream("request.xml"))); 405 new HttpClient().executeMethod(method); 406 407 echo.getMessageList().assertMessagesReceived(1); 408 List msgs = echo.getMessageList().flushMessages(); 409 NormalizedMessage msg = (NormalizedMessage) msgs.get(0); 410 SourceTransformer st = new SourceTransformer(); 411 Element e = st.toDOMElement(msg); 412 String strMsg = DOMUtil.asXML(e); 413 System.err.println(strMsg); 414 415 assertEquals(new QName (SoapMarshaler.SOAP_12_URI, SoapMarshaler.ENVELOPE), DOMUtil.getQName(e)); 416 e = DOMUtil.getFirstChildElement(e); 417 assertEquals(new QName (SoapMarshaler.SOAP_12_URI, SoapMarshaler.BODY), DOMUtil.getQName(e)); 418 e = DOMUtil.getFirstChildElement(e); 419 assertEquals(new QName ("http://ws.location.services.cardinal.com/", "listAllProvider"), DOMUtil.getQName(e)); 420 e = DOMUtil.getFirstChildElement(e); 421 assertEquals(new QName ("", "clientSessionGuid"), DOMUtil.getQName(e)); 422 } 423 424 public void testAttachments() throws Exception { 425 EchoComponent echo = new EchoComponent(); 426 echo.setService(new QName ("urn:test", "echo")); 427 echo.setEndpoint("echo"); 428 container.activateComponent(echo, "echo"); 429 430 HttpSpringComponent http = new HttpSpringComponent(); 431 432 HttpEndpoint ep0 = new HttpEndpoint(); 433 ep0.setService(new QName ("urn:test", "s0")); 434 ep0.setEndpoint("ep0"); 435 ep0.setLocationURI("http://localhost:8192/ep1/"); 436 ep0.setRoleAsString("provider"); 437 ep0.setSoap(true); 438 439 HttpEndpoint ep1 = new HttpEndpoint(); 440 ep1.setService(new QName ("urn:test", "s1")); 441 ep1.setEndpoint("ep1"); 442 ep1.setTargetService(new QName ("urn:test", "s2")); 443 ep1.setLocationURI("http://localhost:8192/ep1/"); 444 ep1.setRoleAsString("consumer"); 445 ep1.setDefaultMep(URI.create("http://www.w3.org/2004/08/wsdl/in-out")); 446 ep1.setSoap(true); 447 448 HttpEndpoint ep2 = new HttpEndpoint(); 449 ep2.setService(new QName ("urn:test", "s2")); 450 ep2.setEndpoint("ep2"); 451 ep2.setLocationURI("http://localhost:8192/ep3/"); 452 ep2.setRoleAsString("provider"); 453 ep2.setSoap(true); 454 455 HttpEndpoint ep3 = new HttpEndpoint(); 456 ep3.setService(new QName ("urn:test", "s3")); 457 ep3.setEndpoint("ep3"); 458 ep3.setTargetService(new QName ("urn:test", "echo")); 459 ep3.setLocationURI("http://localhost:8192/ep3/"); 460 ep3.setRoleAsString("consumer"); 461 ep3.setDefaultMep(URI.create("http://www.w3.org/2004/08/wsdl/in-out")); 462 ep3.setSoap(true); 463 464 http.setEndpoints(new HttpEndpoint[] { ep0, ep1, ep2, ep3 }); 465 466 container.activateComponent(http, "http"); 467 468 container.start(); 469 470 DefaultServiceMixClient client = new DefaultServiceMixClient(container); 471 Destination d = client.createDestination("service:urn:test:s0"); 472 InOut me = d.createInOutExchange(); 473 me.getInMessage().setContent(new StringSource("<hello>world</hello>")); 474 File f = new File (getClass().getResource("servicemix.jpg").getFile()); 475 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 476 FileUtil.copyInputStream(new FileInputStream (f), baos); 477 DataSource ds = new ByteArrayDataSource(baos.toByteArray(), "image/jpeg"); 478 DataHandler dh = new DataHandler (ds); 479 me.getInMessage().addAttachment("image", dh); 480 client.sendSync(me); 481 assertEquals(ExchangeStatus.ACTIVE, me.getStatus()); 482 assertEquals(1, me.getOutMessage().getAttachmentNames().size()); 483 client.done(me); 484 } 485 486 } 487 | Popular Tags |