View Javadoc

1   /*
2    * $Id: CookieTag.java 376840 2006-02-10 21:00:51Z husted $
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  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      // ------------------------------------------------------------- Properties
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     // --------------------------------------------------------- Public Methods
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         // Retrieve the required cookie value(s)
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         // Expose an appropriate variable containing these results
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 }