You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by js...@apache.org on 2007/06/22 19:26:43 UTC

svn commit: r549890 - in /activemq/camel/trunk/components/camel-http/src: main/java/org/apache/camel/component/http/ main/resources/META-INF/services/org/apache/camel/EndpointResolver/ main/resources/META-INF/services/org/apache/camel/component/ test/j...

Author: jstrachan
Date: Fri Jun 22 10:26:41 2007
New Revision: 549890

URL: http://svn.apache.org/viewvc?view=rev&rev=549890
Log:
added test case showing the use of a POST to a HTTP endpoint working, with the headers and body being available to the message

Added:
    activemq/camel/trunk/components/camel-http/src/main/resources/META-INF/services/org/apache/camel/component/
    activemq/camel/trunk/components/camel-http/src/main/resources/META-INF/services/org/apache/camel/component/http
Removed:
    activemq/camel/trunk/components/camel-http/src/main/resources/META-INF/services/org/apache/camel/EndpointResolver/
Modified:
    activemq/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/CamelServlet.java
    activemq/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpBinding.java
    activemq/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpComponent.java
    activemq/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpConsumer.java
    activemq/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpEndpoint.java
    activemq/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpExchange.java
    activemq/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpMessage.java
    activemq/camel/trunk/components/camel-http/src/test/java/org/apache/camel/component/http/HttpRouteTest.java

Modified: activemq/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/CamelServlet.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/CamelServlet.java?view=diff&rev=549890&r1=549889&r2=549890
==============================================================================
--- activemq/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/CamelServlet.java (original)
+++ activemq/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/CamelServlet.java Fri Jun 22 10:26:41 2007
@@ -47,7 +47,7 @@
         	}
         	
         	// Have the camel process the HTTP exchange.
-			HttpExchange exchange =  new HttpExchange(consumer.getEndpoint().getContext(), request, response);			
+			HttpExchange exchange =  new HttpExchange(consumer.getEndpoint(), request, response);			
 			consumer.getProcessor().process(exchange);
 
 			// HC: The getBinding() is interesting because it illustrates the impedance miss-match between

Modified: activemq/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpBinding.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpBinding.java?view=diff&rev=549890&r1=549889&r2=549890
==============================================================================
--- activemq/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpBinding.java (original)
+++ activemq/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpBinding.java Fri Jun 22 10:26:41 2007
@@ -17,6 +17,9 @@
  */
 package org.apache.camel.component.http;
 
+import javax.servlet.http.HttpServletRequest;
+import java.io.IOException;
+
 /**
  * @version $Revision$
  */
@@ -27,5 +30,14 @@
      */
     public void writeResponse(HttpExchange exchange) {
         /** TODO */
+    }
+
+    /**
+     * Parses the body from a HTTP message
+     */
+    public Object parseBody(HttpMessage httpMessage) throws IOException {
+        // lets assume the body is a reader
+        HttpServletRequest request = httpMessage.getRequest();
+        return request.getReader();
     }
 }

Modified: activemq/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpComponent.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpComponent.java?view=diff&rev=549890&r1=549889&r2=549890
==============================================================================
--- activemq/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpComponent.java (original)
+++ activemq/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpComponent.java Fri Jun 22 10:26:41 2007
@@ -27,14 +27,12 @@
  */
 public class HttpComponent extends DefaultComponent<HttpExchange> {
 	
-	CamelServlet camelServlet;
+	private CamelServlet camelServlet;
 	
 	/** 
 	 * Connects the URL specified on the endpoint to the specified processor.
 	 *  
-	 * @param endpoint
-	 * @param processor
-	 * @throws Exception 
+	 * @throws Exception
 	 */
 	public void connect(HttpConsumer consumer) throws Exception {
 		camelServlet.connect(consumer);
@@ -43,9 +41,7 @@
 	/**
 	 * Disconnects the URL specified on the endpoint from the specified processor.
 	 * 
-	 * @param endpoint
-	 * @param processor
-	 * @throws Exception 
+	 * @throws Exception
 	 */
 	public void disconnect(HttpConsumer consumer) throws Exception {
 		camelServlet.disconnect(consumer);

Modified: activemq/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpConsumer.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpConsumer.java?view=diff&rev=549890&r1=549889&r2=549890
==============================================================================
--- activemq/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpConsumer.java (original)
+++ activemq/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpConsumer.java Fri Jun 22 10:26:41 2007
@@ -18,6 +18,7 @@
 package org.apache.camel.component.http;
 
 import org.apache.camel.Processor;
+import org.apache.camel.Endpoint;
 import org.apache.camel.impl.DefaultConsumer;
 
 /**
@@ -31,8 +32,21 @@
 		super(endpoint, processor);
 		this.endpoint = endpoint;
 	}
-	
-	@Override
+
+    @Override
+    public HttpEndpoint getEndpoint() {
+        return (HttpEndpoint) super.getEndpoint();
+    }
+
+    public HttpBinding getBinding() {
+        return endpoint.getBinding();
+    }
+
+    public String getPath() {
+        return endpoint.getPath();
+    }
+
+    @Override
 	protected void doStart() throws Exception {
 		super.doStart();
 		endpoint.connect(this);		
@@ -44,13 +58,4 @@
 		super.doStop();
 	}
 
-	public HttpBinding getBinding() {
-		return endpoint.getBinding();
-	}
-
-	public String getPath() {
-		return endpoint.getPath();
-	}
-	
-	
 }

Modified: activemq/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpEndpoint.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpEndpoint.java?view=diff&rev=549890&r1=549889&r2=549890
==============================================================================
--- activemq/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpEndpoint.java (original)
+++ activemq/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpEndpoint.java Fri Jun 22 10:26:41 2007
@@ -55,11 +55,11 @@
     }
 
     public HttpExchange createExchange() {
-        return new HttpExchange(getContext());
+        return new HttpExchange(this);
     }
 
     public HttpExchange createExchange(HttpServletRequest request, HttpServletResponse response) {
-        return new HttpExchange(getContext(), request, response);
+        return new HttpExchange(this, request, response);
     }
 
     public HttpBinding getBinding() {

Modified: activemq/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpExchange.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpExchange.java?view=diff&rev=549890&r1=549889&r2=549890
==============================================================================
--- activemq/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpExchange.java (original)
+++ activemq/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpExchange.java Fri Jun 22 10:26:41 2007
@@ -18,6 +18,7 @@
 package org.apache.camel.component.http;
 
 import org.apache.camel.CamelContext;
+import org.apache.camel.Endpoint;
 import org.apache.camel.impl.DefaultExchange;
 
 import javax.servlet.http.HttpServletRequest;
@@ -30,20 +31,23 @@
  * @version $Revision$
  */
 public class HttpExchange extends DefaultExchange {
+    private final HttpEndpoint endpoint;
     private HttpServletRequest request;
     private HttpServletResponse response;
 
-    public HttpExchange(CamelContext context) {
-        super(context);
+    public HttpExchange(HttpEndpoint endpoint) {
+        super(endpoint.getContext());
+        this.endpoint = endpoint;
     }
 
-    public HttpExchange(CamelContext context, HttpServletRequest request, HttpServletResponse response) {
-        super(context);
+    public HttpExchange(HttpEndpoint endpoint, HttpServletRequest request, HttpServletResponse response) {
+        this(endpoint);
         this.request = request;
         this.response = response;
-        setIn(new HttpMessage(request));
+        setIn(new HttpMessage(this, request));
     }
 
+
     /**
      * Returns the underlying Servlet request for inbound HTTP requests
      *
@@ -60,5 +64,9 @@
      */
     public HttpServletResponse getResponse() {
         return response;
+    }
+
+    public HttpEndpoint getEndpoint() {
+        return endpoint;
     }
 }

Modified: activemq/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpMessage.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpMessage.java?view=diff&rev=549890&r1=549889&r2=549890
==============================================================================
--- activemq/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpMessage.java (original)
+++ activemq/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpMessage.java Fri Jun 22 10:26:41 2007
@@ -18,9 +18,13 @@
 package org.apache.camel.component.http;
 
 import org.apache.camel.impl.DefaultMessage;
+import org.apache.camel.Exchange;
+import org.apache.camel.RuntimeCamelException;
 
 import javax.servlet.http.HttpServletRequest;
 import java.util.Map;
+import java.util.Enumeration;
+import java.io.IOException;
 
 /**
  * @version $Revision$
@@ -28,20 +32,41 @@
 public class HttpMessage extends DefaultMessage {
     private HttpServletRequest request;
 
-    public HttpMessage() {
-    }
 
-    public HttpMessage(HttpServletRequest request) {
+    public HttpMessage(HttpExchange exchange, HttpServletRequest request) {
+        setExchange(exchange);
         this.request = request;
+
+        // lets force a parse of the body
+        getBody();
+    }
+
+    @Override
+    public HttpExchange getExchange() {
+        return (HttpExchange) super.getExchange();
+    }
+
+    public HttpServletRequest getRequest() {
+        return request;
     }
 
     @Override
     protected Object createBody() {
-        return super.createBody();    /** TODO */
+        try {
+            return getExchange().getEndpoint().getBinding().parseBody(this);
+        }
+        catch (IOException e) {
+            throw new RuntimeCamelException(e);
+        }
     }
 
     @Override
-    protected Map<String, Object> createHeaders() {
-        return super.createHeaders();    /** TODO */
+    protected void populateInitialHeaders(Map<String, Object> map) {
+        Enumeration names = request.getHeaderNames();
+        while (names.hasMoreElements()) {
+            String name = (String) names.nextElement();
+            Object value = request.getHeader(name);
+            map.put(name, value);
+        }
     }
 }

Added: activemq/camel/trunk/components/camel-http/src/main/resources/META-INF/services/org/apache/camel/component/http
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-http/src/main/resources/META-INF/services/org/apache/camel/component/http?view=auto&rev=549890
==============================================================================
--- activemq/camel/trunk/components/camel-http/src/main/resources/META-INF/services/org/apache/camel/component/http (added)
+++ activemq/camel/trunk/components/camel-http/src/main/resources/META-INF/services/org/apache/camel/component/http Fri Jun 22 10:26:41 2007
@@ -0,0 +1,18 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You 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.
+#
+
+class=org.apache.camel.component.http.JettyHttpComponent

Modified: activemq/camel/trunk/components/camel-http/src/test/java/org/apache/camel/component/http/HttpRouteTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-http/src/test/java/org/apache/camel/component/http/HttpRouteTest.java?view=diff&rev=549890&r1=549889&r2=549890
==============================================================================
--- activemq/camel/trunk/components/camel-http/src/test/java/org/apache/camel/component/http/HttpRouteTest.java (original)
+++ activemq/camel/trunk/components/camel-http/src/test/java/org/apache/camel/component/http/HttpRouteTest.java Fri Jun 22 10:26:41 2007
@@ -17,51 +17,85 @@
  */
 package org.apache.camel.component.http;
 
-import java.io.InputStream;
-import java.net.URL;
-
-import junit.framework.TestCase;
-
-import org.apache.camel.CamelContext;
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Exchange;
+import org.apache.camel.Message;
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.component.mock.MockEndpoint;
-import org.apache.camel.impl.DefaultCamelContext;
+
+import java.io.BufferedReader;
+import java.io.BufferedWriter;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+import java.net.URL;
+import java.net.URLConnection;
+import java.util.List;
 
 /**
  * @version $Revision: 520220 $
  */
-public class HttpRouteTest extends TestCase {
-	
-    public void testPojoRoutes() throws Exception {    	
-        CamelContext camelContext = new DefaultCamelContext();
-        
-        // START SNIPPET: register
-        JettyHttpComponent component = new JettyHttpComponent();
-        camelContext.addComponent("http", component);
-        // END SNIPPET: register
-        
-        // START SNIPPET: route
-        // lets add simple route
-        camelContext.addRoutes(new RouteBuilder() {
-            public void configure() {
-                from("http://0.0.0.0:8080/test").to("mock:a");
-            }
-        });
-        // END SNIPPET: route
+public class HttpRouteTest extends ContextTestSupport {
+    protected String expectedBody = "<hello>world!</hello>";
+
+    public void testPojoRoutes() throws Exception {
+        MockEndpoint mockEndpoint = resolveMandatoryEndpoint("mock:a", MockEndpoint.class);
+        mockEndpoint.expectedMessageCount(1);
+
+        invokeHttpEndpoint();
+
+        mockEndpoint.assertIsSatisfied();
+        List<Exchange> list = mockEndpoint.getReceivedExchanges();
+        Exchange exchange = list.get(0);
+        assertNotNull("exchange", exchange);
+
+        Message in = exchange.getIn();
+        assertNotNull("in", in);
 
-        MockEndpoint mockA = (MockEndpoint) camelContext.getEndpoint("mock:a");
-        mockA.expectedMessageCount(1);
-        
-        camelContext.start();
-        
-        // START SNIPPET: invoke
+        String actualBody = in.getBody(String.class);
+
+        log.info("Headers: " + in.getHeaders());
+        log.info("Received body: " + actualBody);
+
+        assertEquals("Body", expectedBody, actualBody);
+    }
+
+    protected void invokeHttpEndpoint() throws IOException {
         URL url = new URL("http://localhost:8080/test");
-        InputStream is = url.openConnection().getInputStream();
-        System.out.println("Content: "+is);
-        // END SNIPPET: invoke
-        
-        mockA.assertIsSatisfied();
-        
-        camelContext.stop();
+        URLConnection urlConnection = url.openConnection();
+        urlConnection.setDoInput(true);
+        urlConnection.setDoOutput(true);
+        urlConnection.setUseCaches(false);
+        urlConnection.setRequestProperty("Content-Type", "application/xml");
+
+        // Send POST data
+        OutputStream out = urlConnection.getOutputStream();
+        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out));
+        writer.write(expectedBody);
+        writer.close();
+
+        // read the response data
+        BufferedReader reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
+        while (true) {
+            String line = reader.readLine();
+            if (line == null) {
+                break;
+            }
+            log.info("Read: " + line);
+        }
+        reader.close();
+
+//        InputStream is = url.openConnection().getInputStream();
+//        System.out.println("Content: "+is);
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            public void configure() {
+                from("http://localhost:8080/test").to("mock:a");
+            }
+        };
     }
 }