Coverage Report - com.mattunderscore.filter.GenericFilter
 
Classes in this File Line Coverage Branch Coverage Complexity
GenericFilter
83%
10/12
50%
1/2
1.143
 
 1  
 /* Copyright © 2012 Matthew Champion
 2  
 All rights reserved.
 3  
 
 4  
 Redistribution and use in source and binary forms, with or without
 5  
 modification, are permitted provided that the following conditions are met:
 6  
     * Redistributions of source code must retain the above copyright
 7  
       notice, this list of conditions and the following disclaimer.
 8  
     * Redistributions in binary form must reproduce the above copyright
 9  
       notice, this list of conditions and the following disclaimer in the
 10  
       documentation and/or other materials provided with the distribution.
 11  
     * Neither the name of mattunderscore.com nor the
 12  
       names of its contributors may be used to endorse or promote products
 13  
       derived from this software without specific prior written permission.
 14  
 
 15  
 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 16  
 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 17  
 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 18  
 DISCLAIMED. IN NO EVENT SHALL MATTHEW CHAMPION BE LIABLE FOR ANY
 19  
 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 20  
 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 21  
 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 22  
 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 23  
 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 24  
 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
 25  
 
 26  
 package com.mattunderscore.filter;
 27  
 
 28  
 import java.io.IOException;
 29  
 import javax.servlet.Filter;
 30  
 import javax.servlet.FilterChain;
 31  
 import javax.servlet.FilterConfig;
 32  
 import javax.servlet.ServletException;
 33  
 import javax.servlet.ServletRequest;
 34  
 import javax.servlet.ServletResponse;
 35  
 import javax.servlet.ServletContext;
 36  
 
 37  
 /**
 38  
  * An abstract generic filter that provides a boilerplate implementation of
 39  
  * {@link Filter#init(FilterConfig)}. It sets the {@link FilterConfig} and {@link ServletContext}
 40  
  * objects.
 41  
  * 
 42  
  * More complex configuration of the filter can be implemented by overriding the configureFilter
 43  
  * method. This is executed by {@link #init(FilterConfig)} method after the {@link FilterConfig} and
 44  
  * {@link ServletContext} have been set.
 45  
  * 
 46  
  * @author Matt Champion
 47  
  * @since 0.0.13
 48  
  */
 49  12
 public abstract class GenericFilter implements Filter
 50  
 {
 51  
 
 52  
     /**
 53  
      * The {@link FilterConfig} set by {@link #setFilterConfig(FilterConfig)}.
 54  
      * 
 55  
      * @since 0.0.13
 56  
      */
 57  
     protected FilterConfig filterConfig;
 58  
 
 59  
     /**
 60  
      * The {@link ServletContext} of the filter configuration set by
 61  
      * {@link #setFilterConfig(FilterConfig)}.
 62  
      * 
 63  
      * @since 0.0.13
 64  
      */
 65  
     protected ServletContext context;
 66  
 
 67  
     public abstract void doFilter(ServletRequest request, ServletResponse response,
 68  
             FilterChain chain) throws IOException, ServletException;
 69  
 
 70  
     /**
 71  
      * Return the filter configuration object for this filter.
 72  
      * 
 73  
      * @return The {@link FilterConfig} that was passed in through {@link #init(FilterConfig)}.
 74  
      * @since 0.0.13
 75  
      * @deprecated This can be accessed through a protected instance variable and public access
 76  
      *      seems unnecessary
 77  
      */
 78  
     @Deprecated
 79  
     public final FilterConfig getFilterConfig()
 80  
     {
 81  4
         return filterConfig;
 82  
     }
 83  
 
 84  
     /**
 85  
      * Return the servlet context object for this filter.
 86  
      * 
 87  
      * @return The {@link ServletContext} that was attached to the {@link FilterConfig} passed in
 88  
      *         through {@link #init(FilterConfig)}.
 89  
      * @since 0.2.4
 90  
      * @deprecated This can be accessed through a protected instance variable and public access
 91  
      *      seems unnecessary
 92  
      */
 93  
     @Deprecated
 94  
     public final ServletContext getServletContext()
 95  
     {
 96  4
         return context;
 97  
     }
 98  
 
 99  
     /**
 100  
      * Set the filter configuration object for this filter.
 101  
      * 
 102  
      * @param filterConfig
 103  
      *            The filter configuration object
 104  
      * @since 0.0.13
 105  
      */
 106  
     public final void setFilterConfig(final FilterConfig filterConfig)
 107  
     {
 108  4
         if (filterConfig != null)
 109  
         {
 110  4
             this.context = filterConfig.getServletContext();
 111  4
             this.filterConfig = filterConfig;
 112  
         }
 113  4
     }
 114  
 
 115  
     /**
 116  
      * Callback method for configuring the filter.
 117  
      * 
 118  
      * @since 0.0.13
 119  
      */
 120  
     public void configureFilter()
 121  
     {
 122  0
     }
 123  
 
 124  
     @Override
 125  
     public void destroy()
 126  
     {
 127  0
     }
 128  
 
 129  
     @Override
 130  
     public final void init(FilterConfig theFilterConfig)
 131  
     {
 132  2
         setFilterConfig(theFilterConfig);
 133  2
         configureFilter();
 134  2
     }
 135  
 }