1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.struts.taglib.bean;
19
20 import org.apache.struts.taglib.TagUtils;
21 import org.apache.struts.util.MessageResources;
22
23 import javax.servlet.http.Cookie;
24 import javax.servlet.http.HttpServletRequest;
25 import javax.servlet.jsp.JspException;
26 import javax.servlet.jsp.tagext.TagSupport;
27
28 import java.util.ArrayList;
29
30 /***
31 * Define a scripting variable based on the value(s) of the specified cookie
32 * received with this request.
33 *
34 * @version $Rev: 376840 $ $Date: 2004-10-16 12:38:42 -0400 (Sat, 16 Oct 2004)
35 * $
36 */
37 public class CookieTag extends TagSupport {
38 /***
39 * The message resources for this package.
40 */
41 protected static MessageResources messages =
42 MessageResources.getMessageResources(
43 "org.apache.struts.taglib.bean.LocalStrings");
44
45
46
47 /***
48 * The name of the scripting variable that will be exposed as a page scope
49 * attribute.
50 */
51 protected String id = null;
52
53 /***
54 * Return an array of Cookies if <code>multiple</code> is non-null.
55 */
56 protected String multiple = null;
57
58 /***
59 * The name of the cookie whose value is to be exposed.
60 */
61 protected String name = null;
62
63 /***
64 * The default value to return if no cookie of the specified name is
65 * found.
66 */
67 protected String value = null;
68
69 public String getId() {
70 return (this.id);
71 }
72
73 public void setId(String id) {
74 this.id = id;
75 }
76
77 public String getMultiple() {
78 return (this.multiple);
79 }
80
81 public void setMultiple(String multiple) {
82 this.multiple = multiple;
83 }
84
85 public String getName() {
86 return (this.name);
87 }
88
89 public void setName(String name) {
90 this.name = name;
91 }
92
93 public String getValue() {
94 return (this.value);
95 }
96
97 public void setValue(String value) {
98 this.value = value;
99 }
100
101
102
103 /***
104 * Retrieve the required property and expose it as a scripting variable.
105 *
106 * @throws JspException if a JSP exception has occurred
107 */
108 public int doStartTag() throws JspException {
109
110 ArrayList values = new ArrayList();
111 Cookie[] cookies =
112 ((HttpServletRequest) pageContext.getRequest()).getCookies();
113
114 if (cookies == null) {
115 cookies = new Cookie[0];
116 }
117
118 for (int i = 0; i < cookies.length; i++) {
119 if (name.equals(cookies[i].getName())) {
120 values.add(cookies[i]);
121 }
122 }
123
124 if ((values.size() < 1) && (value != null)) {
125 values.add(new Cookie(name, value));
126 }
127
128 if (values.size() < 1) {
129 JspException e =
130 new JspException(messages.getMessage("cookie.get", name));
131
132 TagUtils.getInstance().saveException(pageContext, e);
133 throw e;
134 }
135
136
137 if (multiple == null) {
138 Cookie cookie = (Cookie) values.get(0);
139
140 pageContext.setAttribute(id, cookie);
141 } else {
142 cookies = new Cookie[values.size()];
143 pageContext.setAttribute(id, values.toArray(cookies));
144 }
145
146 return (SKIP_BODY);
147 }
148
149 /***
150 * Release all allocated resources.
151 */
152 public void release() {
153 super.release();
154 id = null;
155 multiple = null;
156 name = null;
157 value = null;
158 }
159 }