1 17 18 19 package org.apache.catalina.valves; 20 21 22 import java.io.IOException ; 23 import java.util.ArrayList ; 24 import java.util.regex.Pattern ; 25 import java.util.regex.PatternSyntaxException ; 26 27 import javax.servlet.ServletException ; 28 import javax.servlet.http.HttpServletResponse ; 29 30 import org.apache.catalina.connector.Request; 31 import org.apache.catalina.connector.Response; 32 import org.apache.catalina.util.StringManager; 33 34 67 68 public abstract class RequestFilterValve 69 extends ValveBase { 70 71 72 74 75 78 private static final String info = 79 "org.apache.catalina.valves.RequestFilterValve/1.0"; 80 81 82 85 protected static StringManager sm = 86 StringManager.getManager(Constants.Package); 87 88 89 91 92 95 protected String allow = null; 96 97 98 101 protected Pattern allows[] = new Pattern [0]; 102 103 104 107 protected Pattern denies[] = new Pattern [0]; 108 109 110 113 protected String deny = null; 114 115 116 118 119 123 public String getAllow() { 124 125 return (this.allow); 126 127 } 128 129 130 136 public void setAllow(String allow) { 137 138 this.allow = allow; 139 allows = precalculate(allow); 140 141 } 142 143 144 148 public String getDeny() { 149 150 return (this.deny); 151 152 } 153 154 155 161 public void setDeny(String deny) { 162 163 this.deny = deny; 164 denies = precalculate(deny); 165 166 } 167 168 169 172 public String getInfo() { 173 174 return (info); 175 176 } 177 178 179 181 182 194 public abstract void invoke(Request request, Response response) 195 throws IOException , ServletException ; 196 197 198 200 201 211 protected Pattern [] precalculate(String list) { 212 213 if (list == null) 214 return (new Pattern [0]); 215 list = list.trim(); 216 if (list.length() < 1) 217 return (new Pattern [0]); 218 list += ","; 219 220 ArrayList reList = new ArrayList (); 221 while (list.length() > 0) { 222 int comma = list.indexOf(','); 223 if (comma < 0) 224 break; 225 String pattern = list.substring(0, comma).trim(); 226 try { 227 reList.add(Pattern.compile(pattern)); 228 } catch (PatternSyntaxException e) { 229 IllegalArgumentException iae = new IllegalArgumentException 230 (sm.getString("requestFilterValve.syntax", pattern)); 231 iae.initCause(e); 232 throw iae; 233 } 234 list = list.substring(comma + 1); 235 } 236 237 Pattern reArray[] = new Pattern [reList.size()]; 238 return ((Pattern []) reList.toArray(reArray)); 239 240 } 241 242 243 254 protected void process(String property, 255 Request request, Response response) 256 throws IOException , ServletException { 257 258 for (int i = 0; i < denies.length; i++) { 260 if (denies[i].matcher(property).matches()) { 261 response.sendError(HttpServletResponse.SC_FORBIDDEN); 262 return; 263 } 264 } 265 266 for (int i = 0; i < allows.length; i++) { 268 if (allows[i].matcher(property).matches()) { 269 getNext().invoke(request, response); 270 return; 271 } 272 } 273 274 if ((denies.length > 0) && (allows.length == 0)) { 276 getNext().invoke(request, response); 277 return; 278 } 279 280 response.sendError(HttpServletResponse.SC_FORBIDDEN); 282 283 } 284 285 286 } 287 | Popular Tags |