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 <willem.ji...@gmail.com> Authored: Wed Jun 17 16:12:59 2015 +0800 Committer: Willem Jiang <willem.ji...@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