KickJava   Java API By Example, From Geeks To Geeks.

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


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>headerValuess</b>, used to get HTTP header values for
27  * headers which can have multiple values such as Language
28  * using the standard JSP &lt;jsp:getProperty&gt; tag.
29  * <p>
30  * The script variable of name <b>id</b> is availble only within the
31  * body of the <b>headerValues</b> tag.
32  * <p>
33  * Loops through all the header values for a header.
34  * <p>
35  * Must be nested within a <b>headers</b> tag.
36  * <p>
37  * JSP Tag Lib Descriptor
38  * <p><pre>
39  * &lt;name&gt;headerValues&lt;/name&gt;
40  * &lt;tagclass&gt;org.apache.taglibs.request.HeaderValuesTag&lt;/tagclass&gt;
41  * &lt;teiclass&gt;org.apache.taglibs.request.HeaderValuesTEI&lt;/teiclass&gt;
42  * &lt;bodycontent&gt;JSP&lt;/bodycontent&gt;
43  * &lt;info&gt;Loop through all the values for a header which has multiple values.&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  * </pre>
50  *
51  * @see HeadersTag
52  *
53  * @author Glenn Nielsen
54  */

55
56 public class HeaderValuesTag extends BodyTagSupport
57 {
58     // All the values for header
59
private Enumeration values = null;
60     // Current value
61
private String JavaDoc value = null;
62
63     /**
64      * Gets the header values for the current header.
65      *
66      * @return SKIP_BODY if no header values exist, EVAL_BODY_TAG if header values exist
67      */

68     public final int doStartTag() throws JspException
69     {
70         // Initialize variables
71
values = null;
72         value = null;
73
74     // Get the parent headers tag
75
HeadersTag ht;
76     try {
77         ht = (HeadersTag)this.findAncestorWithClass(this,
78         Class.forName("org.apache.taglibs.request.HeadersTag"));
79     } catch(Exception JavaDoc e) {
80         return SKIP_BODY;
81     }
82
83     // Get the headers
84
values = ((HttpServletRequest)pageContext.getRequest()).getHeaders(ht.getName());
85     if( values == null || !values.hasMoreElements() )
86         return SKIP_BODY;
87
88     value = (String JavaDoc)values.nextElement();
89     if( value == 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 headerValues tag.
98      *
99      * @return EVAL_BODY_TAG if there is another header value, or SKIP_BODY if there are no more header values
100      */

101     public final int doAfterBody() throws JspException
102     {
103     // See if this is the last header value
104
if( !values.hasMoreElements() )
105         return SKIP_BODY;
106     // There is another header value, so loop again
107
value = (String JavaDoc)values.nextElement();
108     if( value == 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      * Returns the value of the header.
134      * <p>
135      * &lt;jsp:getProperty name=<i>"id"</i> property="header"/&gt;
136      *
137      * @return String - value of the header
138      */

139     public final String JavaDoc getHeader()
140     {
141     return value;
142     }
143
144 }
145
Popular Tags