Repository: camel Updated Branches: refs/heads/master bd4534941 -> 1ba5aa2f4
CAMEL-5806: HTTP components with producers should allow GET with data (a bit unusual but its allowed). Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/b0617e68 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/b0617e68 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/b0617e68 Branch: refs/heads/master Commit: b0617e68ab423f278f41a9c526bad0bb64ec8dda Parents: bd45349 Author: Claus Ibsen <davscl...@apache.org> Authored: Mon Feb 16 12:12:11 2015 +0100 Committer: Claus Ibsen <davscl...@apache.org> Committed: Mon Feb 16 12:12:11 2015 +0100 ---------------------------------------------------------------------- .../component/http/DefaultHttpBinding.java | 4 +- .../camel/component/http/HttpGetTest.java | 3 +- .../component/http4/DefaultHttpBinding.java | 4 +- .../component/jetty/JettyHttpProducer.java | 5 +- .../component/jetty/HttpGetWithBodyTest.java | 53 ++++++++++++++++++++ 5 files changed, 60 insertions(+), 9 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/b0617e68/components/camel-http/src/main/java/org/apache/camel/component/http/DefaultHttpBinding.java ---------------------------------------------------------------------- diff --git a/components/camel-http/src/main/java/org/apache/camel/component/http/DefaultHttpBinding.java b/components/camel-http/src/main/java/org/apache/camel/component/http/DefaultHttpBinding.java index 49b9a47..b4c54fb 100644 --- a/components/camel-http/src/main/java/org/apache/camel/component/http/DefaultHttpBinding.java +++ b/components/camel-http/src/main/java/org/apache/camel/component/http/DefaultHttpBinding.java @@ -437,8 +437,8 @@ public class DefaultHttpBinding implements HttpBinding { public Object parseBody(HttpMessage httpMessage) throws IOException { // lets assume the body is a reader HttpServletRequest request = httpMessage.getRequest(); - // Need to handle the GET Method which has no inputStream - if ("GET".equals(request.getMethod())) { + // there is only a body if we have a content length, or its -1 to indicate unknown length + if (request.getContentLength() == 0) { return null; } if (isUseReaderForPayload()) { http://git-wip-us.apache.org/repos/asf/camel/blob/b0617e68/components/camel-http/src/test/java/org/apache/camel/component/http/HttpGetTest.java ---------------------------------------------------------------------- diff --git a/components/camel-http/src/test/java/org/apache/camel/component/http/HttpGetTest.java b/components/camel-http/src/test/java/org/apache/camel/component/http/HttpGetTest.java index a061e7b..02effb5 100644 --- a/components/camel-http/src/test/java/org/apache/camel/component/http/HttpGetTest.java +++ b/components/camel-http/src/test/java/org/apache/camel/component/http/HttpGetTest.java @@ -35,7 +35,7 @@ public class HttpGetTest extends CamelTestSupport { protected String expectedText = "activemq"; @Test - @Ignore("ignore online tests, will be improved in Camel 2.3") + @Ignore("ignore online tests") public void testHttpGet() throws Exception { MockEndpoint mockEndpoint = resolveMandatoryEndpoint("mock:results", MockEndpoint.class); mockEndpoint.expectedMessageCount(1); @@ -64,7 +64,6 @@ public class HttpGetTest extends CamelTestSupport { protected void checkHeaders(Map<String, Object> headers) { assertTrue("Should be more than one header but was: " + headers, headers.size() > 0); - } @Override http://git-wip-us.apache.org/repos/asf/camel/blob/b0617e68/components/camel-http4/src/main/java/org/apache/camel/component/http4/DefaultHttpBinding.java ---------------------------------------------------------------------- diff --git a/components/camel-http4/src/main/java/org/apache/camel/component/http4/DefaultHttpBinding.java b/components/camel-http4/src/main/java/org/apache/camel/component/http4/DefaultHttpBinding.java index b447558..aca11ab 100644 --- a/components/camel-http4/src/main/java/org/apache/camel/component/http4/DefaultHttpBinding.java +++ b/components/camel-http4/src/main/java/org/apache/camel/component/http4/DefaultHttpBinding.java @@ -400,8 +400,8 @@ public class DefaultHttpBinding implements HttpBinding { public Object parseBody(HttpMessage httpMessage) throws IOException { // lets assume the body is a reader HttpServletRequest request = httpMessage.getRequest(); - // Need to handle the GET Method which has no inputStream - if ("GET".equals(request.getMethod())) { + // there is only a body if we have a content length, or its -1 to indicate unknown length + if (request.getContentLength() == 0) { return null; } if (isUseReaderForPayload()) { http://git-wip-us.apache.org/repos/asf/camel/blob/b0617e68/components/camel-jetty-common/src/main/java/org/apache/camel/component/jetty/JettyHttpProducer.java ---------------------------------------------------------------------- diff --git a/components/camel-jetty-common/src/main/java/org/apache/camel/component/jetty/JettyHttpProducer.java b/components/camel-jetty-common/src/main/java/org/apache/camel/component/jetty/JettyHttpProducer.java index 46b0213..88e2204 100644 --- a/components/camel-jetty-common/src/main/java/org/apache/camel/component/jetty/JettyHttpProducer.java +++ b/components/camel-jetty-common/src/main/java/org/apache/camel/component/jetty/JettyHttpProducer.java @@ -133,9 +133,8 @@ public class JettyHttpProducer extends DefaultAsyncProducer implements AsyncProc LOG.trace("Using URL: {} with method: {}", url, method); - // if we post or put then set data - if (HttpMethods.POST.equals(methodToUse) || HttpMethods.PUT.equals(methodToUse)) { - + // if there is a body to send as data + if (exchange.getIn().getBody() != null) { String contentType = ExchangeHelper.getContentType(exchange); if (contentType != null) { httpExchange.setRequestContentType(contentType); http://git-wip-us.apache.org/repos/asf/camel/blob/b0617e68/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpGetWithBodyTest.java ---------------------------------------------------------------------- diff --git a/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpGetWithBodyTest.java b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpGetWithBodyTest.java new file mode 100644 index 0000000..32e0b0c --- /dev/null +++ b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpGetWithBodyTest.java @@ -0,0 +1,53 @@ +/** + * 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; + +import org.apache.camel.Exchange; +import org.apache.camel.Processor; +import org.apache.camel.builder.RouteBuilder; +import org.junit.Test; + +public class HttpGetWithBodyTest extends BaseJettyTest { + + @Test + public void testGetWithBody() throws Exception { + // use jetty http producer as it support GET with body (regular camel-http producer does not) + Exchange result = template.request("jetty:http://localhost:{{port}}/myapp", new Processor() { + @Override + public void process(Exchange exchange) throws Exception { + exchange.getIn().setHeader(Exchange.HTTP_METHOD, "GET"); + exchange.getIn().setBody("Camel"); + } + }); + assertNotNull(result); + + assertEquals("Hello Camel", result.getOut().getBody(String.class)); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + public void configure() throws Exception { + from("jetty:http://localhost:{{port}}/myapp?disableStreamCache=false") + .convertBodyTo(String.class) + .to("log:foo") + .transform(body().prepend("Hello ")); + } + }; + } + +}