Coverage report

  %line %branch
org.apache.turbine.util.parser.DefaultCookieParser
4% 
85% 

 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  21
 public class DefaultCookieParser
 60  
         extends BaseValueParser
 61  
         implements CookieParser, Recyclable
 62  
 {
 63  
     /** Logging */
 64  63
     private static Log log = LogFactory.getLog(DefaultCookieParser.class);
 65  
 
 66  
     /** Internal Run Data object containing the parameters to parse */
 67  0
     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  0
     private URI cookiePath = null;
 77  
 
 78  
     /**
 79  
      * Constructs a new CookieParser.
 80  
      */
 81  
     public DefaultCookieParser()
 82  
     {
 83  0
         super();
 84  0
     }
 85  
 
 86  
     /**
 87  
      * Disposes the parser.
 88  
      */
 89  
     public void dispose()
 90  
     {
 91  0
         this.data = null;
 92  0
         this.cookiePath = null;
 93  0
         this.request = null;
 94  0
         this.response = null;
 95  0
         super.dispose();
 96  0
     }
 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  0
         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  0
         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  0
         this.data = data;
 135  0
         setData(data.getRequest(), data.getResponse());
 136  0
     }
 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  0
         clear();
 150  
 
 151  0
         String enc = request.getCharacterEncoding();
 152  0
         setCharacterEncoding(enc != null ? enc : "US-ASCII");
 153  
 
 154  0
         cookiePath = new DataURI(data);
 155  
 
 156  0
         Cookie[] cookies = request.getCookies();
 157  
 
 158  0
         int cookiesCount = (cookies != null) ? cookies.length : 0;
 159  
 
 160  0
         log.debug ("Number of Cookies: " + cookiesCount);
 161  
 
 162  0
         for (int i = 0; i < cookiesCount; i++)
 163  
         {
 164  0
             String name = convert (cookies[i].getName());
 165  0
             String value = cookies[i].getValue();
 166  0
             log.debug("Adding " + name + "=" + value);
 167  0
             add(name, value);
 168  
         }
 169  
 
 170  0
         this.request = request;
 171  0
         this.response = response;
 172  0
     }
 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  0
         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  0
         this.cookiePath = cookiePath;
 192  0
     }
 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  0
         set(name, value, AGE_SESSION);
 204  0
     }
 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  0
         if (response == null)
 217  
         {
 218  0
             throw new IllegalStateException("Servlet response not available");
 219  
         }
 220  
 
 221  0
         Cookie cookie = new Cookie(name, value);
 222  0
         cookie.setMaxAge(seconds_age);
 223  0
         cookie.setPath(cookiePath.getContextPath()+cookiePath.getScriptName());
 224  0
         response.addCookie (cookie);
 225  0
     }
 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  0
         set(name, " ", AGE_DELETE);
 235  0
     }
 236  
 }

This report is generated by jcoverage, Maven and Maven JCoverage Plugin.