View Javadoc

1   package org.apache.turbine.util.parser;
2   
3   /*
4    * Copyright 2001-2005 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License")
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import javax.servlet.http.Cookie;
20  import javax.servlet.http.HttpServletRequest;
21  import javax.servlet.http.HttpServletResponse;
22  
23  import org.apache.commons.logging.Log;
24  import org.apache.commons.logging.LogFactory;
25  
26  import org.apache.turbine.util.RunData;
27  import org.apache.turbine.util.pool.Recyclable;
28  import org.apache.turbine.util.uri.DataURI;
29  import org.apache.turbine.util.uri.URI;
30  
31  /***
32   * CookieParser is used to get and set values of Cookies on the Client
33   * Browser.  You can use CookieParser to convert Cookie values to
34   * various types or to set Bean values with setParameters(). See the
35   * Servlet Spec for more information on Cookies.
36   * <p>
37   * Use set() or unset() to Create or Destroy Cookies.
38   * <p>
39   * NOTE: The name= portion of a name=value pair may be converted
40   * to lowercase or uppercase when the object is initialized and when
41   * new data is added.  This behaviour is determined by the url.case.folding
42   * property in TurbineResources.properties.  Adding a name/value pair may
43   * overwrite existing name=value pairs if the names match:
44   *
45   * <pre>
46   * CookieParser cp = data.getCookies();
47   * cp.add("ERROR",1);
48   * cp.add("eRrOr",2);
49   * int result = cp.getInt("ERROR");
50   * </pre>
51   *
52   * In the above example, result is 2.
53   *
54   * @author <a href="mailto:ilkka.priha@simsoft.fi">Ilkka Priha</a>
55   * @author <a href="mailto:leon@opticode.co.za">Leon Messerschmidt</a>
56   * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
57   * @version $Id: DefaultCookieParser.java 264148 2005-08-29 14:21:04Z henning $
58   */
59  public class DefaultCookieParser
60          extends BaseValueParser
61          implements CookieParser, Recyclable
62  {
63      /*** Logging */
64      private static Log log = LogFactory.getLog(DefaultCookieParser.class);
65  
66      /*** Internal Run Data object containing the parameters to parse */
67      private RunData data = null;
68  
69      /*** Just like Fulcrum, we actually use the Request and response objects */
70      private HttpServletRequest request;
71  
72      /*** Just like Fulcrum, we actually use the Request and response objects */
73      private HttpServletResponse response;
74  
75      /*** The cookie path. */
76      private URI cookiePath = null;
77  
78      /***
79       * Constructs a new CookieParser.
80       */
81      public DefaultCookieParser()
82      {
83          super();
84      }
85  
86      /***
87       * Disposes the parser.
88       */
89      public void dispose()
90      {
91          this.data = null;
92          this.cookiePath = null;
93          this.request = null;
94          this.response = null;
95          super.dispose();
96      }
97  
98      /***
99       * Gets the parsed RunData.
100      *
101      * @return the parsed RunData object or null.
102      * @deprecated Don't use the Run Data object. use getRequest().
103      */
104     public RunData getRunData()
105     {
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     {
116         return request;
117     }
118 
119     /***
120      * Sets the RunData to be parsed. This is a convenience method to
121      * set the request and response from the RunData object. It is
122      * equivalent to
123      *
124      * <pre>
125      *  setData(data.getRequest(), data.getResponse());
126      * </pre>
127      *
128      * All previous cookies will be cleared.
129      *
130      * @param data the RunData object.
131      */
132     public void setRunData(RunData data)
133     {
134         this.data = data;
135         setData(data.getRequest(), data.getResponse());
136     }
137 
138     /***
139      * Sets Request and Response to be parsed.
140      * <p>
141      * All previous cookies will be cleared.
142      *
143      * @param request The http request from the servlet
144      * @param response The http reponse from the servlet
145      */
146     public void setData (HttpServletRequest request,
147                          HttpServletResponse response)
148     {
149         clear();
150 
151         String enc = request.getCharacterEncoding();
152         setCharacterEncoding(enc != null ? enc : "US-ASCII");
153 
154         cookiePath = new DataURI(data);
155 
156         Cookie[] cookies = request.getCookies();
157 
158         int cookiesCount = (cookies != null) ? cookies.length : 0;
159 
160         log.debug ("Number of Cookies: " + cookiesCount);
161 
162         for (int i = 0; i < cookiesCount; i++)
163         {
164             String name = convert (cookies[i].getName());
165             String value = cookies[i].getValue();
166             log.debug("Adding " + name + "=" + value);
167             add(name, value);
168         }
169 
170         this.request = request;
171         this.response = response;
172     }
173 
174     /***
175      * Get the Path where cookies will be stored
176      *
177      * @return path for cookie storage
178      */
179     public URI getCookiePath()
180     {
181         return cookiePath;
182     }
183 
184     /***
185      * Set the path for cookie storage
186      *
187      * @param cookiePath path for cookie storage
188      */
189     public void setCookiePath(URI cookiePath)
190     {
191         this.cookiePath = cookiePath;
192     }
193 
194     /***
195      * Set a cookie that will be stored on the client for
196      * the duration of the session.
197      *
198      * @param name name of the cookie
199      * @param value value of the cookie
200      */
201     public void set(String name, String value)
202     {
203         set(name, value, AGE_SESSION);
204     }
205 
206     /***
207      * Set a persisten cookie on the client that will expire
208      * after a maximum age (given in seconds).
209      *
210      * @param name name of the cookie
211      * @param value value of the cookie
212      * @param seconds_age max age of the cookie in seconds
213      */
214     public void set(String name, String value, int seconds_age)
215     {
216         if (response == null)
217         {
218             throw new IllegalStateException("Servlet response not available");
219         }
220 
221         Cookie cookie = new Cookie(name, value);
222         cookie.setMaxAge(seconds_age);
223         cookie.setPath(cookiePath.getContextPath()+cookiePath.getScriptName());
224         response.addCookie (cookie);
225     }
226 
227     /***
228      * Remove a previously set cookie from the client machine.
229      *
230      * @param name name of the cookie
231      */
232     public void unset(String name)
233     {
234         set(name, " ", AGE_DELETE);
235     }
236 }