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

siano pushed a commit to branch camel-2.24.x
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/camel-2.24.x by this push:
     new 40c2265  CAMEL-13497: clientConfig parameter creates cookie store per 
endpoint
40c2265 is described below

commit 40c226597454e85dd59d6f8d45cef8feaa31e756
Author: Stephan Siano <stephan.si...@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      | 42 ++++++++++++++++++++--
 3 files changed, 58 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 0f2759d..64ad05e 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 2a487fb..3bc32b0 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
@@ -34,6 +34,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;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -243,6 +245,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 8ade2dc..d94db24 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.http.common.HttpMessage;
 import org.apache.camel.http.common.cookie.ExchangeCookieHandler;
 import org.apache.camel.http.common.cookie.InstanceCookieHandler;
 import org.apache.camel.impl.JndiRegistry;
+import org.asynchttpclient.AsyncHttpClientConfig;
+import org.asynchttpclient.DefaultAsyncHttpClientConfig;
 import org.junit.Test;
 
 public class AhcProducerSessionTest extends BaseAhcTest {
@@ -39,6 +41,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");
@@ -59,6 +77,16 @@ public class AhcProducerSessionTest extends BaseAhcTest {
         JndiRegistry jndiRegistry = super.createRegistry();
         jndiRegistry.bind("instanceCookieHandler", new 
InstanceCookieHandler());
         jndiRegistry.bind("exchangeCookieHandler", new 
ExchangeCookieHandler());
+        /*
+         * Adding an AHC client configuration usually implies also setting a
+         * cookie store per endpoint. This will interfere with the cookie
+         * handler. In this case we want a client configuration and a cookie
+         * handler, so we set the cookie store in the client configuration to
+         * null, to disable native cookie support in the AHC client.
+         */
+        AsyncHttpClientConfig noCookieConfig = (new 
DefaultAsyncHttpClientConfig.Builder()).setCookieStore(null).build();
+        jndiRegistry.bind("noCookieConfig", noCookieConfig);
+        jndiRegistry.bind("defaultConfig", (new 
DefaultAsyncHttpClientConfig.Builder()).build());
         return jndiRegistry;
     }
 
@@ -81,14 +109,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())

Reply via email to