001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.servicemix.components.http;
018:
019: import java.io.ByteArrayInputStream;
020: import java.io.ByteArrayOutputStream;
021: import java.io.File;
022: import java.io.InputStream;
023:
024: import javax.activation.DataHandler;
025: import javax.activation.FileDataSource;
026: import javax.mail.MessagingException;
027: import javax.xml.namespace.QName;
028:
029: import junit.framework.TestCase;
030:
031: import org.apache.commons.httpclient.HttpClient;
032: import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
033: import org.apache.commons.httpclient.methods.PostMethod;
034: import org.apache.commons.logging.Log;
035: import org.apache.commons.logging.LogFactory;
036: import org.apache.servicemix.components.util.EchoComponent;
037: import org.apache.servicemix.jbi.container.ActivationSpec;
038: import org.apache.servicemix.jbi.container.JBIContainer;
039: import org.codehaus.xfire.attachments.JavaMailAttachments;
040: import org.codehaus.xfire.attachments.SimpleAttachment;
041:
042: public class HttpSoapAttachmentsTest extends TestCase {
043: private static transient Log log = LogFactory
044: .getLog(HttpSoapAttachmentsTest.class);
045:
046: private static final int PORT = 7012;
047:
048: protected JBIContainer container;
049:
050: protected void setUp() throws Exception {
051: container = new JBIContainer();
052: container.setMonitorInstallationDirectory(false);
053: container.setUseMBeanServer(false);
054: container.setCreateMBeanServer(false);
055: container.setEmbedded(true);
056: container.init();
057: container.start();
058: }
059:
060: protected void tearDown() throws Exception {
061: if (container != null) {
062: container.shutDown();
063: }
064: }
065:
066: public void testWithAttachments() throws Exception {
067: ActivationSpec as = new ActivationSpec();
068: as.setId("echo");
069: as.setComponent(new EchoComponent());
070: as.setService(new QName("echo"));
071: container.activateComponent(as);
072: as = new ActivationSpec();
073: as.setId("xfireBinding");
074: as.setComponent(new HttpSoapConnector(null, PORT, true));
075: as.setDestinationService(new QName("echo"));
076: container.activateComponent(as);
077:
078: JavaMailAttachments sendAtts = new JavaMailAttachments();
079: sendAtts.setSoapMessage(new SimpleAttachment(
080: "soap-request.xml",
081: createDataHandler("soap-request.xml")));
082: sendAtts.addPart(new SimpleAttachment("ServiceMix.jpg",
083: createDataHandler("ServiceMix.jpg")));
084: ByteArrayOutputStream bos = new ByteArrayOutputStream();
085: sendAtts.write(bos);
086: InputStream is = new ByteArrayInputStream(bos.toByteArray());
087: PostMethod method = new PostMethod("http://localhost:" + PORT);
088: method.setRequestEntity(new InputStreamRequestEntity(is));
089: method.setRequestHeader("Content-Type", sendAtts
090: .getContentType());
091: new HttpClient().executeMethod(method);
092: log.info(method.getResponseBodyAsString());
093: }
094:
095: private DataHandler createDataHandler(String name)
096: throws MessagingException {
097: File f = new File(getClass().getResource(name).getPath());
098: FileDataSource fs = new FileDataSource(f);
099: return new DataHandler(fs);
100: }
101: }
|