Source Code Cross Referenced for CookieGenerator.java in  » J2EE » spring-framework-2.5 » org » springframework » web » util » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » J2EE » spring framework 2.5 » org.springframework.web.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.