svn commit: r550830 - in /activemq/camel/trunk/components/camel-http/src: main/java/org/apache/camel/component/http/ test/java/org/apache/camel/component/http/

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

svn commit: r550830 - in /activemq/camel/trunk/components/camel-http/src: main/java/org/apache/camel/component/http/ test/java/org/apache/camel/component/http/

jstrachan-2
Author: jstrachan
Date: Tue Jun 26 08:30:03 2007
New Revision: 550830

URL: http://svn.apache.org/viewvc?view=rev&rev=550830
Log:
added test case for performing GET/POST on a HTTP endpoint using commons-httpclient

Added:
    activemq/camel/trunk/components/camel-http/src/test/java/org/apache/camel/component/http/HttpRouteUsingUrlPostTest.java
      - copied, changed from r550718, 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/HttpProducer.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/HttpProducer.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java?view=diff&rev=550830&r1=550829&r2=550830
==============================================================================
--- activemq/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java (original)
+++ activemq/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java Tue Jun 26 08:30:03 2007
@@ -24,7 +24,10 @@
 import org.apache.commons.httpclient.Header;
 import org.apache.commons.httpclient.HttpClient;
 import org.apache.commons.httpclient.HttpMethod;
+import org.apache.commons.httpclient.methods.ByteArrayRequestEntity;
+import org.apache.commons.httpclient.methods.GetMethod;
 import org.apache.commons.httpclient.methods.PostMethod;
+import org.apache.commons.httpclient.methods.RequestEntity;
 
 import java.io.InputStream;
 
@@ -39,8 +42,7 @@
     }
 
     public void process(Exchange exchange) throws Exception {
-        String uri = getEndpoint().getEndpointUri();
-        HttpMethod method = createMethod(uri);
+        HttpMethod method = createMethod(exchange);
         int responseCode = httpClient.executeMethod(method);
 
         // lets store the result in the output message.
@@ -59,7 +61,31 @@
         out.setHeader("http.responseCode", responseCode);
     }
 
-    protected PostMethod createMethod(String uri) {
-        return new PostMethod(uri);
+    protected HttpMethod createMethod(Exchange exchange) {
+        String uri = getEndpoint().getEndpointUri();
+        RequestEntity requestEntity = createRequestEntity(exchange);
+        if (requestEntity == null) {
+            return new GetMethod(uri);
+        }
+        // TODO we might be PUT? - have some better way to explicitly choose method
+        PostMethod method = new PostMethod(uri);
+        method.setRequestEntity(requestEntity);
+        return method;
+    }
+
+    protected RequestEntity createRequestEntity(Exchange exchange) {
+        Message in = exchange.getIn();
+        RequestEntity entity = in.getBody(RequestEntity.class);
+        if (entity == null) {
+            byte[] data = in.getBody(byte[].class);
+            String contentType = in.getHeader("Content-Type", String.class);
+            if (contentType != null) {
+                return new ByteArrayRequestEntity(data, contentType);
+            }
+            else {
+                return new ByteArrayRequestEntity(data);
+            }
+        }
+        return entity;
     }
 }

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=550830&r1=550829&r2=550830
==============================================================================
--- 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 Tue Jun 26 08:30:03 2007
@@ -65,32 +65,7 @@
     }
 
     protected void invokeHttpEndpoint() throws IOException {
-        URL url = new URL("http://localhost:8080/test");
-        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);
+        template.sendBody("http://localhost:8080/test", expectedBody, "Content-Type", "application/xml");
     }
 
     @Override

Copied: activemq/camel/trunk/components/camel-http/src/test/java/org/apache/camel/component/http/HttpRouteUsingUrlPostTest.java (from r550718, 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/HttpRouteUsingUrlPostTest.java?view=diff&rev=550830&p1=activemq/camel/trunk/components/camel-http/src/test/java/org/apache/camel/component/http/HttpRouteTest.java&r1=550718&p2=activemq/camel/trunk/components/camel-http/src/test/java/org/apache/camel/component/http/HttpRouteUsingUrlPostTest.java&r2=550830
==============================================================================
--- 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/HttpRouteUsingUrlPostTest.java Tue Jun 26 08:30:03 2007
@@ -37,32 +37,7 @@
 /**
  * @version $Revision: 520220 $
  */
-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);
-
-        Map<String,Object> headers = in.getHeaders();
-        String actualBody = in.getBody(String.class);
-
-        log.info("Headers: " + headers);
-        log.info("Received body: " + actualBody);
-
-        assertEquals("Body", expectedBody, actualBody);
-        assertTrue("Should be more than one header but was: " + headers, headers.size() > 0);
-    }
+public class HttpRouteUsingUrlPostTest extends HttpRouteTest {
 
     protected void invokeHttpEndpoint() throws IOException {
         URL url = new URL("http://localhost:8080/test");
@@ -93,12 +68,4 @@
 //        System.out.println("Content: "+is);
     }
 
-    @Override
-    protected RouteBuilder createRouteBuilder() throws Exception {
-        return new RouteBuilder() {
-            public void configure() {
-                from("http://localhost:8080/test").convertBodyTo(String.class).to("mock:a");
-            }
-        };
-    }
-}
+}
\ No newline at end of file