[ https://issues.apache.org/jira/browse/CAMEL-12642?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16552902#comment-16552902 ]
ASF GitHub Bot commented on CAMEL-12642: ---------------------------------------- oscerd closed pull request #2432: CAMEL-12642:Fix for http4 feature authenticationPreemptive in pollEnrich URL: https://github.com/apache/camel/pull/2432 This is a PR merged from a forked repository. As GitHub hides the original diff on merge, it is displayed below for the sake of provenance: As this is a foreign pull request (from a fork), the diff is supplied below (as it won't show otherwise due to GitHub magic): diff --git a/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpPollingConsumer.java b/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpPollingConsumer.java index 0020094f03f..2b372c0d4f9 100644 --- a/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpPollingConsumer.java +++ b/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpPollingConsumer.java @@ -33,6 +33,8 @@ import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpRequestBase; import org.apache.http.client.protocol.HttpClientContext; +import org.apache.http.impl.auth.BasicScheme; +import org.apache.http.protocol.HttpContext; import org.apache.http.util.EntityUtils; /** @@ -43,11 +45,20 @@ public class HttpPollingConsumer extends PollingConsumerSupport implements ServicePoolAware { private final HttpEndpoint endpoint; private HttpClient httpClient; - + private HttpContext httpContext; + + public HttpPollingConsumer(HttpEndpoint endpoint) { super(endpoint); this.endpoint = endpoint; + this.httpContext = endpoint.getHttpContext(); this.httpClient = endpoint.getHttpClient(); + + } + + @Override + public HttpEndpoint getEndpoint() { + return (HttpEndpoint) super.getEndpoint(); } public Exchange receive() { @@ -76,7 +87,7 @@ protected Exchange doReceive(int timeout) { HttpEntity responeEntity = null; try { // execute request - HttpResponse response = httpClient.execute(method, httpClientContext); + HttpResponse response = executeMethod(method, httpClientContext); int responseCode = response.getStatusLine().getStatusCode(); responeEntity = response.getEntity(); Object body = HttpHelper.readResponseBodyFromInputStream(responeEntity.getContent(), exchange); @@ -117,6 +128,25 @@ protected Exchange doReceive(int timeout) { } } } + + /** + * Strategy when executing the method (calling the remote server). + * + * @param httpRequest the http Request to execute + * @return the response + * @throws IOException can be thrown + */ + protected HttpResponse executeMethod(HttpRequestBase httpRequest, HttpClientContext httpClientContext) throws IOException { + + if (getEndpoint().isAuthenticationPreemptive()) { + BasicScheme basicAuth = new BasicScheme(); + httpClientContext.setAttribute("preemptive-auth", basicAuth); + } + if (httpContext != null) { + httpClientContext = new HttpClientContext(httpContext); + } + return httpClient.execute(httpRequest, httpClientContext); + } // Properties //------------------------------------------------------------------------- diff --git a/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpPollingConsumerTest.java b/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpPollingConsumerTest.java index 6cda2978119..d257810619c 100644 --- a/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpPollingConsumerTest.java +++ b/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpPollingConsumerTest.java @@ -33,6 +33,8 @@ public class HttpPollingConsumerTest extends BaseHttpTest { private HttpServer localServer; + private String user = "camel"; + private String password = "password"; @Before @Override @@ -59,6 +61,22 @@ public void tearDown() throws Exception { } } + @Test + public void basicAuthenticationShouldSuccess() throws Exception { + String body = consumer.receiveBody("http4://" + localServer.getInetAddress().getHostName() + ":" + localServer.getLocalPort() + "/?authUsername=" + user + "&authPassword=" + + password, String.class); + assertEquals(getExpectedContent(), body); + + } + + @Test + public void basicAuthenticationPreemptiveShouldSuccess() throws Exception { + + String body = consumer.receiveBody("http4://" + localServer.getInetAddress().getHostName() + ":" + localServer.getLocalPort() + "/?authUsername=" + user + "&authPassword=" + + password + "&authenticationPreemptive=true", String.class); + assertEquals(getExpectedContent(), body); + } + @Test public void testReceive() throws Exception { String body = consumer.receiveBody("http4://" + localServer.getInetAddress().getHostName() + ":" + localServer.getLocalPort() + "/", String.class); diff --git a/platforms/spring-boot/components-starter/camel-http4-starter/src/main/java/org/apache/camel/component/http4/springboot/HttpComponentConfiguration.java b/platforms/spring-boot/components-starter/camel-http4-starter/src/main/java/org/apache/camel/component/http4/springboot/HttpComponentConfiguration.java index c71dd5ba63e..4c9399c0f67 100644 --- a/platforms/spring-boot/components-starter/camel-http4-starter/src/main/java/org/apache/camel/component/http4/springboot/HttpComponentConfiguration.java +++ b/platforms/spring-boot/components-starter/camel-http4-starter/src/main/java/org/apache/camel/component/http4/springboot/HttpComponentConfiguration.java @@ -32,7 +32,7 @@ ComponentConfigurationPropertiesCommon { /** - * Whether to enable auto configuration of the https4 component. This is + * Whether to enable auto configuration of the http4 component. This is * enabled by default. */ private Boolean enabled; ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org > No support for http4 feature authenticationPreemptive in pollEnrich > ------------------------------------------------------------------- > > Key: CAMEL-12642 > URL: https://issues.apache.org/jira/browse/CAMEL-12642 > Project: Camel > Issue Type: New Feature > Components: camel-http4 > Affects Versions: 2.23.0 > Reporter: Frank Olschewski > Assignee: Ramu > Priority: Major > > authenticationPreemptive=true does not work with pollEnrich. > {code:java} > .pollEnrich("https4:localhost?authenticationPreemptive=true" + > "&authUsername=user&authPassword=passwd"){code} > In Class /org/apache/camel/component/http4/HttpPollingConsumer.java:79 you > see the following code: > {code:java} > HttpResponse response = httpClient.execute(method, httpClientContext);{code} > The setup for authenticationPreemptive is missing. > You can see what to do in > org.apache.camel.component.http4.HttpProducer#executeMethod: > {code:java} > pprotected HttpResponse executeMethod(HttpUriRequest httpRequest) throws > IOException { > HttpContext localContext = new BasicHttpContext(); > if (getEndpoint().isAuthenticationPreemptive()) { > BasicScheme basicAuth = new BasicScheme(); > localContext.setAttribute("preemptive-auth", basicAuth); > } > if (httpContext != null) { > localContext = new BasicHttpContext(httpContext); > } > return httpClient.execute(httpRequest, localContext); > }{code} > In org.apache.camel.component.http4.HttpPollingConsumer#doReceive > getEndpoint().isAuthenticationPreemptive() returns the correct value, so > there is nothing against implementing this feature here as well. > The problem behind the scene ist that the context attribute "preemptive-auth" > is missing. -- This message was sent by Atlassian JIRA (v7.6.3#76005)