camel-http-common - as a common module
Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/481baa67 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/481baa67 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/481baa67 Branch: refs/heads/master Commit: 481baa6715969d1a4a90d5786774214776535d70 Parents: be77f1a Author: Claus Ibsen <davscl...@apache.org> Authored: Thu Jul 23 13:50:37 2015 +0200 Committer: Claus Ibsen <davscl...@apache.org> Committed: Thu Jul 23 15:04:35 2015 +0200 ---------------------------------------------------------------------- .../apache/camel/http/common/HttpHelper.java | 28 ++++++++++++++++---- .../apache/camel/http/common/HttpMethods.java | 2 +- .../component/http4/helper/HttpHelperTest.java | 12 ++++----- .../component/jetty/JettyHttpProducer.java | 9 +++---- 4 files changed, 34 insertions(+), 17 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/481baa67/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 2a7189a..fad8889 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 @@ -469,18 +469,36 @@ public final class HttpHelper { * @throws URISyntaxException */ public static HttpMethods createMethod(Exchange exchange, HttpCommonEndpoint endpoint, boolean hasPayload) 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); + // We need also check the HTTP_URI header query part + String uriString = exchange.getIn().getHeader(Exchange.HTTP_URI, String.class); + // resolve placeholders in uriString + try { + uriString = exchange.getContext().resolvePropertyPlaceholders(uriString); + } catch (Exception e) { + throw new RuntimeExchangeException("Cannot resolve property placeholders with uri: " + uriString, exchange, e); + } + if (uriString != null) { + URI uri = new URI(uriString); + queryString = uri.getQuery(); + } + if (queryString == null) { + queryString = endpoint.getHttpUri().getRawQuery(); + } + // 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 (hasPayload) { - // use POST if we have payload - answer = HttpMethods.POST; - } else { - // fallback to GET + } 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/481baa67/components/camel-http-common/src/main/java/org/apache/camel/http/common/HttpMethods.java ---------------------------------------------------------------------- diff --git a/components/camel-http-common/src/main/java/org/apache/camel/http/common/HttpMethods.java b/components/camel-http-common/src/main/java/org/apache/camel/http/common/HttpMethods.java index 0bba6e9..5762634 100644 --- a/components/camel-http-common/src/main/java/org/apache/camel/http/common/HttpMethods.java +++ b/components/camel-http-common/src/main/java/org/apache/camel/http/common/HttpMethods.java @@ -18,6 +18,6 @@ package org.apache.camel.http.common; */ public enum HttpMethods { - GET, POST, PUT, DELETE, HEAD, OPTIONS, TRACE + GET, POST, PUT, DELETE, HEAD, OPTIONS, TRACE, PATCH } http://git-wip-us.apache.org/repos/asf/camel/blob/481baa67/components/camel-http4/src/test/java/org/apache/camel/component/http4/helper/HttpHelperTest.java ---------------------------------------------------------------------- diff --git a/components/camel-http4/src/test/java/org/apache/camel/component/http4/helper/HttpHelperTest.java b/components/camel-http4/src/test/java/org/apache/camel/component/http4/helper/HttpHelperTest.java index be9168a..d74e43f 100644 --- a/components/camel-http4/src/test/java/org/apache/camel/component/http4/helper/HttpHelperTest.java +++ b/components/camel-http4/src/test/java/org/apache/camel/component/http4/helper/HttpHelperTest.java @@ -26,8 +26,8 @@ import org.apache.camel.CamelContext; import org.apache.camel.Exchange; import org.apache.camel.Message; import org.apache.camel.component.http4.HttpEndpoint; -import org.apache.camel.component.http4.HttpMethods; import org.apache.camel.http.common.HttpHelper; +import org.apache.camel.http.common.HttpMethods; import org.apache.camel.impl.DefaultCamelContext; import org.apache.camel.impl.DefaultExchange; import org.junit.Test; @@ -114,7 +114,7 @@ public class HttpHelperTest { @Test public void createMethodAlwaysUseUserChoosenMethod() throws URISyntaxException { - HttpMethods method = HttpMethodHelper.createMethod( + HttpMethods method = HttpHelper.createMethod( createExchangeWithOptionalHttpQueryAndHttpMethodHeader("q=camel", HttpMethods.POST), createHttpEndpoint(true, "http://www.google.com/search"), false); @@ -124,7 +124,7 @@ public class HttpHelperTest { @Test public void createMethodUseGETIfQueryIsProvidedInHeader() throws URISyntaxException { - HttpMethods method = HttpMethodHelper.createMethod( + HttpMethods method = HttpHelper.createMethod( createExchangeWithOptionalHttpQueryAndHttpMethodHeader("q=camel", null), createHttpEndpoint(true, "http://www.google.com/search"), false); @@ -134,7 +134,7 @@ public class HttpHelperTest { @Test public void createMethodUseGETIfQueryIsProvidedInEndpointURI() throws URISyntaxException { - HttpMethods method = HttpMethodHelper.createMethod( + HttpMethods method = HttpHelper.createMethod( createExchangeWithOptionalHttpQueryAndHttpMethodHeader(null, null), createHttpEndpoint(true, "http://www.google.com/search?q=test"), false); @@ -144,7 +144,7 @@ public class HttpHelperTest { @Test public void createMethodUseGETIfNoneQueryOrPayloadIsProvided() throws URISyntaxException { - HttpMethods method = HttpMethodHelper.createMethod( + HttpMethods method = HttpHelper.createMethod( createExchangeWithOptionalHttpQueryAndHttpMethodHeader(null, null), createHttpEndpoint(true, "http://www.google.com/search"), false); @@ -154,7 +154,7 @@ public class HttpHelperTest { @Test public void createMethodUsePOSTIfNoneQueryButPayloadIsProvided() throws URISyntaxException { - HttpMethods method = HttpMethodHelper.createMethod( + HttpMethods method = HttpHelper.createMethod( createExchangeWithOptionalHttpQueryAndHttpMethodHeader(null, null), createHttpEndpoint(true, "http://www.google.com/search"), true); http://git-wip-us.apache.org/repos/asf/camel/blob/481baa67/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 a609a46..e3089c3 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 @@ -110,13 +110,12 @@ public class JettyHttpProducer extends DefaultAsyncProducer implements AsyncProc url = rewriteUrl; } - // TODO: should not use that - HttpMethods method = HttpHelper.createMethod(exchange, getEndpoint(), exchange.getIn().getBody() != null); + String methodName = HttpHelper.createMethod(exchange, getEndpoint(), exchange.getIn().getBody() != null).name(); JettyContentExchange httpExchange = getEndpoint().createContentExchange(); httpExchange.init(exchange, getBinding(), client, callback); httpExchange.setURL(url); // Url has to be set first - httpExchange.setMethod(method.name()); + httpExchange.setMethod(methodName); if (getEndpoint().getHttpClientParameters() != null) { // For jetty 9 these parameters can not be set on the client @@ -127,11 +126,11 @@ public class JettyHttpProducer extends DefaultAsyncProducer implements AsyncProc } String supportRedirect = (String)getEndpoint().getHttpClientParameters().get("supportRedirect"); if (supportRedirect != null) { - httpExchange.setSupportRedirect(new Boolean(supportRedirect)); + httpExchange.setSupportRedirect(Boolean.valueOf(supportRedirect)); } } - LOG.trace("Using URL: {} with method: {}", url, method); + LOG.trace("Using URL: {} with method: {}", url, methodName); // if there is a body to send as data if (exchange.getIn().getBody() != null) {