Spring Filter

org.springframework.web.filter.CompositeFilter 

public class CompositeFilter implements Filter {

	private List<? extends Filter> filters = new ArrayList<Filter>();

	public void setFilters(List<? extends Filter> filters) {
		this.filters = new ArrayList<Filter>(filters);
	}

	/**
	 * Clean up all the filters supplied, calling each one's destroy method in turn, but in reverse order.
	 * 
	 * @see Filter#init(FilterConfig)
	 */
	public void destroy() {
		for (int i = filters.size(); i-- > 0;) {
			Filter filter = filters.get(i);
			filter.destroy();
		}
	}

	/**
	 * Initialize all the filters, calling each one's init method in turn in the order supplied.
	 * 
	 * @see Filter#init(FilterConfig)
	 */
	public void init(FilterConfig config) throws ServletException {
		for (Filter filter : filters) {
			filter.init(config);
		}
	}

	/**
	 * Forms a temporary chain from the list of delegate filters supplied ({@link #setFilters(List)}) and executes them
	 * in order. Each filter delegates to the next one in the list, achieving the normal behaviour of a
	 * {@link FilterChain}, despite the fact that this is a {@link Filter}.
	 * 
	 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
	 */
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
			ServletException {
		new VirtualFilterChain(chain, filters).doFilter(request, response);
	}

	private static class VirtualFilterChain implements FilterChain {
		private final FilterChain originalChain;
		private final List<? extends Filter> additionalFilters;
		private int currentPosition = 0;

		private VirtualFilterChain(FilterChain chain, List<? extends Filter> additionalFilters) {
			this.originalChain = chain;
			this.additionalFilters = additionalFilters;
		}

		public void doFilter(final ServletRequest request, final ServletResponse response) throws IOException,
				ServletException {
			if (currentPosition == additionalFilters.size()) {
				originalChain.doFilter(request, response);
			} else {
				currentPosition++;
				Filter nextFilter = additionalFilters.get(currentPosition - 1);
				nextFilter.doFilter(request, response, this);
			}
		}

	}

}

首尔这个CompositeFilter 自己也是一个Filter,然后自己包含了一个


private List<? extends Filter> filters = new ArrayList<Filter>();//来存储全部的Filter..

其实 ,我觉得 它应该叫 CompositeFilterChain 的

org.springframework.web.filter.CompositeFilter 在自己的doFilter中 调用了自己内部类VirtualFilterChain的doFilter遍历所有的filter,传递到VirtualFilterChain参数中的chain是tomcat上下文的chain

org.springframework.web.filter.CompositeFilter 在自己的doFilter中 调用了自己内部类VirtualFilterChain的doFilter遍历所有的filter,传递到VirtualFilterChain参数中的chain是tomcat上下文的chain


org.springframework.web.filter.OncePerRequestFilter


public final void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain)
			throws ServletException, IOException {

		if (!(request instanceof HttpServletRequest) || !(response instanceof HttpServletResponse)) {
			throw new ServletException("OncePerRequestFilter just supports HTTP requests");
		}
		HttpServletRequest httpRequest = (HttpServletRequest) request;
		HttpServletResponse httpResponse = (HttpServletResponse) response;

		String alreadyFilteredAttributeName = getAlreadyFilteredAttributeName();
		boolean hasAlreadyFilteredAttribute = request.getAttribute(alreadyFilteredAttributeName) != null;

		if (hasAlreadyFilteredAttribute || skipDispatch(httpRequest) || shouldNotFilter(httpRequest)) {

			// Proceed without invoking this filter...
			filterChain.doFilter(request, response);
		}
		else {
			// Do invoke this filter...
			request.setAttribute(alreadyFilteredAttributeName, Boolean.TRUE);
			try {
				doFilterInternal(httpRequest, httpResponse, filterChain);
			}
			finally {
				// Remove the "already filtered" request attribute for this request.
				request.removeAttribute(alreadyFilteredAttributeName);
			}
		}
	}

这个类中 通过 模板方法 设计模式 声明了  doFilterInternal,抽象方法,下面就是这个类的子类

Spring Filter

图中有一个抽象的Filter , org.springframework.web.filter.AbstractRequestLoggingFilter


protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
			throws ServletException, IOException {

		boolean isFirstRequest = !isAsyncDispatch(request);

		if (isIncludePayload()) {
			if (isFirstRequest) {
				request = new RequestCachingRequestWrapper(request);
			}
		}

		if (isFirstRequest) {
			beforeRequest(request, getBeforeMessage(request));
		}
		try {
			filterChain.doFilter(request, response);
		}
		finally {
			if (!isAsyncStarted(request)) {
				afterRequest(request, getAfterMessage(request));
			}
		}
	}



org.springframework.web.filter.Log4jNestedDiagnosticContextFilter  这个类呢 是 上图中 

org.springframework.web.filter.AbstractRequestLoggingFilter  的子类,

AbstractRequestLoggingFilter  中自己搞了如下俩个抽象方法 ,方便在AbstractRequestLoggingFilter的doFilterInternal 方法前后进行 一些修饰 

protected abstract void beforeRequest(HttpServletRequest request, String message);
protected abstract void afterRequest(HttpServletRequest request, String message);

所有集成了这个AbstractRequestLoggingFilter 的子类如下, 以一个子类进行分析,就看 

org.springframework.web.filter.CommonsRequestLoggingFilter 吧

Spring Filter 


你可能感兴趣的:(Spring Filter)