1 package org.apache.turbine.util.parser;
2
3 /* ====================================================================
4 * The Apache Software License, Version 1.1
5 *
6 * Copyright (c) 2001 The Apache Software Foundation. All rights
7 * reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
19 * distribution.
20 *
21 * 3. The end-user documentation included with the redistribution,
22 * if any, must include the following acknowledgment:
23 * "This product includes software developed by the
24 * Apache Software Foundation (http://www.apache.org/)."
25 * Alternately, this acknowledgment may appear in the software itself,
26 * if and wherever such third-party acknowledgments normally appear.
27 *
28 * 4. The names "Apache" and "Apache Software Foundation" and
29 * "Apache Turbine" must not be used to endorse or promote products
30 * derived from this software without prior written permission. For
31 * written permission, please contact apache@apache.org.
32 *
33 * 5. Products derived from this software may not be called "Apache",
34 * "Apache Turbine", nor may "Apache" appear in their name, without
35 * prior written permission of the Apache Software Foundation.
36 *
37 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48 * SUCH DAMAGE.
49 * ====================================================================
50 *
51 * This software consists of voluntary contributions made by many
52 * individuals on behalf of the Apache Software Foundation. For more
53 * information on the Apache Software Foundation, please see
54 * <http://www.apache.org/>.
55 */
56
57 import javax.servlet.http.Cookie;
58 import org.apache.turbine.util.CookieParser;
59 import org.apache.turbine.util.DynamicURI;
60 import org.apache.turbine.util.Log;
61 import org.apache.turbine.util.RunData;
62 import org.apache.turbine.util.pool.Recyclable;
63
64 /***
65 * CookieParser is used to get and set values of Cookies on the Client
66 * Browser. You can use CookieParser to convert Cookie values to
67 * various types or to set Bean values with setParameters(). See the
68 * Servlet Spec for more information on Cookies.
69 * <p>
70 * Use set() or unset() to Create or Destroy Cookies.
71 * <p>
72 * NOTE: The name= portion of a name=value pair may be converted
73 * to lowercase or uppercase when the object is initialized and when
74 * new data is added. This behaviour is determined by the url.case.folding
75 * property in TurbineResources.properties. Adding a name/value pair may
76 * overwrite existing name=value pairs if the names match:
77 *
78 * <pre>
79 * CookieParser cp = data.getCookies();
80 * cp.add("ERROR",1);
81 * cp.add("eRrOr",2);
82 * int result = cp.getInt("ERROR");
83 * </pre>
84 *
85 * In the above example, result is 2.
86 *
87 * @author <a href="mailto:ilkka.priha@simsoft.fi">Ilkka Priha</a>
88 * @author <a href="mailto:leon@opticode.co.za">Leon Messerschmidt</a>
89 * @version $Id: DefaultCookieParser.java,v 1.3 2002/07/11 16:53:20 mpoeschl Exp $
90 */
91 public class DefaultCookieParser extends BaseValueParser
92 implements CookieParser, Recyclable
93 {
94 /***
95 * The run data to parse.
96 */
97 private RunData data = null;
98
99 /***
100 * The cookie path.
101 */
102 private DynamicURI cookiePath = null;
103
104 /***
105 * Constructs a new CookieParser.
106 */
107 public DefaultCookieParser()
108 {
109 super();
110 }
111
112 /***
113 * Disposes the parser.
114 */
115 public void dispose()
116 {
117 this.data = null;
118 this.cookiePath = null;
119 super.dispose();
120 }
121
122 /***
123 * Gets the parsed RunData.
124 *
125 * @return the parsed RunData object or null.
126 */
127 public RunData getRunData()
128 {
129 return this.data;
130 }
131
132 /***
133 * Sets the RunData to be parsed.
134 * All previous cookies will be cleared.
135 *
136 * @param data the RunData object.
137 */
138 public void setRunData (RunData data)
139 {
140 clear();
141
142 String enc = data.getRequest().getCharacterEncoding();
143 setCharacterEncoding(enc != null ? enc : "US-ASCII");
144
145 cookiePath = new DynamicURI(data);
146
147 cookiePath = new DynamicURI(data);
148
149 Cookie[] cookies = data.getRequest().getCookies();
150
151 Log.debug("Number of Cookies " + cookies.length);
152
153 for (int i = 0; i < cookies.length; i++)
154 {
155 String name = convert(cookies[i].getName());
156 String value = cookies[i].getValue();
157 Log.debug("Adding " + name + "=" + value);
158 add(name,value);
159 }
160
161 this.data = data;
162 }
163
164 /***
165 * Get the Path where cookies will be stored
166 */
167 public DynamicURI getCookiePath()
168 {
169 return cookiePath;
170 }
171
172 /***
173 * Set the path for cookie storage
174 */
175 public void setCookiePath(DynamicURI path)
176 {
177 cookiePath = path;
178 }
179
180 /***
181 * Set a cookie that will be stored on the client for
182 * the duration of the session.
183 */
184 public void set(String name, String value)
185 {
186 set(name,value,AGE_SESSION);
187 }
188
189 /***
190 * Set a persisten cookie on the client that will expire
191 * after a maximum age (given in seconds).
192 */
193 public void set(String name, String value, int seconds_age)
194 {
195 if (data == null)
196 {
197 throw new IllegalStateException("RunData not available");
198 }
199
200 Cookie cookie = new Cookie(name, value);
201 cookie.setMaxAge(seconds_age);
202 cookie.setPath(cookiePath.getScriptName());
203 data.getResponse().addCookie(cookie);
204 }
205
206 /***
207 * Remove a previously set cookie from the client machine.
208 */
209 public void unset(String name)
210 {
211 set(name," ",AGE_DELETE);
212 }
213 }
This page was automatically generated by Maven