You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicemix.apache.org by bs...@apache.org on 2007/02/24 00:03:41 UTC

svn commit: r511163 - in /incubator/servicemix/trunk/deployables/bindingcomponents/servicemix-http/src: main/java/org/apache/servicemix/http/endpoints/ test/java/org/apache/servicemix/http/endpoints/

Author: bsnyder
Date: Fri Feb 23 15:03:40 2007
New Revision: 511163

URL: http://svn.apache.org/viewvc?view=rev&rev=511163
Log:
More additions to the SerializedMarshaler for SM-856 - trying to test it using the Spring HttpInvokerProxyFactoryBean.

Modified:
    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/PersonImpl.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/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=511163&r1=511162&r2=511163
==============================================================================
--- 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 15:03:40 2007
@@ -1,8 +1,12 @@
 package org.apache.servicemix.http.endpoints;
 
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
+import java.io.InputStreamReader;
 import java.io.ObjectInputStream;
+import java.io.OutputStreamWriter;
 import java.io.StringWriter;
 import java.io.Writer;
 import java.net.URI;
@@ -11,6 +15,7 @@
 import javax.jbi.messaging.MessageExchange;
 import javax.jbi.messaging.NormalizedMessage;
 import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
 
 import org.apache.servicemix.jbi.jaxp.StringSource;
 import org.apache.servicemix.jbi.messaging.MessageExchangeSupport;
@@ -31,14 +36,43 @@
         	context.getDeliveryChannel().createExchangeFactory().createExchange(inOnlyMepUri);
         NormalizedMessage in = me.createMessage();
 
-        InputStream is = request.getInputStream();
-		String xmlRequest = marshal(is);
+        InputStream copy = copyInputStream(request.getInputStream());
+        
+		String xmlRequest = marshal(copy);
 		in.setContent(new StringSource(xmlRequest));
         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);
+        }
+        output.flush();
+        
+        InputStream newIn = new ByteArrayInputStream(baos.toByteArray());
+		return newIn;
+	}
 	
-	protected String marshal(InputStream is) throws IOException, ClassNotFoundException {
+	private String marshal(InputStream is) throws IOException, ClassNotFoundException {
 		Object obj = new ObjectInputStream(is).readObject();
 		Writer w = new StringWriter();
 		XStream xstream = new XStream(new DomDriver());

Modified: 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=diff&rev=511163&r1=511162&r2=511163
==============================================================================
--- incubator/servicemix/trunk/deployables/bindingcomponents/servicemix-http/src/test/java/org/apache/servicemix/http/endpoints/PersonImpl.java (original)
+++ incubator/servicemix/trunk/deployables/bindingcomponents/servicemix-http/src/test/java/org/apache/servicemix/http/endpoints/PersonImpl.java Fri Feb 23 15:03:40 2007
@@ -1,12 +1,13 @@
 package org.apache.servicemix.http.endpoints;
 
+import java.io.Serializable;
 import java.io.StringWriter;
 import java.io.Writer;
 
 import com.thoughtworks.xstream.XStream;
 import com.thoughtworks.xstream.io.xml.DomDriver;
 
-public class PersonImpl implements Person {
+public class PersonImpl implements Person, Serializable {
 	
 	protected String givenName; 
 	protected String surName;

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=511163&r1=511162&r2=511163
==============================================================================
--- 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 15:03:40 2007
@@ -1,26 +1,31 @@
 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.ExchangeStatus;
-import javax.jbi.messaging.InOut;
-import javax.jbi.servicedesc.ServiceEndpoint;
 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.components.util.EchoComponent;
 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.apache.servicemix.jbi.jaxp.SourceTransformer;
-import org.apache.servicemix.jbi.resolver.URIResolver;
+//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.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean;
-import org.w3c.dom.DocumentFragment;
+import org.springframework.remoting.httpinvoker.SimpleHttpInvokerRequestExecutor;
+import org.springframework.remoting.support.RemoteInvocationResult;
 
 public class SerializedMarshalerTest extends TestCase {
 	
@@ -41,61 +46,69 @@
         }
     }
 
-	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");
-		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();
-		
-		DefaultServiceMixClient client = new DefaultServiceMixClient(container);
-        InOut inout = client.createInOutExchange();
-        inout.setService(new QName("urn:httpconsumer", "HttpConsumer"));
-        DocumentFragment epr = URIResolver.createWSAEPR("http://localhost:8192/service");
-        ServiceEndpoint se = client.getContext().resolveEndpointReference(epr);
-        inout.setEndpoint(se);
-        inout.getInMessage().setContent(
-                new StreamSource(new ByteArrayInputStream(createRequestMessage())));
-        
-        long t0 = System.currentTimeMillis();
-        client.sendSync(inout);
-        long t1 = System.currentTimeMillis();
-        System.out.println("%%%%%%%%%%%%%%% Exchange: " + inout);
-        System.out.println("Fault: " + inout.getFault());
-        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("##################################################");
-	}
+    /**
+     * Test the SerializedMarshaler using only SMX. I don't think this is going
+     * to work. 
+     */
+//	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");
+//		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();
+//		
+//		DefaultServiceMixClient client = new DefaultServiceMixClient(container);
+//        InOut inout = client.createInOutExchange();
+//        inout.setService(new QName("urn:httpconsumer", "HttpConsumer"));
+//        DocumentFragment epr = URIResolver.createWSAEPR("http://localhost:8192/service");
+//        ServiceEndpoint se = client.getContext().resolveEndpointReference(epr);
+//        inout.setEndpoint(se);
+//        inout.getInMessage().setContent(
+//                new StreamSource(new ByteArrayInputStream(createRequestMessage())));
+//        
+//        long t0 = System.currentTimeMillis();
+//        client.sendSync(inout);
+//        long t1 = System.currentTimeMillis();
+//        System.out.println("%%%%%%%%%%%%%%% Exchange: " + inout);
+//        System.out.println("Fault: " + inout.getFault());
+//        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(createBytesRequestMessage())));
+//        
+//        System.err.println("##################################################");
+//        System.err.println("Msg Sent [" + inputMesage + "]");
+//        System.err.println("Msg Recieved [" + reply + "]");
+//        System.err.println("##################################################");
+//	}
 	
-	public void XtestUsingSpringHttpRemoting() throws Exception {
+    /** 
+     * 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");
@@ -119,11 +132,72 @@
 		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 byte[] createRequestMessage() {
+	
+	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;
 	}
 	
 }