1 17 package org.apache.servicemix.components.http; 18 19 import java.io.ByteArrayInputStream ; 20 import java.io.ByteArrayOutputStream ; 21 import java.io.File ; 22 import java.io.InputStream ; 23 24 import javax.activation.DataHandler ; 25 import javax.activation.FileDataSource ; 26 import javax.mail.MessagingException ; 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.servicemix.components.http.HttpSoapConnector; 35 import org.apache.servicemix.components.util.EchoComponent; 36 import org.apache.servicemix.jbi.container.ActivationSpec; 37 import org.apache.servicemix.jbi.container.JBIContainer; 38 import org.codehaus.xfire.attachments.JavaMailAttachments; 39 import org.codehaus.xfire.attachments.SimpleAttachment; 40 41 public class HttpSoapAttachmentsTest extends TestCase { 42 43 private static final int PORT = 7012; 44 45 protected JBIContainer container; 46 47 protected void setUp() throws Exception { 48 container = new JBIContainer(); 49 container.setMonitorInstallationDirectory(false); 50 container.setUseMBeanServer(false); 51 container.setCreateMBeanServer(false); 52 container.setEmbedded(true); 53 container.init(); 54 container.start(); 55 } 56 57 protected void tearDown() throws Exception { 58 if (container != null) { 59 container.shutDown(); 60 } 61 } 62 63 public void testWithAttachments() throws Exception { 64 ActivationSpec as = new ActivationSpec(); 65 as.setId("echo"); 66 as.setComponent(new EchoComponent()); 67 as.setService(new QName ("echo")); 68 container.activateComponent(as); 69 as = new ActivationSpec(); 70 as.setId("xfireBinding"); 71 as.setComponent(new HttpSoapConnector(null, PORT, true)); 72 as.setDestinationService(new QName ("echo")); 73 container.activateComponent(as); 74 75 JavaMailAttachments sendAtts = new JavaMailAttachments(); 76 sendAtts.setSoapMessage(new SimpleAttachment("soap-request.xml", 77 createDataHandler("soap-request.xml"))); 78 sendAtts.addPart(new SimpleAttachment("ServiceMix.jpg", 79 createDataHandler("ServiceMix.jpg"))); 80 ByteArrayOutputStream bos = new ByteArrayOutputStream (); 81 sendAtts.write(bos); 82 InputStream is = new ByteArrayInputStream (bos.toByteArray()); 83 PostMethod method = new PostMethod("http://localhost:" + PORT); 84 method.setRequestEntity(new InputStreamRequestEntity(is)); 85 method.setRequestHeader("Content-Type", sendAtts.getContentType()); 86 new HttpClient().executeMethod(method); 87 System.out.println(method.getResponseBodyAsString()); 88 } 89 90 private DataHandler createDataHandler(String name) throws MessagingException { 91 File f = new File (getClass().getResource(name).getPath()); 92 FileDataSource fs = new FileDataSource (f); 93 return new DataHandler (fs); 94 } 95 } 96 | Popular Tags |