You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2019/12/07 08:06:34 UTC

[camel] branch camel-2.x updated: Http4+get with body (#3372)

This is an automated email from the ASF dual-hosted git repository.

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


The following commit(s) were added to refs/heads/camel-2.x by this push:
     new 7304b7f  Http4+get with body (#3372)
7304b7f is described below

commit 7304b7f9fdba34a4d73c4045effe5aec8f1c9c88
Author: Michael Thirion <mt...@redhat.com>
AuthorDate: Sat Dec 7 09:06:21 2019 +0100

    Http4+get with body (#3372)
    
    * added support for GET with body on http4
    
    * added support for GET with body on http4
    
    * Update camel-http4 :: Added test method for getWithBody + update doc new param getWithBody
    
    * Fix unit test for camel-http4#getWithBody
    
    * CAMEL-14118: Add getWithBody option to camel-http and also fix so an explicit GET wont attempt to read body as its not in use, this helps CAMEL-14115
    backport of cf8c9a6 (cherry-pick)
---
 .../camel-http4/src/main/docs/http4-component.adoc |  5 +--
 .../apache/camel/component/http4/HttpEndpoint.java | 20 ++++++++++--
 .../component/http4/HttpGetWithBodyMethod.java     | 36 ++++++++++++++++++++++
 .../apache/camel/component/http4/HttpProducer.java | 12 ++++++--
 .../component/http4/helper/HttpMethodHelper.java   |  8 ++---
 .../camel/component/http4/HttpMethodsTest.java     | 17 +++++++++-
 6 files changed, 84 insertions(+), 14 deletions(-)

diff --git a/components/camel-http4/src/main/docs/http4-component.adoc b/components/camel-http4/src/main/docs/http4-component.adoc
index 292ec11..38eec4e 100644
--- a/components/camel-http4/src/main/docs/http4-component.adoc
+++ b/components/camel-http4/src/main/docs/http4-component.adoc
@@ -109,7 +109,7 @@ with the following path and query parameters:
 |===
 
 
-=== Query Parameters (49 parameters):
+=== Query Parameters (50 parameters):
 
 
 [width="100%",cols="2,5,^1,2",options="header"]
@@ -125,7 +125,8 @@ with the following path and query parameters:
 | *connectionClose* (producer) | Specifies whether a Connection Close header must be added to HTTP Request. By default connectionClose is false. | false | boolean
 | *cookieStore* (producer) | To use a custom CookieStore. By default the BasicCookieStore is used which is an in-memory only cookie store. Notice if bridgeEndpoint=true then the cookie store is forced to be a noop cookie store as cookie shouldn't be stored as we are just bridging (eg acting as a proxy). If a cookieHandler is set then the cookie store is also forced to be a noop cookie store as cookie handling is then performed by the cookieHandler. |  | CookieStore
 | *copyHeaders* (producer) | If this option is true then IN exchange headers will be copied to OUT exchange headers according to copy strategy. Setting this to false, allows to only include the headers from the HTTP response (not propagating IN headers). | true | boolean
-| *deleteWithBody* (producer) | Whether the HTTP DELETE should include the message body or not. By default HTTP DELETE do not include any HTTP message. However in some rare cases users may need to be able to include the message body. | false | boolean
+| *deleteWithBody* (producer) | Whether the HTTP DELETE should include the message body or not. By default HTTP DELETE do not include any HTTP body. However in some rare cases users may need to be able to include the message body. | false | boolean
+| *getWithBody* (producer) | Whether the HTTP GET should include the message body or not. By default HTTP GET do not include any HTTP body. However in some rare cases users may need to be able to include the message body. | false | boolean
 | *httpMethod* (producer) | Configure the HTTP method to use. The HttpMethod header cannot override this option if set. |  | HttpMethods
 | *ignoreResponseBody* (producer) | If this option is true, The http producer won't read response body and cache the input stream | false | boolean
 | *preserveHostHeader* (producer) | If the option is true, HttpProducer will set the Host header to the value contained in the current exchange Host header, useful in reverse proxy applications where you want the Host header received by the downstream server to reflect the URL called by the upstream client, this allows applications which use the Host header to generate accurate URL's for a proxied service | false | boolean
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 6049ee3..7ce1a19 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
@@ -109,10 +109,12 @@ public class HttpEndpoint extends HttpCommonEndpoint {
     private boolean clearExpiredCookies = true;
     @UriParam(label = "producer", description = "If this option is true, camel-http4 sends preemptive basic authentication to the server.")
     private boolean authenticationPreemptive;
+    @UriParam(label = "producer", description = "Whether the HTTP GET should include the message body or not."
+        + " By default HTTP GET do not include any HTTP body. However in some rare cases users may need to be able to include the message body.")
+    private boolean getWithBody;
     @UriParam(label = "producer", description = "Whether the HTTP DELETE should include the message body or not."
         + " By default HTTP DELETE do not include any HTTP message. However in some rare cases users may need to be able to include the message body.")
     private boolean deleteWithBody;
-
     @UriParam(label = "advanced", defaultValue = "200", description = "The maximum number of connections.")
     private int maxTotalConnections;
     @UriParam(label = "advanced", defaultValue = "20", description = "The maximum number of connections per route.")
@@ -311,13 +313,27 @@ public class HttpEndpoint extends HttpCommonEndpoint {
     /**
      * Whether the HTTP DELETE should include the message body or not.
      * <p/>
-     * By default HTTP DELETE do not include any HTTP message. However in some rare cases users may need to be able to include the
+     * By default HTTP DELETE do not include any HTTP body. However in some rare cases users may need to be able to include the
      * message body.
      */
     public void setDeleteWithBody(boolean deleteWithBody) {
         this.deleteWithBody = deleteWithBody;
     }
 
+    public boolean isGetWithBody() {
+        return getWithBody;
+    }
+
+    /**
+     * Whether the HTTP GET should include the message body or not.
+     * <p/>
+     * By default HTTP GET do not include any HTTP body. However in some rare cases users may need to be able to include the
+     * message body.
+     */
+    public void setGetWithBody(boolean getWithBody) {
+        this.getWithBody = getWithBody;
+    }
+
     public CookieStore getCookieStore() {
         return cookieStore;
     }
diff --git a/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpGetWithBodyMethod.java b/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpGetWithBodyMethod.java
new file mode 100644
index 0000000..23d5caa
--- /dev/null
+++ b/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpGetWithBodyMethod.java
@@ -0,0 +1,36 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.http4;
+
+import java.net.URI;
+
+import org.apache.http.HttpEntity;
+import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
+
+public class HttpGetWithBodyMethod extends HttpEntityEnclosingRequestBase {
+
+    public static final String METHOD_NAME = "GET";
+
+    public HttpGetWithBodyMethod(String uri, HttpEntity entity) {
+        setURI(URI.create(uri));
+        setEntity(entity);
+    }
+
+    public String getMethod() {
+        return METHOD_NAME;
+    }
+}
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 a565608..d05f44c 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
@@ -452,18 +452,24 @@ public class HttpProducer extends DefaultProducer {
         }
 
         // create http holder objects for the request
-        HttpEntity requestEntity = createRequestEntity(exchange);
-        HttpMethods methodToUse = HttpMethodHelper.createMethod(exchange, getEndpoint(), requestEntity != null);
+        HttpMethods methodToUse = HttpMethodHelper.createMethod(exchange, getEndpoint());
         HttpRequestBase method = methodToUse.createMethod(url);
 
-        // special for HTTP DELETE if the message body should be included
+        // special for HTTP DELETE/GET if the message body should be included
         if (getEndpoint().isDeleteWithBody() && "DELETE".equals(method.getMethod())) {
+            HttpEntity requestEntity = createRequestEntity(exchange);
             method = new HttpDeleteWithBodyMethod(url, requestEntity);
+        } else if (getEndpoint().isGetWithBody() && "GET".equals(method.getMethod())) {
+            HttpEntity requestEntity = createRequestEntity(exchange);
+            method = new HttpGetWithBodyMethod(url, requestEntity);
         }
 
+
         LOG.trace("Using URL: {} with method: {}", url, method);
 
         if (methodToUse.isEntityEnclosing()) {
+            // only create entity for http payload if the HTTP method carries payload (such as POST)
+            HttpEntity requestEntity = createRequestEntity(exchange);
             ((HttpEntityEnclosingRequestBase) method).setEntity(requestEntity);
             if (requestEntity != null && requestEntity.getContentType() == null) {
                 LOG.debug("No Content-Type provided for URL: {} with exchange: {}", url, exchange);
diff --git a/components/camel-http4/src/main/java/org/apache/camel/component/http4/helper/HttpMethodHelper.java b/components/camel-http4/src/main/java/org/apache/camel/component/http4/helper/HttpMethodHelper.java
index c16dca2..a0e8c97 100644
--- a/components/camel-http4/src/main/java/org/apache/camel/component/http4/helper/HttpMethodHelper.java
+++ b/components/camel-http4/src/main/java/org/apache/camel/component/http4/helper/HttpMethodHelper.java
@@ -33,12 +33,8 @@ public final class HttpMethodHelper {
 
     /**
      * Creates the HttpMethod to use to call the remote server, often either its GET or POST.
-     *
-     * @param exchange the exchange
-     * @return the created method
-     * @throws URISyntaxException 
      */
-    public static HttpMethods createMethod(Exchange exchange, HttpEndpoint endpoint, boolean hasPayload) throws URISyntaxException {
+    public static HttpMethods createMethod(Exchange exchange, HttpEndpoint endpoint) throws URISyntaxException {
         // is a query string provided in the endpoint URI or in a header (header
         // overrules endpoint)
         String queryString = exchange.getIn().getHeader(Exchange.HTTP_QUERY, String.class);
@@ -76,7 +72,7 @@ public final class HttpMethodHelper {
                 answer = HttpMethods.GET;
             } else {
                 // fallback to POST if we have payload, otherwise GET
-                answer = hasPayload ? HttpMethods.POST : HttpMethods.GET;
+                answer = exchange.getMessage().getBody() != null ? HttpMethods.POST : HttpMethods.GET;
             }
         }
 
diff --git a/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpMethodsTest.java b/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpMethodsTest.java
index 0e552ac..5bd800e 100644
--- a/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpMethodsTest.java
+++ b/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpMethodsTest.java
@@ -223,6 +223,21 @@ public class HttpMethodsTest extends BaseHttpTest {
     }
 
     @Test
+    public void httpGetWithBody() throws Exception {
+
+        Exchange exchange = template.request("http4://" + localServer.getInetAddress().getHostName() + ":" + localServer.getLocalPort() + "/get?getWithBody=true", new Processor() {
+            public void process(Exchange exchange) throws Exception {
+                exchange.getIn().setHeader(Exchange.HTTP_METHOD, "GET");
+                exchange.getIn().setBody("rocks camel?");
+            }
+        });
+
+        assertExchange(exchange);
+
+        // the http server will not provide body on HTTP GET so we cannot test the server side
+    }
+
+    @Test
     public void httpHead() throws Exception {
 
         Exchange exchange = template.request("http4://" + localServer.getInetAddress().getHostName() + ":" + localServer.getLocalPort() + "/head", new Processor() {
@@ -239,4 +254,4 @@ public class HttpMethodsTest extends BaseHttpTest {
         assertNull(out.getBody(String.class));
     }
 
-}
\ No newline at end of file
+}