KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > velocity > tools > view > tools > CookieTool


1 /*
2  * Copyright 2003 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.velocity.tools.view.tools;
18
19 import javax.servlet.ServletContext JavaDoc;
20 import javax.servlet.http.HttpServletRequest JavaDoc;
21 import javax.servlet.http.HttpServletResponse JavaDoc;
22 import javax.servlet.http.Cookie JavaDoc;
23 import org.apache.velocity.tools.view.context.ViewContext;
24
25 /**
26  * <p>View tool for convenient cookie access and creation.</p>
27  * <p><pre>
28  * Template example(s):
29  * $cookie.foo.value
30  * $cookie.add("bar",'woogie')
31  *
32  * Toolbox configuration:
33  * &lt;tool&gt;
34  * &lt;key&gt;cookie&lt;/key&gt;
35  * &lt;scope&gt;request&lt;/scope&gt;
36  * &lt;class&gt;org.apache.velocity.tools.view.tools.CookieTool&lt;/class&gt;
37  * &lt;/tool&gt;
38  * </pre></p>
39  *
40  * <p>This class is only designed for use as a request-scope tool.</p>
41  *
42  * @author <a HREF="mailto:dim@colebatch.com">Dmitri Colebatch</a>
43  * @author <a HREF="mailto:nathan@esha.com">Nathan Bubna</a>
44  * @since VelocityTools 1.1
45  * @version $Id: CookieTool.java,v 1.3 2004/02/18 20:06:11 nbubna Exp $
46  */

47 public class CookieTool implements ViewTool
48 {
49
50     protected HttpServletRequest JavaDoc request;
51     protected HttpServletResponse JavaDoc response;
52
53
54     /**
55      * Initializes this instance for the current request.
56      *
57      * @param obj the ViewContext of the current request
58      */

59     public void init(Object JavaDoc obj)
60     {
61         ViewContext context = (ViewContext)obj;
62         this.request = context.getRequest();
63         this.response = context.getResponse();
64     }
65
66
67     /**
68      * Expose array of Cookies for this request to the template.
69      *
70      * <p>This is equivalent to <code>$request.cookies</code>.</p>
71      *
72      * @return array of Cookie objects for this request
73      */

74     public Cookie JavaDoc[] getAll()
75     {
76         return request.getCookies();
77     }
78
79
80     /**
81      * Returns the Cookie with the specified name, if it exists.
82      *
83      * <p>So, if you had a cookie named 'foo', you'd get it's value
84      * by <code>$cookies.foo.value</code> or it's max age
85      * by <code>$cookies.foo.maxAge</code></p>
86      */

87     public Cookie JavaDoc get(String JavaDoc name)
88     {
89         Cookie JavaDoc[] all = getAll();
90         if (all == null)
91         {
92             return null;
93         }
94
95         for (int i = 0; i < all.length; i++)
96         {
97             Cookie JavaDoc cookie = all[i];
98             if (cookie.getName().equals(name))
99             {
100                 return cookie;
101             }
102         }
103         return null;
104     }
105
106
107     /**
108      * Adds a new Cookie with the specified name and value
109      * to the HttpServletResponse. This does *not* add a Cookie
110      * to the current request.
111      *
112      * @param name the name to give this cookie
113      * @param value the value to be set for this cookie
114      */

115     public void add(String JavaDoc name, String JavaDoc value)
116     {
117         response.addCookie(new Cookie JavaDoc(name, value));
118     }
119
120
121     /**
122      * Convenience method to add a new Cookie to the response
123      * and set an expiry time for it.
124      *
125      * @param name the name to give this cookie
126      * @param value the value to be set for this cookie
127      * @param maxAge the expiry to be set for this cookie
128      */

129     public void add(String JavaDoc name, String JavaDoc value, int maxAge)
130     {
131         /* c is for cookie. that's good enough for me. */
132         Cookie JavaDoc c = new Cookie JavaDoc(name, value);
133         c.setMaxAge(maxAge);
134         response.addCookie(c);
135     }
136
137 }
138
Popular Tags