1 17 package org.apache.servicemix.http; 18 19 import java.io.ByteArrayOutputStream ; 20 import java.io.File ; 21 22 import javax.jbi.messaging.ExchangeStatus; 23 import javax.jbi.messaging.InOut; 24 import javax.xml.namespace.QName ; 25 26 import org.apache.commons.httpclient.HttpClient; 27 import org.apache.commons.httpclient.methods.PostMethod; 28 import org.apache.commons.httpclient.methods.RequestEntity; 29 import org.apache.commons.httpclient.methods.multipart.FilePart; 30 import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity; 31 import org.apache.commons.httpclient.methods.multipart.Part; 32 import org.apache.commons.httpclient.methods.multipart.StringPart; 33 import org.apache.commons.logging.Log; 34 import org.apache.commons.logging.LogFactory; 35 import org.apache.servicemix.client.DefaultServiceMixClient; 36 import org.apache.servicemix.jbi.jaxp.SourceTransformer; 37 import org.apache.servicemix.jbi.jaxp.StringSource; 38 import org.apache.servicemix.tck.SpringTestSupport; 39 import org.apache.xbean.spring.context.ClassPathXmlApplicationContext; 40 import org.springframework.context.support.AbstractXmlApplicationContext; 41 42 public class HttpSpringTest extends SpringTestSupport { 43 44 private static Log logger = LogFactory.getLog(HttpSpringTest.class); 45 46 protected void setUp() throws Exception { 47 System.setProperty("javax.net.debug", "all"); 48 super.setUp(); 49 } 50 51 public void test() throws Exception { 52 DefaultServiceMixClient client = new DefaultServiceMixClient(jbi); 53 InOut me = client.createInOutExchange(); 54 me.setService(new QName ("http://test", "MyProviderService")); 55 me.getInMessage().setContent(new StringSource("<echo xmlns='http://test'><echoin0>world</echoin0></echo>")); 56 client.sendSync(me); 57 if (me.getStatus() == ExchangeStatus.ERROR) { 58 if (me.getFault() != null) { 59 fail("Received fault: " + new SourceTransformer().toString(me.getFault().getContent())); 60 } else if (me.getError() != null) { 61 throw me.getError(); 62 } else { 63 fail("Received ERROR status"); 64 } 65 } else { 66 logger.info(new SourceTransformer().toString(me.getOutMessage().getContent())); 67 } 68 } 69 70 public void testSsl() throws Exception { 71 DefaultServiceMixClient client = new DefaultServiceMixClient(jbi); 72 InOut me = client.createInOutExchange(); 73 me.setService(new QName ("http://test/ssl", "MyProviderService")); 74 me.getInMessage().setContent(new StringSource("<echo xmlns='http://test'><echoin0>world</echoin0></echo>")); 75 client.sendSync(me); 76 if (me.getStatus() == ExchangeStatus.ERROR) { 77 if (me.getFault() != null) { 78 fail("Received fault: " + new SourceTransformer().toString(me.getFault().getContent())); 79 } else if (me.getError() != null) { 80 throw me.getError(); 81 } else { 82 fail("Received ERROR status"); 83 } 84 } else { 85 logger.info(new SourceTransformer().toString(me.getOutMessage().getContent())); 86 } 87 } 88 89 public void testMimeWithHttpClient() throws Exception { 90 File f = new File (getClass().getResource("servicemix.jpg").getFile()); 91 PostMethod filePost = new PostMethod("http://localhost:8192/Service/"); 92 Part[] parts = { 93 new StringPart("request", "<dummy/>"), 94 new FilePart(f.getName(), f) }; 95 RequestEntity entity = new MultipartRequestEntity(parts, filePost.getParams()); 96 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 97 entity.writeRequest(baos); 98 System.err.println(baos); 99 filePost.setRequestEntity(entity); 100 HttpClient client = new HttpClient(); 101 int status = client.executeMethod(filePost); 102 assertEquals(200, status); 103 filePost.releaseConnection(); 104 } 105 106 protected AbstractXmlApplicationContext createBeanFactory() { 107 return new ClassPathXmlApplicationContext("org/apache/servicemix/http/spring.xml"); 108 } 109 110 } 111 | Popular Tags |