You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ac...@apache.org on 2018/07/23 13:59:22 UTC

[camel] branch master updated: CAMEL-12642:Fix for http4 feature authenticationPreemptive in pollEnrich

This is an automated email from the ASF dual-hosted git repository.

acosentino pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/master by this push:
     new e22bdec  CAMEL-12642:Fix for http4 feature authenticationPreemptive in pollEnrich
e22bdec is described below

commit e22bdec1a5afe73edda706e7f116ff2a9cb33635
Author: Ramu <kk...@redhat.com>
AuthorDate: Thu Jul 19 20:39:48 2018 +0530

    CAMEL-12642:Fix for http4 feature authenticationPreemptive in pollEnrich
---
 .../camel/component/http4/HttpPollingConsumer.java | 34 ++++++++++++++++++++--
 .../component/http4/HttpPollingConsumerTest.java   | 18 ++++++++++++
 2 files changed, 50 insertions(+), 2 deletions(-)

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 0020094..2b372c0 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.config.RequestConfig;
 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 @@ import org.apache.http.util.EntityUtils;
 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 @@ public class HttpPollingConsumer extends PollingConsumerSupport implements Servi
         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 @@ public class HttpPollingConsumer extends PollingConsumerSupport implements Servi
             }
         }
     }
+    
+    /**
+     * 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 6cda297..d257810 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 @@ import org.junit.Test;
 public class HttpPollingConsumerTest extends BaseHttpTest {
 
     private HttpServer localServer;
+    private String user = "camel";
+    private String password = "password";
     
     @Before
     @Override
@@ -60,6 +62,22 @@ public class HttpPollingConsumerTest extends BaseHttpTest {
     }
     
     @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);
         assertEquals(getExpectedContent(), body);