KickJava   Java API By Example, From Geeks To Geeks.

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


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 looks for a specific string
23  * in the URL for a positive match.
24  *
25  * @author Yoav Shapira
26  */

27 public class URLStringMatchRule extends BaseRule {
28     /**
29      * The target string that must be present
30      * in the URL, may not be null.
31      */

32     private String JavaDoc targetString;
33
34     /**
35      * Sets the target string that must
36      * be present in the URL.
37      *
38      * @param theTargetString The target string
39      */

40     public void setTargetString(String JavaDoc theTargetString) {
41         if (theTargetString == null) {
42             throw new IllegalArgumentException JavaDoc(
43                 "The target string cannot be null.");
44         } else {
45             targetString = theTargetString;
46         }
47     }
48
49     /**
50      * Returns the target string that must
51      * be present in the URL.
52      *
53      * @return The target string
54      */

55     protected String JavaDoc getTargetString() {
56         return targetString;
57     }
58
59     /**
60      * @see org.apache.webapp.balancer.Rule#matches(HttpServletRequest)
61      *
62      * Looks for the target string in the request URL.
63      */

64     public boolean matches(HttpServletRequest JavaDoc request) {
65         String JavaDoc requestUrl = request.getRequestURL().toString();
66         int index = requestUrl.indexOf(getTargetString());
67
68         return (index > -1);
69     }
70
71     /**
72      * Returns a String representation of this object.
73      *
74      * @return String
75      */

76     public String JavaDoc toString() {
77         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
78
79         buffer.append("[");
80         buffer.append(getClass().getName());
81         buffer.append(": ");
82
83         buffer.append("Target string: ");
84         buffer.append(getTargetString());
85         buffer.append(" / ");
86
87         buffer.append("Redirect URL: ");
88         buffer.append(getRedirectUrl());
89
90         buffer.append("]");
91
92         return buffer.toString();
93     }
94 }
95
96
97 // End of file: URLStringMatchRule.java
98
Popular Tags