You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by si...@apache.org on 2019/05/10 10:44:45 UTC

[camel] branch master updated: CAMEL-13497: clientConfig parameter creates cookie store per endpoint

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

siano 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 7fa0aa8  CAMEL-13497: clientConfig parameter creates cookie store per endpoint
7fa0aa8 is described below

commit 7fa0aa893a5d9692247005b8cf0aa0674ec8ad17
Author: Stephan Siano <st...@sap.com>
AuthorDate: Fri May 10 08:57:36 2019 +0200

    CAMEL-13497: clientConfig parameter creates cookie store per endpoint
---
 .../camel-ahc/src/main/docs/ahc-component.adoc     |  2 +-
 .../apache/camel/component/ahc/AhcComponent.java   | 17 ++++++++++
 .../component/ahc/AhcProducerSessionTest.java      | 38 ++++++++++++++++++++--
 3 files changed, 54 insertions(+), 3 deletions(-)

diff --git a/components/camel-ahc/src/main/docs/ahc-component.adoc b/components/camel-ahc/src/main/docs/ahc-component.adoc
index 8a69a1f..2dd06ee 100644
--- a/components/camel-ahc/src/main/docs/ahc-component.adoc
+++ b/components/camel-ahc/src/main/docs/ahc-component.adoc
@@ -72,7 +72,7 @@ with the following path and query parameters:
 | *bridgeEndpoint* (producer) | If the option is true, then the Exchange.HTTP_URI header is ignored, and use the endpoint's URI for request. You may also set the throwExceptionOnFailure to be false to let the AhcProducer send all the fault response back. | false | boolean
 | *bufferSize* (producer) | The initial in-memory buffer size used when transferring data between Camel and AHC Client. | 4096 | int
 | *connectionClose* (producer) | Define if the Connection Close header has to be added to HTTP Request. This parameter is false by default | false | boolean
-| *cookieHandler* (producer) | Configure a cookie handler to maintain a HTTP session |  | CookieHandler
+| *cookieHandler* (producer) | Configure a cookie handler to maintain a HTTP session. If this parameter is defined together with the clientConfig parameter, make sure that the latter does not contain a cookie handler. |  | CookieHandler
 | *headerFilterStrategy* (producer) | To use a custom HeaderFilterStrategy to filter header to and from Camel message. |  | HeaderFilterStrategy
 | *throwExceptionOnFailure* (producer) | Option to disable throwing the AhcOperationFailedException in case of failed responses from the remote server. This allows you to get all responses regardless of the HTTP status code. | true | boolean
 | *transferException* (producer) | If enabled and an Exchange failed processing on the consumer side, and if the caused Exception was send back serialized in the response as a application/x-java-serialized-object content type (for example using Jetty or Servlet Camel components). On the producer side the exception will be deserialized and thrown as is, instead of the AhcOperationFailedException. The caused exception is required to be serialized. This is by default turned off. If you enab [...]
diff --git a/components/camel-ahc/src/main/java/org/apache/camel/component/ahc/AhcComponent.java b/components/camel-ahc/src/main/java/org/apache/camel/component/ahc/AhcComponent.java
index 2bc1553..137856b 100644
--- a/components/camel-ahc/src/main/java/org/apache/camel/component/ahc/AhcComponent.java
+++ b/components/camel-ahc/src/main/java/org/apache/camel/component/ahc/AhcComponent.java
@@ -35,6 +35,8 @@ import org.asynchttpclient.AsyncHttpClientConfig;
 import org.asynchttpclient.DefaultAsyncHttpClientConfig;
 import org.asynchttpclient.Realm;
 import org.asynchttpclient.Realm.Builder;
+import org.asynchttpclient.cookie.CookieStore;
+import org.asynchttpclient.cookie.ThreadSafeCookieStore;
 
 /**
  *  To call external HTTP services using <a href="http://github.com/sonatype/async-http-client">Async Http Client</a>
@@ -240,6 +242,21 @@ public class AhcComponent extends HeaderFilterStrategyComponent implements SSLCo
      */
     static DefaultAsyncHttpClientConfig.Builder cloneConfig(AsyncHttpClientConfig clientConfig) {
         DefaultAsyncHttpClientConfig.Builder builder = new DefaultAsyncHttpClientConfig.Builder(clientConfig);
+        /*
+         * The builder creates a new ThreadSafeCookieStore and does not copy the
+         * one from clientConfig. This might be desired in case no explicit
+         * cookie store was set on the builder that built the clientConfig,
+         * because otherwise all endpoints sharing a configuration will also
+         * share the cookie store. On the other hand if someone explicitly
+         * configured a cookie store (or no cookie store) on the provided
+         * config, he likely intends to use it, so we create either a new
+         * default implementation or we keep the non default one (or the null
+         * value).
+         */
+        CookieStore cookieStore = clientConfig.getCookieStore();
+        if (!(cookieStore instanceof ThreadSafeCookieStore)) {
+            builder.setCookieStore(cookieStore);
+        }
         return builder;
     }
 }
diff --git a/components/camel-ahc/src/test/java/org/apache/camel/component/ahc/AhcProducerSessionTest.java b/components/camel-ahc/src/test/java/org/apache/camel/component/ahc/AhcProducerSessionTest.java
index 4b39cfa..8e95f9a 100644
--- a/components/camel-ahc/src/test/java/org/apache/camel/component/ahc/AhcProducerSessionTest.java
+++ b/components/camel-ahc/src/test/java/org/apache/camel/component/ahc/AhcProducerSessionTest.java
@@ -26,6 +26,8 @@ import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.http.common.HttpMessage;
 import org.apache.camel.http.common.cookie.ExchangeCookieHandler;
 import org.apache.camel.http.common.cookie.InstanceCookieHandler;
+import org.asynchttpclient.AsyncHttpClientConfig;
+import org.asynchttpclient.DefaultAsyncHttpClientConfig;
 import org.junit.Test;
 
 public class AhcProducerSessionTest extends BaseAhcTest {
@@ -36,6 +38,12 @@ public class AhcProducerSessionTest extends BaseAhcTest {
     @BindToRegistry("exchangeCookieHandler")
     ExchangeCookieHandler exchangeCookieHandler = new ExchangeCookieHandler();
 
+    @BindToRegistry("noCookieConfig")
+    AsyncHttpClientConfig noCookieConfig = (new DefaultAsyncHttpClientConfig.Builder()).setCookieStore(null).build();
+
+    @BindToRegistry("defaultConfig")
+    AsyncHttpClientConfig defaultConfig = (new DefaultAsyncHttpClientConfig.Builder()).build());
+
     @Test
     public void testProducerNoSession() throws Exception {
         getMockEndpoint("mock:result").expectedBodiesReceived("New New World", "New New World");
@@ -45,6 +53,22 @@ public class AhcProducerSessionTest extends BaseAhcTest {
     }
 
     @Test
+    public void testProducerNoSessionWithConfig() throws Exception {
+        getMockEndpoint("mock:result").expectedBodiesReceived("New New World", "New New World");
+        template.sendBody("direct:config", "World");
+        template.sendBody("direct:config", "World");
+        assertMockEndpointsSatisfied();
+    }
+
+    @Test
+    public void testProducerSessionFromAhcClient() throws Exception {
+        getMockEndpoint("mock:result").expectedBodiesReceived("Old New World", "Old Old World");
+        template.sendBody("direct:defaultconfig", "World");
+        template.sendBody("direct:defaultconfig", "World");
+        assertMockEndpointsSatisfied();
+    }
+
+    @Test
     public void testProducerInstanceSession() throws Exception {
         getMockEndpoint("mock:result").expectedBodiesReceived("Old New World", "Old Old World");
         template.sendBody("direct:instance", "World");
@@ -79,14 +103,24 @@ public class AhcProducerSessionTest extends BaseAhcTest {
                     .to("ahc:" + getTestServerEndpointSessionUrl())
                     .to("mock:result");
 
+                from("direct:config")
+                    .to("ahc:" + getTestServerEndpointSessionUrl() + "?clientConfig=#noCookieConfig")
+                    .to("ahc:" + getTestServerEndpointSessionUrl() + "?clientConfig=#noCookieConfig")
+                    .to("mock:result");
+
+                from("direct:defaultconfig")
+                    .to("ahc:" + getTestServerEndpointSessionUrl() + "?clientConfig=#defaultConfig")
+                    .to("ahc:" + getTestServerEndpointSessionUrl() + "?clientConfig=#defaultConfig")
+                    .to("mock:result");
+
                 from("direct:instance")
                     .to("ahc:" + getTestServerEndpointSessionUrl() + "?cookieHandler=#instanceCookieHandler")
                     .to("ahc:" + getTestServerEndpointSessionUrl() + "?cookieHandler=#instanceCookieHandler")
                     .to("mock:result");
 
                 from("direct:exchange")
-                    .to("ahc:" + getTestServerEndpointSessionUrl() + "?cookieHandler=#exchangeCookieHandler")
-                    .to("ahc:" + getTestServerEndpointSessionUrl() + "?cookieHandler=#exchangeCookieHandler")
+                    .to("ahc:" + getTestServerEndpointSessionUrl() + "?clientConfig=#noCookieConfig&cookieHandler=#exchangeCookieHandler")
+                    .to("ahc:" + getTestServerEndpointSessionUrl() + "?clientConfig=#noCookieConfig&cookieHandler=#exchangeCookieHandler")
                     .to("mock:result");
 
                 from(getTestServerEndpointSessionUri())