Repository: camel
Updated Branches:
  refs/heads/master b88416053 -> db157f495


CAMEL-11568: Allow to set a CookiePolicy to CookieHandler

Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/db157f49
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/db157f49
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/db157f49

Branch: refs/heads/master
Commit: db157f495f02c45d0a02017a367ab9e74096262f
Parents: b884160
Author: Stephan Siano <[email protected]>
Authored: Thu Jul 20 10:54:52 2017 +0200
Committer: Stephan Siano <[email protected]>
Committed: Thu Jul 20 11:10:02 2017 +0200

----------------------------------------------------------------------
 .../camel/http/common/cookie/CookieHandler.java | 11 ++-
 .../common/cookie/ExchangeCookieHandler.java    |  8 ++
 .../common/cookie/InstanceCookieHandler.java    | 10 +++
 .../http/common/cookie/CookieHandlerTest.java   | 95 ++++++++++++++++++++
 docs/user-manual/en/http-session.adoc           | 11 +++
 5 files changed, 134 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/db157f49/components/camel-http-common/src/main/java/org/apache/camel/http/common/cookie/CookieHandler.java
----------------------------------------------------------------------
diff --git 
a/components/camel-http-common/src/main/java/org/apache/camel/http/common/cookie/CookieHandler.java
 
b/components/camel-http-common/src/main/java/org/apache/camel/http/common/cookie/CookieHandler.java
index 1f8829d..485e074 100644
--- 
a/components/camel-http-common/src/main/java/org/apache/camel/http/common/cookie/CookieHandler.java
+++ 
b/components/camel-http-common/src/main/java/org/apache/camel/http/common/cookie/CookieHandler.java
@@ -17,6 +17,7 @@
 package org.apache.camel.http.common.cookie;
 
 import java.io.IOException;
+import java.net.CookiePolicy;
 import java.net.CookieStore;
 import java.net.URI;
 import java.util.List;
@@ -28,7 +29,8 @@ import org.apache.camel.Exchange;
  * The interface for cookie handling will allow components to handle cookies 
for
  * HTTP requests.
  * <p>
- * Note: The usual cookie policies apply, so cookies will only be handled for
+ * Note: The defined cookie policies apply. The default is
+ * CookiePolicy.ACCEPT_ORIGINAL_SERVER, so cookies will only be handled for
  * fully qualified host names in the URI (not local host names like "myhost" or
  * "localhost").
  */
@@ -64,4 +66,11 @@ public interface CookieHandler {
      * @return the CookieStore
      */
     CookieStore getCookieStore(Exchange exchange);
+
+    /**
+     * Define a CookiePolicy for cookies stored by this CookieHandler
+     * 
+     * @param cookiePolicy the CookiePolicy
+     */
+    void setCookiePolicy(CookiePolicy cookiePolicy);
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/db157f49/components/camel-http-common/src/main/java/org/apache/camel/http/common/cookie/ExchangeCookieHandler.java
----------------------------------------------------------------------
diff --git 
a/components/camel-http-common/src/main/java/org/apache/camel/http/common/cookie/ExchangeCookieHandler.java
 
b/components/camel-http-common/src/main/java/org/apache/camel/http/common/cookie/ExchangeCookieHandler.java
index a661ead..3109f51 100644
--- 
a/components/camel-http-common/src/main/java/org/apache/camel/http/common/cookie/ExchangeCookieHandler.java
+++ 
b/components/camel-http-common/src/main/java/org/apache/camel/http/common/cookie/ExchangeCookieHandler.java
@@ -17,6 +17,7 @@
 package org.apache.camel.http.common.cookie;
 
 import java.net.CookieManager;
+import java.net.CookiePolicy;
 
 import org.apache.camel.Exchange;
 
@@ -28,6 +29,7 @@ import org.apache.camel.Exchange;
  * limitation.
  */
 public class ExchangeCookieHandler extends BaseCookieHandler {
+    private CookiePolicy cookiePolicy = CookiePolicy.ACCEPT_ORIGINAL_SERVER;
 
     @Override
     protected CookieManager getCookieManager(Exchange exchange) {
@@ -36,8 +38,14 @@ public class ExchangeCookieHandler extends BaseCookieHandler 
{
             return (CookieManager)handlerObj;
         } else {
             CookieManager handler = new CookieManager();
+            handler.setCookiePolicy(cookiePolicy);
             exchange.setProperty(Exchange.COOKIE_HANDLER, handler);
             return handler;
         }
     }
+
+    @Override
+    public void setCookiePolicy(CookiePolicy cookiePolicy) {
+        this.cookiePolicy = cookiePolicy;
+    }
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/db157f49/components/camel-http-common/src/main/java/org/apache/camel/http/common/cookie/InstanceCookieHandler.java
----------------------------------------------------------------------
diff --git 
a/components/camel-http-common/src/main/java/org/apache/camel/http/common/cookie/InstanceCookieHandler.java
 
b/components/camel-http-common/src/main/java/org/apache/camel/http/common/cookie/InstanceCookieHandler.java
index 30795be..84e7db8 100644
--- 
a/components/camel-http-common/src/main/java/org/apache/camel/http/common/cookie/InstanceCookieHandler.java
+++ 
b/components/camel-http-common/src/main/java/org/apache/camel/http/common/cookie/InstanceCookieHandler.java
@@ -17,6 +17,7 @@
 package org.apache.camel.http.common.cookie;
 
 import java.net.CookieManager;
+import java.net.CookiePolicy;
 
 import org.apache.camel.Exchange;
 
@@ -31,9 +32,18 @@ public class InstanceCookieHandler extends BaseCookieHandler 
{
 
     @Override
     protected CookieManager getCookieManager(Exchange exchange) {
+        return getCookieManagerInternal();
+    }
+
+    private CookieManager getCookieManagerInternal() {
         if (cookieHandler == null) {
             cookieHandler = new CookieManager();
         }
         return cookieHandler;
     }
+
+    @Override
+    public void setCookiePolicy(CookiePolicy cookiePolicy) {
+        getCookieManagerInternal().setCookiePolicy(cookiePolicy);
+    }
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/db157f49/components/camel-http-common/src/test/java/org/apache/camel/http/common/cookie/CookieHandlerTest.java
----------------------------------------------------------------------
diff --git 
a/components/camel-http-common/src/test/java/org/apache/camel/http/common/cookie/CookieHandlerTest.java
 
b/components/camel-http-common/src/test/java/org/apache/camel/http/common/cookie/CookieHandlerTest.java
new file mode 100644
index 0000000..32532b3
--- /dev/null
+++ 
b/components/camel-http-common/src/test/java/org/apache/camel/http/common/cookie/CookieHandlerTest.java
@@ -0,0 +1,95 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.http.common.cookie;
+
+import java.io.IOException;
+import java.net.CookiePolicy;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.junit.runners.Parameterized.Parameters;
+
+@RunWith(Parameterized.class)
+public class CookieHandlerTest extends CamelTestSupport {
+    /*
+     * This test tries to set a cookie for domain .example.com from host
+     * www.example.com or www.sub.example.com According to RFC 2965 section
+     * 3.3.1 the latter cookie has to be rejected, however if we set the cookie
+     * policy to ACCEPT_ALL the cookie will be accepted again. If a cookie is
+     * set, the resulting Cookie header has two lines, one containing the
+     * version and one the (single) cookie.
+     */
+    @Parameters(name = "{index}: {4} policy for {2} returns {3} Cookie header 
lines")
+    public static Iterable<Object[]> data() {
+        return Arrays
+            .asList(new Object[][] {{new InstanceCookieHandler(), 
CookiePolicy.ACCEPT_ORIGINAL_SERVER, "http://www.example.com/acme/foo";, 2,
+                                     "InstanceCookieHandler with 
ACCEPT_ORIGINAL_SERVER"},
+                                    {new InstanceCookieHandler(), 
CookiePolicy.ACCEPT_ORIGINAL_SERVER, "http://www.sub.example.com/acme/foo";, 0,
+                                     "InstanceCookieHandler with 
ACCEPT_ORIGINAL_SERVER"},
+                                    {new InstanceCookieHandler(), 
CookiePolicy.ACCEPT_ALL, "http://www.sub.example.com/acme/foo";, 2, 
"InstanceCookieHandler with ACCEPT_ALL"},
+                                    {new ExchangeCookieHandler(), 
CookiePolicy.ACCEPT_ORIGINAL_SERVER, "http://www.example.com/acme/foo";, 2,
+                                     "ExchangeCookieHandler with 
ACCEPT_ORIGINAL_SERVER"},
+                                    {new ExchangeCookieHandler(), 
CookiePolicy.ACCEPT_ORIGINAL_SERVER, "http://www.sub.example.com/acme/foo";, 0,
+                                     "ExchangeCookieHandler with 
ACCEPT_ORIGINAL_SERVER"},
+                                    {new ExchangeCookieHandler(), 
CookiePolicy.ACCEPT_ALL, "http://www.sub.example.com/acme/foo";, 2, 
"ExchangeCookieHandler with ACCEPT_ALL"}});
+    }
+
+    private String uriStr;
+    private CookieHandler cookieHandler;
+    private CookiePolicy cookiePolicy;
+    private int expectedNumberOfCookieValues;
+
+    private Exchange exchange;
+
+    public CookieHandlerTest(CookieHandler cookieHandler, CookiePolicy 
cookiePolicy, String uri, int expectedNumberOfCookieValues, String description) 
{
+        this.cookieHandler = cookieHandler;
+        this.cookiePolicy = cookiePolicy;
+        this.uriStr = uri;
+        this.expectedNumberOfCookieValues = expectedNumberOfCookieValues;
+    }
+
+    @Before
+    public void setUp() throws Exception {
+        super.setUp();
+        exchange = createExchangeWithBody(null);
+    }
+
+    @Test
+    public void setReceiveAndTestCookie() throws IOException, 
URISyntaxException {
+        URI uri = new URI(uriStr);
+        cookieHandler.setCookiePolicy(cookiePolicy);
+        Map<String, List<String>> headerMap = new HashMap<String, 
List<String>>();
+        headerMap.put("Set-Cookie", 
Collections.singletonList("Customer=\"WILE_E_COYOTE\";Version=1;Path=\"/acme\";Domain=\".example.com\""));
+        cookieHandler.storeCookies(exchange, uri, headerMap);
+
+        Map<String, List<String>> cookieHeaders = 
cookieHandler.loadCookies(exchange, uri);
+        assertNotNull(cookieHeaders);
+        assertNotNull(cookieHeaders.get("Cookie"));
+        assertEquals(expectedNumberOfCookieValues, 
cookieHeaders.get("Cookie").size());
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/db157f49/docs/user-manual/en/http-session.adoc
----------------------------------------------------------------------
diff --git a/docs/user-manual/en/http-session.adoc 
b/docs/user-manual/en/http-session.adoc
index dd5b81d..659f9ca 100644
--- a/docs/user-manual/en/http-session.adoc
+++ b/docs/user-manual/en/http-session.adoc
@@ -117,6 +117,17 @@ not share a session.
   <bean id="exchangeCookieHandler" 
class="org.apache.camel.http.common.cookie.ExchangeCookieHandler"/>  
 -----------------------------------------------------------
 
+Both CookieHandler implementations support setting a CookiePolicy to control 
the policy for storing
+cookies. Default is CookiePolicy.ACCEPT_ORIGINAL_SERVER.
+
+Note: Some EIPs like multicast or splitter create multiple exchanges from a 
single one. If
+no `org.apache.camel.http.common.cookie.ExchangeCookieHandler` is used before 
this, each multicast
+or splitter branch will have its own cookie store. This will not be the case 
if the first invocation
+of one of the endpoints using the cookie handler is before the multicast, 
because in this case the
+cookie store will be attached to the original exchange, and the exchanges 
created by the multicast
+will copy the reference to this cookie store (so there is effectively a shared 
cookie store across branches.
+As a workaround, you can call a cookieHandler.getCookieStore() e.g. by setting 
this to some dummy header.
+
 [[HTTPSession-ComponentDevelopers]]
 Component Developers
 ^^^^^^^^^^^^^^^^^^^^

Reply via email to