Repository: tapestry-5 Updated Branches: refs/heads/master 95413c0c9 -> f161aa89b
TAP-2327: add support for httpOnly cookies and setting cookies' version/comment Project: http://git-wip-us.apache.org/repos/asf/tapestry-5/repo Commit: http://git-wip-us.apache.org/repos/asf/tapestry-5/commit/f161aa89 Tree: http://git-wip-us.apache.org/repos/asf/tapestry-5/tree/f161aa89 Diff: http://git-wip-us.apache.org/repos/asf/tapestry-5/diff/f161aa89 Branch: refs/heads/master Commit: f161aa89b64fc4f8f28aaabcb59ade036dc0fcd0 Parents: 95413c0 Author: Barry Books <[email protected]> Authored: Fri Feb 26 10:30:58 2016 -0600 Committer: Jochen Kemnade <[email protected]> Committed: Thu Jul 14 07:46:04 2016 +0200 ---------------------------------------------------------------------- .../org/apache/tapestry5/CookieBuilder.java | 62 ++++++++++++++++---- .../internal/services/CookiesImpl.java | 21 +++++-- 2 files changed, 66 insertions(+), 17 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/f161aa89/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 index 9068d6a..3f54318 100644 --- a/tapestry-core/src/main/java/org/apache/tapestry5/CookieBuilder.java +++ b/tapestry-core/src/main/java/org/apache/tapestry5/CookieBuilder.java @@ -19,23 +19,27 @@ import org.apache.tapestry5.services.Request; /** * A fluent API to create and write cookies. Used by the * {@link org.apache.tapestry5.services.Cookies} service. - * + * * @since 5.4 */ public abstract class CookieBuilder { - + protected final String name; protected final String value; - + protected String path; protected String domain; protected Integer maxAge; protected Boolean secure; - + + protected Boolean httpOnly; + protected int version = 0; + protected String comment; + /** * Initialize a new CookieBuilder - * + * * @param name the name of the resulting cookie * @param value the value of the resulting cookie */ @@ -72,7 +76,7 @@ public abstract class CookieBuilder * <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 CookieBuilder} @@ -82,10 +86,10 @@ public abstract class CookieBuilder 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 CookieBuilder} */ @@ -94,15 +98,51 @@ public abstract class CookieBuilder this.secure = secure; return this; } - + + /** + * Set the cookie's httpOnly mode. + * + * @param httpOnly prevents javascript access to this cookie + * @return the modified {@link CookieBuilder} + */ + public CookieBuilder setHttpOnly(boolean httpOnly) + { + this.httpOnly = httpOnly; + return this; + } + + /** + * Version 0 complies with the original Netscape cookie specification. + * Version 1 complies with RFC 2109 (experimental) + * + * @param version number + * @return the modified {@link CookieBuilder} + */ + public CookieBuilder setVersion(int version) { + this.version = version; + return this; + } + + /** + * Comments are not supported by version 0 (the default) cookies + * + * @param comment for cookie + * @return the modified {@link CookieBuilder} + */ + public CookieBuilder setComment(String comment) { + this.comment = comment; + return this; + } + + /** * Sets defaults and writes the cookie to the client. */ public abstract void write(); - + /** * Deletes the cookie. */ public abstract void delete(); - + } http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/f161aa89/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 78a3487..495d2c9 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 @@ -123,16 +123,25 @@ public class CookiesImpl implements Cookies 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); + cookie.setVersion(version); + + if (comment != null) + { + cookie.setComment(comment); + } + + cookie.setHttpOnly(httpOnly); + cookieSink.addCookie(cookie); } @@ -140,11 +149,11 @@ public class CookiesImpl implements Cookies public void delete() { setMaxAge(0); - + write(); } }; - + return builder; } }
