001: /* ***** BEGIN LICENSE BLOCK *****
002: * Version: MPL 1.1
003: * The contents of this file are subject to the Mozilla Public License Version
004: * 1.1 (the "License"); you may not use this file except in compliance with
005: * the License. You may obtain a copy of the License at
006: * http://www.mozilla.org/MPL/
007: *
008: * Software distributed under the License is distributed on an "AS IS" basis,
009: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
010: * for the specific language governing rights and limitations under the
011: * License.
012: *
013: * The Original Code is Riot.
014: *
015: * The Initial Developer of the Original Code is
016: * Neteye GmbH.
017: * Portions created by the Initial Developer are Copyright (C) 2006
018: * the Initial Developer. All Rights Reserved.
019: *
020: * Contributor(s):
021: * Felix Gnass [fgnass at neteye dot de]
022: *
023: * ***** END LICENSE BLOCK ***** */
024: package org.riotfamily.common.web.filter;
025:
026: import java.io.IOException;
027:
028: import javax.servlet.ServletContext;
029: import javax.servlet.ServletException;
030: import javax.servlet.http.HttpServletRequest;
031: import javax.servlet.http.HttpServletResponse;
032:
033: import org.apache.commons.logging.Log;
034: import org.apache.commons.logging.LogFactory;
035: import org.springframework.beans.FatalBeanException;
036: import org.springframework.beans.factory.DisposableBean;
037: import org.springframework.beans.factory.InitializingBean;
038: import org.springframework.core.Ordered;
039: import org.springframework.util.Assert;
040: import org.springframework.web.context.ServletContextAware;
041:
042: /**
043: * Class that can be plugged into a {@link PluginFilter} to filter request
044: * and response objects like a {@link javax.servlet.Filter servlet filter}.
045: * <p>
046: * Note that plugins are only invoked once per request, so they won't be
047: * called for included or forwarded requests.
048: * </p>
049: * @author Felix Gnass [fgnass at neteye dot de]
050: * @since 6.4
051: */
052: public abstract class FilterPlugin implements ServletContextAware,
053: InitializingBean, DisposableBean, Ordered {
054:
055: private static Log log = LogFactory.getLog(FilterPlugin.class);
056:
057: private ServletContext servletContext;
058:
059: private PluginFilter filter;
060:
061: private String filterName;
062:
063: private int order = Integer.MAX_VALUE;
064:
065: private boolean ignoreFilterNotPresent;
066:
067: /**
068: * Sets the name of the filter where the plugin should be registered.
069: */
070: public void setFilterName(String filterName) {
071: this .filterName = filterName;
072: }
073:
074: /**
075: * Sets whether errors caused by a missing filter should be ignored.
076: */
077: public void setIgnoreFilterNotPresent(boolean ignoreFilterNotPresent) {
078: this .ignoreFilterNotPresent = ignoreFilterNotPresent;
079: }
080:
081: /**
082: * Subclasses must implement this method to filter the request/response.
083: * The contract is the same as for {@link javax.servlet.FilterFilter#doFilter}.
084: */
085: public abstract void doFilter(HttpServletRequest request,
086: HttpServletResponse response, PluginChain pluginChain)
087: throws IOException, ServletException;
088:
089: /**
090: * Returns the order in which this plugin will be invoked.
091: */
092: public int getOrder() {
093: return order;
094: }
095:
096: /**
097: * Sets the order in which this plugin will be invoked. The default
098: * value is <code>Integer.MAX_VALE</code>.
099: */
100: public void setOrder(int order) {
101: this .order = order;
102: }
103:
104: public final void setServletContext(ServletContext servletContext) {
105: this .servletContext = servletContext;
106: }
107:
108: /**
109: * Returns the ServletContext.
110: */
111: protected final ServletContext 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 {
120: Assert.notNull(filterName, "A filterName must be set.");
121: filter = PluginFilter.getInstance(servletContext, filterName);
122: if (filter == null) {
123: if (ignoreFilterNotPresent) {
124: log
125: .warn("Failed to register FilterPlugin because no filter "
126: + "named "
127: + filterName
128: + " is defined in web.xml");
129:
130: return;
131: } else {
132: throw new FatalBeanException(
133: "No such filter defined in web.xml: "
134: + filterName);
135: }
136: }
137: initPlugin();
138: filter.addPlugin(this );
139: }
140:
141: /**
142: * Subclasses may override this to perform custom initialization.
143: * All properties of this plugin will have been set before this method
144: * is invoked. The default implementation is empty.
145: */
146: protected void initPlugin() {
147: }
148:
149: /**
150: * Removes the plugin from the filter and invokes {@link #destroyPlugin()}.
151: */
152: public final void destroy() throws Exception {
153: filter.removePlugin(this );
154: destroyPlugin();
155: }
156:
157: /**
158: * Subclasses may override this to perform custom cleanup operations
159: * when the bean is destroyed. The default implementation is empty.
160: */
161: protected void destroyPlugin() {
162: }
163:
164: }
|