KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > jsr181 > Jsr181MTOMTest


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.servicemix.jsr181;
18
19 import javax.activation.DataHandler JavaDoc;
20 import javax.jbi.messaging.InOut;
21 import javax.jbi.servicedesc.ServiceEndpoint;
22 import javax.naming.InitialContext JavaDoc;
23 import javax.xml.namespace.QName JavaDoc;
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 JavaDoc;
34
35 public class Jsr181MTOMTest extends TestCase {
36
37     protected JBIContainer container;
38     
39     protected void setUp() throws Exception JavaDoc {
40         container = new JBIContainer();
41         container.setUseMBeanServer(false);
42         container.setCreateMBeanServer(false);
43         container.setMonitorInstallationDirectory(false);
44         container.setNamingContext(new InitialContext JavaDoc());
45         container.setEmbedded(true);
46         container.init();
47     }
48     
49     protected void tearDown() throws Exception JavaDoc {
50         if (container != null) {
51             container.shutDown();
52         }
53     }
54
55     public void testMtom() throws Exception JavaDoc {
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 JavaDoc service = new QName JavaDoc("http://jsr181.servicemix.apache.org", "EchoWithAttachment");
66         ServiceEndpoint se = container.getRegistry().getEndpointsForService(service)[0];
67         Document JavaDoc doc = container.getRegistry().getEndpointDescriptor(se);
68         String JavaDoc 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 JavaDoc(new ByteArrayDataSource(new byte[] { 0, 1 , 2}, "image/jpg")));
76         
77         client.sendSync(me);
78         
79     }
80     
81     public static class EchoWithAttachment {
82         
83         /*
84          * Do not use byte[] until xfire-1.2
85         public String echo(String msg, byte[] binary) {
86             if (binary == null || binary.length == 0) {
87                 throw new NullPointerException("binary is null");
88             }
89             return "Echo: " + msg;
90         }
91         */

92         
93         public String JavaDoc echo(String JavaDoc msg, DataHandler JavaDoc binary) {
94             if (binary == null || binary.getDataSource() == null) {
95                 throw new NullPointerException JavaDoc("binary is null");
96             }
97             return "Echo: " + msg;
98         }
99     }
100     
101 }
102
Popular Tags