tapestry-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Ulrich Stärk <...@apache.org>
Subject Re: [PATCH] First shot at reworking the Cookies interface.
Date Mon, 19 Nov 2012 10:15:40 GMT
After struggling for days with my Git infrastructure (it sucks on Windows), I was finally able
to
produce and mail the following patch for reworking the Cookies interface. Please review and
comment.
Tests will follow.

Cheers,

Uli

On 19.11.2012 11:13, Ulrich Stärk wrote:
> From: Ulrich Staerk <uli@apache.org>
> 
> ---
>  .../java/org/apache/tapestry5/CookieBuilder.java   | 87 ++++++++++++++++++++++
>  .../tapestry5/internal/services/CookiesImpl.java   | 69 +++++++++--------
>  .../org/apache/tapestry5/services/Cookies.java     | 35 ++++++++-
>  3 files changed, 155 insertions(+), 36 deletions(-)
>  create mode 100644 tapestry-core/src/main/java/org/apache/tapestry5/CookieBuilder.java
> 
> diff --git a/tapestry-core/src/main/java/org/apache/tapestry5/CookieBuilder.java b/tapestry-core/src/main/java/org/apache/tapestry5/CookieBuilder.java
> new file mode 100644
> index 0000000..fb04c51
> --- /dev/null
> +++ b/tapestry-core/src/main/java/org/apache/tapestry5/CookieBuilder.java
> @@ -0,0 +1,87 @@
> +package org.apache.tapestry5;
> +
> +import org.apache.tapestry5.services.Request;
> +
> +/**
> + * A fluent API to create and write cookies. Used by the
> + * {@link org.apache.tapestry5.services.Cookies} service.
> + */
> +public abstract class CookieBuilder
> +{
> +    
> +    protected final String name;
> +    protected final String value;
> +    
> +    protected String path;
> +    protected String domain;
> +    protected Integer maxAge;
> +    protected Boolean secure;
> +    
> +    /**
> +     * Initialize a new CookieBuilder
> +     * 
> +     * @param name  the name of the resulting cookie
> +     * @param value the value of the resulting cookie
> +     */
> +    protected CookieBuilder(String name, String value)
> +    {
> +        this.name = name;
> +        this.value = value;
> +    }
> +
> +    /**
> +     * Set the path for the cookie to be created. Defaults to {@link Request#getContextPath()}.
> +     * @param  path the path for the cookie
> +     * @return the modified {@link ICookieBuilder}
> +     */
> +    public CookieBuilder setPath(String path)
> +    {
> +        this.path = path;
> +        return this;
> +    }
> +
> +    /**
> +     * Set the domain for the cookie to be created. Will not be set by default.
> +     * @param  domain the domain for the cookie
> +     * @return the modified {@link ICookieBuilder}
> +     */
> +    public CookieBuilder setDomain(String domain)
> +    {
> +        this.domain = domain;
> +        return this;
> +    }
> +
> +    /**
> +     * Set how long the cookie should live. A value of <code>0</code> deletes
a cookie, a value of
> +     * <code>-1</code> deletes a cookie upon closing the browser. The default
is 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.
> +     * 
> +     * @param maxAge
> +     *            the cookie's maximum age in seconds
> +     * @return the modified {@link ICookieBuilder}
> +     */
> +    public CookieBuilder setMaxAge(int maxAge)
> +    {
> +        this.maxAge = maxAge;
> +        return this;
> +    }
> +    
> +    /**
> +     * Set the cookie's secure mode. Defaults to {@link Request#isSecure()}.
> +     * 
> +     * @param secure whether to send the cookie over a secure channel only
> +     * @return the modified {@link ICookieBuilder}
> +     */
> +    public CookieBuilder setSecure(boolean secure)
> +    {
> +        this.secure = secure;
> +        return this;
> +    }
> +    
> +    /**
> +     * Sets defaults and writes the cookie to the client.
> +     */
> +    public abstract void write();
> +    
> +}
> 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 3c88006..7fad97a 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,14 +14,15 @@
>  
>  package org.apache.tapestry5.internal.services;
>  
> +import javax.servlet.http.Cookie;
> +
> +import org.apache.tapestry5.CookieBuilder;
>  import org.apache.tapestry5.ioc.annotations.IntermediateType;
>  import org.apache.tapestry5.ioc.annotations.Symbol;
>  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.
>   */
> @@ -72,63 +73,61 @@ 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(request.getContextPath() + "/");
> -        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(request.getContextPath() + "/");
> -        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(request.getContextPath() + "/");
> -        cookie.setMaxAge(0);
> -        cookie.setSecure(request.isSecure());
> +        getBuilder(name, null).setMaxAge(0).write();
> +    }
>  
> -        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 ? request.getContextPath() + "/" : path);
> +                
> +                if(domain != null)
> +                    cookie.setDomain(domain);
> +                
> +                cookie.setMaxAge(maxAge == null ? defaultMaxAge : maxAge);
> +                
> +                cookie.setSecure(secure == null ? request.isSecure() : secure);
> +
> +                cookieSink.addCookie(cookie);
> +            }
> +        };
> +        
> +        return builder;
>      }
>  }
> 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..875e63b 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
> @@ -1,4 +1,4 @@
> -// Copyright 2007, 2009 The Apache Software Foundation
> +// Copyright 2007, 2009, 2012 The Apache Software Foundation
>  //
>  // Licensed under the Apache License, Version 2.0 (the "License");
>  // you may not use this file except in compliance with the License.
> @@ -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,27 +46,37 @@ 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);
>  
> @@ -70,4 +84,23 @@ public interface Cookies
>       * Removes a previously written cookie, by writing a new cookie with a maxAge of
0.
>       */
>      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);
>  }
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tapestry.apache.org
For additional commands, e-mail: dev-help@tapestry.apache.org


Mime
View raw message