KickJava   Java API By Example, From Geeks To Geeks.

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


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
28 import javax.servlet.ServletContext JavaDoc;
29 import javax.servlet.ServletException JavaDoc;
30 import javax.servlet.http.HttpServletRequest JavaDoc;
31 import javax.servlet.http.HttpServletResponse JavaDoc;
32
33 import org.apache.commons.logging.Log;
34 import org.apache.commons.logging.LogFactory;
35 import org.springframework.beans.FatalBeanException;
36 import org.springframework.beans.factory.DisposableBean;
37 import org.springframework.beans.factory.InitializingBean;
38 import org.springframework.core.Ordered;
39 import org.springframework.util.Assert;
40 import org.springframework.web.context.ServletContextAware;
41
42 /**
43  * Class that can be plugged into a {@link PluginFilter} to filter request
44  * and response objects like a {@link javax.servlet.Filter servlet filter}.
45  * <p>
46  * Note that plugins are only invoked once per request, so they won't be
47  * called for included or forwarded requests.
48  * </p>
49  * @author Felix Gnass [fgnass at neteye dot de]
50  * @since 6.4
51  */

52 public abstract class FilterPlugin implements ServletContextAware,
53         InitializingBean, DisposableBean, Ordered {
54
55     private static Log log = LogFactory.getLog(FilterPlugin.class);
56     
57     private ServletContext JavaDoc servletContext;
58     
59     private PluginFilter filter;
60     
61     private String JavaDoc filterName;
62     
63     private int order = Integer.MAX_VALUE;
64     
65     private boolean ignoreFilterNotPresent;
66     
67     /**
68      * Sets the name of the filter where the plugin should be registered.
69      */

70     public void setFilterName(String JavaDoc filterName) {
71         this.filterName = filterName;
72     }
73
74     /**
75      * Sets whether errors caused by a missing filter should be ignored.
76      */

77     public void setIgnoreFilterNotPresent(boolean ignoreFilterNotPresent) {
78         this.ignoreFilterNotPresent = ignoreFilterNotPresent;
79     }
80     
81     /**
82      * Subclasses must implement this method to filter the request/response.
83      * The contract is the same as for {@link javax.servlet.FilterFilter#doFilter}.
84      */

85     public abstract void doFilter(HttpServletRequest JavaDoc request,
86             HttpServletResponse JavaDoc response, PluginChain pluginChain)
87             throws IOException JavaDoc, ServletException JavaDoc;
88     
89     /**
90      * Returns the order in which this plugin will be invoked.
91      */

92     public int getOrder() {
93         return order;
94     }
95     
96     /**
97      * Sets the order in which this plugin will be invoked. The default
98      * value is <code>Integer.MAX_VALE</code>.
99      */

100     public void setOrder(int order) {
101         this.order = order;
102     }
103     
104     public final void setServletContext(ServletContext JavaDoc servletContext) {
105         this.servletContext = servletContext;
106     }
107     
108     /**
109      * Returns the ServletContext.
110      */

111     protected final ServletContext JavaDoc getServletContext() {
112         return servletContext;
113     }
114     
115     /**
116      * Retrieves the {@link PluginFilter} with the specified name from the
117      * ServletContext and registers the plugin.
118      */

119     public final void afterPropertiesSet() throws Exception JavaDoc {
120         Assert.notNull(filterName, "A filterName must be set.");
121         filter = PluginFilter.getInstance(servletContext, filterName);
122         if (filter == null) {
123             if (ignoreFilterNotPresent) {
124                 log.warn("Failed to register FilterPlugin because no filter "
125                         + "named " + filterName + " is defined in web.xml");
126                 
127                 return;
128             }
129             else {
130                 throw new FatalBeanException(
131                         "No such filter defined in web.xml: " + filterName);
132             }
133         }
134         initPlugin();
135         filter.addPlugin(this);
136     }
137     
138     /**
139      * Subclasses may override this to perform custom initialization.
140      * All properties of this plugin will have been set before this method
141      * is invoked. The default implementation is empty.
142      */

143     protected void initPlugin() {
144     }
145
146     /**
147      * Removes the plugin from the filter and invokes {@link #destroyPlugin()}.
148      */

149     public final void destroy() throws Exception JavaDoc {
150         filter.removePlugin(this);
151         destroyPlugin();
152     }
153     
154     /**
155      * Subclasses may override this to perform custom cleanup operations
156      * when the bean is destroyed. The default implementation is empty.
157      */

158     protected void destroyPlugin() {
159     }
160
161 }
162
Popular Tags