KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > components > http > HttpSoapAttachmentsTest


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.components.http;
18
19 import java.io.ByteArrayInputStream JavaDoc;
20 import java.io.ByteArrayOutputStream JavaDoc;
21 import java.io.File JavaDoc;
22 import java.io.InputStream JavaDoc;
23
24 import javax.activation.DataHandler JavaDoc;
25 import javax.activation.FileDataSource JavaDoc;
26 import javax.mail.MessagingException JavaDoc;
27 import javax.xml.namespace.QName JavaDoc;
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 JavaDoc {
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 JavaDoc {
58         if (container != null) {
59             container.shutDown();
60         }
61     }
62     
63     public void testWithAttachments() throws Exception JavaDoc {
64         ActivationSpec as = new ActivationSpec();
65         as.setId("echo");
66         as.setComponent(new EchoComponent());
67         as.setService(new QName JavaDoc("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 JavaDoc("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 JavaDoc bos = new ByteArrayOutputStream JavaDoc();
81         sendAtts.write(bos);
82         InputStream JavaDoc is = new ByteArrayInputStream JavaDoc(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 JavaDoc createDataHandler(String JavaDoc name) throws MessagingException JavaDoc {
91         File JavaDoc f = new File JavaDoc(getClass().getResource(name).getPath());
92         FileDataSource JavaDoc fs = new FileDataSource JavaDoc(f);
93         return new DataHandler JavaDoc(fs);
94     }
95 }
96
Popular Tags