KickJava   Java API By Example, From Geeks To Geeks.

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


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 redirects the request based
23  * on the user's role.
24  *
25  * @author Yoav Shapira
26  */

27 public class UserRoleRule extends BaseRule {
28     /**
29      * The desired role for the user.
30      * If the user is in this role, the
31      * match will succeed.
32      */

33     private String JavaDoc role;
34
35     /**
36      * Sets the desired role.
37      *
38      * @param theRole The desire role
39      */

40     public void setRole(String JavaDoc theRole) {
41         if (theRole == null) {
42             throw new IllegalArgumentException JavaDoc("The role cannot be null.");
43         } else {
44             role = theRole;
45         }
46     }
47
48     /**
49      * Returns the target role.
50      *
51      * @return The desired role
52      */

53     protected String JavaDoc getRole() {
54         return role;
55     }
56
57     /**
58      * @see org.apache.webapp.balancer.Rule#matches(HttpServletRequest)
59      */

60     public boolean matches(HttpServletRequest JavaDoc request) {
61         return request.isUserInRole(getRole());
62     }
63
64     /**
65      * Returns a String representation of this object.
66      *
67      * @return String
68      */

69     public String JavaDoc toString() {
70         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
71
72         buffer.append("[");
73         buffer.append(getClass().getName());
74         buffer.append(": ");
75
76         buffer.append("Target role: ");
77         buffer.append(getRole());
78         buffer.append(" / ");
79
80         buffer.append("Redirect URL: ");
81         buffer.append(getRedirectUrl());
82
83         buffer.append("]");
84
85         return buffer.toString();
86     }
87 }
88
89
90 // End of file: UserRoleRule.java
91
Popular Tags