001 package org.rakeshv.filters;
002
003 import java.io.IOException;
004 import javax.servlet.Filter;
005 import javax.servlet.FilterChain;
006 import javax.servlet.FilterConfig;
007 import javax.servlet.ServletException;
008 import javax.servlet.ServletRequest;
009 import javax.servlet.ServletResponse;
010
011 /**
012 * A <code>Filter</code> class that implements the <code>Filter</code>
013 * interface, and provides default implementations for all the methods.
014 * In most cases, <code>sub-classes</code> need only <code>over-ride
015 * </code> the {@link #doFilter( ServletRequest, ServletResponse,
016 * FilterChain )} method.
017 *
018 * <p>© Copyright 2003, Rakesh Vidyadharan</p>
019 * @author Rakesh Vidyadharan 2<sup><small>nd</small></sup> September 2003
020 *
021 * @version $Id: FilterAdapter.java,v 1.4 2005/09/16 13:21:38 rakesh Exp $
022 */
023 public class FilterAdapter implements Filter
024 {
025 /**
026 * A reference to the Servlet container's Filter configuration.
027 */
028 protected FilterConfig filterConfig = null;
029
030 /**
031 * Called by the web container to indicate to a filter that it is
032 * being placed into service. The servlet container calls the init
033 * method exactly once after instantiating the filter. The init
034 * method must complete successfully before the filter is asked to do
035 * any filtering work.
036 *
037 * @param filterConfig - A filter configuration object
038 * used by a servlet container used to pass information to a filter
039 * during initialization. This is used to set the {@link
040 * #filterConfig} reference.
041 * @throws ServletException - If an exception is encountered while
042 * fetching the initialisation parameters from the {@link
043 * #filterConfig} object reference.
044 */
045 public void init( FilterConfig filterConfig ) throws ServletException
046 {
047 this.filterConfig = filterConfig;
048 }
049
050 /**
051 * Called by the web container to indicate to a filter that it is
052 * being taken out of service. This method is only called once all
053 * threads within the filter's doFilter method have exited or after
054 * a timeout period has passed. After the web container calls this
055 * method, it will not call the doFilter method again on this
056 * instance of the filter. Just sets the {@link #filterConfig}
057 * reference to <code>null</code>.
058 */
059 public void destroy()
060 {
061 this.filterConfig = null;
062 }
063
064 /**
065 * This method of the Filter is called by the container each time a
066 * request/response pair is passed through the chain due to a client
067 * request for a resource at the end of the chain. The FilterChain
068 * passed in to this method allows the Filter to pass on the request
069 * and response to the next entity in the chain.
070 *
071 * <p>The default implementation simply passes control on to the
072 * next filter in the chain.</p>
073 *
074 * @throws IOException - If exceptions are encountered while
075 * applying the filter.
076 * @throws ServletException - If exceptions are encountered while
077 * interacting with the request or response objects.
078 */
079 public void doFilter( ServletRequest request,
080 ServletResponse response, FilterChain chain )
081 throws IOException, ServletException
082 {
083 chain.doFilter( request, response );
084 }
085
086 /**
087 * Set {@link #filterConfig}.
088 *
089 * @param filterConfig The value to set.
090 */
091 protected void setFilterConfig( FilterConfig filterConfig )
092 {
093 this.filterConfig = filterConfig;
094 }
095 }