1 24 package org.riotfamily.common.web.filter; 25 26 import java.io.IOException ; 27 28 import javax.servlet.ServletContext ; 29 import javax.servlet.ServletException ; 30 import javax.servlet.http.HttpServletRequest ; 31 import javax.servlet.http.HttpServletResponse ; 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 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 servletContext; 58 59 private PluginFilter filter; 60 61 private String filterName; 62 63 private int order = Integer.MAX_VALUE; 64 65 private boolean ignoreFilterNotPresent; 66 67 70 public void setFilterName(String filterName) { 71 this.filterName = filterName; 72 } 73 74 77 public void setIgnoreFilterNotPresent(boolean ignoreFilterNotPresent) { 78 this.ignoreFilterNotPresent = ignoreFilterNotPresent; 79 } 80 81 85 public abstract void doFilter(HttpServletRequest request, 86 HttpServletResponse response, PluginChain pluginChain) 87 throws IOException , ServletException ; 88 89 92 public int getOrder() { 93 return order; 94 } 95 96 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 111 protected final ServletContext getServletContext() { 112 return servletContext; 113 } 114 115 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.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 143 protected void initPlugin() { 144 } 145 146 149 public final void destroy() throws Exception { 150 filter.removePlugin(this); 151 destroyPlugin(); 152 } 153 154 158 protected void destroyPlugin() { 159 } 160 161 } 162 | Popular Tags |