CAMEL-9833: Add mapHttpMessage option to allow to turn off mapping by default


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

Branch: refs/heads/master
Commit: f483e70f788086aee41e333cd06dcb5a41737f5e
Parents: cebc58c
Author: Andrea Cosentino <anco...@gmail.com>
Authored: Thu Apr 7 15:10:16 2016 +0200
Committer: Andrea Cosentino <anco...@gmail.com>
Committed: Thu Apr 7 16:27:24 2016 +0200

----------------------------------------------------------------------
 .../camel/http/common/DefaultHttpBinding.java   | 61 +++++++++++++-------
 .../apache/camel/http/common/HttpBinding.java   | 24 ++++++++
 .../camel/http/common/HttpCommonEndpoint.java   | 31 ++++++++++
 components/camel-http4/src/main/docs/http4.adoc |  6 +-
 .../jetty/HttpBridgeBigFormPostRouteTest.java   |  2 +-
 5 files changed, 102 insertions(+), 22 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/f483e70f/components/camel-http-common/src/main/java/org/apache/camel/http/common/DefaultHttpBinding.java
----------------------------------------------------------------------
diff --git 
a/components/camel-http-common/src/main/java/org/apache/camel/http/common/DefaultHttpBinding.java
 
b/components/camel-http-common/src/main/java/org/apache/camel/http/common/DefaultHttpBinding.java
index ad9adfe..974b869 100644
--- 
a/components/camel-http-common/src/main/java/org/apache/camel/http/common/DefaultHttpBinding.java
+++ 
b/components/camel-http-common/src/main/java/org/apache/camel/http/common/DefaultHttpBinding.java
@@ -75,6 +75,8 @@ public class DefaultHttpBinding implements HttpBinding {
     private boolean eagerCheckContentAvailable;
     private boolean transferException;
     private boolean allowJavaSerializedObject;
+    private boolean mapHttpMessageBody = true;
+    private boolean mapHttpMessageHeaders = true;
     private HeaderFilterStrategy headerFilterStrategy = new 
HttpHeaderFilterStrategy();
 
     public DefaultHttpBinding() {
@@ -97,25 +99,28 @@ public class DefaultHttpBinding implements HttpBinding {
     public void readRequest(HttpServletRequest request, HttpMessage message) {
         LOG.trace("readRequest {}", request);
         
-        // lets force a parse of the body and headers
-        message.getBody();
-        // populate the headers from the request
-        Map<String, Object> headers = message.getHeaders();
-        
-        //apply the headerFilterStrategy
-        Enumeration<?> names = request.getHeaderNames();
-        while (names.hasMoreElements()) {
-            String name = (String)names.nextElement();
-            String value = request.getHeader(name);
-            // use http helper to extract parameter value as it may contain 
multiple values
-            Object extracted = HttpHelper.extractHttpParameterValue(value);
-            // mapping the content-type
-            if (name.toLowerCase().equals("content-type")) {
-                name = Exchange.CONTENT_TYPE;
-            }
-            if (headerFilterStrategy != null
-                && !headerFilterStrategy.applyFilterToExternalHeaders(name, 
extracted, message.getExchange())) {
-                HttpHelper.appendHeader(headers, name, extracted);
+        // lets parse the body if mapHttpMessageBody is true
+        if (mapHttpMessageBody) {
+            message.getBody();
+        }
+        // populate the headers from the request if mapHttpHeaders is true
+        Map<String, Object> headers = message.getHeaders();  
+        if (mapHttpMessageHeaders) {
+            //apply the headerFilterStrategy
+            Enumeration<?> names = request.getHeaderNames();
+            while (names.hasMoreElements()) {
+                String name = (String)names.nextElement();
+                String value = request.getHeader(name);
+                // use http helper to extract parameter value as it may 
contain multiple values
+                Object extracted = HttpHelper.extractHttpParameterValue(value);
+                // mapping the content-type
+                if (name.toLowerCase().equals("content-type")) {
+                    name = Exchange.CONTENT_TYPE;
+                }
+                if (headerFilterStrategy != null
+                    && 
!headerFilterStrategy.applyFilterToExternalHeaders(name, extracted, 
message.getExchange())) {
+                    HttpHelper.appendHeader(headers, name, extracted);
+                }
             }
         }
                 
@@ -555,7 +560,23 @@ public class DefaultHttpBinding implements HttpBinding {
         this.headerFilterStrategy = headerFilterStrategy;
     }
 
-    protected static SimpleDateFormat getHttpDateFormat() {
+    public boolean isMapHttpMessageBody() {
+               return mapHttpMessageBody;
+       }
+
+       public void setMapHttpMessageBody(boolean mapHttpMessageBody) {
+               this.mapHttpMessageBody = mapHttpMessageBody;
+       }
+
+       public boolean isMapHttpMessageHeaders() {
+               return mapHttpMessageHeaders;
+       }
+
+       public void setMapHttpMessageHeaders(boolean mapHttpMessageHeaders) {
+               this.mapHttpMessageHeaders = mapHttpMessageHeaders;
+       }
+
+       protected static SimpleDateFormat getHttpDateFormat() {
         SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT, 
Locale.US);
         dateFormat.setTimeZone(TIME_ZONE_GMT);
         return dateFormat;

http://git-wip-us.apache.org/repos/asf/camel/blob/f483e70f/components/camel-http-common/src/main/java/org/apache/camel/http/common/HttpBinding.java
----------------------------------------------------------------------
diff --git 
a/components/camel-http-common/src/main/java/org/apache/camel/http/common/HttpBinding.java
 
b/components/camel-http-common/src/main/java/org/apache/camel/http/common/HttpBinding.java
index 9402301..1fa36a4 100644
--- 
a/components/camel-http-common/src/main/java/org/apache/camel/http/common/HttpBinding.java
+++ 
b/components/camel-http-common/src/main/java/org/apache/camel/http/common/HttpBinding.java
@@ -137,6 +137,16 @@ public interface HttpBinding {
      * This can be turned on in case HTTP clients do not send streamed data.
      */
     boolean isEagerCheckContentAvailable();
+    
+    /**
+     * Whether to allow Exchange Body HTTP mapping
+     */
+    boolean isMapHttpMessageBody();
+    
+    /**
+     * Whether to allow Exchange Headers HTTP mapping
+     */
+    boolean isMapHttpMessageHeaders();
 
     /**
      * Whether to eager check whether the HTTP requests has content if the 
content-length header is 0 or not present.
@@ -180,5 +190,19 @@ public interface HttpBinding {
      * @param headerFilterStrategy the custom strategy
      */
     void setHeaderFilterStrategy(HeaderFilterStrategy headerFilterStrategy);
+    
+    /**
+     * Whether to allow Exchange Body HTTP mapping
+     * <p/>
+     * This is by default turned on. If you disable this then be aware that 
the Exchange body won't be mapped to HTTP
+     */
+    void setMapHttpMessageBody(boolean mapHttpMessageBody);
+    
+    /**
+     * Whether to allow Exchange Headers HTTP mapping
+     * <p/>
+     * This is by default turned on. If you disable this then be aware that 
the Exchange headers won't be mapped to HTTP
+     */
+    void setMapHttpMessageHeaders(boolean mapHttpMessageHeaders);
 
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/f483e70f/components/camel-http-common/src/main/java/org/apache/camel/http/common/HttpCommonEndpoint.java
----------------------------------------------------------------------
diff --git 
a/components/camel-http-common/src/main/java/org/apache/camel/http/common/HttpCommonEndpoint.java
 
b/components/camel-http-common/src/main/java/org/apache/camel/http/common/HttpCommonEndpoint.java
index d6cea0a..e9b45fb 100644
--- 
a/components/camel-http-common/src/main/java/org/apache/camel/http/common/HttpCommonEndpoint.java
+++ 
b/components/camel-http-common/src/main/java/org/apache/camel/http/common/HttpCommonEndpoint.java
@@ -107,6 +107,14 @@ public abstract class HttpCommonEndpoint extends 
DefaultEndpoint implements Head
             description = "Refers to a custom 
org.apache.camel.component.http.UrlRewrite which allows you to rewrite urls 
when you bridge/proxy endpoints."
                     + " See more details at 
http://camel.apache.org/urlrewrite.html";)
     private UrlRewrite urlRewrite;
+    @UriParam(label = "advanced", defaultValue = "true",
+            description = "If this option is true then IN exchange Body of the 
exchange will be mapped to HTTP body." 
+            + " Setting this to false will avoid the HTTP mapping.")
+    boolean mapHttpMessageBody = true;
+    @UriParam(label = "advanced", defaultValue = "true",
+            description = "If this option is true then IN exchange Headers of 
the exchange will be mapped to HTTP headers."
+            + " Setting this to false will avoid the HTTP Headers mapping.")
+    boolean mapHttpMessageHeaders = true;
 
     public HttpCommonEndpoint() {
     }
@@ -169,6 +177,8 @@ public abstract class HttpCommonEndpoint extends 
DefaultEndpoint implements Head
                 
httpBinding.setAllowJavaSerializedObject(getComponent().isAllowJavaSerializedObject());
             }
             
httpBinding.setEagerCheckContentAvailable(isEagerCheckContentAvailable());
+            httpBinding.setMapHttpMessageBody(isMapHttpMessageBody());
+            httpBinding.setMapHttpMessageHeaders(isMapHttpMessageHeaders());
         }
         return httpBinding;
     }
@@ -446,4 +456,25 @@ public abstract class HttpCommonEndpoint extends 
DefaultEndpoint implements Head
         this.okStatusCodeRange = okStatusCodeRange;
     }
 
+       public boolean isMapHttpMessageBody() {
+               return mapHttpMessageBody;
+       }
+
+    /**
+     * If this option is true, the IN exchange body will be mapped to HTTP
+     */
+       public void setMapHttpMessageBody(boolean mapHttpMessageBody) {
+               this.mapHttpMessageBody = mapHttpMessageBody;
+       }
+
+       public boolean isMapHttpMessageHeaders() {
+               return mapHttpMessageHeaders;
+       }
+
+    /**
+     * If this option is true, the IN exchange headers will be mapped to HTTP 
Headers
+     */
+       public void setMapHttpMessageHeaders(boolean mapHttpMessageHeaders) {
+               this.mapHttpMessageHeaders = mapHttpMessageHeaders;
+       }
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/f483e70f/components/camel-http4/src/main/docs/http4.adoc
----------------------------------------------------------------------
diff --git a/components/camel-http4/src/main/docs/http4.adoc 
b/components/camel-http4/src/main/docs/http4.adoc
index acd9314..eb420c3 100644
--- a/components/camel-http4/src/main/docs/http4.adoc
+++ b/components/camel-http4/src/main/docs/http4.adoc
@@ -80,8 +80,9 @@ The HTTP4 component supports 13 options which are listed 
below.
 
 
 
+
 // endpoint options: START
-The HTTP4 component supports 27 endpoint options which are listed below:
+The HTTP4 component supports 29 endpoint options which are listed below:
 
 [width="100%",cols="2s,1,1m,1m,5",options="header"]
 |=======================================================================
@@ -111,12 +112,15 @@ The HTTP4 component supports 27 endpoint options which 
are listed below:
 | httpClientConfigurer | advanced |  | HttpClientConfigurer | Register a 
custom configuration strategy for new HttpClient instances created by producers 
or consumers such as to configure authentication mechanisms etc
 | httpClientOptions | advanced |  | Map | To configure the HttpClient using 
the key/values from the Map.
 | httpContext | advanced |  | HttpContext | To use a custom HttpContext 
instance
+| mapHttpMessageBody | advanced | true | boolean | If this option is true then 
IN exchange Body of the exchange will be mapped to HTTP body. Setting this to 
false will avoid the HTTP mapping.
+| mapHttpMessageHeaders | advanced | true | boolean | If this option is true 
then IN exchange Headers of the exchange will be mapped to HTTP headers. 
Setting this to false will avoid the HTTP Headers mapping.
 | synchronous | advanced | false | boolean | Sets whether synchronous 
processing should be strictly used or Camel is allowed to use asynchronous 
processing (if supported).
 | useSystemProperties | advanced | false | boolean | To use System Properties 
as fallback for configuration
 |=======================================================================
 // endpoint options: END
 
 
+
 [[HTTP4-MessageHeaders]]
 Message Headers
 ^^^^^^^^^^^^^^^

http://git-wip-us.apache.org/repos/asf/camel/blob/f483e70f/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpBridgeBigFormPostRouteTest.java
----------------------------------------------------------------------
diff --git 
a/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpBridgeBigFormPostRouteTest.java
 
b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpBridgeBigFormPostRouteTest.java
index 9d4221d..dd0ee1f 100644
--- 
a/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpBridgeBigFormPostRouteTest.java
+++ 
b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpBridgeBigFormPostRouteTest.java
@@ -118,7 +118,7 @@ public class HttpBridgeBigFormPostRouteTest extends 
BaseJettyTest {
 
                 from("jetty:http://localhost:"; + port2 + 
"/test/hello?matchOnUriPrefix=true")
                     .removeHeaders("formMetaData")
-                    .to("http://localhost:"; + port1 + "?bridgeEndpoint=true");
+                    .to("http://localhost:"; + port1 + 
"?bridgeEndpoint=true&mapHttpMessageHeaders=false");
             }
         };
     }  

Reply via email to