KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > webapp > balancer > rules > RequestHeaderRule


1 /*
2  * Copyright 2000,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 package org.apache.webapp.balancer.rules;
17
18 import javax.servlet.http.HttpServletRequest JavaDoc;
19
20
21 /**
22  * This rule checks for the presence
23  * of a specific request header, optionally
24  * with a specific value. The value may
25  * be null.
26  *
27  * @author Yoav Shapira
28  */

29 public class RequestHeaderRule extends BaseRule {
30     /**
31      * The header name, cannot be null.
32      */

33     private String JavaDoc headerName;
34
35     /**
36      * The header value. This may be
37      * null to indicate any value is OK.
38      */

39     private String JavaDoc headerValue;
40
41     /**
42      * Sets the header name.
43      *
44      * @param theName The name
45      */

46     public void setHeaderName(String JavaDoc theName) {
47         if (theName == null) {
48             throw new IllegalArgumentException JavaDoc(
49                 "The header name cannot be null.");
50         } else {
51             headerName = theName;
52         }
53     }
54
55     /**
56      * Returns the header name to match.
57      *
58      * @return The header name
59      */

60     protected String JavaDoc getHeaderName() {
61         return headerName;
62     }
63
64     /**
65      * Sets the header value.
66      *
67      * @param theValue The header value
68      */

69     public void setHeaderValue(String JavaDoc theValue) {
70         headerValue = theValue;
71     }
72
73     /**
74      * Returns the desired header value,
75      * which may be null.
76      *
77      * @return String
78      */

79     protected String JavaDoc getHeaderValue() {
80         return headerValue;
81     }
82
83     /**
84      * @see org.apache.webapp.balancer.Rule#matches(HttpServletRequest request)
85      */

86     public boolean matches(HttpServletRequest JavaDoc request) {
87         String JavaDoc actualHeaderValue = request.getHeader(getHeaderName());
88
89         if (actualHeaderValue == null) {
90             return (getHeaderValue() == null);
91         } else {
92             return (actualHeaderValue.compareTo(getHeaderValue()) == 0);
93         }
94     }
95
96     /**
97      * Returns a String representation of this object.
98      *
99      * @return String
100      */

101     public String JavaDoc toString() {
102         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
103
104         buffer.append("[");
105         buffer.append(getClass().getName());
106         buffer.append(": ");
107
108         buffer.append("Header name: ");
109         buffer.append(getHeaderName());
110         buffer.append(" / ");
111
112         buffer.append("Header value: ");
113         buffer.append(getHeaderValue());
114         buffer.append(" / ");
115
116         buffer.append("Redirect URL: ");
117         buffer.append(getRedirectUrl());
118
119         buffer.append("]");
120
121         return buffer.toString();
122     }
123 }
124
125
126 // End of file: RequestHeaderRule.java
127
Popular Tags