| 1 | |
package org.restafarian.core.filters; |
| 2 | |
|
| 3 | |
import java.io.IOException; |
| 4 | |
|
| 5 | |
import javax.servlet.Filter; |
| 6 | |
import javax.servlet.FilterChain; |
| 7 | |
import javax.servlet.FilterConfig; |
| 8 | |
import javax.servlet.ServletContext; |
| 9 | |
import javax.servlet.ServletException; |
| 10 | |
import javax.servlet.ServletRequest; |
| 11 | |
import javax.servlet.ServletResponse; |
| 12 | |
import javax.servlet.http.HttpServletRequest; |
| 13 | |
import javax.servlet.http.HttpServletResponse; |
| 14 | |
|
| 15 | |
import org.apache.commons.configuration.Configuration; |
| 16 | |
import org.apache.commons.configuration.ConfigurationException; |
| 17 | |
import org.apache.commons.configuration.ConfigurationFactory; |
| 18 | |
import org.apache.commons.logging.Log; |
| 19 | |
import org.apache.commons.logging.LogFactory; |
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | 0 | public abstract class FilterBase implements Filter { |
| 25 | 0 | protected ServletContext context = null; |
| 26 | 0 | protected Log log = LogFactory.getLog(getClass()); |
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
public abstract void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws ServletException, IOException; |
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
protected void sendError(HttpServletRequest req, HttpServletResponse res, int errorCode, String errorMessage) throws IOException { |
| 48 | |
|
| 49 | 0 | if (log.isDebugEnabled()) { |
| 50 | 0 | log.debug("Sending error " + errorCode + "; message=" + errorMessage); |
| 51 | |
} |
| 52 | |
|
| 53 | |
|
| 54 | 0 | res.sendError(errorCode, errorMessage); |
| 55 | 0 | } |
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
public void init(FilterConfig config) { |
| 63 | 0 | log.info("Initializing " + getClass().getName()); |
| 64 | |
|
| 65 | |
|
| 66 | 0 | context = config.getServletContext(); |
| 67 | |
|
| 68 | |
|
| 69 | 0 | String cfgsrc = config.getInitParameter("configFileName"); |
| 70 | 0 | if (cfgsrc != null && cfgsrc.length() > 0) { |
| 71 | |
|
| 72 | 0 | log.info("Configuration source file specified: " + cfgsrc); |
| 73 | |
|
| 74 | 0 | ConfigurationFactory factory = new ConfigurationFactory(cfgsrc); |
| 75 | |
try { |
| 76 | |
|
| 77 | 0 | Configuration configuration = factory.getConfiguration(); |
| 78 | |
|
| 79 | 0 | context.setAttribute("configuration", configuration); |
| 80 | |
|
| 81 | 0 | log.info("Configuration loaded using bootstrap file " + cfgsrc); |
| 82 | 0 | } catch (ConfigurationException e) { |
| 83 | |
|
| 84 | 0 | log.error("Exception encountered when attempting to obtain current configuration: " + e, e); |
| 85 | 0 | } |
| 86 | |
} |
| 87 | 0 | } |
| 88 | |
|
| 89 | |
|
| 90 | |
|
| 91 | |
|
| 92 | |
public void destroy() { |
| 93 | 0 | } |
| 94 | |
} |