camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From acosent...@apache.org
Subject [camel] 02/04: CAMEL-12751 - Default http4 producer to ignore content-length header.
Date Thu, 06 Sep 2018 08:12:44 GMT
This is an automated email from the ASF dual-hosted git repository.

acosentino pushed a commit to branch camel-2.22.x
in repository https://gitbox.apache.org/repos/asf/camel.git

commit f3057d75b7548e09eb62892b503c4abc8544b1b7
Author: Bob Paulin <bob@bobpaulin.com>
AuthorDate: Wed Aug 29 22:24:43 2018 -0500

    CAMEL-12751 - Default http4 producer to ignore content-length header.
---
 .../src/main/java/org/apache/camel/Exchange.java   |  1 +
 .../apache/camel/component/http4/HttpEndpoint.java | 13 ++++++
 .../camel/component/http4/HttpEntityConverter.java |  3 +-
 .../apache/camel/component/http4/HttpProducer.java |  7 +++-
 .../http4/HttpProducerContentLengthTest.java       | 49 ++++++++++++++++++----
 5 files changed, 64 insertions(+), 9 deletions(-)

diff --git a/camel-core/src/main/java/org/apache/camel/Exchange.java b/camel-core/src/main/java/org/apache/camel/Exchange.java
index 6ca2ac6..ded516f 100644
--- a/camel-core/src/main/java/org/apache/camel/Exchange.java
+++ b/camel-core/src/main/java/org/apache/camel/Exchange.java
@@ -160,6 +160,7 @@ public interface Exchange {
     String HTTP_SERVLET_REQUEST    = "CamelHttpServletRequest";
     String HTTP_SERVLET_RESPONSE   = "CamelHttpServletResponse";
 
+    String IGNORE_CONTENT_LENGTH_HEADER   = "CamelIgnoreContentLengthHeader";
     String INTERCEPTED_ENDPOINT = "CamelInterceptedEndpoint";
     String INTERCEPT_SEND_TO_ENDPOINT_WHEN_MATCHED = "CamelInterceptSendToEndpointWhenMatched";
     String INTERRUPTED = "CamelInterrupted";
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 3c8f875..be54d54 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
@@ -108,6 +108,8 @@ public class HttpEndpoint extends HttpCommonEndpoint {
     private int connectionsPerRoute;
     @UriParam(label = "security", description = "To use a custom X509HostnameVerifier such
as DefaultHostnameVerifier or NoopHostnameVerifier")
     private HostnameVerifier x509HostnameVerifier;
+    @UriParam(label = "producer,proxy", description = "Ignore Content-Length Header")
+    private boolean ignoreContentLengthHeader = true;
 
     public HttpEndpoint() {
     }
@@ -454,5 +456,16 @@ public class HttpEndpoint extends HttpCommonEndpoint {
     public void setSocketTimeout(int socketTimeout) {
         this.socketTimeout = socketTimeout;
     }
+    
+    /**
+     * Ignore Content-Length Header
+     */
+    public boolean isIgnoreContentLengthHeader() {
+        return ignoreContentLengthHeader;
+    }
+    
+    public void setIgnoreContentLengthHeader(boolean ignoreContentLengthHeader) {
+        this.ignoreContentLengthHeader = ignoreContentLengthHeader;
+    }
 
 }
diff --git a/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpEntityConverter.java
b/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpEntityConverter.java
index bcbe502..f827112 100644
--- a/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpEntityConverter.java
+++ b/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpEntityConverter.java
@@ -72,7 +72,8 @@ public final class HttpEntityConverter {
             Message inMessage = exchange.getIn();
             String length = inMessage.getHeader(Exchange.CONTENT_LENGTH, String.class);
             
-            if (ObjectHelper.isEmpty(length)) {
+            if (exchange.getProperty(Exchange.IGNORE_CONTENT_LENGTH_HEADER, Boolean.FALSE,
Boolean.class) || 
+                    ObjectHelper.isEmpty(length)) {
                 entity = new InputStreamEntity(in, -1);
             } else {
                 entity = new InputStreamEntity(in, Long.parseLong(length));
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 36f49ff..6654a26 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
@@ -111,6 +111,10 @@ public class HttpProducer extends DefaultProducer {
                 skipRequestHeaders = URISupport.parseQuery(queryString, false, true);
             }
         }
+        
+        if(getEndpoint().isIgnoreContentLengthHeader()) {
+            exchange.setProperty(Exchange.IGNORE_CONTENT_LENGTH_HEADER, Boolean.TRUE);
+        }
         HttpRequestBase httpRequest = createMethod(exchange);
         Message in = exchange.getIn();
         String httpProtocolVersion = in.getHeader(Exchange.HTTP_PROTOCOL_VERSION, String.class);
@@ -560,7 +564,8 @@ public class HttpProducer extends DefaultProducer {
                         InputStream is = in.getMandatoryBody(InputStream.class);
                         String length = in.getHeader(Exchange.CONTENT_LENGTH, String.class);
                         InputStreamEntity entity = null;
-                        if (ObjectHelper.isEmpty(length)) {
+                        if (exchange.getProperty(Exchange.IGNORE_CONTENT_LENGTH_HEADER, Boolean.FALSE,
Boolean.class) ||
+                                ObjectHelper.isEmpty(length)) {
                             entity = new InputStreamEntity(is, -1);
                         } else {
                             entity = new InputStreamEntity(is, Long.parseLong(length));
diff --git a/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpProducerContentLengthTest.java
b/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpProducerContentLengthTest.java
index 462f62b..fc28b86 100644
--- a/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpProducerContentLengthTest.java
+++ b/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpProducerContentLengthTest.java
@@ -52,17 +52,35 @@ public class HttpProducerContentLengthTest extends BaseHttpTest {
                 setResponseFactory(getHttpResponseFactory()).
                 setExpectationVerifier(getHttpExpectationVerifier()).
                 setSslContext(getSSLContext()).
-                registerHandler("/content", new HttpRequestHandler() {
+                registerHandler("/content-ignore", new HttpRequestHandler() {
                     @Override
                     public void handle(HttpRequest request, HttpResponse response, HttpContext
context) throws HttpException, IOException {
                         Header contentLengthHeader = request.getFirstHeader(Exchange.CONTENT_LENGTH);
                         String contentLength = contentLengthHeader != null ? contentLengthHeader.getValue()
: "";
+                        Header transferEncodingHeader = request.getFirstHeader(Exchange.TRANSFER_ENCODING);
+                        String transferEncoding = transferEncodingHeader != null ? transferEncodingHeader.getValue()
: "";
                         
-                        //Should we expect the length in the case that we remove the header
or should the header be empty?
-                        assertEquals(Integer.toString(bodyContent.length()), contentLength);
+                        //Request Body Chunked if no Content-Length set.
+                        assertEquals("", contentLength);
+                        assertEquals("chunked", transferEncoding);
                         response.setStatusCode(HttpStatus.SC_OK);
                     }
-                }).create();
+                }).registerHandler("/content-no-ignore", new HttpRequestHandler() {
+                    @Override
+                    public void handle(HttpRequest request, HttpResponse response, HttpContext
context) throws HttpException, IOException {
+                        Header contentLengthHeader = request.getFirstHeader(Exchange.CONTENT_LENGTH);
+                        String contentLength = contentLengthHeader != null ? contentLengthHeader.getValue()
: "";
+                        Header transferEncodingHeader = request.getFirstHeader(Exchange.TRANSFER_ENCODING);
+                        String transferEncoding = transferEncodingHeader != null ? transferEncodingHeader.getValue()
: "";
+                        
+                        //Content-Length was overridden to 10
+                        assertEquals("10", contentLength);
+                        assertEquals("", transferEncoding);
+                        response.setStatusCode(HttpStatus.SC_OK);
+                    }
+                })
+                .create();
+            
         localServer.start();
 
         super.setUp();
@@ -79,14 +97,31 @@ public class HttpProducerContentLengthTest extends BaseHttpTest {
     }
     
     @Test
-    public void testContentLengthIncorrect() throws Exception {
-        Exchange out = template.request("http4://" + localServer.getInetAddress().getHostName()
+ ":" + localServer.getLocalPort() + "/content", new Processor() {
+    public void testContentLengthIgnore() throws Exception {
+        Exchange out = template.request("http4://" + localServer.getInetAddress().getHostName()
+ ":" + localServer.getLocalPort() + "/content-ignore?bridgeEndpoint=true", new Processor()
{
 
             @Override
             public void process(Exchange exchange) throws Exception {
                 exchange.getIn().setHeader(Exchange.CONTENT_LENGTH, "1000");
                 exchange.getIn().setHeader(Exchange.CONTENT_TYPE, "application/json");
-                exchange.setProperty(Exchange.SKIP_GZIP_ENCODING, Boolean.TRUE);
+                exchange.getIn().setBody(new ByteArrayInputStreamCache(new ByteArrayInputStream(bodyContent.getBytes())));
+            }
+            
+        });
+
+        assertNotNull(out);
+        assertFalse("Should not fail", out.isFailed());
+        
+    }
+    
+    @Test
+    public void testContentLengthNoIgnore() throws Exception {
+        Exchange out = template.request("http4://" + localServer.getInetAddress().getHostName()
+ ":" + localServer.getLocalPort() + "/content-no-ignore?bridgeEndpoint=true&ignoreContentLengthHeader=false",
new Processor() {
+
+            @Override
+            public void process(Exchange exchange) throws Exception {
+                exchange.getIn().setHeader(Exchange.CONTENT_LENGTH, "10");
+                exchange.getIn().setHeader(Exchange.CONTENT_TYPE, "application/json");
                 exchange.getIn().setBody(new ByteArrayInputStreamCache(new ByteArrayInputStream(bodyContent.getBytes())));
             }
             


Mime
View raw message