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 org.apache.tapestry5.services.Request; 018 019/** 020 * A fluent API to create and write cookies. Used by the 021 * {@link org.apache.tapestry5.services.Cookies} service. 022 * 023 * @since 5.4 024 */ 025public abstract class CookieBuilder 026{ 027 028 protected final String name; 029 protected final String value; 030 031 protected String path; 032 protected String domain; 033 protected Integer maxAge; 034 protected Boolean secure; 035 036 protected Boolean httpOnly; 037 protected int version = 0; 038 protected String comment; 039 040 /** 041 * Initialize a new CookieBuilder 042 * 043 * @param name the name of the resulting cookie 044 * @param value the value of the resulting cookie 045 */ 046 protected CookieBuilder(String name, String value) 047 { 048 this.name = name; 049 this.value = value; 050 } 051 052 /** 053 * Set the path for the cookie to be created. Defaults to {@link Request#getContextPath()}. 054 * @param path the path for the cookie 055 * @return the modified {@link CookieBuilder} 056 */ 057 public CookieBuilder setPath(String path) 058 { 059 this.path = path; 060 return this; 061 } 062 063 /** 064 * Set the domain for the cookie to be created. Will not be set by default. 065 * @param domain the domain for the cookie 066 * @return the modified {@link CookieBuilder} 067 */ 068 public CookieBuilder setDomain(String domain) 069 { 070 this.domain = domain; 071 return this; 072 } 073 074 /** 075 * Set how long the cookie should live. A value of <code>0</code> deletes a cookie, a value of 076 * <code>-1</code> deletes a cookie upon closing the browser. The default is defined by 077 * the symbol <code>org.apache.tapestry5.default-cookie-max-age</code>. The factory default for 078 * this value is the equivalent of one week. 079 * 080 * @param maxAge 081 * the cookie's maximum age in seconds 082 * @return the modified {@link CookieBuilder} 083 */ 084 public CookieBuilder setMaxAge(int maxAge) 085 { 086 this.maxAge = maxAge; 087 return this; 088 } 089 090 /** 091 * Set the cookie's secure mode. Defaults to {@link Request#isSecure()}. 092 * 093 * @param secure whether to send the cookie over a secure channel only 094 * @return the modified {@link CookieBuilder} 095 */ 096 public CookieBuilder setSecure(boolean secure) 097 { 098 this.secure = secure; 099 return this; 100 } 101 102 /** 103 * Set the cookie's httpOnly mode. 104 * 105 * @param httpOnly prevents javascript access to this cookie 106 * @return the modified {@link CookieBuilder} 107 */ 108 public CookieBuilder setHttpOnly(boolean httpOnly) 109 { 110 this.httpOnly = httpOnly; 111 return this; 112 } 113 114 /** 115 * Version 0 complies with the original Netscape cookie specification. 116 * Version 1 complies with RFC 2109 (experimental) 117 * 118 * @param version number 119 * @return the modified {@link CookieBuilder} 120 */ 121 public CookieBuilder setVersion(int version) { 122 this.version = version; 123 return this; 124 } 125 126 /** 127 * Comments are not supported by version 0 (the default) cookies 128 * 129 * @param comment for cookie 130 * @return the modified {@link CookieBuilder} 131 */ 132 public CookieBuilder setComment(String comment) { 133 this.comment = comment; 134 return this; 135 } 136 137 138 /** 139 * Sets defaults and writes the cookie to the client. 140 */ 141 public abstract void write(); 142 143 /** 144 * Deletes the cookie. 145 */ 146 public abstract void delete(); 147 148}