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 <willem.ji...@gmail.com> Authored: Wed Jun 17 23:13:45 2015 +0800 Committer: Willem Jiang <willem.ji...@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 {