KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > cms > filters > MagnoliaManagedFilter


1 package info.magnolia.cms.filters;
2
3 import info.magnolia.cms.beans.config.FilterManager;
4
5 import java.io.IOException JavaDoc;
6
7 import javax.servlet.Filter JavaDoc;
8 import javax.servlet.FilterChain JavaDoc;
9 import javax.servlet.FilterConfig JavaDoc;
10 import javax.servlet.ServletException JavaDoc;
11 import javax.servlet.ServletRequest JavaDoc;
12 import javax.servlet.ServletResponse JavaDoc;
13 import javax.servlet.http.HttpServletRequest JavaDoc;
14
15 import org.slf4j.Logger;
16 import org.slf4j.LoggerFactory;
17
18
19 /**
20  * A single filter that in turn executed a chain of other filters not configured in web.xml.
21  * @author fgiust
22  * @version $Revision: 6641 $ ($Author: fgiust $)
23  */

24 public class MagnoliaManagedFilter implements Filter JavaDoc {
25
26     /**
27      * Logger.
28      */

29     private static Logger log = LoggerFactory.getLogger(ContentTypeFilter.class);
30
31     /**
32      * FilterConfig. ServletContext will be passed over to managed filters.
33      */

34     private FilterConfig JavaDoc filterConfig;
35
36     /**
37      * FilterManager instance. Holds filter configuration.
38      */

39     private FilterManager filterManager = FilterManager.getInstance();
40
41     /**
42      * @see javax.servlet.Filter#destroy()
43      */

44     public void destroy() {
45         for (int j = 0; j < filterManager.getFilters().length; j++) {
46             Filter JavaDoc filter = filterManager.getFilters()[j];
47             filter.destroy();
48
49         }
50     }
51
52     public void doFilter(ServletRequest JavaDoc request, ServletResponse JavaDoc response, FilterChain JavaDoc chain) throws IOException JavaDoc,
53         ServletException JavaDoc {
54         FilterChain JavaDoc fullchain = new CustomFilterChain(chain, filterManager.getFilters());
55
56         if (log.isDebugEnabled()) {
57             String JavaDoc pathInfo = ((HttpServletRequest JavaDoc) request).getPathInfo();
58             String JavaDoc requestURI = ((HttpServletRequest JavaDoc) 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 JavaDoc filterConfig) throws ServletException JavaDoc {
69         this.filterConfig = filterConfig;
70         filterManager.initFilters(filterConfig);
71     }
72
73     public class CustomFilterChain implements FilterChain JavaDoc {
74
75         private Filter JavaDoc[] filters;
76
77         private int position;
78
79         private FilterChain JavaDoc originalChain;
80
81         public CustomFilterChain(FilterChain JavaDoc originalChain, Filter JavaDoc[] filters) {
82             this.filters = filters;
83             this.originalChain = originalChain;
84         }
85
86         public void doFilter(ServletRequest JavaDoc request, ServletResponse JavaDoc response) throws IOException JavaDoc, ServletException JavaDoc {
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