KickJava   Java API By Example, From Geeks To Geeks.

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


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 host 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 RemoteHostValveForm extends ValveForm {
41     
42     // ----------------------------------------------------- Instance Variables
43

44
45     /**
46      * The text for the allow hosts 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 hosts 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      * Reset all properties to their default values.
110      *
111      * @param mapping The mapping used to select this instance
112      * @param request The servlet request we are processing
113      */

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

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

154     
155     public ActionErrors validate(ActionMapping mapping,
156     HttpServletRequest JavaDoc request) {
157         
158         ActionErrors errors = new ActionErrors();
159         
160         String JavaDoc submit = request.getParameter("submit");
161         
162         // front end validation when save is clicked.
163
//if (submit != null) {
164
// TBD
165
// validate allow/deny IPs
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 host = request.getRemoteHost();
189         // check for IP address also in case DNS is not configured
190
// to give a host name for the client machine
191
String JavaDoc ip = request.getRemoteAddr();
192     
193         if (host == null) {
194             return errors;
195         }
196         
197         for (int i = 0; i < denies.length; i++) {
198             if (denies[i].matcher(host).matches()) {
199                 if (allows.length < 1) {
200                     errors.add("deny",
201                         new ActionError("error.denyHost"));
202                 }
203                 for (int j = 0; j < allows.length; j++) {
204                     if (!allows[j].matcher(host).matches()) {
205                         errors.add("deny",
206                         new ActionError("error.denyHost"));
207                     }
208                 }
209             } else if (denies[i].matcher(ip).matches()) {
210                 if (allows.length < 1) {
211                     errors.add("deny",
212                         new ActionError("error.denyHost"));
213                 }
214                 for (int j = 0; j < allows.length; j++) {
215                     if (!allows[j].matcher(ip).matches()) {
216                         errors.add("deny",
217                         new ActionError("error.denyHost"));
218                     }
219                 }
220             }
221         }
222         
223         boolean allowMatch = true;
224         
225         if ((allows != null) && (allows.length > 0)) {
226             allowMatch = false;
227         }
228         
229         for (int i = 0; i < allows.length; i++) {
230             if (allows[i].matcher(host).matches()) {
231                 allowMatch = true;
232             }
233         }
234         
235         if (!allowMatch) {
236             errors.add("allow", new ActionError("error.allowHost"));
237         }
238         
239         return errors;
240     }
241     
242 }
243
Popular Tags