You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@velocity.apache.org by nb...@apache.org on 2006/12/07 20:21:22 UTC

svn commit: r483618 - /velocity/tools/trunk/src/java/org/apache/velocity/tools/view/tools/CookieTool.java

Author: nbubna
Date: Thu Dec  7 11:21:21 2006
New Revision: 483618

URL: http://svn.apache.org/viewvc?view=rev&rev=483618
Log:
add create() methods to support more complex cookie creation

Modified:
    velocity/tools/trunk/src/java/org/apache/velocity/tools/view/tools/CookieTool.java

Modified: velocity/tools/trunk/src/java/org/apache/velocity/tools/view/tools/CookieTool.java
URL: http://svn.apache.org/viewvc/velocity/tools/trunk/src/java/org/apache/velocity/tools/view/tools/CookieTool.java?view=diff&rev=483618&r1=483617&r2=483618
==============================================================================
--- velocity/tools/trunk/src/java/org/apache/velocity/tools/view/tools/CookieTool.java (original)
+++ velocity/tools/trunk/src/java/org/apache/velocity/tools/view/tools/CookieTool.java Thu Dec  7 11:21:21 2006
@@ -117,7 +117,7 @@
      */
     public void add(String name, String value)
     {
-        response.addCookie(new Cookie(name, value));
+        response.addCookie(create(name, value));
     }
 
 
@@ -129,12 +129,67 @@
      * @param value the value to be set for this cookie
      * @param maxAge the expiry to be set for this cookie
      */
-    public void add(String name, String value, int maxAge)
+    public void add(String name, String value, Object maxAge)
     {
-        /* c is for cookie.  that's good enough for me. */
-        Cookie c = new Cookie(name, value);
-        c.setMaxAge(maxAge);
+        Cookie c = create(name, value, maxAge);
+        if (c == null)
+        {
+            /* TODO: something better? */
+            return;
+        }
         response.addCookie(c);
     }
 
+
+    /**
+     * 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
+     * <code>$response.addCookie($myCookie)</code>.
+     *
+     * @param name the name to give this cookie
+     * @param value the value to be set for this cookie
+     * @returns the new Cookie object
+     * @since VelocityTools 1.3
+     */
+    public Cookie create(String name, String value)
+    {
+        return new Cookie(name, value);
+    }
+
+
+    /**
+     * Convenience method to create a new Cookie
+     * and set an expiry time for it.
+     *
+     * @param name the name to give this cookie
+     * @param value the value to be set for this cookie
+     * @param maxAge the expiry to be set for this cookie
+     * @returns the new Cookie object
+     * @since VelocityTools 1.3
+     */
+    public Cookie create(String name, String value, Object maxAge)
+    {
+        int expiry;
+        if (maxAge instanceof Number)
+        {
+            expiry = ((Number)maxAge).intValue();
+        }
+        else
+        {
+            try
+            {
+                expiry = Integer.parseInt(String.valueOf(maxAge));
+            }
+            catch (NumberFormatException nfe)
+            {
+                return null;
+            }
+        }
+        
+        /* c is for cookie.  that's good enough for me. */
+        Cookie c = new Cookie(name, value);
+        c.setMaxAge(expiry);
+        return c;
+    }
 }