View Javadoc

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