KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > common > web > filter > PluginFilter


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1
3  * The contents of this file are subject to the Mozilla Public License Version
4  * 1.1 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  * http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the
11  * License.
12  *
13  * The Original Code is Riot.
14  *
15  * The Initial Developer of the Original Code is
16  * Neteye GmbH.
17  * Portions created by the Initial Developer are Copyright (C) 2006
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * Felix Gnass [fgnass at neteye dot de]
22  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.common.web.filter;
25
26 import java.io.IOException JavaDoc;
27 import java.util.Arrays JavaDoc;
28
29 import javax.servlet.FilterChain JavaDoc;
30 import javax.servlet.ServletContext JavaDoc;
31 import javax.servlet.ServletException JavaDoc;
32 import javax.servlet.http.HttpServletRequest JavaDoc;
33 import javax.servlet.http.HttpServletResponse JavaDoc;
34
35 import org.springframework.core.OrderComparator;
36 import org.springframework.util.AntPathMatcher;
37 import org.springframework.web.filter.OncePerRequestFilter;
38 import org.springframework.web.util.UrlPathHelper;
39
40 /**
41  * Servlet filter delegates the filtering to {@link FilterPlugin} beans.
42  * <p>
43  * In contrast to Spring's {@link org.springframework.web.filter.DelegatingFilterProxy}
44  * multiple targets are supported which are processed as a chain. Additionally
45  * the beans can be located in any BeanFactory that is aware of the
46  * ServletContext, not only the root ApplicationContext.
47  * <p>
48  * Riot uses this mechanism to allow modules to register servlet filters without
49  * having to modify the web.xml deployment descriptor.
50  *
51  * @see FilterPlugin
52  * @author Felix Gnass [fgnass at neteye dot de]
53  * @since 6.4
54  */

55 public final class PluginFilter extends OncePerRequestFilter {
56
57     private static final String JavaDoc ATTRIBUTE_PREFIX =
58             PluginFilter.class.getName() + '.';
59     
60     private String JavaDoc[] exclude;
61     
62     private UrlPathHelper urlPathHelper = new UrlPathHelper();
63     
64     private AntPathMatcher pathMatcher = new AntPathMatcher();
65     
66     private OrderComparator orderComparator = new OrderComparator();
67     
68     private FilterPlugin[] plugins = new FilterPlugin[0];
69     
70     /**
71      * Sets Ant-style path patterns that should not be filtered.
72      */

73     public void setExclude(String JavaDoc[] exclude) {
74         this.exclude = exclude;
75     }
76     
77     protected void initFilterBean() throws ServletException JavaDoc {
78         getServletContext().setAttribute(
79                 ATTRIBUTE_PREFIX + getFilterName(), this);
80     }
81     
82     synchronized void addPlugin(FilterPlugin plugin) {
83         int n = plugins.length;
84         FilterPlugin[] newPlugins = new FilterPlugin[n + 1];
85         System.arraycopy(plugins, 0, newPlugins, 0, n);
86         newPlugins[n++] = plugin;
87         Arrays.sort(newPlugins, orderComparator);
88         plugins = newPlugins;
89     }
90     
91     synchronized void removePlugin(FilterPlugin plugin) {
92         int n = plugins.length;
93         FilterPlugin[] newPlugins = new FilterPlugin[n - 1];
94         int j = 0;
95         for (int i = 0; i < n; i++) {
96             if (plugins[i] != plugin) {
97                 newPlugins[j++] = plugins[i];
98             }
99         }
100         plugins = newPlugins;
101     }
102
103     /**
104      * Skips the filter if the requested path matches one of the Ant-style
105      * pattern set via {@link #setExclude}.
106      */

107     protected boolean shouldNotFilter(HttpServletRequest JavaDoc request) {
108         if (exclude != null) {
109             String JavaDoc path = urlPathHelper.getPathWithinApplication(request);
110             for (int i = 0; i < exclude.length; i++) {
111                 if (pathMatcher.match(exclude[i], path)) {
112                     return true;
113                 }
114             }
115         }
116         return false;
117     }
118     
119     protected void doFilterInternal(HttpServletRequest JavaDoc request,
120             HttpServletResponse JavaDoc response, FilterChain JavaDoc filterChain)
121             throws ServletException JavaDoc, IOException JavaDoc {
122         
123         new PluginChain(filterChain, plugins).doFilter(request, response);
124     }
125     
126     static PluginFilter getInstance(ServletContext JavaDoc servletContext,
127             String JavaDoc filterName) {
128         
129         return (PluginFilter) servletContext.getAttribute(
130                 ATTRIBUTE_PREFIX + filterName);
131     }
132     
133 }
134
Popular Tags