Class CookieTool

java.lang.Object
org.apache.velocity.tools.view.CookieTool
Direct Known Subclasses:
CookieTool

@DefaultKey("cookies") @ValidScope("request") public class CookieTool extends Object

View tool for convenient cookie access and creation.

 Template example(s):
  $cookie.foo
  $cookie.add("bar",'woogie')

 tools.xml configuration:
 <tools>
   <toolbox scope="request">
     <tool class="org.apache.velocity.tools.view.CookieTool"/>
   </toolbox>
 </tools>
 

This class is only designed for use as a request-scope tool.

Since:
VelocityTools 2.0
Version:
$Id: CookieTool.java 740389 2009-02-03 19:38:36Z nbubna $
Author:
Dmitri Colebatch, Nathan Bubna
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static class 
    Extends Cookie to add some fluid API sugar and a toString() method that renders the Cookie's value instead of the usual Object.toString() shenanigans.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private List<Cookie>
     
    protected org.apache.velocity.runtime.log.Log
     
    protected HttpServletRequest
     
    protected HttpServletResponse
     
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    add(Cookie c)
    Adds the specified Cookie to the HttpServletResponse.
    add(String name, String value)
    Adds a new Cookie with the specified name and value to the HttpServletResponse.
    add(String name, String value, Object maxAge)
    Convenience method to add a new Cookie to the response and set an expiry time for it.
    Cookie
    create(String name, String value)
    Creates a new Cookie with the specified name and value.
    Cookie
    create(String name, String value, Object maxAge)
    Convenience method to create a new Cookie and set an expiry time for it.
    delete(String name)
    Retrieves the specified cookie and sets the Max-Age to 0 to tell the browser to delete the cookie.
    Cookie
    get(String name)
    Returns the Cookie with the specified name, if it exists.
    List<Cookie>
    Expose array of Cookies for this request to the template.
    void
    setLog(org.apache.velocity.runtime.log.Log log)
    Sets the Log used for logging messages when Cookie creation fails due to an invalid name.
    void
    setRequest(HttpServletRequest request)
    Sets the current HttpServletRequest.
    void
    setResponse(HttpServletResponse response)
    Sets the current HttpServletResponse.
     

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Field Details

    • request

      protected HttpServletRequest request
    • response

      protected HttpServletResponse response
    • log

      protected org.apache.velocity.runtime.log.Log log
    • jar

      private List<Cookie> jar
  • Constructor Details

    • CookieTool

      public CookieTool()
  • Method Details

    • setRequest

      public void setRequest(HttpServletRequest request)
      Sets the current HttpServletRequest. This is required for this tool to operate and will throw a NullPointerException if this is not set or is set to null.
    • setResponse

      public void setResponse(HttpServletResponse response)
      Sets the current HttpServletResponse. This is required for this tool to operate and will throw a NullPointerException if this is not set or is set to null.
    • setLog

      public void setLog(org.apache.velocity.runtime.log.Log log)
      Sets the Log used for logging messages when Cookie creation fails due to an invalid name.
    • getAll

      public List<Cookie> getAll()
      Expose array of Cookies for this request to the template.

      This is equivalent to $request.cookies.

      Returns:
      list of Cookie objects for this request
    • get

      public Cookie get(String name)
      Returns the Cookie with the specified name, if it exists.

      So, if you had a cookie named 'foo', you'd get it's value by $cookies.foo.value or it's max age by $cookies.foo.maxAge

    • add

      public String add(Cookie c)
      Adds the specified Cookie to the HttpServletResponse. This does *not* add the Cookie to the current request.
      Parameters:
      c - is for cookie (that's good enough for me)
      Returns:
      an empty String to prevent the reference from rendering unless the cookie passed in is null, then it returns null
      Since:
      VelocityTools 2.0
    • add

      public String add(String name, String value)
      Adds a new Cookie with the specified name and value to the HttpServletResponse. This does *not* add a Cookie to the current request.
      Parameters:
      name - the name to give this cookie
      value - the value to be set for this cookie
      Returns:
      an empty String to prevent the reference from rendering
    • add

      public String add(String name, String value, Object maxAge)
      Convenience method to add a new Cookie to the response and set an expiry time for it.
      Parameters:
      name - the name to give this cookie
      value - the value to be set for this cookie
      maxAge - the expiry to be set for this cookie
      Returns:
      an empty String to prevent the reference from rendering
    • create

      public Cookie create(String name, String value)
      Creates a new Cookie with the specified name and value. This does *not* add the Cookie to the response, so the created Cookie will not be set unless you do $cookies.add($myCookie).
      Parameters:
      name - the name to give this cookie
      value - the value to be set for this cookie
      Returns:
      The new SugarCookie object.
      Since:
      VelocityTools 1.3
    • create

      public Cookie create(String name, String value, Object maxAge)
      Convenience method to create a new Cookie and set an expiry time for it.
      Parameters:
      name - the name to give this cookie
      value - the value to be set for this cookie
      maxAge - the expiry to be set for this cookie
      Returns:
      The new SugarCookie object.
      Since:
      VelocityTools 1.3
    • delete

      public String delete(String name)
      Retrieves the specified cookie and sets the Max-Age to 0 to tell the browser to delete the cookie. Then this returns an empty string to make this render silently. If no such cookie exists, then it returns null to show the error.
      Parameters:
      name - the name of the cookie to be eaten
      Returns:
      empty string, or null if no such cookie exists
      See Also:
    • toString

      public String toString()
      Overrides:
      toString in class Object