Title: [928] trunk/etc: Handle attachments and in-only on HttpSoapInOutBinding.

Diff

Modified: trunk/components/base/project.xml (927 => 928)

--- trunk/components/base/project.xml	2005-11-24 11:15:32 UTC (rev 927)
+++ trunk/components/base/project.xml	2005-11-24 15:04:45 UTC (rev 928)
@@ -589,6 +589,15 @@
       <id>junit</id>
       <version>${junit_version}</version>
     </dependency>
+    <dependency>
+      <groupId>httpunit</groupId>
+      <artifactId>httpunit</artifactId>
+      <version>${httpunit_version}</version>
+      <url>http://httpunit.sourceforge.net/</url>
+      <properties>
+        <scope>test</scope>
+      </properties>
+    </dependency>
 
 	<dependency>
 	  <groupId>backport-util-concurrent</groupId>
@@ -645,6 +654,9 @@
         <exclude>**/XFireOutBindingTest.*</exclude>
         <exclude>**/XFireBindingTest.*</exclude>
         <exclude>**/ReflectionBindingTest.*</exclude>
+        
+        <!--  Disable until geronimo javamail handles mime multipart -->
+        <exclude>**/HttpSoapAttachmentsTest.*</exclude>
 
       </excludes>
     </unitTest>

Modified: trunk/components/base/src/main/java/org/servicemix/components/http/HttpSoapConnector.java (927 => 928)

--- trunk/components/base/src/main/java/org/servicemix/components/http/HttpSoapConnector.java	2005-11-24 11:15:32 UTC (rev 927)
+++ trunk/components/base/src/main/java/org/servicemix/components/http/HttpSoapConnector.java	2005-11-24 15:04:45 UTC (rev 928)
@@ -36,16 +36,17 @@
 
 	private int port;
 
-	/**
-	 * Constructor
-	 *
-	 * @param host
-	 * @param port
-	 */
-	public HttpSoapConnector(String host, int port) {
-		this.host = host;
-		this.port = port;
-	}
+    /**
+     * Constructor
+     *
+     * @param host
+     * @param port
+     */
+    public HttpSoapConnector(String host, int port, boolean inOut) {
+        this.host = host;
+        this.port = port;
+        this.inOut = inOut;
+    }
 
 	public HttpSoapConnector() {
 	}

Modified: trunk/components/base/src/main/java/org/servicemix/components/http/HttpSoapInOutBinding.java (927 => 928)

--- trunk/components/base/src/main/java/org/servicemix/components/http/HttpSoapInOutBinding.java	2005-11-24 11:15:32 UTC (rev 927)
+++ trunk/components/base/src/main/java/org/servicemix/components/http/HttpSoapInOutBinding.java	2005-11-24 15:04:45 UTC (rev 928)
@@ -18,11 +18,15 @@
 package org.servicemix.components.http;
 
 import java.io.IOException;
+import java.util.Iterator;
 
 import javax.jbi.JBIException;
+import javax.jbi.component.ComponentContext;
 import javax.jbi.messaging.DeliveryChannel;
 import javax.jbi.messaging.ExchangeStatus;
+import javax.jbi.messaging.InOnly;
 import javax.jbi.messaging.InOut;
+import javax.jbi.messaging.MessageExchange;
 import javax.jbi.messaging.MessageExchangeFactory;
 import javax.jbi.messaging.NormalizedMessage;
 import javax.servlet.ServletException;
@@ -34,6 +38,8 @@
 import org.codehaus.xfire.XFire;
 import org.codehaus.xfire.XFireFactory;
 import org.codehaus.xfire.aegis.AegisBindingProvider;
+import org.codehaus.xfire.attachments.Attachment;
+import org.codehaus.xfire.attachments.Attachments;
 import org.codehaus.xfire.fault.XFireFault;
 import org.codehaus.xfire.service.Service;
 import org.codehaus.xfire.service.binding.BeanInvoker;
@@ -47,21 +53,27 @@
 
     protected XFire xfire;
     protected XFireServletController controller;
-    protected Service inOnlyService;
-    protected Service inOutService;
+    protected Service service;
     protected boolean inOut = true;
 
     public HttpSoapInOutBinding() {
+    }
+    
+    public void init(ComponentContext context) throws JBIException {
+        super.init(context);
         xfire = XFireFactory.newInstance().getXFire();
         ObjectServiceFactory factory = new ObjectServiceFactory(xfire.getTransportManager(),
                                                                 new AegisBindingProvider());
+        factory.setVoidOneWay(true);
         factory.setStyle(SoapConstants.STYLE_DOCUMENT);
-        inOnlyService = factory.create(InOnlyService.class);
-        inOnlyService.setInvoker(new BeanInvoker(new InOnlyService(this)));
-        xfire.getServiceRegistry().register(inOnlyService);
-        inOutService = factory.create(InOutService.class);
-        inOutService.setInvoker(new BeanInvoker(new InOutService(this)));
-        xfire.getServiceRegistry().register(inOutService);
+        if (inOut) {
+            service = factory.create(InOutService.class);
+            service.setInvoker(new BeanInvoker(new InOutService(this)));
+        } else {
+            service = factory.create(InOnlyService.class);
+            service.setInvoker(new BeanInvoker(new InOnlyService(this)));
+        }
+        xfire.getServiceRegistry().register(service);
         controller = new Controller(xfire);
     }
     
@@ -71,7 +83,21 @@
     }
 
     public void invokeInOnly(Source source, MessageContext context) throws XFireFault {
-        throw new XFireFault("Not implemented", XFireFault.SENDER);
+        if (source == null) { 
+            throw new XFireFault("Invalid source.", XFireFault.SENDER);
+        }
+        try {
+            DeliveryChannel channel = getDeliveryChannel();
+            MessageExchangeFactory factory = channel.createExchangeFactory();
+            InOnly exchange = factory.createInOnlyExchange();
+            populateExchange(exchange, source, context);
+            boolean result = channel.sendSync(exchange);
+            if (!result) {
+                throw new XFireFault("Error sending exchange", XFireFault.SENDER);
+            }
+        } catch (JBIException e) {
+            throw new XFireFault(e);
+        }
     }
     
     public Source invokeInOut(Source source, MessageContext context) throws XFireFault {
@@ -82,9 +108,7 @@
             DeliveryChannel channel = getDeliveryChannel();
             MessageExchangeFactory factory = channel.createExchangeFactory();
             InOut exchange = factory.createInOutExchange();
-            NormalizedMessage inMessage = exchange.createMessage();
-            inMessage.setContent(source);
-            exchange.setInMessage(inMessage);
+            populateExchange(exchange, source, context);
             boolean result = channel.sendSync(exchange);
             if (!result) {
                 throw new XFireFault("Error sending exchange", XFireFault.SENDER);
@@ -114,6 +138,20 @@
         }
     }
     
+    protected void populateExchange(MessageExchange exchange, Source src, MessageContext ctx) throws JBIException {
+        // TODO: Retrieve properties
+        NormalizedMessage inMessage = exchange.createMessage();
+        inMessage.setContent(src);
+        Attachments attachments = (Attachments) ctx.getProperty(Attachments.ATTACHMENTS_KEY);
+        if (attachments != null) {
+            for (Iterator it = attachments.getParts(); it.hasNext();) {
+                Attachment part = (Attachment) it.next();
+                inMessage.addAttachment(part.getId(), part.getDataHandler());
+            }
+        }
+        exchange.setMessage(inMessage, "in");
+    }
+    
     public static class InOnlyService {
         private HttpSoapInOutBinding component;
         public InOnlyService() {}
@@ -135,14 +173,14 @@
             return this.component.invokeInOut(source, context);
         }
     }
+
     
     public class Controller extends XFireServletController {
         public Controller(XFire xfire) {
             super(xfire);
         }
         protected String getService(HttpServletRequest request) {
-            return inOut ? inOutService.getName() : inOnlyService.getName();
+            return service.getName();
         }        
     }
-
 }

Added: trunk/components/base/src/test/java/org/servicemix/components/http/HttpSoapAttachmentsTest.java (927 => 928)

--- trunk/components/base/src/test/java/org/servicemix/components/http/HttpSoapAttachmentsTest.java	2005-11-24 11:15:32 UTC (rev 927)
+++ trunk/components/base/src/test/java/org/servicemix/components/http/HttpSoapAttachmentsTest.java	2005-11-24 15:04:45 UTC (rev 928)
@@ -0,0 +1,81 @@
+/**
+ * 
+ * Copyright 2005 LogicBlaze, Inc. http://www.logicblaze.com
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License"); 
+ * you may not use this file except in compliance with the License. 
+ * You may obtain a copy of the License at 
+ * 
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, 
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
+ * See the License for the specific language governing permissions and 
+ * limitations under the License. 
+ * 
+ **/
+package org.servicemix.components.http;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.InputStream;
+
+import javax.activation.DataHandler;
+import javax.activation.FileDataSource;
+import javax.mail.MessagingException;
+import javax.xml.namespace.QName;
+
+import junit.framework.TestCase;
+
+import org.codehaus.xfire.attachments.JavaMailAttachments;
+import org.codehaus.xfire.attachments.SimpleAttachment;
+import org.servicemix.components.util.EchoComponent;
+import org.servicemix.jbi.container.ActivationSpec;
+import org.servicemix.jbi.container.JBIContainer;
+
+import com.meterware.httpunit.PostMethodWebRequest;
+import com.meterware.httpunit.WebConversation;
+import com.meterware.httpunit.WebResponse;
+
+public class HttpSoapAttachmentsTest extends TestCase {
+
+    public void testWithAttachments() throws Exception {
+        JBIContainer container = new JBIContainer();
+        container.setMonitorInstallationDirectory(false);
+        container.setUseMBeanServer(false);
+        container.setCreateMBeanServer(false);
+        container.init();
+        container.start();
+        ActivationSpec as = new ActivationSpec();
+        as.setId("echo");
+        as.setComponent(new EchoComponent());
+        as.setService(new QName("echo"));
+        container.activateComponent(as);
+        as = new ActivationSpec();
+        as.setId("xfireBinding");
+        as.setComponent(new HttpSoapConnector(null, 8081, true));
+        as.setDestinationService(new QName("echo"));
+        container.activateComponent(as);
+
+        JavaMailAttachments sendAtts = new JavaMailAttachments();
+        sendAtts.setSoapMessage(new SimpleAttachment("soap-request.xml",
+                createDataHandler("soap-request.xml")));
+        sendAtts.addPart(new SimpleAttachment("ServiceMix.jpg",
+                createDataHandler("ServiceMix.jpg")));
+        ByteArrayOutputStream bos = new ByteArrayOutputStream();
+        sendAtts.write(bos);
+        InputStream is = new ByteArrayInputStream(bos.toByteArray());
+        PostMethodWebRequest req = new PostMethodWebRequest(
+                "http://localhost:8081", is, sendAtts.getContentType());
+        WebResponse response = new WebConversation().getResponse(req);
+        System.out.println(response.getText());
+    }
+
+    private DataHandler createDataHandler(String name) throws MessagingException {
+        File f = new File(getClass().getResource(name).getPath());
+        FileDataSource fs = new FileDataSource(f);
+        return new DataHandler(fs);
+    }
+}

Modified: trunk/components/base/src/test/java/org/servicemix/components/http/HttpSoapTest.java (927 => 928)

--- trunk/components/base/src/test/java/org/servicemix/components/http/HttpSoapTest.java	2005-11-24 11:15:32 UTC (rev 927)
+++ trunk/components/base/src/test/java/org/servicemix/components/http/HttpSoapTest.java	2005-11-24 15:04:45 UTC (rev 928)
@@ -17,9 +17,8 @@
  **/
 package org.servicemix.components.http;
 
-import java.io.BufferedReader;
+import java.io.ByteArrayOutputStream;
 import java.io.InputStream;
-import java.io.InputStreamReader;
 import java.io.OutputStream;
 import java.net.URL;
 import java.net.URLConnection;
@@ -29,45 +28,70 @@
 import junit.framework.TestCase;
 
 import org.servicemix.components.util.EchoComponent;
+import org.servicemix.components.util.TraceComponent;
 import org.servicemix.jbi.container.ActivationSpec;
 import org.servicemix.jbi.container.JBIContainer;
 import org.servicemix.jbi.util.FileUtil;
 
+import com.meterware.httpunit.PostMethodWebRequest;
+import com.meterware.httpunit.WebConversation;
+import com.meterware.httpunit.WebResponse;
+
 public class HttpSoapTest extends TestCase {
 
-	public void test() throws Exception {
-		JBIContainer container = new JBIContainer();
-		container.setMonitorInstallationDirectory(false);
-		container.setUseMBeanServer(false);
-		container.setCreateMBeanServer(false);
-		container.init();
-		container.start();
-		ActivationSpec as = new ActivationSpec();
-		as.setId("echo");
-		as.setComponent(new EchoComponent());
-		as.setService(new QName("echo"));
-		container.activateComponent(as);
-		as = new ActivationSpec();
-		as.setId("xfireBinding");
-		as.setComponent(new HttpSoapConnector(null, 8081));
-		as.setDestinationService(new QName("echo"));
-		container.activateComponent(as);
+    public void testInOut() throws Exception {
+        JBIContainer container = new JBIContainer();
+        container.setMonitorInstallationDirectory(false);
+        container.setUseMBeanServer(false);
+        container.setCreateMBeanServer(false);
+        container.init();
+        container.start();
+        ActivationSpec as = new ActivationSpec();
+        as.setId("echo");
+        as.setComponent(new EchoComponent());
+        as.setService(new QName("echo"));
+        container.activateComponent(as);
+        as = new ActivationSpec();
+        as.setId("xfireBinding");
+        as.setComponent(new HttpSoapConnector(null, 8081, true));
+        as.setDestinationService(new QName("echo"));
+        container.activateComponent(as);
 
-		URLConnection connection = new URL("http://localhost:8081") .openConnection();
-		connection.setDoOutput(true);
-		connection.setDoInput(true);
-		OutputStream os = connection.getOutputStream();
-		// Post the request file.
-		InputStream fis = getClass().getResourceAsStream("soap-request.xml");
-		FileUtil.copyInputStream(fis, os);
-		// Read the response.
-		BufferedReader in = new BufferedReader(new InputStreamReader(connection
-				.getInputStream()));
-		String inputLine;
-		while ((inputLine = in.readLine()) != null) {
-			System.out.println(inputLine);
-		}
-		in.close();
-	}
+        URLConnection connection = new URL("http://localhost:8081").openConnection();
+        connection.setDoOutput(true);
+        connection.setDoInput(true);
+        OutputStream os = connection.getOutputStream();
+        // Post the request file.
+        InputStream fis = getClass().getResourceAsStream("soap-request.xml");
+        FileUtil.copyInputStream(fis, os);
+        // Read the response.
+        InputStream is = connection.getInputStream();
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        FileUtil.copyInputStream(is, baos);
+        System.out.println(baos.toString());
+    }
 
+    public void testInOnly() throws Exception {
+        JBIContainer container = new JBIContainer();
+        container.setMonitorInstallationDirectory(false);
+        container.setUseMBeanServer(false);
+        container.setCreateMBeanServer(false);
+        container.init();
+        container.start();
+        ActivationSpec as = new ActivationSpec();
+        as.setId("trace");
+        as.setComponent(new TraceComponent());
+        as.setService(new QName("trace"));
+        container.activateComponent(as);
+        as = new ActivationSpec();
+        as.setId("xfireBinding");
+        as.setComponent(new HttpSoapConnector(null, 8081, false));
+        as.setDestinationService(new QName("trace"));
+        container.activateComponent(as);
+
+        PostMethodWebRequest req = new PostMethodWebRequest(
+                "http://localhost:8081/?name=Guillaume", getClass().getResourceAsStream("soap-request.xml"), null);
+        WebResponse response = new WebConversation().getResponse(req);
+        System.out.println(response.getText());
+    }
 }

Added: trunk/components/base/src/test/resources/org/servicemix/components/http/ServiceMix.jpg

(Binary files differ)
Property changes on: trunk/components/base/src/test/resources/org/servicemix/components/http/ServiceMix.jpg ___________________________________________________________________ Name: svn:mime-type + application/octet-stream

Modified: trunk/etc/project.properties (927 => 928)

--- trunk/etc/project.properties	2005-11-24 11:15:32 UTC (rev 927)
+++ trunk/etc/project.properties	2005-11-24 15:04:45 UTC (rev 928)
@@ -137,6 +137,7 @@
 geronimo_spec_servlet_version=2.4-rc4
 geronimo_transaction_version=1.0-SNAPSHOT
 groovy_version=1.0-jsr-03
+httpunit_version=1.5.1
 hsqldb_version=1.7.3.3
 janino_version=2.3.2
 jasper_version = 5.5.9

Reply via email to