Updated Branches:
  refs/heads/master 3ef60bc88 -> d04c14c90

TAP5-336: The Cookies service interface could be simplified using a builder 
pattern
TAP5-1394: Adding method to Cookies api that lets you set path, domain, and 
maxAge at once.
TAP5-1937: removeCookieValue with custom path


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

Branch: refs/heads/master
Commit: d04c14c90b59d223e7591749f079a2637042a553
Parents: 3ef60bc
Author: Ulrich Staerk <u...@apache.org>
Authored: Wed Jan 9 10:18:04 2013 +0100
Committer: Ulrich Staerk <u...@apache.org>
Committed: Wed Jan 9 10:18:04 2013 +0100

----------------------------------------------------------------------
 .../tapestry5/internal/services/CookiesImpl.java   |   77 ++++++++-------
 .../org/apache/tapestry5/services/Cookies.java     |   37 +++++++-
 .../internal/services/CookiesImplTest.java         |   12 +++
 .../internal/services/NoOpCookieSource.java        |   17 +++
 4 files changed, 107 insertions(+), 36 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/d04c14c9/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/CookiesImpl.java
----------------------------------------------------------------------
diff --git 
a/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/CookiesImpl.java
 
b/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/CookiesImpl.java
index ea31c45..78a3487 100644
--- 
a/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/CookiesImpl.java
+++ 
b/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/CookiesImpl.java
@@ -14,6 +14,9 @@
 
 package org.apache.tapestry5.internal.services;
 
+import javax.servlet.http.Cookie;
+
+import org.apache.tapestry5.CookieBuilder;
 import org.apache.tapestry5.SymbolConstants;
 import org.apache.tapestry5.ioc.annotations.IntermediateType;
 import org.apache.tapestry5.ioc.annotations.Symbol;
@@ -21,8 +24,6 @@ import org.apache.tapestry5.ioc.util.TimeInterval;
 import org.apache.tapestry5.services.Cookies;
 import org.apache.tapestry5.services.Request;
 
-import javax.servlet.http.Cookie;
-
 /**
  * Implementation of the {@link org.apache.tapestry5.services.Cookies} service 
interface.
  */
@@ -81,63 +82,69 @@ public class CookiesImpl implements Cookies
 
     public void writeCookieValue(String name, String value)
     {
-        writeCookieValue(name, value, defaultMaxAge);
+        getBuilder(name, value).write();
     }
 
     public void writeCookieValue(String name, String value, int maxAge)
     {
-        Cookie cookie = new Cookie(name, value);
-        cookie.setPath(defaultCookiePath);
-        cookie.setMaxAge(maxAge);
-        cookie.setSecure(request.isSecure());
-
-        cookieSink.addCookie(cookie);
+        getBuilder(name, value).setMaxAge(maxAge).write();
     }
 
     public void writeCookieValue(String name, String value, String path)
     {
-        Cookie cookie = new Cookie(name, value);
-        cookie.setPath(path);
-        cookie.setMaxAge(defaultMaxAge);
-        cookie.setSecure(request.isSecure());
-
-        cookieSink.addCookie(cookie);
+        getBuilder(name, value).setPath(path).write();
     }
 
     public void writeDomainCookieValue(String name, String value, String 
domain)
     {
-        writeDomainCookieValue(name, value, domain, defaultMaxAge);
+        getBuilder(name, value).setDomain(domain).write();
     }
 
     public void writeDomainCookieValue(String name, String value, String 
domain, int maxAge)
     {
-        Cookie cookie = new Cookie(name, value);
-        cookie.setPath(defaultCookiePath);
-        cookie.setDomain(domain);
-        cookie.setMaxAge(maxAge);
-        cookie.setSecure(request.isSecure());
-
-        cookieSink.addCookie(cookie);
+        getBuilder(name, value).setDomain(domain).setMaxAge(maxAge).write();
     }
 
     public void writeCookieValue(String name, String value, String path, 
String domain)
     {
-        Cookie cookie = new Cookie(name, value);
-        cookie.setPath(path);
-        cookie.setDomain(domain);
-        cookie.setMaxAge(defaultMaxAge);
-        cookie.setSecure(request.isSecure());
-
-        cookieSink.addCookie(cookie);
+        getBuilder(name, value).setPath(path).setDomain(domain).write();
     }
 
     public void removeCookieValue(String name)
     {
-        Cookie cookie = new Cookie(name, null);
-        cookie.setPath(defaultCookiePath);
-        cookie.setMaxAge(0);
-        cookie.setSecure(request.isSecure());
+        getBuilder(name, null).delete();
+    }
 
-        cookieSink.addCookie(cookie);
+    public CookieBuilder getBuilder(String name, String value)
+    {
+        CookieBuilder builder = new CookieBuilder(name, value)
+        {
+            @Override
+            public void write()
+            {
+                Cookie cookie = new Cookie(name, value);
+                
+                cookie.setPath(path == null ? defaultCookiePath : path);
+                
+                if(domain != null)
+                    cookie.setDomain(domain);
+                
+                cookie.setMaxAge(maxAge == null ? defaultMaxAge : maxAge);
+                
+                cookie.setSecure(secure == null ? request.isSecure() : secure);
+
+                cookieSink.addCookie(cookie);
+            }
+
+            @Override
+            public void delete()
+            {
+                setMaxAge(0);
+                
+                write();
+            }
+        };
+        
+        return builder;
     }
 }

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/d04c14c9/tapestry-core/src/main/java/org/apache/tapestry5/services/Cookies.java
----------------------------------------------------------------------
diff --git 
a/tapestry-core/src/main/java/org/apache/tapestry5/services/Cookies.java 
b/tapestry-core/src/main/java/org/apache/tapestry5/services/Cookies.java
index 4bcd653..a275ae9 100644
--- a/tapestry-core/src/main/java/org/apache/tapestry5/services/Cookies.java
+++ b/tapestry-core/src/main/java/org/apache/tapestry5/services/Cookies.java
@@ -14,6 +14,8 @@
 
 package org.apache.tapestry5.services;
 
+import org.apache.tapestry5.CookieBuilder;
+
 /**
  * Used by other services to obtain cookie values for the current request, or 
to write cookie values as part of the
  * request.  Note that when writing cookies, the cookie's secure flag will 
match {@link
@@ -32,6 +34,8 @@ public interface Cookies
      * Creates or updates a cookie value. The value is stored using a max age 
(in seconds) defined by the symbol
      * <code>org.apache.tapestry5.default-cookie-max-age</code>. The factory 
default for this value is the equivalent of
      * one week.
+     * 
+     * @deprecated Use the {@link CookieBuilder} API, obtained with {@link 
#getBuilder(String, String)}, instead.
      */
 
     void writeCookieValue(String name, String value);
@@ -42,32 +46,63 @@ public interface Cookies
      * @param name   the name of the cookie
      * @param value  the value to be stored in the cookie
      * @param maxAge the maximum age, in seconds, to store the cookie
+     * 
+     * @deprecated Use the {@link CookieBuilder} API, obtained with {@link 
#getBuilder(String, String)}, instead.
      */
 
     void writeCookieValue(String name, String value, int maxAge);
 
     /**
      * As with {@link #writeCookieValue(String, String)} but an explicit path 
may be set.
+     * 
+     * @deprecated Use the {@link CookieBuilder} API, obtained with {@link 
#getBuilder(String, String)}, instead.
      */
     void writeCookieValue(String name, String value, String path);
 
     /**
      * As with {@link #writeCookieValue(String, String)} but an explicit 
domain may be set.
+     * 
+     * @deprecated Use the {@link CookieBuilder} API, obtained with {@link 
#getBuilder(String, String)}, instead.
      */
     void writeDomainCookieValue(String name, String value, String domain);
 
     /**
      * As with {@link #writeCookieValue(String, String)} but an explicit 
domain and maximum age may be set.
+     * 
+     * @deprecated Use the {@link CookieBuilder} API, obtained with {@link 
#getBuilder(String, String)}, instead.
      */
     void writeDomainCookieValue(String name, String value, String domain, int 
maxAge);
 
     /**
      * As with {@link #writeCookieValue(String, String, String)} but an 
explicit domain and path may be set.
+     * 
+     * @deprecated Use the {@link CookieBuilder} API, obtained with {@link 
#getBuilder(String, String)}, instead.
      */
     void writeCookieValue(String name, String value, String path, String 
domain);
 
     /**
-     * Removes a previously written cookie, by writing a new cookie with a 
maxAge of 0.
+     * Removes a previously written cookie, by writing a new cookie with a 
maxAge of 0. Only deletes a cookie
+     * with the default path and no domain set. For deleting other cookies use 
{@link CookieBuilder#delete()}.
+     * An instance of the {@link CookieBuilder} API can be obtained with 
{@link #getBuilder(String, String)}.
      */
     void removeCookieValue(String name);
+    
+    /**
+     * Returns a {@link CookieBuilder} to build and write a {@link 
javax.servlet.http.Cookie}. The default
+     * implementation creates a cookie who's value is stored using a max age 
(in seconds) defined by
+     * the symbol <code>org.apache.tapestry5.default-cookie-max-age</code>. 
The factory default for
+     * this value is the equivalent of one week. The default path is the 
context path (see
+     * {@link Request#getContextPath()}) of the current Request, the default 
secure setting is to
+     * send the cookie over secure channels only, if the original request was 
secure (see
+     * {@link Request#isSecure()}
+     * 
+     * @param name
+     *            the name of the cookie
+     * @param value
+     *            the value of the cookie
+     * @return a {@link CookieBuilder} for setting additional cookie 
attributes and writing it out
+     * 
+     * @since 5.4
+     */
+    CookieBuilder getBuilder(String name, String value);
 }

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/d04c14c9/tapestry-core/src/test/java/org/apache/tapestry5/internal/services/CookiesImplTest.java
----------------------------------------------------------------------
diff --git 
a/tapestry-core/src/test/java/org/apache/tapestry5/internal/services/CookiesImplTest.java
 
b/tapestry-core/src/test/java/org/apache/tapestry5/internal/services/CookiesImplTest.java
index 906f0b2..90ad924 100644
--- 
a/tapestry-core/src/test/java/org/apache/tapestry5/internal/services/CookiesImplTest.java
+++ 
b/tapestry-core/src/test/java/org/apache/tapestry5/internal/services/CookiesImplTest.java
@@ -167,4 +167,16 @@ public class CookiesImplTest extends Assert
         assertEquals(cookies.size(), 1);
         assertEquals(cookies.get(0), expectedCookie);
     }
+    
+    public void test_remove_cookie_with_nondefault_path()
+    {
+        final List<Cookie> cookies = CollectionFactory.newList();
+        CookiesImpl cs = createCookiesFixture("/ctx", cookies);
+
+        cs.getBuilder("foo", null).setPath("/nondefault/").delete();
+        Cookie expectedCookie = new ComparableCookie("foo", null, 0);
+        expectedCookie.setPath("/nondefault/");
+        assertEquals(cookies.size(), 1);
+        assertEquals(cookies.get(0), expectedCookie);
+    }
 }

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/d04c14c9/tapestry-core/src/test/java/org/apache/tapestry5/internal/services/NoOpCookieSource.java
----------------------------------------------------------------------
diff --git 
a/tapestry-core/src/test/java/org/apache/tapestry5/internal/services/NoOpCookieSource.java
 
b/tapestry-core/src/test/java/org/apache/tapestry5/internal/services/NoOpCookieSource.java
index 86e2c13..fac1123 100644
--- 
a/tapestry-core/src/test/java/org/apache/tapestry5/internal/services/NoOpCookieSource.java
+++ 
b/tapestry-core/src/test/java/org/apache/tapestry5/internal/services/NoOpCookieSource.java
@@ -14,6 +14,7 @@
 
 package org.apache.tapestry5.internal.services;
 
+import org.apache.tapestry5.CookieBuilder;
 import org.apache.tapestry5.services.Cookies;
 
 public class NoOpCookieSource implements Cookies
@@ -60,4 +61,20 @@ public class NoOpCookieSource implements Cookies
 
     }
 
+    public CookieBuilder getBuilder(String name, String value)
+    {
+        return new CookieBuilder(name, value)
+        {
+            @Override
+            public void write()
+            {
+            }
+
+            @Override
+            public void delete()
+            {
+            }
+        };
+    }
+
 }

Reply via email to