KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > webapp > admin > valve > RemoteAddrValveForm


1 /*
2  * Copyright 2001,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
17 package org.apache.webapp.admin.valve;
18
19 import java.lang.IllegalArgumentException JavaDoc;
20 import java.net.InetAddress JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.regex.Pattern JavaDoc;
23 import javax.servlet.http.HttpServletRequest JavaDoc;
24
25 import org.apache.struts.action.ActionError;
26 import org.apache.struts.action.ActionErrors;
27 import org.apache.struts.action.ActionForm;
28 import org.apache.struts.action.ActionMapping;
29
30 import org.apache.webapp.admin.ApplicationServlet;
31 import org.apache.webapp.admin.LabelValueBean;
32
33 /**
34  * Form bean for the remote addr valve page.
35  *
36  * @author Manveen Kaur
37  * @version $Revision: 1.6 $ $Date: 2004/09/03 09:33:06 $
38  */

39
40 public final class RemoteAddrValveForm extends ValveForm {
41     
42     // ----------------------------------------------------- Instance Variables
43

44     
45     /**
46      * The text for the allow IP addresses.
47      * A comma-separated list of regular expression patterns
48      * that the remote client's IP address is compared to.
49      */

50     private String JavaDoc allow = "";
51
52     /**
53      * The text for the deny IP addresses.
54      */

55     private String JavaDoc deny = "";
56     
57     /**
58      * The set of <code>allow</code> regular expressions we will evaluate.
59      */

60     private Pattern JavaDoc allows[] = new Pattern JavaDoc[0];
61
62     /**
63      * The set of <code>deny</code> regular expressions we will evaluate.
64      */

65     private Pattern JavaDoc denies[] = new Pattern JavaDoc[0];
66
67
68     // ------------------------------------------------------------- Properties
69

70     /**
71      * Return the allow hosts IP adddresses.
72      */

73     public String JavaDoc getAllow() {
74         
75         return this.allow;
76         
77     }
78     
79     /**
80      * Set the allow hosts.
81      */

82     public void setAllow(String JavaDoc allow) {
83         
84         this.allow = allow;
85         
86     }
87     
88     /**
89      * Return the deny hosts IP adddresses.
90      */

91     public String JavaDoc getDeny() {
92         
93         return this.deny;
94         
95     }
96     
97     /**
98      * Set the deny hosts IP addresses.
99      */

100     public void setDeny(String JavaDoc deny) {
101         
102         this.deny = deny;
103         
104     }
105
106     // --------------------------------------------------------- Public Methods
107

108     
109     /**
110      * Reset all properties to their default values.
111      *
112      * @param mapping The mapping used to select this instance
113      * @param request The servlet request we are processing
114      */

115     public void reset(ActionMapping mapping, HttpServletRequest JavaDoc request) {
116                 
117         super.reset(mapping, request);
118         this.allow = null;
119         this.deny = null;
120         this.allows = null;
121         this.denies = null;
122         
123     }
124     
125     /**
126      * Render this object as a String.
127      */

128     public String JavaDoc toString() {
129
130         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("RemoteAddrValveForm[adminAction=");
131         sb.append(getAdminAction());
132         sb.append("',valveType=");
133         sb.append(getValveType());
134         sb.append(",allow=");
135         sb.append(getAllow());
136         sb.append(",deny=");
137         sb.append(getDeny());
138         sb.append("',objectName='");
139         sb.append(getObjectName());
140         sb.append("]");
141         return (sb.toString());
142
143     }
144     
145     /**
146      * Validate the properties that have been set from this HTTP request,
147      * and return an <code>ActionErrors</code> object that encapsulates any
148      * validation errors that have been found. If no errors are found, return
149      * <code>null</code> or an <code>ActionErrors</code> object with no
150      * recorded error messages.
151      *
152      * @param mapping The mapping used to select this instance
153      * @param request The servlet request we are processing
154      */

155     
156     public ActionErrors validate(ActionMapping mapping,
157     HttpServletRequest JavaDoc request) {
158         
159         ActionErrors errors = new ActionErrors();
160         
161         String JavaDoc submit = request.getParameter("submit");
162         
163         // front end validation when save is clicked.
164
//if (submit != null) {
165
// validate allow/deny patterns
166
if ((allow == null) || (allow.length() < 1)) {
167                 if ((deny == null) || (deny.length() < 1)) {
168                     errors.add("allow",
169                     new ActionError("error.allow.deny.required"));
170                 }
171             }
172         //}
173

174         try {
175             allows = ValveUtil.precalculate(allow);
176         } catch (IllegalArgumentException JavaDoc e) {
177             errors.add("allow", new ActionError("error.syntax"));
178             return errors;
179         }
180          
181         try {
182             denies = ValveUtil.precalculate(deny);
183         } catch (IllegalArgumentException JavaDoc e) {
184             errors.add("allow", new ActionError("error.syntax"));
185             return errors;
186         }
187         
188         String JavaDoc ip = request.getRemoteAddr();
189         
190         if (ip == null) {
191             return errors;
192         }
193         
194         for (int i = 0; i < denies.length; i++) {
195             if (denies[i].matcher(ip).matches()) {
196                 if (allows.length < 1) {
197                     errors.add("deny",
198                         new ActionError("error.denyIP"));
199                 }
200                 for (int j = 0; j < allows.length; j++) {
201                     if (!allows[j].matcher(ip).matches()) {
202                         errors.add("deny",
203                         new ActionError("error.denyIP"));
204                     }
205                 }
206             }
207         }
208         
209         boolean allowMatch = true;
210         if (allows.length > 0) {
211             allowMatch = false;
212         }
213         for (int i = 0; i < allows.length; i++) {
214             if (allows[i].matcher(ip).matches()) {
215                 allowMatch = true;
216             }
217         }
218         if (!allowMatch) {
219             errors.add("allow", new ActionError("error.allowIP"));
220         }
221         
222         return errors;
223     }
224 }
225
Popular Tags