KickJava   Java API By Example, From Geeks To Geeks.

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


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>equalsHeader</b>, used to determine if a Request
27  * header equals the value of the "match" tag attribute.
28  * <p>
29  * Includes the body of the tag if the header equals the value of the
30  * "match" tag header.
31  * <p>
32  * You can set the optional tag header <b>value</b> to <i>true</i> or
33  * <i>false</i>. The body of the tag is included if equalsHeader matches
34  * the value.
35  * <p>
36  * You can set the optional tag header <b>ignoreCase</b> to <i>true</i> or
37  * <i>false</i>. If ignoreCase is set to true, then the comparison between the
38  * request header and the "match" tag attribute will <i>not</i> be
39  * case-sensitive.
40  * <p>
41  * JSP Tag Lib Descriptor
42  * <p><pre>
43  * &lt;name>equalsHeader&lt;/name>
44  * &lt;tagclass>org.apache.taglibs.request.EqualsHeaderTag&lt;/tagclass>
45  * &lt;bodycontent>JSP&lt;/bodycontent>
46  * &lt;info>Includes the body of the tag if the request header equals the
47  * value of the "match" tag header.&lt;/info>
48  * &lt;header>
49  * &lt;name>name&lt;/name>
50  * &lt;required>true&lt;/required>
51  * &lt;rtexprvalue>false&lt;/rtexprvalue>
52  * &lt;/header>
53  * &lt;header>
54  * &lt;name>value&lt;/name>
55  * &lt;required>false&lt;/required>
56  * &lt;rtexprvalue>false&lt;/rtexprvalue>
57  * &lt;/header>
58  * &lt;header>
59  * &lt;name>match&lt;/name>
60  * &lt;required>true&lt;/required>
61  * &lt;rtexprvalue>true&lt;/rtexprvalue>
62  * &lt;/header>
63  * &lt;header>
64  * &lt;name>ignoreCase&lt;/name>
65  * &lt;required>false&lt;/required>
66  * &lt;rtexprvalue>false&lt;/rtexprvalue>
67  * &lt;/header>
68  * </pre>
69  *
70  * @author Glenn Nielsen
71  */

72
73
74 public class EqualsHeaderTag extends TagSupport
75 {
76     private String JavaDoc name = null;
77     private String JavaDoc match = null;
78     //default behaviour is don't ignore case and execute body when tag contents
79
//equal value set by setMatch
80
private boolean ignoreCase = false;
81     private boolean value = true;
82
83     /**
84      * Includes the body of the tag if the request header equals the value set in the
85      * 'match' header.
86      *
87      * @return SKIP_BODY if equalsHeader body content does not equal the value of
88      * the match header, EVAL_BODY_include if it does
89      */

90     public final int doStartTag() throws JspException
91     {
92         //result is whether or not tag contents equal the match header
93
boolean result = false;
94         // Get the header value
95
String JavaDoc header = ((HttpServletRequest)pageContext.getRequest()).getHeader(name);
96
97         if (header != null) {
98             if (ignoreCase) {
99                 result = header.equalsIgnoreCase(match);
100             } else {
101                 result = header.equals(match);
102             }
103         }
104         
105         if( value == result )
106             return EVAL_BODY_INCLUDE;
107
108         return SKIP_BODY;
109     }
110
111     /**
112      * Set the required tag header <b>name</b>.
113      *
114      * @param String name of request header
115      */

116     public final void setName(String JavaDoc str)
117     {
118     name = str;
119     }
120
121
122     /**
123      * Set the String that will be compared to the request
124      * header.
125      *
126      * @param String value to match against the request header
127      */

128     public final void setMatch(String JavaDoc str) {
129         match=str;
130     }
131     
132
133     /**
134      * If ignoreCase is set to true, then the comparison
135      * between the "match" attribute and the
136      * request header will <i>not</i> be case sensitive
137      *
138      * @param boolean true = ignore case<BR>false = case sensitive<BR>
139      * default value = false
140      */

141     public final void setIgnoreCase(boolean value) {
142         ignoreCase = value;
143     }
144     
145     /**
146      * Set the optional tag header <b>value</b> to true or false.
147      *
148      * @param boolean true or false
149      */

150     public final void setValue(boolean value)
151     {
152     this.value = value;
153     }
154
155 }
156
Popular Tags