KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > util > CookieGenerator


1 /*
2  * Copyright 2002-2006 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.web.util;
18
19 import javax.servlet.http.Cookie JavaDoc;
20 import javax.servlet.http.HttpServletResponse JavaDoc;
21
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24
25 /**
26  * Helper class for cookie generation, carrying cookie descriptor settings
27  * as bean properties and being able to add and remove cookie to/from a
28  * given response.
29  *
30  * <p>Can serve as base class for components that generate specific cookies,
31  * like CookieLocaleResolcer and CookieThemeResolver.
32  *
33  * @author Juergen Hoeller
34  * @since 1.1.4
35  * @see #addCookie
36  * @see #removeCookie
37  * @see org.springframework.web.servlet.i18n.CookieLocaleResolver
38  * @see org.springframework.web.servlet.theme.CookieThemeResolver
39  */

40 public class CookieGenerator {
41
42     /**
43      * Default path that cookies will be visible to: "/", i.e. the entire server.
44      */

45     public static final String JavaDoc DEFAULT_COOKIE_PATH = "/";
46
47     /**
48      * Default maximum age of cookies: maximum integer value, i.e. forever.
49      */

50     public static final int DEFAULT_COOKIE_MAX_AGE = Integer.MAX_VALUE;
51
52
53     protected final Log logger = LogFactory.getLog(getClass());
54
55     private String JavaDoc cookieName;
56
57     private String JavaDoc cookieDomain;
58
59     private String JavaDoc cookiePath = DEFAULT_COOKIE_PATH;
60
61     private int cookieMaxAge = DEFAULT_COOKIE_MAX_AGE;
62
63     private boolean cookieSecure = false;
64
65
66     /**
67      * Use the given name for cookies created by this generator.
68      */

69     public void setCookieName(String JavaDoc cookieName) {
70         this.cookieName = cookieName;
71     }
72
73     /**
74      * Return the given name for cookies created by this generator.
75      */

76     public String JavaDoc getCookieName() {
77         return cookieName;
78     }
79
80     /**
81      * Use the given domain for cookies created by this generator.
82      * The cookie is only visible to servers in this domain.
83      */

84     public void setCookieDomain(String JavaDoc cookieDomain) {
85         this.cookieDomain = cookieDomain;
86     }
87
88     /**
89      * Return the domain for cookies created by this generator, if any.
90      */

91     public String JavaDoc getCookieDomain() {
92         return cookieDomain;
93     }
94
95     /**
96      * Use the given path for cookies created by this generator.
97      * The cookie is only visible to URLs in this path and below.
98      */

99     public void setCookiePath(String JavaDoc cookiePath) {
100         this.cookiePath = cookiePath;
101     }
102
103     /**
104      * Return the path for cookies created by this generator.
105      */

106     public String JavaDoc getCookiePath() {
107         return cookiePath;
108     }
109
110     /**
111      * Use the given maximum age (in seconds) for cookies created by this generator.
112      * Useful special value: -1 ... not persistent, deleted when client shuts down
113      */

114     public void setCookieMaxAge(int cookieMaxAge) {
115         this.cookieMaxAge = cookieMaxAge;
116     }
117
118     /**
119      * Return the maximum age for cookies created by this generator.
120      */

121     public int getCookieMaxAge() {
122         return cookieMaxAge;
123     }
124
125     /**
126      * Set whether the cookie should only be sent using a secure protocol,
127      * such as HTTPS (SSL). This is an indication to the receiving browser,
128      * not processed by the HTTP server itself. Default is "false".
129      */

130     public void setCookieSecure(boolean cookieSecure) {
131         this.cookieSecure = cookieSecure;
132     }
133
134     /**
135      * Return whether the cookie should only be sent using a secure protocol,
136      * such as HTTPS (SSL).
137      */

138     public boolean isCookieSecure() {
139         return cookieSecure;
140     }
141
142
143     /**
144      * Add a cookie with the given value to the response,
145      * using the cookie descriptor settings of this generator.
146      * <p>Delegates to <code>createCookie</code> for cookie creation.
147      * @param response the HTTP response to add the cookie to
148      * @param cookieValue the value of the cookie to add
149      * @see #setCookieName
150      * @see #setCookieDomain
151      * @see #setCookiePath
152      * @see #setCookieMaxAge
153      * @see #createCookie
154      */

155     public void addCookie(HttpServletResponse JavaDoc response, String JavaDoc cookieValue) {
156         Cookie JavaDoc cookie = createCookie(cookieValue);
157         cookie.setMaxAge(getCookieMaxAge());
158         if (isCookieSecure()) {
159             cookie.setSecure(true);
160         }
161         response.addCookie(cookie);
162         if (logger.isDebugEnabled()) {
163             logger.debug("Added cookie with name [" + getCookieName() + "] and value [" + cookieValue + "]");
164         }
165     }
166
167     /**
168      * Remove the cookie that this generator describes from the response.
169      * Will generate a cookie with empty value and max age 0.
170      * <p>Delegates to <code>createCookie</code> for cookie creation.
171      * @param response the HTTP response to remove the cookie from
172      * @see #setCookieName
173      * @see #setCookieDomain
174      * @see #setCookiePath
175      * @see #createCookie
176      */

177     public void removeCookie(HttpServletResponse JavaDoc response) {
178         Cookie JavaDoc cookie = createCookie("");
179         cookie.setMaxAge(0);
180         response.addCookie(cookie);
181         if (logger.isDebugEnabled()) {
182             logger.debug("Removed cookie with name [" + getCookieName() + "]");
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 JavaDoc createCookie(String JavaDoc cookieValue) {
196         Cookie JavaDoc cookie = new Cookie JavaDoc(getCookieName(), cookieValue);
197         if (getCookieDomain() != null) {
198             cookie.setDomain(getCookieDomain());
199         }
200         cookie.setPath(getCookiePath());
201         return cookie;
202     }
203
204 }
205
Popular Tags