    001// Copyright 2012-2013 The Apache Software Foundation
    002//
    003// Licensed under the Apache License, Version 2.0 (the "License");
    004// you may not use this file except in compliance with the License.
    005// You may obtain a copy of the License at
    006//
    007// http://www.apache.org/licenses/LICENSE-2.0
    008//
    009// Unless required by applicable law or agreed to in writing, software
    010// distributed under the License is distributed on an "AS IS" BASIS,
    011// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    012// See the License for the specific language governing permissions and
    013// limitations under the License.
    014
    015package org.apache.tapestry5;
    016
    017import java.time.Duration;
    018
    019import org.apache.tapestry5.http.services.Request;
    020
    021/**
    022 * A fluent API to create and write cookies. Used by the
    023 * {@link org.apache.tapestry5.services.Cookies} service.
    024 *
    025 * @since 5.4
    026 */
    027public abstract class CookieBuilder
    028{
    029
    030    protected final String name;
    031    protected final String value;
    032
    033    protected String path;
    034    protected String domain;
    035    protected Integer maxAge;
    036    protected Boolean secure;
    037
    038    protected Boolean httpOnly;
    039    protected int version = 0;
    040    protected String comment;
    041
    042    /**
    043     * Initialize a new CookieBuilder
    044     *
    045     * @param name  the name of the resulting cookie
    046     * @param value the value of the resulting cookie
    047     */
    048    protected CookieBuilder(String name, String value)
    049    {
    050        this.name = name;
    051        this.value = value;
    052    }
    053
    054    /**
    055     * Set the path for the cookie to be created. Defaults to {@link Request#getContextPath()}.
    056     * @param  path the path for the cookie
    057     * @return the modified {@link CookieBuilder}
    058     */
    059    public CookieBuilder setPath(String path)
    060    {
    061        this.path = path;
    062        return this;
    063    }
    064
    065    /**
    066     * Set the domain for the cookie to be created. Will not be set by default.
    067     * @param  domain the domain for the cookie
    068     * @return the modified {@link CookieBuilder}
    069     */
    070    public CookieBuilder setDomain(String domain)
    071    {
    072        this.domain = domain;
    073        return this;
    074    }
    075
    076    /**
    077     * Set how long the cookie should live. A value of <code>0</code> deletes a cookie, a value of
    078     * <code>-1</code> deletes a cookie upon closing the browser. The default is defined by
    079     * the symbol <code>org.apache.tapestry5.default-cookie-max-age</code>. The factory default for
    080     * this value is the equivalent of one week.
    081     *
    082     * @param maxAge
    083     *            the cookie's maximum age in seconds
    084     * @return the modified {@link CookieBuilder}
    085     */
    086    public CookieBuilder setMaxAge(int maxAge)
    087    {
    088        this.maxAge = maxAge;
    089        return this;
    090    }
    091
    092
    093    /**
    094     * Set how long the cookie should live. A value of <code>java.time.Duration.ZERO</code> deletes a cookie,
    095     * a negative value deletes a cookie upon closing the browser. The default is defined by
    096     * the symbol <code>org.apache.tapestry5.default-cookie-max-age</code>. The factory default for
    097     * this value is the equivalent of one week.
    098     *
    099     * @param maxAge
    100     *            the cookie's maximum age in seconds
    101     * @return the modified {@link CookieBuilder}
    102     *
    103     * @since 5.8.3
    104     */
    105    public CookieBuilder setMaxAge(Duration maxAge)
    106    {
    107        long maxAgeAsLong = maxAge.getSeconds();
    108        if (maxAgeAsLong < 0L)
    109        {
    110            this.maxAge = -1;
    111        }
    112        else if (maxAgeAsLong >= Integer.MAX_VALUE) {
    113            this.maxAge = Integer.MAX_VALUE;
    114        }
    115        else
    116        {
    117            this.maxAge = (int) maxAgeAsLong;
    118        }
    119
    120        return this;
    121    }
    122
    123    /**
    124     * Set the cookie's secure mode. Defaults to {@link Request#isSecure()}.
    125     *
    126     * @param secure whether to send the cookie over a secure channel only
    127     * @return the modified {@link CookieBuilder}
    128     */
    129    public CookieBuilder setSecure(boolean secure)
    130    {
    131        this.secure = secure;
    132        return this;
    133    }
    134
    135    /**
    136     * Set the cookie's httpOnly mode.
    137     *
    138     * @param httpOnly prevents javascript access to this cookie
    139     * @return the modified {@link CookieBuilder}
    140     */
    141    public CookieBuilder setHttpOnly(boolean httpOnly)
    142    {
    143        this.httpOnly = httpOnly;
    144        return this;
    145    }
    146
    147    /**
    148     * Version 0 complies with the original Netscape cookie specification.
    149     * Version 1 complies with RFC 2109 (experimental)
    150     *
    151     * @param version number
    152     * @return the modified {@link CookieBuilder}
    153     */
    154    public CookieBuilder setVersion(int version) {
    155        this.version = version;
    156        return this;
    157    }
    158
    159    /**
    160     * Comments are not supported by version 0 (the default) cookies
    161     *
    162     * @param comment for cookie
    163     * @return the modified {@link CookieBuilder}
    164     */
    165    public CookieBuilder setComment(String comment) {
    166        this.comment = comment;
    167        return this;
    168    }
    169
    170
    171    /**
    172     * Sets defaults and writes the cookie to the client.
    173     */
    174    public abstract void write();
    175
    176    /**
    177     * Deletes the cookie.
    178     */
    179    public abstract void delete();
    180
    181}