1 17 package org.apache.servicemix.jsr181; 18 19 import javax.activation.DataHandler ; 20 import javax.jbi.messaging.InOut; 21 import javax.jbi.servicedesc.ServiceEndpoint; 22 import javax.naming.InitialContext ; 23 import javax.xml.namespace.QName ; 24 25 import junit.framework.TestCase; 26 27 import org.apache.servicemix.client.DefaultServiceMixClient; 28 import org.apache.servicemix.client.Destination; 29 import org.apache.servicemix.jbi.container.JBIContainer; 30 import org.apache.servicemix.jbi.jaxp.SourceTransformer; 31 import org.apache.servicemix.jbi.jaxp.StringSource; 32 import org.apache.servicemix.jbi.util.ByteArrayDataSource; 33 import org.w3c.dom.Document ; 34 35 public class Jsr181MTOMTest extends TestCase { 36 37 protected JBIContainer container; 38 39 protected void setUp() throws Exception { 40 container = new JBIContainer(); 41 container.setUseMBeanServer(false); 42 container.setCreateMBeanServer(false); 43 container.setMonitorInstallationDirectory(false); 44 container.setNamingContext(new InitialContext ()); 45 container.setEmbedded(true); 46 container.init(); 47 } 48 49 protected void tearDown() throws Exception { 50 if (container != null) { 51 container.shutDown(); 52 } 53 } 54 55 public void testMtom() throws Exception { 56 Jsr181SpringComponent jsr181 = new Jsr181SpringComponent(); 57 Jsr181Endpoint ep = new Jsr181Endpoint(); 58 ep.setPojo(new EchoWithAttachment()); 59 ep.setMtomEnabled(true); 60 jsr181.setEndpoints(new Jsr181Endpoint[] { ep }); 61 62 container.activateComponent(jsr181, "jsr181"); 63 container.start(); 64 65 QName service = new QName ("http://jsr181.servicemix.apache.org", "EchoWithAttachment"); 66 ServiceEndpoint se = container.getRegistry().getEndpointsForService(service)[0]; 67 Document doc = container.getRegistry().getEndpointDescriptor(se); 68 String wsdl = new SourceTransformer().toString(doc); 69 System.err.println(wsdl); 70 71 DefaultServiceMixClient client = new DefaultServiceMixClient(container); 72 Destination dest = client.createDestination("service:http://jsr181.servicemix.apache.org/EchoWithAttachment"); 73 InOut me = dest.createInOutExchange(); 74 me.getInMessage().setContent(new StringSource("<echo xmlns:xop='http://www.w3.org/2004/08/xop/include'><msg>hello world</msg><binary><xop:Include HREF='binary'/></binary></echo>")); 75 me.getInMessage().addAttachment("binary", new DataHandler (new ByteArrayDataSource(new byte[] { 0, 1 , 2}, "image/jpg"))); 76 77 client.sendSync(me); 78 79 } 80 81 public static class EchoWithAttachment { 82 83 92 93 public String echo(String msg, DataHandler binary) { 94 if (binary == null || binary.getDataSource() == null) { 95 throw new NullPointerException ("binary is null"); 96 } 97 return "Echo: " + msg; 98 } 99 } 100 101 } 102 | Popular Tags |