Updated Branches: refs/heads/camel-2.11.x 36acab20f -> fa4b5a8dd refs/heads/camel-2.12.x 7b428956a -> 53d891565
CAMEL-6786 Added the AuthenticationPreemptive option for the camel-http4 endpoint with thanks to Martin Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/53d89156 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/53d89156 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/53d89156 Branch: refs/heads/camel-2.12.x Commit: 53d8915654b32071f08f0d487980f4a90767f4cb Parents: 7b42895 Author: Willem Jiang <ningji...@apache.org> Authored: Wed Oct 30 15:56:12 2013 +0800 Committer: Willem Jiang <willem.ji...@gmail.com> Committed: Wed Oct 30 16:02:07 2013 +0800 ---------------------------------------------------------------------- .../org/apache/camel/component/http4/HttpEndpoint.java | 9 +++++++++ .../org/apache/camel/component/http4/HttpProducer.java | 7 +++++++ .../camel/component/http4/HttpAuthenticationTest.java | 11 +++++++++++ 3 files changed, 27 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/53d89156/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpEndpoint.java ---------------------------------------------------------------------- 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 0c9a355..c5086a3 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 @@ -63,6 +63,7 @@ public class HttpEndpoint extends DefaultPollingEndpoint implements HeaderFilter private boolean disableStreamCache; private boolean transferException; private boolean traceEnabled; + private boolean authenticationPreemptive; private String httpMethodRestrict; private UrlRewrite urlRewrite; private boolean clearExpiredCookies = true; @@ -374,4 +375,12 @@ public class HttpEndpoint extends DefaultPollingEndpoint implements HeaderFilter public void setCookieStore(CookieStore cookieStore) { this.cookieStore = cookieStore; } + + public boolean isAuthenticationPreemptive() { + return authenticationPreemptive; + } + + public void setAuthenticationPreemptive(boolean authenticationPreemptive) { + this.authenticationPreemptive = authenticationPreemptive; + } } http://git-wip-us.apache.org/repos/asf/camel/blob/53d89156/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpProducer.java ---------------------------------------------------------------------- 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 6d95677..4d6f911 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 @@ -49,6 +49,8 @@ import org.apache.camel.util.URISupport; import org.apache.http.Header; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; +import org.apache.http.auth.AuthScope; +import org.apache.http.auth.Credentials; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpEntityEnclosingRequestBase; import org.apache.http.client.methods.HttpRequestBase; @@ -58,6 +60,7 @@ import org.apache.http.entity.ContentType; import org.apache.http.entity.FileEntity; import org.apache.http.entity.InputStreamEntity; import org.apache.http.entity.StringEntity; +import org.apache.http.impl.auth.BasicScheme; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.params.CoreProtocolPNames; import org.apache.http.protocol.HttpContext; @@ -106,6 +109,10 @@ public class HttpProducer extends DefaultProducer { exchange.getIn().getHeaders().remove("host"); } HttpRequestBase httpRequest = createMethod(exchange); + if (getEndpoint().isAuthenticationPreemptive()) { + Credentials creds = ((DefaultHttpClient) httpClient).getCredentialsProvider().getCredentials(AuthScope.ANY); + httpRequest.addHeader(new BasicScheme().authenticate(creds, httpRequest)); + } Message in = exchange.getIn(); String httpProtocolVersion = in.getHeader(Exchange.HTTP_PROTOCOL_VERSION, String.class); if (httpProtocolVersion != null) { http://git-wip-us.apache.org/repos/asf/camel/blob/53d89156/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpAuthenticationTest.java ---------------------------------------------------------------------- diff --git a/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpAuthenticationTest.java b/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpAuthenticationTest.java index 4b31bb5..2d4c3de 100644 --- a/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpAuthenticationTest.java +++ b/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpAuthenticationTest.java @@ -48,6 +48,17 @@ public class HttpAuthenticationTest extends BaseHttpTest { assertExchange(exchange); } + + + @Test + public void basicAuthenticationPreemptive() throws Exception { + Exchange exchange = template.request("http4://" + getHostName() + ":" + getPort() + "/search?authUsername=" + user + "&authPassword=" + password + "&preemptiveAuth=true", new Processor() { + public void process(Exchange exchange) throws Exception { + } + }); + + assertExchange(exchange); + } @Test public void basicAuthenticationShouldFailWithoutCreds() throws Exception {