1 源码解析
1.1 源码解析1(Servlet过滤器的应用)
1 源码解析
1.1 源码解析1(Servlet过滤器的应用)
重点讲解:
1 filterchain中有很多的filter,filter按照一定的顺序组装成一个链条,可以理解成责任链模式。
2 每次filter 执行dofilter方法的时候,都会调到filterchain,filterchain又会执行下一个filter的dofiltet方法,这样实现了循环调用dofilter方法(见filterchain和filter实现类标红部分)。
Filter:
// // Source code recreated from a .class file by IntelliJ IDEA // (powered by Fernflower decompiler) // package javax.servlet; import java.io.IOException; public interface Filter { void init(FilterConfig var1) throws ServletException; void doFilter(ServletRequest var1, ServletResponse var2, FilterChain var3) throws IOException, ServletException; void destroy(); }
FilterChain:
// // Source code recreated from a .class file by IntelliJ IDEA // (powered by Fernflower decompiler) // package javax.servlet; import java.io.IOException; public interface FilterChain { void doFilter(ServletRequest var1, ServletResponse var2) throws IOException, ServletException; }
Filter实现类:
// // Source code recreated from a .class file by IntelliJ IDEA // (powered by Fernflower decompiler) // package ch.qos.logback.classic.selector.servlet; import ch.qos.logback.classic.LoggerContext; import ch.qos.logback.classic.selector.ContextJNDISelector; import ch.qos.logback.classic.selector.ContextSelector; import ch.qos.logback.classic.util.ContextSelectorStaticBinder; import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import org.slf4j.LoggerFactory; public class LoggerContextFilter implements Filter { public LoggerContextFilter() { } public void destroy() { } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { LoggerContext lc = (LoggerContext)LoggerFactory.getILoggerFactory(); ContextSelector selector = ContextSelectorStaticBinder.getSingleton().getContextSelector(); ContextJNDISelector sel = null; if (selector instanceof ContextJNDISelector) { sel = (ContextJNDISelector)selector; sel.setLocalContext(lc); } try { chain.doFilter(request, response); } finally { if (sel != null) { sel.removeLocalContext(); } } } public void init(FilterConfig arg0) throws ServletException { } }
FilterChain实现类:
// // Source code recreated from a .class file by IntelliJ IDEA // (powered by Fernflower decompiler) // package org.springframework.mock.web; import java.io.IOException; import java.util.Arrays; import java.util.Collections; import java.util.Iterator; import java.util.List; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.Servlet; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import org.springframework.util.Assert; import org.springframework.util.ObjectUtils; public class MockFilterChain implements FilterChain { private ServletRequest request; private ServletResponse response; private final List<Filter> filters; private Iterator<Filter> iterator; public MockFilterChain() { this.filters = Collections.emptyList(); } public MockFilterChain(Servlet servlet) { this.filters = initFilterList(servlet); } public MockFilterChain(Servlet servlet, Filter... filters) { Assert.notNull(filters, "filters cannot be null"); Assert.noNullElements(filters, "filters cannot contain null values"); this.filters = initFilterList(servlet, filters); } private static List<Filter> initFilterList(Servlet servlet, Filter... filters) { Filter[] allFilters = (Filter[])ObjectUtils.addObjectToArray(filters, new MockFilterChain.ServletFilterProxy(servlet)); return Arrays.asList(allFilters); } public ServletRequest getRequest() { return this.request; } public ServletResponse getResponse() { return this.response; } public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException { Assert.notNull(request, "Request must not be null"); Assert.notNull(response, "Response must not be null"); if (this.request != null) { throw new IllegalStateException("This FilterChain has already been called!"); } else { if (this.iterator == null) { this.iterator = this.filters.iterator(); } if (this.iterator.hasNext()) { Filter nextFilter = (Filter)this.iterator.next(); nextFilter.doFilter(request, response, this); } this.request = request; this.response = response; } } public void reset() { this.request = null; this.response = null; this.iterator = null; } private static class ServletFilterProxy implements Filter { private final Servlet delegateServlet; private ServletFilterProxy(Servlet servlet) { Assert.notNull(servlet, "servlet cannot be null"); this.delegateServlet = servlet; } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { this.delegateServlet.service(request, response); } public void init(FilterConfig filterConfig) throws ServletException { } public void destroy() { } public String toString() { return this.delegateServlet.toString(); } } }
诸葛