1 package info.magnolia.cms.filters; 2 3 import info.magnolia.cms.beans.config.FilterManager; 4 5 import java.io.IOException ; 6 7 import javax.servlet.Filter ; 8 import javax.servlet.FilterChain ; 9 import javax.servlet.FilterConfig ; 10 import javax.servlet.ServletException ; 11 import javax.servlet.ServletRequest ; 12 import javax.servlet.ServletResponse ; 13 import javax.servlet.http.HttpServletRequest ; 14 15 import org.slf4j.Logger; 16 import org.slf4j.LoggerFactory; 17 18 19 24 public class MagnoliaManagedFilter implements Filter { 25 26 29 private static Logger log = LoggerFactory.getLogger(ContentTypeFilter.class); 30 31 34 private FilterConfig filterConfig; 35 36 39 private FilterManager filterManager = FilterManager.getInstance(); 40 41 44 public void destroy() { 45 for (int j = 0; j < filterManager.getFilters().length; j++) { 46 Filter filter = filterManager.getFilters()[j]; 47 filter.destroy(); 48 49 } 50 } 51 52 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException , 53 ServletException { 54 FilterChain fullchain = new CustomFilterChain(chain, filterManager.getFilters()); 55 56 if (log.isDebugEnabled()) { 57 String pathInfo = ((HttpServletRequest ) request).getPathInfo(); 58 String requestURI = ((HttpServletRequest ) request).getRequestURI(); 59 60 if (pathInfo == null || !requestURI.startsWith("/.")) { 61 log.debug("handling: {} path info: {}", requestURI, pathInfo); 62 } 63 } 64 65 fullchain.doFilter(request, response); 66 } 67 68 public void init(FilterConfig filterConfig) throws ServletException { 69 this.filterConfig = filterConfig; 70 filterManager.initFilters(filterConfig); 71 } 72 73 public class CustomFilterChain implements FilterChain { 74 75 private Filter [] filters; 76 77 private int position; 78 79 private FilterChain originalChain; 80 81 public CustomFilterChain(FilterChain originalChain, Filter [] filters) { 82 this.filters = filters; 83 this.originalChain = originalChain; 84 } 85 86 public void doFilter(ServletRequest request, ServletResponse response) throws IOException , ServletException { 87 if (position == filters.length) { 88 originalChain.doFilter(request, response); 89 } 90 else { 91 position++; 92 filters[position - 1].doFilter(request, response, this); 93 } 94 } 95 96 public void reset() { 97 position = 0; 98 } 99 } 100 101 } 102 | Popular Tags |