View Javadoc

1   /*
2    * $Id: HeaderTag.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.HttpServletRequest;
24  import javax.servlet.jsp.JspException;
25  import javax.servlet.jsp.tagext.TagSupport;
26  
27  import java.util.ArrayList;
28  import java.util.Enumeration;
29  
30  /***
31   * Define a scripting variable based on the value(s) of the specified header
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 HeaderTag 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 header values if <code>multiple</code> is non-null.
55       */
56      protected String multiple = null;
57  
58      /***
59       * The name of the header whose value is to be exposed.
60       */
61      protected String name = null;
62  
63      /***
64       * The default value to return if no header 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         if (this.multiple == null) {
110             this.handleSingleHeader();
111         } else {
112             this.handleMultipleHeaders();
113         }
114 
115         return SKIP_BODY;
116     }
117 
118     /***
119      * Expose an array of header values.
120      *
121      * @throws JspException
122      * @since Struts 1.2
123      */
124     protected void handleMultipleHeaders()
125         throws JspException {
126         ArrayList values = new ArrayList();
127         Enumeration items =
128             ((HttpServletRequest) pageContext.getRequest()).getHeaders(name);
129 
130         while (items.hasMoreElements()) {
131             values.add(items.nextElement());
132         }
133 
134         if (values.isEmpty() && (this.value != null)) {
135             values.add(this.value);
136         }
137 
138         String[] headers = new String[values.size()];
139 
140         if (headers.length == 0) {
141             JspException e =
142                 new JspException(messages.getMessage("header.get", name));
143 
144             TagUtils.getInstance().saveException(pageContext, e);
145             throw e;
146         }
147 
148         pageContext.setAttribute(id, values.toArray(headers));
149     }
150 
151     /***
152      * Expose a single header value.
153      *
154      * @throws JspException
155      * @since Struts 1.2
156      */
157     protected void handleSingleHeader()
158         throws JspException {
159         String value =
160             ((HttpServletRequest) pageContext.getRequest()).getHeader(name);
161 
162         if ((value == null) && (this.value != null)) {
163             value = this.value;
164         }
165 
166         if (value == null) {
167             JspException e =
168                 new JspException(messages.getMessage("header.get", name));
169 
170             TagUtils.getInstance().saveException(pageContext, e);
171             throw e;
172         }
173 
174         pageContext.setAttribute(id, value);
175     }
176 
177     /***
178      * Release all allocated resources.
179      */
180     public void release() {
181         super.release();
182         id = null;
183         multiple = null;
184         name = null;
185         value = null;
186     }
187 }