将几个Filter整合成一个Filter

 CAS-Client的客户端配置还是多Filter,这个对于接入系统比较繁琐,我试着将这几个Filter合并成一个。

 

FilterChainProxy :其职责是给web.xml使用,接受其他需要合并的Filter的参数,初始化其他Filter.

FilterInvocation:doFilter的参数的封装,取得路径信息等。

VirtualFilterChain:实现FilterChain,

代码如下:

FilterChainProxy

public class FilterChainProxy extends AbstractConfigurationFilter {

	Logger logger = Logger.getLogger(FilterChainProxy.class);

	private Filter[] filters;

	/* (non-Javadoc)
	 * @see javax.servlet.Filter#destroy()
	 */
	public void destroy() {

		for (int i = 0; i < filters.length; i++) {
			if (filters[i] != null) {
				if (logger.isDebugEnabled()) {
					logger.debug("Destroying Filter defined in ApplicationContext: '" + filters[i].toString() + "'");
				}

				filters[i].destroy();
			}
		}

	}

	/* (non-Javadoc)
	 * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)
	 */
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
			ServletException {
		 FilterInvocation fi = new FilterInvocation(request, response, chain);
		
		if (filters.length == 0) {
			if (logger.isDebugEnabled()) {
				logger.debug(fi.getRequestUrl()+" has an empty filter list");
			}
			chain.doFilter(request, response);

			return;
		}
		
		VirtualFilterChain virtualFilterChain = new VirtualFilterChain(fi, filters);
		virtualFilterChain.doFilter(fi.getRequest(), fi.getResponse());

	}

	/* (non-Javadoc)
	 * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
	 */
	public void init(FilterConfig filterConfig) throws ServletException {
		filters = obtainAllDefinedFilters();

		for (int i = 0; i < filters.length; i++) {
			if (filters[i] != null) {
				if (logger.isDebugEnabled()) {
					logger.debug("Initializing Filter defined in ApplicationContext: '" + filters[i].toString() + "'");
				}
				filters[i].init(filterConfig);
			}
		}

	}
	/**
	 * 初始化单点登录需要的过滤器
	 * @return
	 */
	private Filter[] obtainAllDefinedFilters() {

		Set<Filter> list = new LinkedHashSet<Filter>();
		// SingleSignOutFilter
		list.add(new SingleSignOutFilter());
		// AuthenticationFilter
		list.add(new AuthenticationFilter());
		// Cas20ProxyReceivingTicketValidationFilter
		list.add(new Cas20ProxyReceivingTicketValidationFilter());
		// AssertionThreadLocalFilter
		list.add(new AssertionThreadLocalFilter());
		// HttpServletRequestWrapperFilter
		list.add(new HttpServletRequestWrapperFilter());

		return (Filter[]) list.toArray(new Filter[0]);
	}

VirtualFilterChain

private class VirtualFilterChain implements FilterChain {
		private FilterInvocation fi;
		private Filter[] additionalFilters;
		private int currentPosition = 0;

		public VirtualFilterChain(FilterInvocation filterInvocation, Filter[] additionalFilters) {
			  this.fi = filterInvocation;
	          this.additionalFilters = additionalFilters;
		}

		public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException {
			if (currentPosition == additionalFilters.length) {
				if (logger.isDebugEnabled()) {
					logger.debug(fi.getRequestUrl()
							+ " reached end of additional filter chain; proceeding with original chain");
				}

				fi.getChain().doFilter(request, response);
			} else {
				currentPosition++;

				if (logger.isDebugEnabled()) {
					logger.debug(fi.getRequestUrl() + " at position " + currentPosition + " of "
							+ additionalFilters.length + " in additional filter chain; firing Filter: '"
							+ additionalFilters[currentPosition - 1] + "'");
				}

				additionalFilters[currentPosition - 1].doFilter(request, response, this);
			}
		}

	}


FilterInvocation

public class FilterInvocation {
	private FilterChain chain;
	private ServletRequest request;
	private ServletResponse response;

	public FilterInvocation(ServletRequest request, ServletResponse response, FilterChain chain) {
		if ((request == null) || (response == null) || (chain == null)) {
			throw new IllegalArgumentException("Cannot pass null values to constructor");
		}

		if (!(request instanceof HttpServletRequest)) {
			throw new IllegalArgumentException("Can only process HttpServletRequest");
		}

		if (!(response instanceof HttpServletResponse)) {
			throw new IllegalArgumentException("Can only process HttpServletResponse");
		}

		this.request = request;
		this.response = response;
		this.chain = chain;
	}

	public FilterChain getChain() {
		return chain;
	}

	/**
	 * Indicates the URL that the user agent used for this request.<P>The returned URL does <b>not</b> reflect
	 * the port number determined from a {@link org.acegisecurity.util.PortResolver}.</p>
	 *
	 * @return the full URL of this request
	 */
	public String getFullRequestUrl() {
		return UrlUtils.getFullRequestUrl(this);
	}

	public HttpServletRequest getHttpRequest() {
		return (HttpServletRequest) request;
	}

	public HttpServletResponse getHttpResponse() {
		return (HttpServletResponse) response;
	}

	public ServletRequest getRequest() {
		return request;
	}

	/**
	 * Obtains the web application-specific fragment of the URL.
	 *
	 * @return the URL, excluding any server name, context path or servlet path
	 */
	public String getRequestUrl() {
		return UrlUtils.getRequestUrl(this);
	}

	public ServletResponse getResponse() {
		return response;
	}

	public String toString() {
		return "FilterInvocation: URL: " + getRequestUrl();
	}
}



 

你可能感兴趣的:(String,filter,null,url,Constructor,firing)