1 17 18 19 package org.apache.catalina.core; 20 21 22 import javax.servlet.Servlet ; 23 import javax.servlet.ServletRequest ; 24 import javax.servlet.http.HttpServletRequest ; 25 26 import org.apache.catalina.CometFilter; 27 import org.apache.catalina.Globals; 28 import org.apache.catalina.Wrapper; 29 import org.apache.catalina.connector.Request; 30 import org.apache.catalina.deploy.FilterMap; 31 32 40 41 public final class ApplicationFilterFactory { 42 43 44 46 47 public static final int ERROR = 1; 48 public static final Integer ERROR_INTEGER = new Integer (ERROR); 49 public static final int FORWARD = 2; 50 public static final Integer FORWARD_INTEGER = new Integer (FORWARD); 51 public static final int INCLUDE = 4; 52 public static final Integer INCLUDE_INTEGER = new Integer (INCLUDE); 53 public static final int REQUEST = 8; 54 public static final Integer REQUEST_INTEGER = new Integer (REQUEST); 55 56 public static final String DISPATCHER_TYPE_ATTR = 57 Globals.DISPATCHER_TYPE_ATTR; 58 public static final String DISPATCHER_REQUEST_PATH_ATTR = 59 Globals.DISPATCHER_REQUEST_PATH_ATTR; 60 61 private static final SecurityManager securityManager = 62 System.getSecurityManager(); 63 64 private static ApplicationFilterFactory factory = null;; 65 66 67 69 70 73 private ApplicationFilterFactory() { 74 } 75 76 77 79 80 83 public static ApplicationFilterFactory getInstance() { 84 if (factory == null) { 85 factory = new ApplicationFilterFactory(); 86 } 87 return factory; 88 } 89 90 91 99 public ApplicationFilterChain createFilterChain 100 (ServletRequest request, Wrapper wrapper, Servlet servlet) { 101 102 int dispatcher = -1; 104 if (request.getAttribute(DISPATCHER_TYPE_ATTR) != null) { 105 Integer dispatcherInt = 106 (Integer ) request.getAttribute(DISPATCHER_TYPE_ATTR); 107 dispatcher = dispatcherInt.intValue(); 108 } 109 String requestPath = null; 110 Object attribute = request.getAttribute(DISPATCHER_REQUEST_PATH_ATTR); 111 112 if (attribute != null){ 113 requestPath = attribute.toString(); 114 } 115 116 HttpServletRequest hreq = null; 117 if (request instanceof HttpServletRequest ) 118 hreq = (HttpServletRequest )request; 119 if (servlet == null) 121 return (null); 122 123 boolean comet = false; 124 125 ApplicationFilterChain filterChain = null; 127 if ((securityManager == null) && (request instanceof Request )) { 128 Request req = (Request ) request; 129 filterChain = (ApplicationFilterChain) req.getFilterChain(); 130 if (filterChain == null) { 131 filterChain = new ApplicationFilterChain(); 132 req.setFilterChain(filterChain); 133 } 134 comet = req.isComet(); 135 } else { 136 filterChain = new ApplicationFilterChain(); 138 } 139 140 filterChain.setServlet(servlet); 141 142 filterChain.setSupport 143 (((StandardWrapper)wrapper).getInstanceSupport()); 144 145 StandardContext context = (StandardContext) wrapper.getParent(); 147 FilterMap filterMaps[] = context.findFilterMaps(); 148 149 if ((filterMaps == null) || (filterMaps.length == 0)) 151 return (filterChain); 152 153 String servletName = wrapper.getName(); 155 156 for (int i = 0; i < filterMaps.length; i++) { 158 if (!matchDispatcher(filterMaps[i] ,dispatcher)) { 159 continue; 160 } 161 if (!matchFiltersURL(filterMaps[i], requestPath)) 162 continue; 163 ApplicationFilterConfig filterConfig = (ApplicationFilterConfig) 164 context.findFilterConfig(filterMaps[i].getFilterName()); 165 if (filterConfig == null) { 166 ; continue; 168 } 169 boolean isCometFilter = false; 170 if (comet) { 171 try { 172 isCometFilter = filterConfig.getFilter() instanceof CometFilter; 173 } catch (Exception e) { 174 } 178 if (isCometFilter) { 179 filterChain.addFilter(filterConfig); 180 } 181 } else { 182 filterChain.addFilter(filterConfig); 183 } 184 } 185 186 for (int i = 0; i < filterMaps.length; i++) { 188 if (!matchDispatcher(filterMaps[i] ,dispatcher)) { 189 continue; 190 } 191 if (!matchFiltersServlet(filterMaps[i], servletName)) 192 continue; 193 ApplicationFilterConfig filterConfig = (ApplicationFilterConfig) 194 context.findFilterConfig(filterMaps[i].getFilterName()); 195 if (filterConfig == null) { 196 ; continue; 198 } 199 boolean isCometFilter = false; 200 if (comet) { 201 try { 202 isCometFilter = filterConfig.getFilter() instanceof CometFilter; 203 } catch (Exception e) { 204 } 208 if (isCometFilter) { 209 filterChain.addFilter(filterConfig); 210 } 211 } else { 212 filterChain.addFilter(filterConfig); 213 } 214 } 215 216 return (filterChain); 218 219 } 220 221 222 224 225 233 private boolean matchFiltersURL(FilterMap filterMap, String requestPath) { 234 235 if (filterMap.getAllMatch()) 238 return (true); 239 240 if (requestPath == null) 241 return (false); 242 243 String [] testPaths = filterMap.getURLPatterns(); 245 246 for (int i = 0; i < testPaths.length; i++) { 247 if (matchFiltersURL(testPaths[i], requestPath)) { 248 return (true); 249 } 250 } 251 252 return (false); 254 255 } 256 257 258 266 private boolean matchFiltersURL(String testPath, String requestPath) { 267 268 if (testPath == null) 269 return (false); 270 271 if (testPath.equals(requestPath)) 273 return (true); 274 275 if (testPath.equals("/*")) 277 return (true); 278 if (testPath.endsWith("/*")) { 279 if (testPath.regionMatches(0, requestPath, 0, 280 testPath.length() - 2)) { 281 if (requestPath.length() == (testPath.length() - 2)) { 282 return (true); 283 } else if ('/' == requestPath.charAt(testPath.length() - 2)) { 284 return (true); 285 } 286 } 287 return (false); 288 } 289 290 if (testPath.startsWith("*.")) { 292 int slash = requestPath.lastIndexOf('/'); 293 int period = requestPath.lastIndexOf('.'); 294 if ((slash >= 0) && (period > slash) 295 && (period != requestPath.length() - 1) 296 && ((requestPath.length() - period) 297 == (testPath.length() - 1))) { 298 return (testPath.regionMatches(2, requestPath, period + 1, 299 testPath.length() - 2)); 300 } 301 } 302 303 return (false); 306 } 307 308 309 317 private boolean matchFiltersServlet(FilterMap filterMap, 318 String servletName) { 319 320 if (servletName == null) { 321 return (false); 322 } else { 323 String [] servletNames = filterMap.getServletNames(); 324 for (int i = 0; i < servletNames.length; i++) { 325 if (servletName.equals(servletNames[i])) { 326 return (true); 327 } 328 } 329 return false; 330 } 331 332 } 333 334 335 339 private boolean matchDispatcher(FilterMap filterMap, int dispatcher) { 340 switch (dispatcher) { 341 case FORWARD : { 342 if (filterMap.getDispatcherMapping() == FilterMap.FORWARD || 343 filterMap.getDispatcherMapping() == FilterMap.FORWARD_ERROR || 344 filterMap.getDispatcherMapping() == FilterMap.INCLUDE_FORWARD || 345 filterMap.getDispatcherMapping() == FilterMap.INCLUDE_ERROR_FORWARD || 346 filterMap.getDispatcherMapping() == FilterMap.REQUEST_FORWARD || 347 filterMap.getDispatcherMapping() == FilterMap.REQUEST_ERROR_FORWARD || 348 filterMap.getDispatcherMapping() == FilterMap.REQUEST_ERROR_FORWARD_INCLUDE || 349 filterMap.getDispatcherMapping() == FilterMap.REQUEST_FORWARD_INCLUDE) { 350 return true; 351 } 352 break; 353 } 354 case INCLUDE : { 355 if (filterMap.getDispatcherMapping() == FilterMap.INCLUDE || 356 filterMap.getDispatcherMapping() == FilterMap.INCLUDE_ERROR || 357 filterMap.getDispatcherMapping() == FilterMap.INCLUDE_FORWARD || 358 filterMap.getDispatcherMapping() == FilterMap.INCLUDE_ERROR_FORWARD || 359 filterMap.getDispatcherMapping() == FilterMap.REQUEST_INCLUDE || 360 filterMap.getDispatcherMapping() == FilterMap.REQUEST_ERROR_INCLUDE || 361 filterMap.getDispatcherMapping() == FilterMap.REQUEST_ERROR_FORWARD_INCLUDE || 362 filterMap.getDispatcherMapping() == FilterMap.REQUEST_FORWARD_INCLUDE) { 363 return true; 364 } 365 break; 366 } 367 case REQUEST : { 368 if (filterMap.getDispatcherMapping() == FilterMap.REQUEST || 369 filterMap.getDispatcherMapping() == FilterMap.REQUEST_ERROR || 370 filterMap.getDispatcherMapping() == FilterMap.REQUEST_INCLUDE || 371 filterMap.getDispatcherMapping() == FilterMap.REQUEST_ERROR_INCLUDE || 372 filterMap.getDispatcherMapping() == FilterMap.REQUEST_FORWARD || 373 filterMap.getDispatcherMapping() == FilterMap.REQUEST_ERROR_FORWARD || 374 filterMap.getDispatcherMapping() == FilterMap.REQUEST_FORWARD_INCLUDE || 375 filterMap.getDispatcherMapping() == FilterMap.REQUEST_ERROR_FORWARD_INCLUDE) { 376 return true; 377 } 378 break; 379 } 380 case ERROR : { 381 if (filterMap.getDispatcherMapping() == FilterMap.ERROR || 382 filterMap.getDispatcherMapping() == FilterMap.FORWARD_ERROR || 383 filterMap.getDispatcherMapping() == FilterMap.INCLUDE_ERROR || 384 filterMap.getDispatcherMapping() == FilterMap.INCLUDE_ERROR_FORWARD || 385 filterMap.getDispatcherMapping() == FilterMap.REQUEST_ERROR || 386 filterMap.getDispatcherMapping() == FilterMap.REQUEST_ERROR_FORWARD || 387 filterMap.getDispatcherMapping() == FilterMap.REQUEST_ERROR_FORWARD_INCLUDE || 388 filterMap.getDispatcherMapping() == FilterMap.REQUEST_ERROR_INCLUDE) { 389 return true; 390 } 391 break; 392 } 393 } 394 return false; 395 } 396 397 398 } 399 | Popular Tags |