Author: aco
Date: Fri Feb 23 21:12:12 2007
New Revision: 511213

URL: http://svn.apache.org/viewvc?view=rev&rev=511213
Log:
Updated the SerializedMarshaler class and test case.

Modified:
    
incubator/servicemix/trunk/deployables/bindingcomponents/servicemix-http/pom.xml
    
incubator/servicemix/trunk/deployables/bindingcomponents/servicemix-http/src/main/java/org/apache/servicemix/http/endpoints/SerializedMarshaler.java
    
incubator/servicemix/trunk/deployables/bindingcomponents/servicemix-http/src/test/java/org/apache/servicemix/http/endpoints/SerializedMarshalerTest.java

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=511213&r1=511212&r2=511213
==============================================================================
--- 
incubator/servicemix/trunk/deployables/bindingcomponents/servicemix-http/pom.xml
 (original)
+++ 
incubator/servicemix/trunk/deployables/bindingcomponents/servicemix-http/pom.xml
 Fri Feb 23 21:12:12 2007
@@ -157,12 +157,6 @@
     </dependency>
     <dependency>
       <groupId>org.springframework</groupId>
-      <artifactId>spring-web</artifactId>
-      <version>${spring-version}</version>
-      <scope>test</scope>
-    </dependency>
-    <dependency>
-      <groupId>org.springframework</groupId>
       <artifactId>spring-jmx</artifactId>
       <version>${spring-version}</version>
       <scope>test</scope>
@@ -193,14 +187,20 @@
     </dependency>
     <dependency>
       <groupId>org.springframework</groupId>
-      <artifactId>spring</artifactId>
+      <artifactId>spring-beans</artifactId>
+      <version>${spring-version}</version>
+      <scope>provided</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.springframework</groupId>
+      <artifactId>spring-remoting</artifactId>
       <version>${spring-version}</version>
       <scope>test</scope>
     </dependency>
     <dependency>
       <groupId>xstream</groupId>
       <artifactId>xstream</artifactId>
-      <version>1.1.3</version>
+      <scope>provided</scope>
     </dependency>
   </dependencies>
 

Modified: 
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=diff&rev=511213&r1=511212&r2=511213
==============================================================================
--- 
incubator/servicemix/trunk/deployables/bindingcomponents/servicemix-http/src/main/java/org/apache/servicemix/http/endpoints/SerializedMarshaler.java
 (original)
+++ 
incubator/servicemix/trunk/deployables/bindingcomponents/servicemix-http/src/main/java/org/apache/servicemix/http/endpoints/SerializedMarshaler.java
 Fri Feb 23 21:12:12 2007
@@ -9,6 +9,8 @@
 import java.io.OutputStreamWriter;
 import java.io.StringWriter;
 import java.io.Writer;
+import java.io.OutputStream;
+import java.io.ObjectOutputStream;
 import java.net.URI;
 
 import javax.jbi.component.ComponentContext;
@@ -16,8 +18,11 @@
 import javax.jbi.messaging.NormalizedMessage;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
+import javax.xml.transform.Source;
+import javax.xml.transform.TransformerException;
 
 import org.apache.servicemix.jbi.jaxp.StringSource;
+import org.apache.servicemix.jbi.jaxp.SourceTransformer;
 import org.apache.servicemix.jbi.messaging.MessageExchangeSupport;
 
 import com.thoughtworks.xstream.XStream;
@@ -31,53 +36,74 @@
 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);
+               MessageExchange me =
+               
context.getDeliveryChannel().createExchangeFactory().createExchange(getDefaultMep());
         NormalizedMessage in = me.createMessage();
 
-        InputStream copy = copyInputStream(request.getInputStream());
-        
-               String xmlRequest = marshal(copy);
-               in.setContent(new StringSource(xmlRequest));
+//        InputStream copy = copyInputStream(request.getInputStream());
+
+               in.setContent(marshal(request.getInputStream()));
         me.setMessage(in, "in");
         return me;
        }
 
        public void sendOut(MessageExchange exchange, NormalizedMessage outMsg, 
HttpServletRequest request, HttpServletResponse response) throws Exception {
-               // TODO Auto-generated method stub
-               super.sendOut(exchange, outMsg, request, response);
-       }
-       
-       /**
-        * Copy the input stream in an attempt to get around 'stream is closed 
error'. 
-        * 
-        * @param in
-        * @return
-        * @throws IOException
-        */
-       private InputStream copyInputStream(InputStream in) throws IOException 
{   
-               InputStreamReader input = new InputStreamReader(in);
-               ByteArrayOutputStream baos = new ByteArrayOutputStream();
-               OutputStreamWriter output = new OutputStreamWriter(baos);
-               
-        char[] buffer1 = new char[1024*2];
-        int i = 0;
-        while (-1 != (i = input.read(buffer1))) {
-            output.write(buffer1, 0, i);
+        if (outMsg.getContent() != null) {
+            unmarshal(response.getOutputStream(), outMsg.getContent());
         }
-        output.flush();
-        
-        InputStream newIn = new ByteArrayInputStream(baos.toByteArray());
-               return newIn;
-       }
+    }
        
-       private String marshal(InputStream is) throws IOException, 
ClassNotFoundException {
+//     /**
+//      * Copy the input stream in an attempt to get around 'stream is closed 
error'.
+//      *
+//      * @param in
+//      * @return
+//      * @throws IOException
+//      */
+//     private InputStream copyInputStream(InputStream in) throws IOException {
+//             InputStreamReader input = new InputStreamReader(in);
+//             ByteArrayOutputStream baos = new ByteArrayOutputStream();
+//             OutputStreamWriter output = new OutputStreamWriter(baos);
+//
+//        char[] buffer1 = new char[1024*2];
+//        int i = 0;
+//        while (-1 != (i = input.read(buffer1))) {
+//            output.write(buffer1, 0, i);
+//        }
+//        output.flush();
+//
+//        InputStream newIn = new ByteArrayInputStream(baos.toByteArray());
+//             return newIn;
+//     }
+
+    /**
+     * Marshal the byte content of the input stream to an xml source
+     * @param is - input stream to read the object from
+     * @return xml source
+     * @throws IOException
+     * @throws ClassNotFoundException
+     */
+    private Source 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();
+               return new StringSource(w.toString());
        }
 
+    /**
+     * Unmarshal the xml content to the specified output stream
+     * @param os - output stream to unmarshal to
+     * @param content - xml source
+     * @throws TransformerException
+     * @throws IOException
+     */
+    private void unmarshal(OutputStream os, Source content) throws 
TransformerException, IOException {
+        SourceTransformer transform = new SourceTransformer();
+        XStream xstream = new XStream(new DomDriver());
+
+        Object obj = xstream.fromXML(transform.toString(content));
+        ObjectOutputStream oos = new ObjectOutputStream(os);
+        oos.writeObject(obj);
+    }
 }

Modified: 
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=diff&rev=511213&r1=511212&r2=511213
==============================================================================
--- 
incubator/servicemix/trunk/deployables/bindingcomponents/servicemix-http/src/test/java/org/apache/servicemix/http/endpoints/SerializedMarshalerTest.java
 (original)
+++ 
incubator/servicemix/trunk/deployables/bindingcomponents/servicemix-http/src/test/java/org/apache/servicemix/http/endpoints/SerializedMarshalerTest.java
 Fri Feb 23 21:12:12 2007
@@ -1,39 +1,36 @@
 package org.apache.servicemix.http.endpoints;
 
-import java.io.ByteArrayInputStream;
-import java.io.ByteArrayOutputStream;
-import java.io.IOException;
-import java.io.ObjectOutputStream;
-import java.io.OutputStream;
-import java.net.HttpURLConnection;
-import java.net.URL;
-import java.net.URLConnection;
-
 import javax.jbi.component.ComponentContext;
+import javax.jbi.messaging.MessageExchange;
+import javax.jbi.messaging.MessagingException;
+import javax.jbi.messaging.NormalizedMessage;
 import javax.xml.namespace.QName;
 
 import junit.framework.TestCase;
 
-import org.apache.servicemix.components.util.EchoComponent;
+import org.apache.servicemix.components.util.TransformComponentSupport;
 import org.apache.servicemix.http.HttpComponent;
 import org.apache.servicemix.http.HttpEndpointType;
 import org.apache.servicemix.jbi.container.ActivationSpec;
 import org.apache.servicemix.jbi.container.JBIContainer;
-//import org.springframework.mock.web.MockHttpServletRequest;
-//import org.springframework.mock.web.MockHttpServletResponse;
-import 
org.springframework.remoting.httpinvoker.AbstractHttpInvokerRequestExecutor;
-import org.springframework.remoting.httpinvoker.HttpInvokerClientConfiguration;
+import org.apache.servicemix.jbi.jaxp.SourceTransformer;
+import org.apache.servicemix.jbi.jaxp.StringSource;
+import org.apache.servicemix.jbi.messaging.MessageExchangeSupport;
 import org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean;
 import 
org.springframework.remoting.httpinvoker.SimpleHttpInvokerRequestExecutor;
 import org.springframework.remoting.support.RemoteInvocationResult;
+import org.springframework.remoting.support.DefaultRemoteInvocationExecutor;
+import org.springframework.remoting.support.RemoteInvocation;
+import com.thoughtworks.xstream.XStream;
+import com.thoughtworks.xstream.io.xml.DomDriver;
 
 public class SerializedMarshalerTest extends TestCase {
-       
-       protected JBIContainer container; 
-       protected ComponentContext context; 
-       
-       public void setUp() throws Exception {          
-               container = new JBIContainer();
+
+    protected JBIContainer container;
+    protected ComponentContext context;
+
+    public void setUp() throws Exception {
+        container = new JBIContainer();
         container.setUseMBeanServer(false);
         container.setCreateMBeanServer(false);
         container.setEmbedded(true);
@@ -103,101 +100,167 @@
 //        System.err.println("Msg Recieved [" + reply + "]");
 //        
System.err.println("##################################################");
 //     }
-       
-    /** 
-     * Test the SerializedMarshaler using the Spring 
HttpInvokerProxyFactoryBean 
-     * to initiate the request.  
-     */
-       public void testUsingSpringHttpRemoting() throws Exception {
-               HttpConsumerEndpoint ep = new HttpConsumerEndpoint();
-               ep.setService(new QName("urn:httpconsumer", "HttpConsumer"));
-               ep.setEndpoint("HttpConsumer");
-               ep.setLocationURI("http://localhost:8192/service";);
-               ep.setTargetService(new 
QName("http://http.servicemix.org/Test";, "ConsumerInOut"));
-               
-               SerializedMarshaler marshaler = new SerializedMarshaler();
-               ep.setMarshaler(marshaler);
-               
-               HttpComponent component = new HttpComponent();
+
+//    /**
+//     * Test the SerializedMarshaler using the Spring 
HttpInvokerProxyFactoryBean
+//     * to initiate the request.
+//     */
+//     public void testUsingSpringHttpRemoting() throws Exception {
+//             HttpConsumerEndpoint ep = new HttpConsumerEndpoint();
+//             ep.setService(new QName("urn:httpconsumer", "HttpConsumer"));
+//             ep.setEndpoint("HttpConsumer");
+//             ep.setLocationURI("http://localhost:8192/service";);
+//             ep.setTargetService(new 
QName("http://http.servicemix.org/Test";, "ConsumerInOut"));
+//
+//             SerializedMarshaler marshaler = new SerializedMarshaler();
+//             ep.setMarshaler(marshaler);
+//
+//             HttpComponent component = new HttpComponent();
+//        component.setEndpoints(new HttpEndpointType[] { ep });
+//             container.activateComponent(component, "HttpConsumer");
+//
+//             EchoComponent echo = new EchoComponent();
+//        ActivationSpec asReceiver = new ActivationSpec("echo", echo);
+//        asReceiver.setService(new QName("http://http.servicemix.org/Test";, 
"ConsumerInOut"));
+//        container.activateComponent(asReceiver);
+//
+//             container.start();
+//
+//             HttpInvokerProxyFactoryBean pfb = new 
HttpInvokerProxyFactoryBean();
+//             pfb.setServiceInterface(Person.class);
+//             pfb.setServiceUrl("http://localhost:8192/service";);
+//
+//             // Not sure what to do about this mess
+//             pfb.setHttpInvokerRequestExecutor(new 
AbstractHttpInvokerRequestExecutor() {
+//                     protected RemoteInvocationResult doExecuteRequest(
+//                                     HttpInvokerClientConfiguration config, 
ByteArrayOutputStream baos) throws Exception {
+//                             assertEquals("http://localhost:8192/service";, 
config.getServiceUrl());
+////                           MockHttpServletRequest request = new 
MockHttpServletRequest();
+////                           MockHttpServletResponse response = new 
MockHttpServletResponse();
+////                           request.setContent(baos.toByteArray());
+////                           exporter.handleRequest(request, response);
+////                           return readRemoteInvocationResult(
+////                                           new 
ByteArrayInputStream(response.getContentAsByteArray()), 
config.getCodebaseUrl());
+//                             HttpURLConnection con = openConnection(config);
+//                             prepareConnection(con, baos.size());
+//                             writeRequestBody(config, con, baos);
+//
+//                             return new RemoteInvocationResult(new Object());
+//                     }
+//             });
+//
+//             pfb.afterPropertiesSet();
+//     }
+//
+//     private HttpURLConnection openConnection(HttpInvokerClientConfiguration 
config) throws IOException {
+//             URLConnection con = new 
URL(config.getServiceUrl()).openConnection();
+//             if (!(con instanceof HttpURLConnection)) {
+//                     throw new IOException("Service URL [" + 
config.getServiceUrl() + "] is not an HTTP URL");
+//             }
+//             return (HttpURLConnection) con;
+//     }
+//
+//     private void prepareConnection(HttpURLConnection con, int 
contentLength) throws IOException {
+//             con.setDoOutput(true);
+//             con.setRequestMethod("POST");
+//             con.setRequestProperty("Content-Type", 
"application/x-java-serialized-object");
+//             con.setRequestProperty("Content-Length", 
Integer.toString(contentLength));
+//     }
+//
+//     private void writeRequestBody(HttpInvokerClientConfiguration config,
+//             HttpURLConnection con, ByteArrayOutputStream baos) throws 
IOException {
+//             baos.writeTo(con.getOutputStream());
+//     }
+//
+//     private byte[] createBytesRequestMessage() {
+//             Person p = new PersonImpl("Hunter", "Thompson", 67);
+//             return p.toString().getBytes();
+//     }
+//
+//     private OutputStream createSerializedRequestMessage() {
+//             Person p = new PersonImpl("Hunter", "Thompson", 67);
+//             return serializeData(p.toString());
+//     }
+//
+//     private OutputStream serializeData(String str) {
+//             ByteArrayOutputStream baos = new ByteArrayOutputStream();
+//             ObjectOutputStream stream = null;
+//
+//             try {
+//                     stream = new ObjectOutputStream(baos);
+//                     stream.writeUTF(str);
+//             } catch (IOException e) {
+//                     // TODO Auto-generated catch block
+//                     e.printStackTrace();
+//             }
+//
+//             return stream;
+//     }
+
+    public void testUsingSpringHttpRemoting() throws Exception {
+        final Person person = new PersonImpl("Hunter", "Thompson", 67);
+
+        HttpConsumerEndpoint ep = new HttpConsumerEndpoint();
+        ep.setService(new QName("urn:HttpConsumer", "HttpConsumer"));
+        ep.setEndpoint("HttpConsumer");
+        ep.setLocationURI("http://localhost:8192/service/";);
+        ep.setTargetService(new QName("urn:HttpInvoker", "Endpoint"));
+
+        SerializedMarshaler marshaler = new SerializedMarshaler();
+        marshaler.setDefaultMep(MessageExchangeSupport.IN_OUT);
+        ep.setMarshaler(marshaler);
+
+        HttpComponent component = new HttpComponent();
         component.setEndpoints(new HttpEndpointType[] { ep });
-               container.activateComponent(component, "HttpConsumer");
-               
-               EchoComponent echo = new EchoComponent();
-        ActivationSpec asReceiver = new ActivationSpec("echo", echo);
-        asReceiver.setService(new QName("http://http.servicemix.org/Test";, 
"ConsumerInOut"));
+        container.activateComponent(component, "HttpConsumer");
+
+        TransformComponentSupport rmiComponent = new 
TransformComponentSupport() {
+            protected boolean transform(MessageExchange exchange, 
NormalizedMessage in, NormalizedMessage out) throws MessagingException {
+                try {
+                    // Deserialize rmi invocation
+                    XStream xstream = new XStream(new DomDriver());
+                    SourceTransformer st = new SourceTransformer();
+                    Object rmi = xstream.fromXML(st.toString(in.getContent()));
+
+                    DefaultRemoteInvocationExecutor executor = new 
DefaultRemoteInvocationExecutor();
+                    Object result = executor.invoke((RemoteInvocation)rmi, 
person);
+
+                    // Convert result to an rmi invocation
+                    RemoteInvocationResult rmiResult = new 
RemoteInvocationResult(result);
+                    out.setContent(new StringSource(xstream.toXML(rmiResult)));
+                } catch (Exception e) {
+                    throw new MessagingException (e);
+                }
+
+                return true;
+            }
+        };
+        ActivationSpec asReceiver = new ActivationSpec("rmiComponent", 
rmiComponent);
+        asReceiver.setService(new QName("urn:HttpInvoker", "Endpoint"));
+
         container.activateComponent(asReceiver);
-               
-               container.start();
-               
-               HttpInvokerProxyFactoryBean pfb = new 
HttpInvokerProxyFactoryBean();
-               pfb.setServiceInterface(Person.class);
-               pfb.setServiceUrl("http://localhost:8192/service";);
-               
-               // Not sure what to do about this mess 
-               pfb.setHttpInvokerRequestExecutor(new 
AbstractHttpInvokerRequestExecutor() {
-                       protected RemoteInvocationResult doExecuteRequest(
-                                       HttpInvokerClientConfiguration config, 
ByteArrayOutputStream baos) throws Exception {
-                               assertEquals("http://localhost:8192/service";, 
config.getServiceUrl());
-//                             MockHttpServletRequest request = new 
MockHttpServletRequest();
-//                             MockHttpServletResponse response = new 
MockHttpServletResponse();
-//                             request.setContent(baos.toByteArray());
-//                             exporter.handleRequest(request, response);
-//                             return readRemoteInvocationResult(
-//                                             new 
ByteArrayInputStream(response.getContentAsByteArray()), 
config.getCodebaseUrl());
-                               HttpURLConnection con = openConnection(config);
-                               prepareConnection(con, baos.size());
-                               writeRequestBody(config, con, baos);
-                               
-                               return new RemoteInvocationResult(new Object());
-                       }
-               });
-               
-               pfb.afterPropertiesSet();
-       }
-       
-       private HttpURLConnection openConnection(HttpInvokerClientConfiguration 
config) throws IOException {
-               URLConnection con = new 
URL(config.getServiceUrl()).openConnection();
-               if (!(con instanceof HttpURLConnection)) {
-                       throw new IOException("Service URL [" + 
config.getServiceUrl() + "] is not an HTTP URL");
-               }
-               return (HttpURLConnection) con;
-       }
-       
-       private void prepareConnection(HttpURLConnection con, int 
contentLength) throws IOException {
-               con.setDoOutput(true);
-               con.setRequestMethod("POST");
-               con.setRequestProperty("Content-Type", 
"application/x-java-serialized-object");
-               con.setRequestProperty("Content-Length", 
Integer.toString(contentLength));
-       }
-       
-       private void writeRequestBody(HttpInvokerClientConfiguration config, 
-               HttpURLConnection con, ByteArrayOutputStream baos) throws 
IOException {
-               baos.writeTo(con.getOutputStream());
-       }
-       
-       private byte[] createBytesRequestMessage() {
-               Person p = new PersonImpl("Hunter", "Thompson", 67); 
-               return p.toString().getBytes();
-       }
-
-       private OutputStream createSerializedRequestMessage() {
-               Person p = new PersonImpl("Hunter", "Thompson", 67); 
-               return serializeData(p.toString());
-       }
-       
-       private OutputStream serializeData(String str) {
-               ByteArrayOutputStream baos = new ByteArrayOutputStream();
-               ObjectOutputStream stream = null; 
-               
-               try {
-                       stream = new ObjectOutputStream(baos);
-                       stream.writeUTF(str);
-               } catch (IOException e) {
-                       // TODO Auto-generated catch block
-                       e.printStackTrace();
-               }
-               
-               return stream;
-       }
-       
+        container.start();
+
+        HttpInvokerProxyFactoryBean pfb = new HttpInvokerProxyFactoryBean();
+        pfb.setServiceInterface(Person.class);
+        pfb.setServiceUrl("http://localhost:8192/service/";);
+        pfb.setHttpInvokerRequestExecutor(new 
SimpleHttpInvokerRequestExecutor());
+        pfb.afterPropertiesSet();
+
+        Person test = (Person)pfb.getObject();
+
+        // Test getters
+        assertEquals("Hunter", test.getGivenName());
+        assertEquals("Thompson", test.getSurName());
+        assertEquals(67, test.getAge());
+
+        // Test setters
+        test.setGivenName("John");
+        test.setSurName("Doe");
+        test.setAge(34);
+
+        assertEquals(person.getGivenName(), "John");
+        assertEquals(person.getSurName(), "Doe");
+        assertEquals(person.getAge(), 34);
+    }
 }


Reply via email to