Coverage Report - org.acegisecurity.intercept.web.FilterSecurityInterceptor
 
Classes in this File Line Coverage Branch Coverage Complexity
FilterSecurityInterceptor
62% 
100% 
1.2
 
 1  
 /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
 2  
  *
 3  
  * Licensed under the Apache License, Version 2.0 (the "License");
 4  
  * you may not use this file except in compliance with the License.
 5  
  * You may obtain a copy of the License at
 6  
  *
 7  
  *     http://www.apache.org/licenses/LICENSE-2.0
 8  
  *
 9  
  * Unless required by applicable law or agreed to in writing, software
 10  
  * distributed under the License is distributed on an "AS IS" BASIS,
 11  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12  
  * See the License for the specific language governing permissions and
 13  
  * limitations under the License.
 14  
  */
 15  
 
 16  
 package org.acegisecurity.intercept.web;
 17  
 
 18  
 import org.acegisecurity.intercept.AbstractSecurityInterceptor;
 19  
 import org.acegisecurity.intercept.InterceptorStatusToken;
 20  
 import org.acegisecurity.intercept.ObjectDefinitionSource;
 21  
 
 22  
 import java.io.IOException;
 23  
 
 24  
 import javax.servlet.Filter;
 25  
 import javax.servlet.FilterChain;
 26  
 import javax.servlet.FilterConfig;
 27  
 import javax.servlet.ServletException;
 28  
 import javax.servlet.ServletRequest;
 29  
 import javax.servlet.ServletResponse;
 30  
 
 31  
 
 32  
 /**
 33  
  * Performs security handling of HTTP resources via a filter implementation.<p>The
 34  
  * <code>ObjectDefinitionSource</code> required by this security interceptor is of type {@link
 35  
  * FilterInvocationDefinitionSource}.</p>
 36  
  *  <P>Refer to {@link AbstractSecurityInterceptor} for details on the workflow.</p>
 37  
  *
 38  
  * @author Ben Alex
 39  
  * @version $Id: FilterSecurityInterceptor.java 1496 2006-05-23 13:38:33Z benalex $
 40  
  */
 41  9
 public class FilterSecurityInterceptor extends AbstractSecurityInterceptor implements Filter {
 42  
     //~ Static fields/initializers =====================================================================================
 43  
 
 44  
     private static final String FILTER_APPLIED = "__acegi_filterSecurityInterceptor_filterApplied";
 45  
 
 46  
     //~ Instance fields ================================================================================================
 47  
 
 48  
     private FilterInvocationDefinitionSource objectDefinitionSource;
 49  9
     private boolean observeOncePerRequest = true;
 50  
 
 51  
     //~ Methods ========================================================================================================
 52  
 
 53  
     /**
 54  
      * Not used (we rely on IoC container lifecycle services instead)
 55  
      */
 56  0
     public void destroy() {}
 57  
 
 58  
     /**
 59  
      * Method that is actually called by the filter chain. Simply delegates to the {@link
 60  
      * #invoke(FilterInvocation)} method.
 61  
      *
 62  
      * @param request the servlet request
 63  
      * @param response the servlet response
 64  
      * @param chain the filter chain
 65  
      *
 66  
      * @throws IOException if the filter chain fails
 67  
      * @throws ServletException if the filter chain fails
 68  
      */
 69  
     public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
 70  
         throws IOException, ServletException {
 71  0
         FilterInvocation fi = new FilterInvocation(request, response, chain);
 72  0
         invoke(fi);
 73  0
     }
 74  
 
 75  
     public FilterInvocationDefinitionSource getObjectDefinitionSource() {
 76  1
         return this.objectDefinitionSource;
 77  
     }
 78  
 
 79  
     public Class getSecureObjectClass() {
 80  47
         return FilterInvocation.class;
 81  
     }
 82  
 
 83  
     /**
 84  
      * Not used (we rely on IoC container lifecycle services instead)
 85  
      *
 86  
      * @param arg0 ignored
 87  
      *
 88  
      * @throws ServletException never thrown
 89  
      */
 90  0
     public void init(FilterConfig arg0) throws ServletException {}
 91  
 
 92  
     public void invoke(FilterInvocation fi) throws IOException, ServletException {
 93  3
         if ((fi.getRequest() != null) && (fi.getRequest().getAttribute(FILTER_APPLIED) != null)
 94  
             && observeOncePerRequest) {
 95  
             // filter already applied to this request and user wants us to observce
 96  
             // once-per-request handling, so don't re-do security checking
 97  0
             fi.getChain().doFilter(fi.getRequest(), fi.getResponse());
 98  
         } else {
 99  
             // first time this request being called, so perform security checking
 100  3
             if (fi.getRequest() != null) {
 101  3
                 fi.getRequest().setAttribute(FILTER_APPLIED, Boolean.TRUE);
 102  
             }
 103  
 
 104  3
             InterceptorStatusToken token = super.beforeInvocation(fi);
 105  
 
 106  
             try {
 107  3
                 fi.getChain().doFilter(fi.getRequest(), fi.getResponse());
 108  
             } finally {
 109  3
                 super.afterInvocation(token, null);
 110  3
             }
 111  
         }
 112  3
     }
 113  
 
 114  
     /**
 115  
      * Indicates whether once-per-request handling will be observed. By default this is <code>true</code>,
 116  
      * meaning the <code>FilterSecurityInterceptor</code> will only execute once-per-request. Sometimes users may wish
 117  
      * it to execute more than once per request, such as when JSP forwards are being used and filter security is
 118  
      * desired on each included fragment of the HTTP request.
 119  
      *
 120  
      * @return <code>true</code> (the default) if once-per-request is honoured, otherwise <code>false</code> if
 121  
      *         <code>FilterSecurityInterceptor</code> will enforce authorizations for each and every fragment of the
 122  
      *         HTTP request.
 123  
      */
 124  
     public boolean isObserveOncePerRequest() {
 125  0
         return observeOncePerRequest;
 126  
     }
 127  
 
 128  
     public ObjectDefinitionSource obtainObjectDefinitionSource() {
 129  22
         return this.objectDefinitionSource;
 130  
     }
 131  
 
 132  
     public void setObjectDefinitionSource(FilterInvocationDefinitionSource newSource) {
 133  9
         this.objectDefinitionSource = newSource;
 134  9
     }
 135  
 
 136  
     public void setObserveOncePerRequest(boolean observeOncePerRequest) {
 137  0
         this.observeOncePerRequest = observeOncePerRequest;
 138  0
     }
 139  
 }