KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > request > HeadersTag


1 /*
2  * Copyright 1999,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.taglibs.request;
18
19 import java.util.*;
20 import javax.servlet.*;
21 import javax.servlet.http.*;
22 import javax.servlet.jsp.*;
23 import javax.servlet.jsp.tagext.*;
24
25 /**
26  * JSP Tag <b>headers</b>, used to get HttpServletRequest header information
27  * using the standard JSP &lt;jsp:getProperty&gt; tag.
28  * <p>
29  * The script variable of name <b>id</b> is availble only within the
30  * body of the <b>headers</b> tag.
31  * <p>
32  * Loops through all the headers received by the HttpServletRequest.
33  * <p>
34  * If the optional attribute <b>name</b> is present only the header of
35  * that name is retreived.
36  * <p>
37  * JSP Tag Lib Descriptor
38  * <p><pre>
39  * &lt;name&gt;headers&lt;/name&gt;
40  * &lt;tagclass&gt;org.apache.taglibs.request.HeadersTag&lt;/tagclass&gt;
41  * &lt;teiclass&gt;org.apache.taglibs.request.HeadersTEI&lt;/teiclass&gt;
42  * &lt;bodycontent&gt;JSP&lt;/bodycontent&gt;
43  * &lt;info&gt;Loop through all headers, or get the properties of a single header.&lt;/info&gt;
44  * &lt;attribute&gt;
45  * &lt;name&gt;id&lt;/name&gt;
46  * &lt;required&gt;true&lt;/required&gt;
47  * &lt;rtexprvalue&gt;false&lt;/rtexprvalue&gt;
48  * &lt;/attribute&gt;
49  * &lt;attribute&gt;
50  * &lt;name&gt;name&lt;/name&gt;
51  * &lt;required&gt;false&lt;/required&gt;
52  * &lt;rtexprvalue&gt;false&lt;/rtexprvalue&gt;
53  * &lt;/attribute&gt;
54  * </pre>
55  *
56  * @author Glenn Nielsen
57  */

58
59 public class HeadersTag extends BodyTagSupport
60 {
61     // Optional attribute, name of HTTP header to get
62
private String JavaDoc name = null;
63     // The current request
64
private HttpServletRequest req = null;
65     // All the headers for request
66
private Enumeration headers = null;
67     // Name of current header
68
private String JavaDoc header = null;
69
70     /**
71      * Gets the headers that came with the request or header with <b>name</b>.
72      *
73      * @return SKIP_BODY if no headers or header with <b>name</b> is not found, EVAL_BODY_TAG if header exists
74      */

75     public final int doStartTag() throws JspException
76     {
77     // Get the request
78
req = (HttpServletRequest)pageContext.getRequest();
79
80     if( name != null ) {
81         if( req.getHeader(name) != null )
82         header = name;
83     } else {
84         headers = req.getHeaderNames();
85         if( headers == null || !headers.hasMoreElements() )
86         return SKIP_BODY;
87         header = (String JavaDoc)headers.nextElement();
88     }
89     if( header == null )
90         return SKIP_BODY;
91
92     pageContext.setAttribute(id,this,PageContext.PAGE_SCOPE);
93     return EVAL_BODY_TAG;
94     }
95
96     /**
97      * Method called at end of each headers tag body.
98      *
99      * @return EVAL_BODY_TAG if there is another header, or SKIP_BODY if there are no more headers or this is a named header
100      */

101     public final int doAfterBody() throws JspException
102     {
103     // See if this is the last or a named header
104
if( name != null || !headers.hasMoreElements() )
105         return SKIP_BODY;
106     // There is another header, so loop again
107
header = (String JavaDoc)headers.nextElement();
108     if( header == null )
109         return SKIP_BODY;
110     return EVAL_BODY_TAG;
111     }
112
113     /**
114      * Method called at end of Tag
115      *
116      * @return EVAL_PAGE
117      */

118     public final int doEndTag() throws JspException
119     {
120         pageContext.removeAttribute(id,PageContext.PAGE_SCOPE);
121     try
122     {
123         if(bodyContent != null)
124         bodyContent.writeOut(bodyContent.getEnclosingWriter());
125     } catch(java.io.IOException JavaDoc e)
126     {
127         throw new JspException("IO Error: " + e.getMessage());
128     }
129     return EVAL_PAGE;
130     }
131
132     /**
133      * Set the optional tag attribute <b>name</b>.
134      *
135      * @param String name of HTTP header
136      */

137     public final void setName(String JavaDoc str)
138     {
139     name = str;
140     }
141
142     /**
143      * Returns the name of the header.
144      * <p>
145      * &lt;jsp:getProperty name=<i>"id"</i> property="name"/&gt;
146      *
147      * @return String - header name
148      */

149     public final String JavaDoc getName()
150     {
151     return header;
152     }
153
154     /**
155      * Returns the value of the header.
156      * <p>
157      * &lt;jsp:getProperty name=<i>"id"</i> property="header"/&gt;
158      *
159      * @return String - value of the header
160      */

161     public final String JavaDoc getHeader()
162     {
163     String JavaDoc value = req.getHeader(header);
164     if( value == null )
165         return "";
166     return value;
167     }
168
169     /**
170      * Returns the value of a date header as the number of ms since the epoch.
171      * <p>
172      * &lt;jsp:getProperty name=<i>"id"</i> property="dateHeader"/&gt;
173      *
174      * @return String - value of a date header
175      */

176     public final String JavaDoc getDateHeader()
177     {
178     return "" + req.getDateHeader(header);
179     }
180
181     /**
182      * Returns the value of a header as an integer.
183      * <p>
184      * &lt;jsp:getProperty name=<i>"id"</i> property="intHeader"/&gt;
185      *
186      * @return String - value of an integer header
187      */

188     public final String JavaDoc getIntHeader()
189     {
190     return "" + req.getIntHeader(header);
191     }
192
193 }
194
Popular Tags