KickJava   Java API By Example, From Geeks To Geeks.

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


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 accepts or rejects requests
23  * based on the presence of a attribute
24  * in the request.
25  *
26  * @author Yoav Shapira
27  */

28 public class RequestAttributeRule extends BaseRule {
29     /**
30      * The target attribute name (attribute
31      * must be present for match to succeed).
32      */

33     private String JavaDoc attributeName;
34
35     /**
36      * The target attribute value. This
37      * is optional: null means any attribute
38      * value is OK for a match. A non-null
39      * value will be matches exactly.
40      */

41     private Object JavaDoc attributeValue;
42
43     /**
44      * Sets the target attribute name.
45      *
46      * @param theAttributeName The attribute name
47      */

48     public void setAttributeName(String JavaDoc theAttributeName) {
49         if (theAttributeName == null) {
50             throw new IllegalArgumentException JavaDoc(
51                 "attributeName cannot be null.");
52         } else {
53             attributeName = theAttributeName;
54         }
55     }
56
57     /**
58      * Returns the target attribute name.
59      *
60      * @return String The target attribute name.
61      */

62     protected String JavaDoc getAttributeName() {
63         return attributeName;
64     }
65
66     /**
67      * Sets the attribute value, which may be null.
68      *
69      * @param theAttributeValue The attribute value
70      */

71     public void setAttributeValue(Object JavaDoc theAttributeValue) {
72         attributeValue = theAttributeValue;
73     }
74
75     /**
76      * Returns the target attribute value,
77      * which may be null.
78      *
79      * @return Object The target attribute value
80      */

81     protected Object JavaDoc getAttributeValue() {
82         return attributeValue;
83     }
84
85     /**
86      * @see org.apache.webapp.balancer.Rule#matches(HttpServletRequest)
87      */

88     public boolean matches(HttpServletRequest JavaDoc request) {
89         Object JavaDoc actualAttributeValue = request.getAttribute(getAttributeName());
90
91         if (actualAttributeValue == null) {
92             return (getAttributeValue() == null);
93         } else {
94             return (actualAttributeValue.equals(getAttributeValue()));
95         }
96     }
97
98     /**
99      * Returns a String representation of this object.
100      *
101      * @return String
102      */

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