KickJava   Java API By Example, From Geeks To Geeks.

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


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

37 public class HeaderTag extends TagSupport JavaDoc {
38
39     // ------------------------------------------------------------- Properties
40

41     /**
42      * The name of the scripting variable that will be exposed as a page
43      * scope attribute.
44      */

45     protected String JavaDoc id = null;
46
47     public String JavaDoc getId() {
48         return (this.id);
49     }
50
51     public void setId(String JavaDoc id) {
52         this.id = id;
53     }
54
55     /**
56      * The message resources for this package.
57      */

58     protected static MessageResources messages =
59         MessageResources.getMessageResources(
60             "org.apache.struts.taglib.bean.LocalStrings");
61
62     /**
63      * Return an array of header values if <code>multiple</code> is non-null.
64      */

65     protected String JavaDoc multiple = null;
66
67     public String JavaDoc getMultiple() {
68         return (this.multiple);
69     }
70
71     public void setMultiple(String JavaDoc multiple) {
72         this.multiple = multiple;
73     }
74
75     /**
76      * The name of the header whose value is to be exposed.
77      */

78     protected String JavaDoc name = null;
79
80     public String JavaDoc getName() {
81         return (this.name);
82     }
83
84     public void setName(String JavaDoc name) {
85         this.name = name;
86     }
87
88     /**
89      * The default value to return if no header of the specified name is found.
90      */

91     protected String JavaDoc value = null;
92
93     public String JavaDoc getValue() {
94         return (this.value);
95     }
96
97     public void setValue(String JavaDoc 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      * @exception JspException if a JSP exception has occurred
107      */

108     public int doStartTag() throws JspException JavaDoc {
109
110         if (this.multiple == null) {
111             this.handleSingleHeader();
112         } else {
113             this.handleMultipleHeaders();
114         }
115
116         return SKIP_BODY;
117     }
118
119     /**
120      * Expose an array of header values.
121      * @throws JspException
122      * @since Struts 1.2
123      */

124     protected void handleMultipleHeaders() throws JspException JavaDoc {
125         ArrayList JavaDoc values = new ArrayList JavaDoc();
126         Enumeration JavaDoc items =
127             ((HttpServletRequest JavaDoc) pageContext.getRequest()).getHeaders(name);
128             
129         while (items.hasMoreElements()){
130             values.add(items.nextElement());
131         }
132             
133         if (values.isEmpty() && (this.value != null)){
134             values.add(this.value);
135         }
136             
137         String JavaDoc headers[] = new String JavaDoc[values.size()];
138         if (headers.length == 0) {
139             JspException JavaDoc e =
140                 new JspException JavaDoc(messages.getMessage("header.get", name));
141             TagUtils.getInstance().saveException(pageContext, e);
142             throw e;
143         }
144         
145         pageContext.setAttribute(id, values.toArray(headers));
146     }
147
148     /**
149      * Expose a single header value.
150      * @throws JspException
151      * @since Struts 1.2
152      */

153     protected void handleSingleHeader() throws JspException JavaDoc {
154         String JavaDoc value =
155             ((HttpServletRequest JavaDoc) pageContext.getRequest()).getHeader(name);
156
157         if ((value == null) && (this.value != null)) {
158             value = this.value;
159         }
160
161         if (value == null) {
162             JspException JavaDoc e =
163                 new JspException JavaDoc(messages.getMessage("header.get", name));
164             TagUtils.getInstance().saveException(pageContext, e);
165             throw e;
166         }
167         
168         pageContext.setAttribute(id, value);
169     }
170
171     /**
172      * Release all allocated resources.
173      */

174     public void release() {
175
176         super.release();
177         id = null;
178         multiple = null;
179         name = null;
180         value = null;
181
182     }
183
184 }
185
Popular Tags