KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > struts > taglib > bean > CookieTag


1 /*
2  * $Id: CookieTag.java 54929 2004-10-16 16:38:42Z germuska $
3  *
4  * Copyright 1999-2004 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
20 package org.apache.struts.taglib.bean;
21
22
23 import java.util.ArrayList JavaDoc;
24 import javax.servlet.http.Cookie JavaDoc;
25 import javax.servlet.http.HttpServletRequest JavaDoc;
26 import javax.servlet.jsp.JspException JavaDoc;
27 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
28 import org.apache.struts.util.MessageResources;
29 import org.apache.struts.taglib.TagUtils;
30
31
32 /**
33  * Define a scripting variable based on the value(s) of the specified
34  * cookie received with this request.
35  *
36  * @version $Rev: 54929 $ $Date: 2004-10-16 17:38:42 +0100 (Sat, 16 Oct 2004) $
37  */

38
39 public class CookieTag extends TagSupport JavaDoc {
40
41
42     // ------------------------------------------------------------- Properties
43

44
45     /**
46      * The name of the scripting variable that will be exposed as a page
47      * scope attribute.
48      */

49     protected String JavaDoc id = null;
50
51     public String JavaDoc getId() {
52         return (this.id);
53     }
54
55     public void setId(String JavaDoc id) {
56         this.id = id;
57     }
58
59
60     /**
61      * The message resources for this package.
62      */

63     protected static MessageResources messages =
64         MessageResources.getMessageResources
65         ("org.apache.struts.taglib.bean.LocalStrings");
66
67
68     /**
69      * Return an array of Cookies if <code>multiple</code> is non-null.
70      */

71     protected String JavaDoc multiple = null;
72
73     public String JavaDoc getMultiple() {
74         return (this.multiple);
75     }
76
77     public void setMultiple(String JavaDoc multiple) {
78         this.multiple = multiple;
79     }
80
81
82     /**
83      * The name of the cookie whose value is to be exposed.
84      */

85     protected String JavaDoc name = null;
86
87     public String JavaDoc getName() {
88         return (this.name);
89     }
90
91     public void setName(String JavaDoc name) {
92         this.name = name;
93     }
94
95
96     /**
97      * The default value to return if no cookie of the specified name is found.
98      */

99     protected String JavaDoc value = null;
100
101     public String JavaDoc getValue() {
102         return (this.value);
103     }
104
105     public void setValue(String JavaDoc value) {
106         this.value = value;
107     }
108
109
110     // --------------------------------------------------------- Public Methods
111

112
113     /**
114      * Retrieve the required property and expose it as a scripting variable.
115      *
116      * @exception JspException if a JSP exception has occurred
117      */

118     public int doStartTag() throws JspException JavaDoc {
119
120         // Retrieve the required cookie value(s)
121
ArrayList JavaDoc values = new ArrayList JavaDoc();
122         Cookie JavaDoc cookies[] =
123             ((HttpServletRequest JavaDoc) pageContext.getRequest()).getCookies();
124         if (cookies == null)
125             cookies = new Cookie JavaDoc[0];
126
127         for (int i = 0; i < cookies.length; i++) {
128             if (name.equals(cookies[i].getName()))
129                 values.add(cookies[i]);
130         }
131         if ((values.size() < 1) && (value != null))
132             values.add(new Cookie JavaDoc(name, value));
133         if (values.size() < 1) {
134             JspException JavaDoc e = new JspException JavaDoc
135                 (messages.getMessage("cookie.get", name));
136             TagUtils.getInstance().saveException(pageContext, e);
137             throw e;
138         }
139
140         // Expose an appropriate variable containing these results
141
if (multiple == null) {
142             Cookie JavaDoc cookie = (Cookie JavaDoc) values.get(0);
143             pageContext.setAttribute(id, cookie);
144         } else {
145             cookies = new Cookie JavaDoc[values.size()];
146             pageContext.setAttribute(id, values.toArray(cookies));
147         }
148         return (SKIP_BODY);
149
150     }
151
152
153     /**
154      * Release all allocated resources.
155      */

156     public void release() {
157
158         super.release();
159         id = null;
160         multiple = null;
161         name = null;
162         value = null;
163
164     }
165
166
167 }
168
Popular Tags