This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/camel.git
commit a5068fdd877f070d80aaf1fa5e8409c2d6ac4db4 Author: Claus Ibsen <[email protected]> AuthorDate: Fri Feb 26 07:20:30 2021 +0100 camel-http - Optimize a bit --- .../component/http/HttpDeleteWithBodyMethod.java | 5 +++++ .../component/http/HttpGetWithBodyMethod.java | 5 +++++ .../apache/camel/component/http/HttpMethods.java | 25 ++++++++++++++++++++++ .../apache/camel/component/http/HttpProducer.java | 21 ++++++++++-------- 4 files changed, 47 insertions(+), 9 deletions(-) diff --git a/components/camel-http/src/main/java/org/apache/camel/component/http/HttpDeleteWithBodyMethod.java b/components/camel-http/src/main/java/org/apache/camel/component/http/HttpDeleteWithBodyMethod.java index d8a37d9..5c30f35 100644 --- a/components/camel-http/src/main/java/org/apache/camel/component/http/HttpDeleteWithBodyMethod.java +++ b/components/camel-http/src/main/java/org/apache/camel/component/http/HttpDeleteWithBodyMethod.java @@ -30,6 +30,11 @@ public class HttpDeleteWithBodyMethod extends HttpEntityEnclosingRequestBase { setEntity(entity); } + public HttpDeleteWithBodyMethod(URI uri, HttpEntity entity) { + setURI(uri); + setEntity(entity); + } + @Override public String getMethod() { return METHOD_NAME; diff --git a/components/camel-http/src/main/java/org/apache/camel/component/http/HttpGetWithBodyMethod.java b/components/camel-http/src/main/java/org/apache/camel/component/http/HttpGetWithBodyMethod.java index 47aa477..efa7cb5 100644 --- a/components/camel-http/src/main/java/org/apache/camel/component/http/HttpGetWithBodyMethod.java +++ b/components/camel-http/src/main/java/org/apache/camel/component/http/HttpGetWithBodyMethod.java @@ -30,6 +30,11 @@ public class HttpGetWithBodyMethod extends HttpEntityEnclosingRequestBase { setEntity(entity); } + public HttpGetWithBodyMethod(URI uri, HttpEntity entity) { + setURI(uri); + setEntity(entity); + } + @Override public String getMethod() { return METHOD_NAME; diff --git a/components/camel-http/src/main/java/org/apache/camel/component/http/HttpMethods.java b/components/camel-http/src/main/java/org/apache/camel/component/http/HttpMethods.java index d57f84c..22fff8a 100644 --- a/components/camel-http/src/main/java/org/apache/camel/component/http/HttpMethods.java +++ b/components/camel-http/src/main/java/org/apache/camel/component/http/HttpMethods.java @@ -16,6 +16,8 @@ */ package org.apache.camel.component.http; +import java.net.URI; + import org.apache.camel.Exchange; import org.apache.camel.Expression; import org.apache.camel.builder.ExpressionBuilder; @@ -70,6 +72,29 @@ public enum HttpMethods implements Expression { } } + public HttpRequestBase createMethod(final URI uri) { + switch (this) { + case GET: + return new HttpGet(uri); + case PATCH: + return new HttpPatch(uri); + case POST: + return new HttpPost(uri); + case PUT: + return new HttpPut(uri); + case DELETE: + return new HttpDelete(uri); + case HEAD: + return new HttpHead(uri); + case OPTIONS: + return new HttpOptions(uri); + case TRACE: + return new HttpTrace(uri); + default: + throw new RuntimeException("no such method " + this); + } + } + public final boolean isEntityEnclosing() { return entity; } diff --git a/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java b/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java index 4b6497d..e1869eb 100644 --- a/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java +++ b/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java @@ -85,13 +85,14 @@ public class HttpProducer extends DefaultProducer { private static final Logger LOG = LoggerFactory.getLogger(HttpProducer.class); private HttpClient httpClient; - private HttpContext httpContext; - private boolean throwException; - private boolean transferException; - private HeaderFilterStrategy httpProtocolHeaderFilterStrategy = new HttpProtocolHeaderFilterStrategy(); + private final HttpContext httpContext; + private final boolean throwException; + private final boolean transferException; + private final HeaderFilterStrategy httpProtocolHeaderFilterStrategy = new HttpProtocolHeaderFilterStrategy(); private int minOkRange; private int maxOkRange; private String defaultUrl; + private URI defaultUri; public HttpProducer(HttpEndpoint endpoint) { super(endpoint); @@ -124,6 +125,7 @@ public class HttpProducer extends DefaultProducer { queryString = UnsafeUriCharactersEncoder.encodeHttpURI(queryString); uri = URISupport.createURIWithQuery(uri, queryString); } + defaultUri = uri; defaultUrl = uri.toASCIIString(); } @@ -539,10 +541,11 @@ public class HttpProducer extends DefaultProducer { * @throws Exception is thrown if error creating RequestEntity */ protected HttpRequestBase createMethod(Exchange exchange) throws Exception { - if (defaultUrl == null) { + if (defaultUri == null || defaultUrl == null) { throw new IllegalArgumentException("Producer must be started"); } String url = defaultUrl; + URI uri = defaultUri; // the exchange can have some headers that override the default url and forces to create // a new url that is dynamic based on header values @@ -565,22 +568,22 @@ public class HttpProducer extends DefaultProducer { if (create) { // creating the url to use takes 2-steps url = HttpHelper.createURL(exchange, getEndpoint()); - URI uri = HttpHelper.createURI(exchange, url, getEndpoint()); + uri = HttpHelper.createURI(exchange, url, getEndpoint()); // get the url from the uri url = uri.toASCIIString(); } // create http holder objects for the request HttpMethods methodToUse = HttpMethodHelper.createMethod(exchange, getEndpoint()); - HttpRequestBase method = methodToUse.createMethod(url); + HttpRequestBase method = methodToUse.createMethod(uri); // 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); + method = new HttpDeleteWithBodyMethod(uri, requestEntity); } else if (getEndpoint().isGetWithBody() && "GET".equals(method.getMethod())) { HttpEntity requestEntity = createRequestEntity(exchange); - method = new HttpGetWithBodyMethod(url, requestEntity); + method = new HttpGetWithBodyMethod(uri, requestEntity); } LOG.trace("Using URL: {} with method: {}", url, method);
