Author: bsnyder
Date: Fri Feb 23 09:23:39 2007
New Revision: 511025

URL: http://svn.apache.org/viewvc?view=rev&rev=511025
Log:
Adding initial code for SM-856 - Add a marshaler to the servicemix-http 
component that handles Java serialization.

Added:
    
incubator/servicemix/trunk/deployables/bindingcomponents/servicemix-http/src/main/java/org/apache/servicemix/http/endpoints/SerializedMarshaler.java
   (with props)
    
incubator/servicemix/trunk/deployables/bindingcomponents/servicemix-http/src/test/java/org/apache/servicemix/http/endpoints/
    
incubator/servicemix/trunk/deployables/bindingcomponents/servicemix-http/src/test/java/org/apache/servicemix/http/endpoints/Person.java
   (with props)
    
incubator/servicemix/trunk/deployables/bindingcomponents/servicemix-http/src/test/java/org/apache/servicemix/http/endpoints/PersonImpl.java
   (with props)
    
incubator/servicemix/trunk/deployables/bindingcomponents/servicemix-http/src/test/java/org/apache/servicemix/http/endpoints/SerializedMarshalerTest.java
   (with props)
Modified:
    
incubator/servicemix/trunk/deployables/bindingcomponents/servicemix-http/pom.xml

Modified: 
incubator/servicemix/trunk/deployables/bindingcomponents/servicemix-http/pom.xml
URL: 
http://svn.apache.org/viewvc/incubator/servicemix/trunk/deployables/bindingcomponents/servicemix-http/pom.xml?view=diff&rev=511025&r1=511024&r2=511025
==============================================================================
--- 
incubator/servicemix/trunk/deployables/bindingcomponents/servicemix-http/pom.xml
 (original)
+++ 
incubator/servicemix/trunk/deployables/bindingcomponents/servicemix-http/pom.xml
 Fri Feb 23 09:23:39 2007
@@ -191,6 +191,11 @@
       <scope>provided</scope>
       <optional>true</optional>
     </dependency>
+    <dependency>
+      <groupId>xstream</groupId>
+      <artifactId>xstream</artifactId>
+      <version>1.1.3</version>
+    </dependency>
   </dependencies>
 
 </project>

Added: 
incubator/servicemix/trunk/deployables/bindingcomponents/servicemix-http/src/main/java/org/apache/servicemix/http/endpoints/SerializedMarshaler.java
URL: 
http://svn.apache.org/viewvc/incubator/servicemix/trunk/deployables/bindingcomponents/servicemix-http/src/main/java/org/apache/servicemix/http/endpoints/SerializedMarshaler.java?view=auto&rev=511025
==============================================================================
--- 
incubator/servicemix/trunk/deployables/bindingcomponents/servicemix-http/src/main/java/org/apache/servicemix/http/endpoints/SerializedMarshaler.java
 (added)
+++ 
incubator/servicemix/trunk/deployables/bindingcomponents/servicemix-http/src/main/java/org/apache/servicemix/http/endpoints/SerializedMarshaler.java
 Fri Feb 23 09:23:39 2007
@@ -0,0 +1,48 @@
+package org.apache.servicemix.http.endpoints;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.ObjectInputStream;
+import java.io.StringWriter;
+import java.io.Writer;
+import java.net.URI;
+
+import javax.jbi.component.ComponentContext;
+import javax.jbi.messaging.MessageExchange;
+import javax.jbi.messaging.NormalizedMessage;
+import javax.servlet.http.HttpServletRequest;
+
+import org.apache.servicemix.jbi.jaxp.StringSource;
+import org.apache.servicemix.jbi.messaging.MessageExchangeSupport;
+
+import com.thoughtworks.xstream.XStream;
+import com.thoughtworks.xstream.io.xml.DomDriver;
+
+/**
+ * 
+ * @author bsnyder
+ * @org.apache.xbean.XBean element="serializedMarshaler"
+ */
+public class SerializedMarshaler extends DefaultHttpConsumerMarshaler {
+       
+       public MessageExchange createExchange(HttpServletRequest request, 
ComponentContext context) throws Exception {
+        URI inOnlyMepUri = MessageExchangeSupport.IN_ONLY;
+               MessageExchange me = 
+               
context.getDeliveryChannel().createExchangeFactory().createExchange(inOnlyMepUri);
+        NormalizedMessage in = me.createMessage();
+
+               String xmlRequest = marshal(request.getInputStream());
+               in.setContent(new StringSource(xmlRequest));
+        me.setMessage(in, "in");
+        return me;
+       }
+       
+       protected String marshal(InputStream is) throws IOException, 
ClassNotFoundException {
+               Object obj = new ObjectInputStream(is).readObject();
+               Writer w = new StringWriter();
+               XStream xstream = new XStream(new DomDriver());
+               xstream.toXML(obj, w);
+               return w.toString();
+       }
+
+}

Propchange: 
incubator/servicemix/trunk/deployables/bindingcomponents/servicemix-http/src/main/java/org/apache/servicemix/http/endpoints/SerializedMarshaler.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
incubator/servicemix/trunk/deployables/bindingcomponents/servicemix-http/src/main/java/org/apache/servicemix/http/endpoints/SerializedMarshaler.java
------------------------------------------------------------------------------
    svn:keywords = Id Revision

Added: 
incubator/servicemix/trunk/deployables/bindingcomponents/servicemix-http/src/test/java/org/apache/servicemix/http/endpoints/Person.java
URL: 
http://svn.apache.org/viewvc/incubator/servicemix/trunk/deployables/bindingcomponents/servicemix-http/src/test/java/org/apache/servicemix/http/endpoints/Person.java?view=auto&rev=511025
==============================================================================
--- 
incubator/servicemix/trunk/deployables/bindingcomponents/servicemix-http/src/test/java/org/apache/servicemix/http/endpoints/Person.java
 (added)
+++ 
incubator/servicemix/trunk/deployables/bindingcomponents/servicemix-http/src/test/java/org/apache/servicemix/http/endpoints/Person.java
 Fri Feb 23 09:23:39 2007
@@ -0,0 +1,18 @@
+package org.apache.servicemix.http.endpoints;
+
+public interface Person {
+
+       String getGivenName(); 
+       
+       void setGivenName(String givenName);
+       
+       String getSurName();
+       
+       void setSurName(String surName);
+       
+       int getAge();
+       
+       void setAge(int age);
+       
+       String toString();
+}

Propchange: 
incubator/servicemix/trunk/deployables/bindingcomponents/servicemix-http/src/test/java/org/apache/servicemix/http/endpoints/Person.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
incubator/servicemix/trunk/deployables/bindingcomponents/servicemix-http/src/test/java/org/apache/servicemix/http/endpoints/Person.java
------------------------------------------------------------------------------
    svn:keywords = Id Revision

Added: 
incubator/servicemix/trunk/deployables/bindingcomponents/servicemix-http/src/test/java/org/apache/servicemix/http/endpoints/PersonImpl.java
URL: 
http://svn.apache.org/viewvc/incubator/servicemix/trunk/deployables/bindingcomponents/servicemix-http/src/test/java/org/apache/servicemix/http/endpoints/PersonImpl.java?view=auto&rev=511025
==============================================================================
--- 
incubator/servicemix/trunk/deployables/bindingcomponents/servicemix-http/src/test/java/org/apache/servicemix/http/endpoints/PersonImpl.java
 (added)
+++ 
incubator/servicemix/trunk/deployables/bindingcomponents/servicemix-http/src/test/java/org/apache/servicemix/http/endpoints/PersonImpl.java
 Fri Feb 23 09:23:39 2007
@@ -0,0 +1,43 @@
+package org.apache.servicemix.http.endpoints;
+
+public class PersonImpl implements Person {
+       
+       protected String givenName; 
+       protected String surName;
+       protected int age;
+       
+       public PersonImpl(String givenName, String surName, int age) {
+               this.givenName = givenName;
+               this.surName = surName;
+               this.age = age;
+       }
+
+       public String getGivenName() {
+               return givenName;
+       }
+
+       public String getSurName() {
+               return surName;
+       }
+
+       public void setGivenName(String givenName) {
+               this.givenName = givenName;             
+       }
+
+       public void setSurName(String surName) {
+               this.surName = surName;
+       }
+       
+       public int getAge() {
+               return age;
+       }
+       
+       public void setAge(int age) {
+               this.age = age;
+       }
+
+       public String toString() {
+               return "Person: " + 
+                       "surName [" + surName + "], givenName [" + givenName + 
"], age [" + age + "]";
+       }
+}

Propchange: 
incubator/servicemix/trunk/deployables/bindingcomponents/servicemix-http/src/test/java/org/apache/servicemix/http/endpoints/PersonImpl.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
incubator/servicemix/trunk/deployables/bindingcomponents/servicemix-http/src/test/java/org/apache/servicemix/http/endpoints/PersonImpl.java
------------------------------------------------------------------------------
    svn:keywords = Id Revision

Added: 
incubator/servicemix/trunk/deployables/bindingcomponents/servicemix-http/src/test/java/org/apache/servicemix/http/endpoints/SerializedMarshalerTest.java
URL: 
http://svn.apache.org/viewvc/incubator/servicemix/trunk/deployables/bindingcomponents/servicemix-http/src/test/java/org/apache/servicemix/http/endpoints/SerializedMarshalerTest.java?view=auto&rev=511025
==============================================================================
--- 
incubator/servicemix/trunk/deployables/bindingcomponents/servicemix-http/src/test/java/org/apache/servicemix/http/endpoints/SerializedMarshalerTest.java
 (added)
+++ 
incubator/servicemix/trunk/deployables/bindingcomponents/servicemix-http/src/test/java/org/apache/servicemix/http/endpoints/SerializedMarshalerTest.java
 Fri Feb 23 09:23:39 2007
@@ -0,0 +1,87 @@
+package org.apache.servicemix.http.endpoints;
+
+import java.io.ByteArrayInputStream;
+
+import javax.jbi.component.ComponentContext;
+import javax.jbi.messaging.ExchangeStatus;
+import javax.jbi.messaging.InOut;
+import javax.xml.namespace.QName;
+import javax.xml.transform.stream.StreamSource;
+
+import junit.framework.TestCase;
+
+import org.apache.servicemix.client.DefaultServiceMixClient;
+import org.apache.servicemix.http.HttpComponent;
+import org.apache.servicemix.http.HttpEndpointType;
+import org.apache.servicemix.jbi.container.JBIContainer;
+import org.apache.servicemix.jbi.jaxp.SourceTransformer;
+import org.springframework.mock.web.MockHttpServletRequest;
+
+public class SerializedMarshalerTest extends TestCase {
+       
+       protected JBIContainer container; 
+       protected ComponentContext context; 
+       
+       public void setUp() throws Exception {          
+               container = new JBIContainer();
+        container.setUseMBeanServer(false);
+        container.setCreateMBeanServer(false);
+        container.setEmbedded(true);
+        container.init();
+    }
+
+    protected void tearDown() throws Exception {
+        if (container != null) {
+            container.shutDown();
+        }
+    }
+
+       public void testCreateExchange() throws Exception{
+               HttpConsumerEndpoint ep = new HttpConsumerEndpoint();
+               ep.setService(new QName("urn:httpconsumer", "HttpConsumer"));
+               ep.setEndpoint("HttpConsumer");
+               ep.setLocationURI("http://localhost:8192/service";);
+               
+               SerializedMarshaler marshaler = new SerializedMarshaler();
+               ep.setMarshaler(marshaler);
+               
+               HttpComponent component = new HttpComponent();
+        component.setEndpoints(new HttpEndpointType[] { ep });
+               container.activateComponent(component, "HttpConsumer");
+               
+               container.start();
+               
+               DefaultServiceMixClient client = new 
DefaultServiceMixClient(container);
+        InOut inout = client.createInOutExchange();
+        inout.setInterfaceName(new QName("urn:httpconsumer", "HttpConsumer"));
+        inout.getInMessage().setContent(
+                new StreamSource(new 
ByteArrayInputStream(createRequestMessage())));
+        
+        long t0 = System.currentTimeMillis();
+        client.sendSync(inout);
+        long t1 = System.currentTimeMillis();
+        assertTrue(inout.getStatus() == ExchangeStatus.ACTIVE);
+        
+        System.err.println("Executed in " + (t1 - t0) + "ms");
+
+        assertNotNull(inout.getOutMessage());
+        assertNotNull(inout.getOutMessage().getContent());
+        
+        SourceTransformer sourceTransformer = new SourceTransformer();
+        String reply = 
sourceTransformer.toString(inout.getOutMessage().getContent());
+        
+        String inputMesage = sourceTransformer.toString(new StreamSource(
+                new ByteArrayInputStream(createRequestMessage())));
+        
+        
System.err.println("##################################################");
+        System.err.println("Msg Sent [" + inputMesage + "]");
+        System.err.println("Msg Recieved [" + reply + "]");
+        
System.err.println("##################################################");
+       }
+
+       private byte[] createRequestMessage() {
+               Person p = new PersonImpl("Hunter", "Thompson", 67); 
+               return p.toString().getBytes();
+       }
+       
+}

Propchange: 
incubator/servicemix/trunk/deployables/bindingcomponents/servicemix-http/src/test/java/org/apache/servicemix/http/endpoints/SerializedMarshalerTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
incubator/servicemix/trunk/deployables/bindingcomponents/servicemix-http/src/test/java/org/apache/servicemix/http/endpoints/SerializedMarshalerTest.java
------------------------------------------------------------------------------
    svn:keywords = Id Revision


Reply via email to