// get the dispatcher type DispatcherTypedispatcher=null; if (request.getAttribute(Globals.DISPATCHER_TYPE_ATTR) != null) { dispatcher = (DispatcherType) request.getAttribute(Globals.DISPATCHER_TYPE_ATTR); } StringrequestPath=null; Objectattribute= request.getAttribute( Globals.DISPATCHER_REQUEST_PATH_ATTR);
if (attribute != null){ requestPath = attribute.toString(); }
// If there is no servlet to execute, return null if (servlet == null) returnnull;
// Acquire the filter mappings for this Context StandardContextcontext= (StandardContext) wrapper.getParent(); FilterMap filterMaps[] = context.findFilterMaps();
// If there are no filter mappings, we are done if ((filterMaps == null) || (filterMaps.length == 0)) return filterChain;
// Acquire the information we will need to match filter mappings StringservletName= wrapper.getName();
// Add the relevant path-mapped filters to this filter chain for (FilterMap filterMap : filterMaps) { if (!matchDispatcher(filterMap, dispatcher)) { continue; } if (!matchFiltersURL(filterMap, requestPath)) continue; ApplicationFilterConfigfilterConfig= (ApplicationFilterConfig) context.findFilterConfig(filterMap.getFilterName()); if (filterConfig == null) { // FIXME - log configuration problem continue; } booleanisCometFilter=false; if (comet) { try { isCometFilter = filterConfig.getFilter() instanceof CometFilter; } catch (Exception e) { // Note: The try catch is there because getFilter has a lot of // declared exceptions. However, the filter is allocated much // earlier Throwablet= ExceptionUtils.unwrapInvocationTargetException(e); ExceptionUtils.handleThrowable(t); } if (isCometFilter) { filterChain.addFilter(filterConfig); } } else { filterChain.addFilter(filterConfig); } }
// Add filters that match on servlet name second for (FilterMap filterMap : filterMaps) { if (!matchDispatcher(filterMap, dispatcher)) { continue; } if (!matchFiltersServlet(filterMap, servletName)) continue; ApplicationFilterConfigfilterConfig= (ApplicationFilterConfig) context.findFilterConfig(filterMap.getFilterName()); if (filterConfig == null) { // FIXME - log configuration problem continue; } booleanisCometFilter=false; if (comet) { try { isCometFilter = filterConfig.getFilter() instanceof CometFilter; } catch (Exception e) { // Note: The try catch is there because getFilter has a lot of // declared exceptions. However, the filter is allocated much // earlier } if (isCometFilter) { filterChain.addFilter(filterConfig); } } else { filterChain.addFilter(filterConfig); } }
// Return the completed filter chain return filterChain;
/** * * This method will be used to set the current state of the FilterMap * representing the state of when filters should be applied. */ publicvoidsetDispatcher(String dispatcherString) { Stringdispatcher= dispatcherString.toUpperCase(Locale.ENGLISH);
if (dispatcher.equals(DispatcherType.FORWARD.name())) { // apply FORWARD to the global dispatcherMapping. dispatcherMapping |= FORWARD; } elseif (dispatcher.equals(DispatcherType.INCLUDE.name())) { // apply INCLUDE to the global dispatcherMapping. dispatcherMapping |= INCLUDE; } elseif (dispatcher.equals(DispatcherType.REQUEST.name())) { // apply REQUEST to the global dispatcherMapping. dispatcherMapping |= REQUEST; } elseif (dispatcher.equals(DispatcherType.ERROR.name())) { // apply ERROR to the global dispatcherMapping. dispatcherMapping |= ERROR; } elseif (dispatcher.equals(DispatcherType.ASYNC.name())) { // apply ERROR to the global dispatcherMapping. dispatcherMapping |= ASYNC; } }
StandardContext
org.apache.catalina.core.StandardContext 类是 Tomcat 中的一个核心组件,表示一个 Web 应用的上下文。它负责管理 Web 应用的所有组件,包括 Servlet、Filter 和 Listener 等。在 Filter 方法,StandardContext 通过 FilterDef 和 FilterMap 来管理过滤器的定义和映射。
/** * Add a filter definition to this Context. * * @param filterDef The filter definition to be added */ @Override publicvoidaddFilterDef(FilterDef filterDef) {
/** * Add a filter mapping to this Context before the mappings defined in the * deployment descriptor but after any other mappings added via this method. * * @param filterMap The filter mapping to be added * * @exception IllegalArgumentException if the specified filter name * does not match an existing filter definition, or the filter mapping * is malformed */ @Override publicvoidaddFilterMapBefore(FilterMap filterMap) { validateFilterMap(filterMap); // Add this filter mapping to our registered set filterMaps.addBefore(filterMap); fireContainerEvent("addFilterMap", filterMap); }