You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ni...@apache.org on 2015/06/17 17:35:55 UTC

[1/2] camel git commit: CAMEL-8876 Added the option to camel-http

Repository: camel
Updated Branches:
  refs/heads/master 42afd8793 -> ac13da377


CAMEL-8876 Added the option to camel-http


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/ac13da37
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/ac13da37
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/ac13da37

Branch: refs/heads/master
Commit: ac13da3774522926e5b1992f4ee7b95aa4c78847
Parents: 601681c
Author: Willem Jiang <wi...@gmail.com>
Authored: Wed Jun 17 23:13:45 2015 +0800
Committer: Willem Jiang <wi...@gmail.com>
Committed: Wed Jun 17 23:35:40 2015 +0800

----------------------------------------------------------------------
 .../apache/camel/component/http/HttpEndpoint.java    | 14 ++++++++++++++
 .../apache/camel/component/http/HttpProducer.java    | 15 ++++++++++-----
 .../apache/camel/component/jetty/HttpRouteTest.java  |  6 ++++++
 3 files changed, 30 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/ac13da37/components/camel-http/src/main/java/org/apache/camel/component/http/HttpEndpoint.java
----------------------------------------------------------------------
diff --git a/components/camel-http/src/main/java/org/apache/camel/component/http/HttpEndpoint.java b/components/camel-http/src/main/java/org/apache/camel/component/http/HttpEndpoint.java
index a78142c..5ad32b6 100644
--- a/components/camel-http/src/main/java/org/apache/camel/component/http/HttpEndpoint.java
+++ b/components/camel-http/src/main/java/org/apache/camel/component/http/HttpEndpoint.java
@@ -106,6 +106,8 @@ public class HttpEndpoint extends DefaultEndpoint implements HeaderFilterStrateg
     @UriParam(label = "consumer",
             description = "To use a custom buffer size on the javax.servlet.ServletResponse.")
     private Integer responseBufferSize;
+    @UriParam(label = "producer", defaultValue = "false")
+    private boolean ignoreResponseBody;
 
     public HttpEndpoint() {
     }
@@ -475,4 +477,16 @@ public class HttpEndpoint extends DefaultEndpoint implements HeaderFilterStrateg
     public void setResponseBufferSize(Integer responseBufferSize) {
         this.responseBufferSize = responseBufferSize;
     }
+
+    public boolean isIgnoreResponseBody() {
+        return ignoreResponseBody;
+    }
+
+    /**
+     * If this option is true, The http producer won't read response body and cache the input stream.
+     *
+     */
+    public void setIgnoreResponseBody(boolean ignoreResponseBody) {
+        this.ignoreResponseBody = ignoreResponseBody;
+    }
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/ac13da37/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java
----------------------------------------------------------------------
diff --git a/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java b/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java
index 32b7c05..1519362 100644
--- a/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java
+++ b/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java
@@ -169,7 +169,7 @@ public class HttpProducer extends DefaultProducer {
 
     protected void populateResponse(Exchange exchange, HttpMethod method, Message in, HeaderFilterStrategy strategy, int responseCode) throws IOException, ClassNotFoundException {
         //We just make the out message is not create when extractResponseBody throws exception,
-        Object response = extractResponseBody(method, exchange);
+        Object response = extractResponseBody(method, exchange, getEndpoint().isIgnoreResponseBody());
         Message answer = exchange.getOut();
 
         answer.setHeader(Exchange.HTTP_RESPONSE_CODE, responseCode);
@@ -205,7 +205,7 @@ public class HttpProducer extends DefaultProducer {
         String statusText = method.getStatusLine() != null ? method.getStatusLine().getReasonPhrase() : null;
         Map<String, String> headers = extractResponseHeaders(method.getResponseHeaders());
 
-        Object responseBody = extractResponseBody(method, exchange);
+        Object responseBody = extractResponseBody(method, exchange, getEndpoint().isIgnoreResponseBody());
         if (transferException && responseBody != null && responseBody instanceof Exception) {
             // if the response was a serialized exception then use that
             return (Exception) responseBody;
@@ -269,10 +269,11 @@ public class HttpProducer extends DefaultProducer {
      * Extracts the response from the method as a InputStream.
      *
      * @param method the method that was executed
+     * @param ignoreResponseBody if it is true, camel don't read the response and cached the input stream
      * @return the response either as a stream, or as a deserialized java object
      * @throws IOException can be thrown
      */
-    protected static Object extractResponseBody(HttpMethod method, Exchange exchange) throws IOException, ClassNotFoundException {
+    protected static Object extractResponseBody(HttpMethod method, Exchange exchange, boolean ignoreResponseBody) throws IOException, ClassNotFoundException {
         InputStream is = method.getResponseBodyAsStream();
         if (is == null) {
             return null;
@@ -293,11 +294,15 @@ public class HttpProducer extends DefaultProducer {
             // find the charset and set it to the Exchange
             HttpHelper.setCharsetFromContentType(contentType, exchange);
         }
-        InputStream response = doExtractResponseBodyAsStream(is, exchange);
+        
         // if content type is a serialized java object then de-serialize it back to a Java object
         if (contentType != null && contentType.equals(HttpConstants.CONTENT_TYPE_JAVA_SERIALIZED_OBJECT)) {
-            return HttpHelper.deserializeJavaObjectFromStream(response, exchange.getContext());
+            return HttpHelper.deserializeJavaObjectFromStream(is, exchange.getContext());
         } else {
+            InputStream response = null;
+            if (!ignoreResponseBody) {
+                response = doExtractResponseBodyAsStream(is, exchange);
+            }
             return response;
         }
     }

http://git-wip-us.apache.org/repos/asf/camel/blob/ac13da37/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpRouteTest.java
----------------------------------------------------------------------
diff --git a/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpRouteTest.java b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpRouteTest.java
index adb78c7..b21455d 100644
--- a/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpRouteTest.java
+++ b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpRouteTest.java
@@ -93,6 +93,12 @@ public class HttpRouteTest extends BaseJettyTest {
         String out = template.requestBody("http://localhost:" + port1 + "/echo", "HelloWorld", String.class);
         assertEquals("Get a wrong output " , "HelloWorld", out);
     }
+    
+    @Test
+    public void testEchoEndpointWithIgnoreResponseBody() throws Exception {
+        String out = template.requestBody("http://localhost:" + port1 + "/echo?ignoreResponseBody=true", "HelloWorld", String.class);
+        assertNull("Get a wrong output " , out);
+    }
 
     @Test
     public void testPostParameter() throws Exception {


[2/2] camel git commit: CAMEL-8876 Added an option for HttpProducer to ignore response body avoiding stream caching

Posted by ni...@apache.org.
CAMEL-8876 Added an option for HttpProducer to ignore response body avoiding stream caching


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/601681cf
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/601681cf
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/601681cf

Branch: refs/heads/master
Commit: 601681cf3f11bb66f9b0d416d1671117677a44c8
Parents: 42afd87
Author: Willem Jiang <wi...@gmail.com>
Authored: Wed Jun 17 16:12:59 2015 +0800
Committer: Willem Jiang <wi...@gmail.com>
Committed: Wed Jun 17 23:35:40 2015 +0800

----------------------------------------------------------------------
 .../camel/component/http4/HttpEndpoint.java     | 14 ++++++++
 .../camel/component/http4/HttpProducer.java     | 13 +++++---
 .../http4/HttpProducerContentTypeTest.java      | 18 +++++++++++
 .../http4/HttpThrowExceptionOnFailureTest.java  | 34 ++++++++++++++++++++
 4 files changed, 74 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/601681cf/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpEndpoint.java
----------------------------------------------------------------------
diff --git a/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpEndpoint.java b/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpEndpoint.java
index c22f9ac..14db1ca 100644
--- a/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpEndpoint.java
+++ b/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpEndpoint.java
@@ -87,6 +87,8 @@ public class HttpEndpoint extends DefaultEndpoint implements HeaderFilterStrateg
     private String httpMethodRestrict;
     @UriParam(label = "producer", defaultValue = "true")
     private boolean clearExpiredCookies = true;
+    @UriParam(label = "producer", defaultValue = "false")
+    private boolean ignoreResponseBody;
 
     public HttpEndpoint() {
     }
@@ -475,4 +477,16 @@ public class HttpEndpoint extends DefaultEndpoint implements HeaderFilterStrateg
     public void setAuthenticationPreemptive(boolean authenticationPreemptive) {
         this.authenticationPreemptive = authenticationPreemptive;
     }
+
+   
+    public boolean isIgnoreResponseBody() {
+        return ignoreResponseBody;
+    }
+
+    /**
+     * If this option is true, The http producer won't read response body and cached the input stream.
+     */
+    public void setIgnoreResponseBody(boolean ignoreResponseBody) {
+        this.ignoreResponseBody = ignoreResponseBody;
+    }
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/601681cf/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpProducer.java
----------------------------------------------------------------------
diff --git a/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpProducer.java b/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpProducer.java
index 612c6ce..c2790cc 100644
--- a/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpProducer.java
+++ b/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpProducer.java
@@ -185,7 +185,7 @@ public class HttpProducer extends DefaultProducer {
     protected void populateResponse(Exchange exchange, HttpRequestBase httpRequest, HttpResponse httpResponse,
                                     Message in, HeaderFilterStrategy strategy, int responseCode) throws IOException, ClassNotFoundException {
         // We just make the out message is not create when extractResponseBody throws exception
-        Object response = extractResponseBody(httpRequest, httpResponse, exchange);
+        Object response = extractResponseBody(httpRequest, httpResponse, exchange, getEndpoint().isIgnoreResponseBody());
         Message answer = exchange.getOut();
 
         answer.setHeader(Exchange.HTTP_RESPONSE_CODE, responseCode);
@@ -220,7 +220,7 @@ public class HttpProducer extends DefaultProducer {
         String statusText = httpResponse.getStatusLine() != null ? httpResponse.getStatusLine().getReasonPhrase() : null;
         Map<String, String> headers = extractResponseHeaders(httpResponse.getAllHeaders());
 
-        Object responseBody = extractResponseBody(httpRequest, httpResponse, exchange);
+        Object responseBody = extractResponseBody(httpRequest, httpResponse, exchange, getEndpoint().isIgnoreResponseBody());
         if (transferException && responseBody != null && responseBody instanceof Exception) {
             // if the response was a serialized exception then use that
             return (Exception) responseBody;
@@ -287,7 +287,7 @@ public class HttpProducer extends DefaultProducer {
      * @return the response either as a stream, or as a deserialized java object
      * @throws IOException can be thrown
      */
-    protected static Object extractResponseBody(HttpRequestBase httpRequest, HttpResponse httpResponse, Exchange exchange) throws IOException, ClassNotFoundException {
+    protected static Object extractResponseBody(HttpRequestBase httpRequest, HttpResponse httpResponse, Exchange exchange, boolean ignoreResponseBody) throws IOException, ClassNotFoundException {
         HttpEntity entity = httpResponse.getEntity();
         if (entity == null) {
             return null;
@@ -312,11 +312,14 @@ public class HttpProducer extends DefaultProducer {
             // find the charset and set it to the Exchange
             HttpHelper.setCharsetFromContentType(contentType, exchange);
         }
-        InputStream response = doExtractResponseBodyAsStream(is, exchange);
         // if content type is a serialized java object then de-serialize it back to a Java object
         if (contentType != null && contentType.equals(HttpConstants.CONTENT_TYPE_JAVA_SERIALIZED_OBJECT)) {
-            return HttpHelper.deserializeJavaObjectFromStream(response);
+            return HttpHelper.deserializeJavaObjectFromStream(is, exchange.getContext());
         } else {
+            InputStream response = null;
+            if (!ignoreResponseBody) {
+                response = doExtractResponseBodyAsStream(is, exchange);
+            }
             return response;
         }
     }

http://git-wip-us.apache.org/repos/asf/camel/blob/601681cf/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpProducerContentTypeTest.java
----------------------------------------------------------------------
diff --git a/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpProducerContentTypeTest.java b/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpProducerContentTypeTest.java
index 575b83d..e252595 100644
--- a/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpProducerContentTypeTest.java
+++ b/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpProducerContentTypeTest.java
@@ -92,4 +92,22 @@ public class HttpProducerContentTypeTest extends BaseHttpTest {
         assertEquals(CONTENT_TYPE, out.getOut().getBody(String.class));
         
     }
+    
+    @Test
+    public void testContentTypeWithBoundaryWithIgnoreResponseBody() throws Exception {
+        Exchange out = template.request("http4://" + localServer.getInetAddress().getHostName() + ":" + localServer.getLocalPort() + "/content?ignoreResponseBody=true", new Processor() {
+
+            @Override
+            public void process(Exchange exchange) throws Exception {
+                exchange.getIn().setHeader(Exchange.CONTENT_TYPE, CONTENT_TYPE);
+                exchange.getIn().setBody("This is content");
+            }
+            
+        });
+
+        assertNotNull(out);
+        assertFalse("Should not fail", out.isFailed());
+        assertNull(out.getOut().getBody());
+        
+    }
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/camel/blob/601681cf/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpThrowExceptionOnFailureTest.java
----------------------------------------------------------------------
diff --git a/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpThrowExceptionOnFailureTest.java b/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpThrowExceptionOnFailureTest.java
index 237dfc2..1ec73f5 100644
--- a/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpThrowExceptionOnFailureTest.java
+++ b/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpThrowExceptionOnFailureTest.java
@@ -91,4 +91,38 @@ public class HttpThrowExceptionOnFailureTest extends BaseHttpTest {
         HttpOperationFailedException cause = assertIsInstanceOf(HttpOperationFailedException.class, e);
         assertEquals(501, cause.getStatusCode());
     }
+    
+    @Test
+    public void httpGetWhichReturnsHttp501WithIgnoreResponseBody() throws Exception {
+        Exchange exchange = template.request("http4://" + localServer.getInetAddress().getHostName() + ":" 
+            + localServer.getLocalPort() + "/XXX?throwExceptionOnFailure=false&ignoreResponseBody=true", new Processor() {
+                public void process(Exchange exchange) throws Exception {
+                }
+            });
+
+        assertNotNull(exchange);
+
+        Message out = exchange.getOut();
+        assertNotNull(out);
+        assertNull(out.getBody());
+
+        Map<String, Object> headers = out.getHeaders();
+        assertEquals(HttpStatus.SC_NOT_IMPLEMENTED, headers.get(Exchange.HTTP_RESPONSE_CODE));
+        assertEquals("0", headers.get("Content-Length"));
+    }
+
+    @Test
+    public void httpGetWhichReturnsHttp501ShouldThrowAnExceptionWithIgnoreResponseBody() throws Exception {
+        Exchange reply = template.request("http4://" + localServer.getInetAddress().getHostName() + ":" 
+            + localServer.getLocalPort() + "/XXX?throwExceptionOnFailure=true&ignoreResponseBody=true", new Processor() {
+                public void process(Exchange exchange) throws Exception {
+                }
+            });
+
+        Exception e = reply.getException();
+        assertNotNull("Should have thrown an exception", e);
+        HttpOperationFailedException cause = assertIsInstanceOf(HttpOperationFailedException.class, e);
+        assertEquals(501, cause.getStatusCode());
+    }
+    
 }
\ No newline at end of file