001: /*
002: * Copyright 2002-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.web.util;
018:
019: import javax.servlet.http.Cookie;
020: import javax.servlet.http.HttpServletResponse;
021:
022: import org.apache.commons.logging.Log;
023: import org.apache.commons.logging.LogFactory;
024:
025: /**
026: * Helper class for cookie generation, carrying cookie descriptor settings
027: * as bean properties and being able to add and remove cookie to/from a
028: * given response.
029: *
030: * <p>Can serve as base class for components that generate specific cookies,
031: * like CookieLocaleResolcer and CookieThemeResolver.
032: *
033: * @author Juergen Hoeller
034: * @since 1.1.4
035: * @see #addCookie
036: * @see #removeCookie
037: * @see org.springframework.web.servlet.i18n.CookieLocaleResolver
038: * @see org.springframework.web.servlet.theme.CookieThemeResolver
039: */
040: public class CookieGenerator {
041:
042: /**
043: * Default path that cookies will be visible to: "/", i.e. the entire server.
044: */
045: public static final String DEFAULT_COOKIE_PATH = "/";
046:
047: /**
048: * Default maximum age of cookies: maximum integer value, i.e. forever.
049: */
050: public static final int DEFAULT_COOKIE_MAX_AGE = Integer.MAX_VALUE;
051:
052: protected final Log logger = LogFactory.getLog(getClass());
053:
054: private String cookieName;
055:
056: private String cookieDomain;
057:
058: private String cookiePath = DEFAULT_COOKIE_PATH;
059:
060: private int cookieMaxAge = DEFAULT_COOKIE_MAX_AGE;
061:
062: private boolean cookieSecure = false;
063:
064: /**
065: * Use the given name for cookies created by this generator.
066: */
067: public void setCookieName(String cookieName) {
068: this .cookieName = cookieName;
069: }
070:
071: /**
072: * Return the given name for cookies created by this generator.
073: */
074: public String getCookieName() {
075: return cookieName;
076: }
077:
078: /**
079: * Use the given domain for cookies created by this generator.
080: * The cookie is only visible to servers in this domain.
081: */
082: public void setCookieDomain(String cookieDomain) {
083: this .cookieDomain = cookieDomain;
084: }
085:
086: /**
087: * Return the domain for cookies created by this generator, if any.
088: */
089: public String getCookieDomain() {
090: return cookieDomain;
091: }
092:
093: /**
094: * Use the given path for cookies created by this generator.
095: * The cookie is only visible to URLs in this path and below.
096: */
097: public void setCookiePath(String cookiePath) {
098: this .cookiePath = cookiePath;
099: }
100:
101: /**
102: * Return the path for cookies created by this generator.
103: */
104: public String getCookiePath() {
105: return cookiePath;
106: }
107:
108: /**
109: * Use the given maximum age (in seconds) for cookies created by this generator.
110: * Useful special value: -1 ... not persistent, deleted when client shuts down
111: */
112: public void setCookieMaxAge(int cookieMaxAge) {
113: this .cookieMaxAge = cookieMaxAge;
114: }
115:
116: /**
117: * Return the maximum age for cookies created by this generator.
118: */
119: public int getCookieMaxAge() {
120: return cookieMaxAge;
121: }
122:
123: /**
124: * Set whether the cookie should only be sent using a secure protocol,
125: * such as HTTPS (SSL). This is an indication to the receiving browser,
126: * not processed by the HTTP server itself. Default is "false".
127: */
128: public void setCookieSecure(boolean cookieSecure) {
129: this .cookieSecure = cookieSecure;
130: }
131:
132: /**
133: * Return whether the cookie should only be sent using a secure protocol,
134: * such as HTTPS (SSL).
135: */
136: public boolean isCookieSecure() {
137: return cookieSecure;
138: }
139:
140: /**
141: * Add a cookie with the given value to the response,
142: * using the cookie descriptor settings of this generator.
143: * <p>Delegates to <code>createCookie</code> for cookie creation.
144: * @param response the HTTP response to add the cookie to
145: * @param cookieValue the value of the cookie to add
146: * @see #setCookieName
147: * @see #setCookieDomain
148: * @see #setCookiePath
149: * @see #setCookieMaxAge
150: * @see #createCookie
151: */
152: public void addCookie(HttpServletResponse response,
153: String cookieValue) {
154: Cookie cookie = createCookie(cookieValue);
155: cookie.setMaxAge(getCookieMaxAge());
156: if (isCookieSecure()) {
157: cookie.setSecure(true);
158: }
159: response.addCookie(cookie);
160: if (logger.isDebugEnabled()) {
161: logger.debug("Added cookie with name [" + getCookieName()
162: + "] and value [" + cookieValue + "]");
163: }
164: }
165:
166: /**
167: * Remove the cookie that this generator describes from the response.
168: * Will generate a cookie with empty value and max age 0.
169: * <p>Delegates to <code>createCookie</code> for cookie creation.
170: * @param response the HTTP response to remove the cookie from
171: * @see #setCookieName
172: * @see #setCookieDomain
173: * @see #setCookiePath
174: * @see #createCookie
175: */
176: public void removeCookie(HttpServletResponse response) {
177: Cookie cookie = createCookie("");
178: cookie.setMaxAge(0);
179: response.addCookie(cookie);
180: if (logger.isDebugEnabled()) {
181: logger.debug("Removed cookie with name [" + getCookieName()
182: + "]");
183: }
184: }
185:
186: /**
187: * Create a cookie with the given value, using the cookie descriptor
188: * settings of this generator (except for "cookieMaxAge").
189: * @param cookieValue the value of the cookie to crate
190: * @return the cookie
191: * @see #setCookieName
192: * @see #setCookieDomain
193: * @see #setCookiePath
194: */
195: protected Cookie createCookie(String cookieValue) {
196: Cookie cookie = new Cookie(getCookieName(), cookieValue);
197: if (getCookieDomain() != null) {
198: cookie.setDomain(getCookieDomain());
199: }
200: cookie.setPath(getCookiePath());
201: return cookie;
202: }
203:
204: }
|