KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > request > CookiesTag


1 /*
2  * Copyright 1999,2004 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.taglibs.request;
18
19 import javax.servlet.*;
20 import javax.servlet.http.*;
21 import javax.servlet.jsp.*;
22 import javax.servlet.jsp.tagext.*;
23
24 /**
25  * JSP Tag <b>cookies</b>, used to get HttpServletRequest cookie information
26  * using the standard JSP &lt;jsp:getProperty&gt; tag.
27  * <p>
28  * The script variable of name <b>id</b> is availble only within the
29  * body of the <b>cookies</b> tag.
30  * <p>
31  * Loops through all the cookies received by the HttpServletRequest.
32  * <p>
33  * If the optional attribute <b>name</b> is present only the cookie of
34  * that name is retreived.
35  * <p>
36  * JSP Tag Lib Descriptor
37  * <p><pre>
38  * &lt;name&gt;cookies&lt;/name&gt;
39  * &lt;tagclass&gt;org.apache.taglibs.request.CookiesTag&lt;/tagclass&gt;
40  * &lt;teiclass&gt;org.apache.taglibs.request.CookiesTEI&lt;/teiclass&gt;
41  * &lt;bodycontent&gt;JSP&lt;/bodycontent&gt;
42  * &lt;info&gt;Loop through all cookies or get a single cookie.&lt;/info&gt;
43  * &lt;attribute&gt;
44  * &lt;name&gt;id&lt;/name&gt;
45  * &lt;required&gt;true&lt;/required&gt;
46  * &lt;rtexprvalue&gt;false&lt;/rtexprvalue&gt;
47  * &lt;/attribute&gt;
48  * &lt;attribute&gt;
49  * &lt;name&gt;name&lt;/name&gt;
50  * &lt;required&gt;false&lt;/required&gt;
51  * &lt;rtexprvalue&gt;false&lt;/rtexprvalue&gt;
52  * &lt;/attribute&gt;
53  * </pre>
54  *
55  * @author Glenn Nielsen
56  */

57
58 public class CookiesTag extends BodyTagSupport
59 {
60     // All the cookies received with request
61
private Cookie [] cookies = null;
62     // Current cookie
63
private Cookie cookie = null;
64     // Number of cookies
65
private int cookie_num = 0;
66     // Optional attribute, name of single cookie to get
67
private String JavaDoc name = null;
68
69     /**
70      * Gets the cookies that came with the request or cookie with <b>name</b>.
71      *
72      * @return SKIP_BODY if no cookies or cookie with <b>name</b> is not found, EVAL_BODY_TAG if cookie exists
73      */

74     public final int doStartTag() throws JspException
75     {
76     // Get the cookies
77
cookies = ((HttpServletRequest)pageContext.getRequest()).getCookies();
78     if( cookies == null || cookies.length == 0 )
79         return SKIP_BODY;
80
81     if( name != null ) {
82         // If interested in only one cookie, find it
83
for( int i = 0; i < cookies.length; i++ ) {
84         if( cookies[i].getName().equals(name) ) {
85             cookie = cookies[i];
86             break;
87         }
88         }
89         if( cookie == null ) // Cookie wasn't found
90
return SKIP_BODY;
91     } else {
92         cookie = cookies[0];
93     }
94     pageContext.setAttribute(id,this,PageContext.PAGE_SCOPE);
95     return EVAL_BODY_TAG;
96     }
97
98     /**
99      * Method called at end of each cookies tag.
100      *
101      * @return EVAL_BODY_TAG if there is another cookie, or SKIP_BODY if there are no more cookies or this is a named cookie
102      */

103     public final int doAfterBody() throws JspException
104     {
105     // increment the cookie number from the cookies array
106
cookie_num++;
107     // See if this is the last or a named cookie
108
if( name != null || cookies == null || cookie_num >= cookies.length )
109         return SKIP_BODY;
110     // There is another cookie, so loop again
111
cookie = cookies[cookie_num];
112     if( cookie == null )
113         return SKIP_BODY;
114     return EVAL_BODY_TAG;
115     }
116
117     /**
118      * Method called at end of Tag
119      *
120      * @return EVAL_PAGE
121      */

122     public final int doEndTag() throws JspException
123     {
124         pageContext.removeAttribute(id,PageContext.PAGE_SCOPE);
125     try
126     {
127         if(bodyContent != null)
128         bodyContent.writeOut(bodyContent.getEnclosingWriter());
129     } catch(java.io.IOException JavaDoc e)
130     {
131         throw new JspException("IO Error: " + e.getMessage());
132     }
133     return EVAL_PAGE;
134     }
135
136     /**
137      * Set the optional tag attribute <b>name</b>.
138      *
139      * @param String name of cookie
140      */

141     public final void setName(String JavaDoc str)
142     {
143     name = str;
144     }
145
146     /**
147      * Returns the comment describing the purpose of this cookie, or ""
148      * if the cookie has no comment.
149      * <p>
150      * &lt;jsp:getProperty name=<i>"id"</i> property="comment"/&gt;
151      *
152      * @return String - cookie comment
153      */

154     public final String JavaDoc getComment()
155     {
156     return cookie.getComment();
157     }
158
159     /**
160      * Returns the domain name set for this cookie.
161      * <p>
162      * &lt;jsp:getProperty name=<i>"id"</i> property="domain"/&gt;
163      *
164      * @return String - cookie domain
165      */

166     public final String JavaDoc getDomain()
167     {
168     return cookie.getDomain();
169     }
170
171     /**
172      * Returns the maximum age of the cookie, specified in seconds,
173      * By default, -1 indicating the cookie will persist until browser
174      * shutdown.
175      * <p>
176      * &lt;jsp:getProperty name=<i>"id"</i> property="maxAge"/&gt;
177      *
178      * @return String - cookie max age
179      */

180     public final String JavaDoc getMaxAge()
181     {
182     return "" + cookie.getMaxAge();
183     }
184
185     /**
186      * Returns the name of the cookie.
187      * <p>
188      * &lt;jsp:getProperty name=<i>"id"</i> property="name"/&gt;
189      *
190      * @return String - cookie name
191      */

192     public final String JavaDoc getName()
193     {
194     return cookie.getName();
195     }
196
197     /**
198      * Returns the path on the server to which the browser returns this cookie.
199      * <p>
200      * &lt;jsp:getProperty name=<i>"id"</i> property="path"/&gt;
201      *
202      * @return String - cookie path on the server
203      */

204     public final String JavaDoc getPath()
205     {
206     return cookie.getPath();
207     }
208
209     /**
210      * Returns the value of the cookie.
211      * <p>
212      * &lt;jsp:getProperty name=<i>"id"</i> property="value"/&gt;
213      *
214      * @return String - cookie value
215      */

216     public final String JavaDoc getValue()
217     {
218     return cookie.getValue();
219     }
220
221     /**
222      * Returns "1" if the browser is sending cookies only over a secure
223      * protocol, or "0" if the browser can send cookies using any protocol.
224      * <p>
225      * &lt;jsp:getProperty name=<i>"id"</i> property="secure"/&gt;
226      *
227      * @return String - "1" if the cookies are secure, "0" if not
228      */

229     public final String JavaDoc getSecure()
230     {
231     if( cookie.getSecure() )
232         return "1";
233     return "0";
234     }
235
236     /**
237      * Returns the version of the protocol this cookie complies with.
238      * <p>
239      * &lt;jsp:getProperty name=<i>"id"</i> property="version"/&gt;
240      *
241      * @return String - cookie version
242      */

243     public final String JavaDoc getVersion()
244     {
245     return "" + cookie.getVersion();
246     }
247
248 }
249
Popular Tags