Repository: camel
Updated Branches:
  refs/heads/master f0a4852ba -> f7a75746a


CAMEL-10878: camel-http / camel-http4 - Allow to specify HTTP operation as uri 
parameter


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

Branch: refs/heads/master
Commit: c0b091376ae79f8d29bb7c60a1fcfc4abc8871d6
Parents: f0a4852
Author: Andrea Cosentino <[email protected]>
Authored: Thu Feb 23 13:19:13 2017 +0100
Committer: Andrea Cosentino <[email protected]>
Committed: Thu Feb 23 13:49:27 2017 +0100

----------------------------------------------------------------------
 .../camel/http/common/HttpCommonEndpoint.java   | 13 ++++
 .../apache/camel/http/common/HttpHelper.java    | 22 +++---
 .../src/main/docs/http-component.adoc           |  5 +-
 .../component/http/HttpGetUriParamTest.java     | 75 ++++++++++++++++++++
 .../http4/helper/HttpMethodHelper.java          | 23 +++---
 .../camel/component/http4/HttpMethodsTest.java  | 12 ++++
 .../JettyHttpProducerGetAsUriParamTest.java     | 70 ++++++++++++++++++
 7 files changed, 200 insertions(+), 20 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/c0b09137/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 8890cca..0854085 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
@@ -132,6 +132,8 @@ public abstract class HttpCommonEndpoint extends 
DefaultEndpoint implements Head
     private boolean async;
     @UriParam(label = "producer", description = "Configure a cookie handler to 
maintain a HTTP session")
     private CookieHandler cookieHandler;
+    @UriParam(label = "producer", description = "Configure the http Method to 
use as URI param. In case this is set, it can't be overriden by the HttpMethod 
header.")
+    private String httpMethod;
 
     public HttpCommonEndpoint() {
     }
@@ -553,4 +555,15 @@ public abstract class HttpCommonEndpoint extends 
DefaultEndpoint implements Head
     public void setCookieHandler(CookieHandler cookieHandler) {
         this.cookieHandler = cookieHandler;
     }
+
+       public String getHttpMethod() {
+               return httpMethod;
+       }
+
+    /**
+     * Configure the Http method to use
+     */
+       public void setHttpMethod(String httpMethod) {
+               this.httpMethod = httpMethod;
+       }
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/c0b09137/components/camel-http-common/src/main/java/org/apache/camel/http/common/HttpHelper.java
----------------------------------------------------------------------
diff --git 
a/components/camel-http-common/src/main/java/org/apache/camel/http/common/HttpHelper.java
 
b/components/camel-http-common/src/main/java/org/apache/camel/http/common/HttpHelper.java
index 8d64351..8252063 100644
--- 
a/components/camel-http-common/src/main/java/org/apache/camel/http/common/HttpHelper.java
+++ 
b/components/camel-http-common/src/main/java/org/apache/camel/http/common/HttpHelper.java
@@ -514,16 +514,20 @@ public final class HttpHelper {
 
         // compute what method to use either GET or POST
         HttpMethods answer;
-        HttpMethods m = exchange.getIn().getHeader(Exchange.HTTP_METHOD, 
HttpMethods.class);
-        if (m != null) {
-            // always use what end-user provides in a header
-            answer = m;
-        } else if (queryString != null) {
-            // if a query string is provided then use GET
-            answer = HttpMethods.GET;
+        if (ObjectHelper.isNotEmpty(endpoint.getHttpMethod())) {
+               answer = HttpMethods.valueOf(endpoint.getHttpMethod());
         } else {
-            // fallback to POST if we have payload, otherwise GET
-            answer = hasPayload ? HttpMethods.POST : HttpMethods.GET;
+            HttpMethods m = exchange.getIn().getHeader(Exchange.HTTP_METHOD, 
HttpMethods.class);
+            if (m != null) {
+                // always use what end-user provides in a header
+                answer = m;
+            } else if (queryString != null) {
+                // if a query string is provided then use GET
+                answer = HttpMethods.GET;
+            } else {
+                // fallback to POST if we have payload, otherwise GET
+                answer = hasPayload ? HttpMethods.POST : HttpMethods.GET;
+            }
         }
 
         return answer;

http://git-wip-us.apache.org/repos/asf/camel/blob/c0b09137/components/camel-http/src/main/docs/http-component.adoc
----------------------------------------------------------------------
diff --git a/components/camel-http/src/main/docs/http-component.adoc 
b/components/camel-http/src/main/docs/http-component.adoc
index 5f524ba..5cc67ea 100644
--- a/components/camel-http/src/main/docs/http-component.adoc
+++ b/components/camel-http/src/main/docs/http-component.adoc
@@ -141,7 +141,7 @@ The HTTP component supports 6 options which are listed 
below.
 
 
 // endpoint options: START
-The HTTP component supports 26 endpoint options which are listed below:
+The HTTP component supports 27 endpoint options which are listed below:
 
 {% raw %}
 [width="100%",cols="2,1,1m,1m,5",options="header"]
@@ -149,6 +149,7 @@ The HTTP component supports 26 endpoint options which are 
listed below:
 | Name | Group | Default | Java Type | Description
 | httpUri | producer |  | URI | *Required* The url of the HTTP endpoint to 
call.
 | disableStreamCache | common | false | boolean | Determines whether or not 
the raw input stream from Servlet is cached or not (Camel will read the stream 
into a in memory/overflow to file Stream caching) cache. By default Camel will 
cache the Servlet input stream to support reading it multiple times to ensure 
it Camel can retrieve all data from the stream. However you can set this option 
to true when you for example need to access the raw stream such as streaming it 
directly to a file or other persistent store. DefaultHttpBinding will copy the 
request input stream into a stream cache and put it into message body if this 
option is false to support reading the stream multiple times. If you use 
Servlet to bridge/proxy an endpoint then consider enabling this option to 
improve performance in case you do not need to read the message payload 
multiple times. The http/http4 producer will by default cache the response body 
stream. If setting this option to true then the producers will not ca
 che the response body stream but use the response stream as-is as the message 
body.
+| httpMethod | common |  | String | Configure the http Method to use as URI 
param. In case this is set it can't be overriden by the HttpMethod header.
 | authMethodPriority | producer |  | String | Authentication method for proxy 
either as Basic Digest or NTLM.
 | bridgeEndpoint | producer | false | boolean | If the option is true 
HttpProducer will ignore the Exchange.HTTP_URI header and use the endpoint's 
URI for request. You may also set the option throwExceptionOnFailure to be 
false to let the HttpProducer send all the fault response back.
 | chunked | producer | true | boolean | If this option is false the Servlet 
will disable the HTTP streaming and set the content-length header on the 
response
@@ -522,4 +523,4 @@ keystore and truststore as described above, it will work 
fine.
 * link:endpoint.html[Endpoint]
 * link:getting-started.html[Getting Started]
 
-* link:jetty.html[Jetty]
\ No newline at end of file
+* link:jetty.html[Jetty]

http://git-wip-us.apache.org/repos/asf/camel/blob/c0b09137/components/camel-http/src/test/java/org/apache/camel/component/http/HttpGetUriParamTest.java
----------------------------------------------------------------------
diff --git 
a/components/camel-http/src/test/java/org/apache/camel/component/http/HttpGetUriParamTest.java
 
b/components/camel-http/src/test/java/org/apache/camel/component/http/HttpGetUriParamTest.java
new file mode 100644
index 0000000..6c63033
--- /dev/null
+++ 
b/components/camel-http/src/test/java/org/apache/camel/component/http/HttpGetUriParamTest.java
@@ -0,0 +1,75 @@
+/**
+ * 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.http;
+
+import java.util.List;
+import java.util.Map;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.Message;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.junit.Ignore;
+import org.junit.Test;
+
+/**
+ */
+public class HttpGetUriParamTest extends CamelTestSupport {
+    
+    @Test
+    @Ignore("ignore online tests")
+    public void testHttpGet() throws Exception {
+        MockEndpoint mockEndpoint = resolveMandatoryEndpoint("mock:results", 
MockEndpoint.class);
+        mockEndpoint.expectedMessageCount(1);
+
+        template.sendBody("direct:start", null);
+
+        mockEndpoint.assertIsSatisfied();
+        List<Exchange> list = mockEndpoint.getReceivedExchanges();
+        Exchange exchange = list.get(0);
+        assertNotNull("exchange", exchange);
+
+        Message in = exchange.getIn();
+        assertNotNull("in", in);
+
+        Map<String, Object> headers = in.getHeaders();
+
+        log.debug("Headers: " + headers);
+        checkHeaders(headers);       
+
+        String body = in.getBody(String.class);
+
+        log.debug("Body: " + body);
+        assertNotNull("Should have a body!", body);
+    }
+
+    protected void checkHeaders(Map<String, Object> headers) {
+        assertTrue("Should be more than one header but was: " + headers, 
headers.size() > 0);
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            public void configure() {
+                getContext().addComponent("http2", new HttpComponent());
+                from("direct:start").setHeader(Exchange.HTTP_QUERY, 
constant("hl=en&q=activemq"))
+                    
.to("http2://www.google.com/search?httpMethod=GET").to("mock:results");
+            }
+        };
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/c0b09137/components/camel-http4/src/main/java/org/apache/camel/component/http4/helper/HttpMethodHelper.java
----------------------------------------------------------------------
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 57c4c9d..36714ae 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
@@ -23,6 +23,7 @@ import org.apache.camel.Exchange;
 import org.apache.camel.RuntimeExchangeException;
 import org.apache.camel.component.http4.HttpEndpoint;
 import org.apache.camel.component.http4.HttpMethods;
+import org.apache.camel.util.ObjectHelper;
 import org.apache.camel.util.UnsafeUriCharactersEncoder;
 
 public final class HttpMethodHelper {
@@ -62,16 +63,20 @@ public final class HttpMethodHelper {
 
         // compute what method to use either GET or POST
         HttpMethods answer;
-        HttpMethods m = exchange.getIn().getHeader(Exchange.HTTP_METHOD, 
HttpMethods.class);
-        if (m != null) {
-            // always use what end-user provides in a header
-            answer = m;
-        } else if (queryString != null) {
-            // if a query string is provided then use GET
-            answer = HttpMethods.GET;
+        if (ObjectHelper.isNotEmpty(endpoint.getHttpMethod())) {
+               answer = HttpMethods.valueOf(endpoint.getHttpMethod());
         } else {
-            // fallback to POST if we have payload, otherwise GET
-            answer = hasPayload ? HttpMethods.POST : HttpMethods.GET;
+            HttpMethods m = exchange.getIn().getHeader(Exchange.HTTP_METHOD, 
HttpMethods.class);
+            if (m != null) {
+                // always use what end-user provides in a header
+                answer = m;
+            } else if (queryString != null) {
+                // if a query string is provided then use GET
+                answer = HttpMethods.GET;
+            } else {
+                // fallback to POST if we have payload, otherwise GET
+                answer = hasPayload ? HttpMethods.POST : HttpMethods.GET;
+            }
         }
 
         return answer;

http://git-wip-us.apache.org/repos/asf/camel/blob/c0b09137/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpMethodsTest.java
----------------------------------------------------------------------
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 a65371d..397618f 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
@@ -81,6 +81,18 @@ public class HttpMethodsTest extends BaseHttpTest {
 
         assertExchange(exchange);
     }
+    
+    @Test
+    public void httpGetWithUriParam() throws Exception {
+
+        Exchange exchange = template.request("http4://" + 
localServer.getInetAddress().getHostName() + ":" + localServer.getLocalPort() + 
"/get?httpMethod=GET", new Processor() {
+            public void process(Exchange exchange) throws Exception {
+                exchange.getIn().setHeader(Exchange.HTTP_METHOD, "POST");
+            }
+        });
+
+        assertExchange(exchange);
+    }
 
     @Test
     public void httpPatch() throws Exception {

http://git-wip-us.apache.org/repos/asf/camel/blob/c0b09137/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/jettyproducer/JettyHttpProducerGetAsUriParamTest.java
----------------------------------------------------------------------
diff --git 
a/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/jettyproducer/JettyHttpProducerGetAsUriParamTest.java
 
b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/jettyproducer/JettyHttpProducerGetAsUriParamTest.java
new file mode 100644
index 0000000..3043221
--- /dev/null
+++ 
b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/jettyproducer/JettyHttpProducerGetAsUriParamTest.java
@@ -0,0 +1,70 @@
+/**
+ * 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.jetty.jettyproducer;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.jetty.BaseJettyTest;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.http.common.HttpMessage;
+import org.junit.Test;
+
+/**
+ * Unit test to verify that we can have URI options for external system 
(endpoint is lenient)
+ */
+public class JettyHttpProducerGetAsUriParamTest extends BaseJettyTest {
+
+    private String serverUri = "jetty://http://localhost:"; + getPort() + 
"/myservice?httpMethod=GET";
+    private MyParamsProcessor processor = new MyParamsProcessor();
+
+    @Test
+    public void testHttpGetWithParamsViaURI() throws Exception {
+        // these tests does not run well on Windows
+        if (isPlatform("windows")) {
+            return;
+        }
+
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedBodiesReceived("Bye World");
+
+        // give Jetty time to startup properly
+        Thread.sleep(1000);
+
+        template.requestBody(serverUri, "Hello World");
+
+        assertMockEndpointsSatisfied();
+    }
+
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            public void configure() throws Exception {
+                from(serverUri).process(processor).to("mock:result");
+            }
+        };
+    }
+
+    private static class MyParamsProcessor implements Processor {
+        public void process(Exchange exchange) throws Exception {
+            HttpMessage message = (HttpMessage)exchange.getIn();
+            assertNotNull(message.getRequest());
+
+            exchange.getOut().setBody("Bye World");
+            exchange.getOut().setHeader(Exchange.HTTP_METHOD, "POST");
+        }
+    }
+}
\ No newline at end of file

Reply via email to