001: package org.apache.turbine.util.parser;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: import javax.servlet.http.Cookie;
023: import javax.servlet.http.HttpServletRequest;
024: import javax.servlet.http.HttpServletResponse;
025:
026: import org.apache.commons.logging.Log;
027: import org.apache.commons.logging.LogFactory;
028:
029: import org.apache.turbine.util.RunData;
030: import org.apache.turbine.util.ServerData;
031: import org.apache.turbine.util.pool.Recyclable;
032: import org.apache.turbine.util.uri.DataURI;
033: import org.apache.turbine.util.uri.URI;
034:
035: /**
036: * CookieParser is used to get and set values of Cookies on the Client
037: * Browser. You can use CookieParser to convert Cookie values to
038: * various types or to set Bean values with setParameters(). See the
039: * Servlet Spec for more information on Cookies.
040: * <p>
041: * Use set() or unset() to Create or Destroy Cookies.
042: * <p>
043: * NOTE: The name= portion of a name=value pair may be converted
044: * to lowercase or uppercase when the object is initialized and when
045: * new data is added. This behaviour is determined by the url.case.folding
046: * property in TurbineResources.properties. Adding a name/value pair may
047: * overwrite existing name=value pairs if the names match:
048: *
049: * <pre>
050: * CookieParser cp = data.getCookies();
051: * cp.add("ERROR",1);
052: * cp.add("eRrOr",2);
053: * int result = cp.getInt("ERROR");
054: * </pre>
055: *
056: * In the above example, result is 2.
057: *
058: * @author <a href="mailto:ilkka.priha@simsoft.fi">Ilkka Priha</a>
059: * @author <a href="mailto:leon@opticode.co.za">Leon Messerschmidt</a>
060: * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
061: * @version $Id: DefaultCookieParser.java 534527 2007-05-02 16:10:59Z tv $
062: */
063: public class DefaultCookieParser extends BaseValueParser implements
064: CookieParser, Recyclable {
065: /** Logging */
066: private static Log log = LogFactory
067: .getLog(DefaultCookieParser.class);
068:
069: /** Internal Run Data object containing the parameters to parse */
070: private RunData data = null;
071:
072: /** Just like Fulcrum, we actually use the Request and response objects */
073: private HttpServletRequest request;
074:
075: /** Just like Fulcrum, we actually use the Request and response objects */
076: private HttpServletResponse response;
077:
078: /** The cookie path. */
079: private URI cookiePath = null;
080:
081: /**
082: * Constructs a new CookieParser.
083: */
084: public DefaultCookieParser() {
085: super ();
086: }
087:
088: /**
089: * Disposes the parser.
090: */
091: public void dispose() {
092: this .data = null;
093: this .cookiePath = null;
094: this .request = null;
095: this .response = null;
096: super .dispose();
097: }
098:
099: /**
100: * Gets the parsed RunData.
101: *
102: * @return the parsed RunData object or null.
103: * @deprecated Don't use the Run Data object. use getRequest().
104: */
105: public RunData getRunData() {
106: return data;
107: }
108:
109: /**
110: * Gets the Request Object for this parser.
111: *
112: * @return the HttpServletRequest or null.
113: */
114: public HttpServletRequest getRequest() {
115: return request;
116: }
117:
118: /**
119: * Sets the RunData to be parsed. This is a convenience method to
120: * set the request and response from the RunData object. It is
121: * equivalent to
122: *
123: * <pre>
124: * setData(data.getRequest(), data.getResponse());
125: * </pre>
126: *
127: * All previous cookies will be cleared.
128: *
129: * @param data the RunData object.
130: */
131: public void setRunData(RunData data) {
132: this .data = data;
133: setData(data.getRequest(), data.getResponse());
134: }
135:
136: /**
137: * Sets Request and Response to be parsed.
138: * <p>
139: * All previous cookies will be cleared.
140: *
141: * @param request The http request from the servlet
142: * @param response The http reponse from the servlet
143: */
144: public void setData(HttpServletRequest request,
145: HttpServletResponse response) {
146: clear();
147:
148: String enc = request.getCharacterEncoding();
149: setCharacterEncoding(enc != null ? enc : "US-ASCII");
150:
151: cookiePath = new DataURI(new ServerData(request));
152:
153: Cookie[] cookies = request.getCookies();
154:
155: int cookiesCount = (cookies != null) ? cookies.length : 0;
156:
157: log.debug("Number of Cookies: " + cookiesCount);
158:
159: for (int i = 0; i < cookiesCount; i++) {
160: String name = convert(cookies[i].getName());
161: String value = cookies[i].getValue();
162: log.debug("Adding " + name + "=" + value);
163: add(name, value);
164: }
165:
166: this .request = request;
167: this .response = response;
168: }
169:
170: /**
171: * Get the Path where cookies will be stored
172: *
173: * @return path for cookie storage
174: */
175: public URI getCookiePath() {
176: return cookiePath;
177: }
178:
179: /**
180: * Set the path for cookie storage
181: *
182: * @param cookiePath path for cookie storage
183: */
184: public void setCookiePath(URI cookiePath) {
185: this .cookiePath = cookiePath;
186: }
187:
188: /**
189: * Set a cookie that will be stored on the client for
190: * the duration of the session.
191: *
192: * @param name name of the cookie
193: * @param value value of the cookie
194: */
195: public void set(String name, String value) {
196: set(name, value, AGE_SESSION);
197: }
198:
199: /**
200: * Set a persisten cookie on the client that will expire
201: * after a maximum age (given in seconds).
202: *
203: * @param name name of the cookie
204: * @param value value of the cookie
205: * @param seconds_age max age of the cookie in seconds
206: */
207: public void set(String name, String value, int seconds_age) {
208: if (response == null) {
209: throw new IllegalStateException(
210: "Servlet response not available");
211: }
212:
213: Cookie cookie = new Cookie(name, value);
214: cookie.setMaxAge(seconds_age);
215: cookie.setPath(cookiePath.getContextPath()
216: + cookiePath.getScriptName());
217: response.addCookie(cookie);
218: }
219:
220: /**
221: * Remove a previously set cookie from the client machine.
222: *
223: * @param name name of the cookie
224: */
225: public void unset(String name) {
226: set(name, " ", AGE_DELETE);
227: }
228: }
|