KickJava   Java API By Example, From Geeks To Geeks.

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


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 parameter
24  * in the request.
25  *
26  * @author Yoav Shapira
27  */

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

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

41     private String JavaDoc paramValue;
42
43     /**
44      * Sets the target parameter name.
45      *
46      * @param theParamName The parameter name
47      */

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

61     protected String JavaDoc getParamName() {
62         return paramName;
63     }
64
65     /**
66      * Sets the parameter value, which may be null.
67      *
68      * @param theParamValue The parameter value
69      */

70     public void setParamValue(String JavaDoc theParamValue) {
71         paramValue = theParamValue;
72     }
73
74     /**
75      * Returns the target parameter value,
76      * which may be null.
77      *
78      * @return String The target parameter value
79      */

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

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

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