Coverage Report - org.acegisecurity.captcha.CaptchaChannelProcessorTemplate
 
Classes in this File Line Coverage Branch Coverage Complexity
CaptchaChannelProcessorTemplate
98% 
100% 
1.727
 
 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.captcha;
 17  
 
 18  
 import org.acegisecurity.ConfigAttribute;
 19  
 import org.acegisecurity.ConfigAttributeDefinition;
 20  
 
 21  
 import org.acegisecurity.context.SecurityContextHolder;
 22  
 
 23  
 import org.acegisecurity.intercept.web.FilterInvocation;
 24  
 
 25  
 import org.acegisecurity.securechannel.ChannelEntryPoint;
 26  
 import org.acegisecurity.securechannel.ChannelProcessor;
 27  
 
 28  
 import org.apache.commons.logging.Log;
 29  
 import org.apache.commons.logging.LogFactory;
 30  
 
 31  
 import org.springframework.beans.factory.InitializingBean;
 32  
 
 33  
 import org.springframework.util.Assert;
 34  
 
 35  
 import java.io.IOException;
 36  
 
 37  
 import java.util.Iterator;
 38  
 
 39  
 import javax.servlet.ServletException;
 40  
 
 41  
 
 42  
 /**
 43  
  * <p>CaptchaChannel template : Ensures the user has enough human privileges by review of the {@link
 44  
  * CaptchaSecurityContext} and using an abstract routine {@link
 45  
  * #isContextValidConcerningHumanity(CaptchaSecurityContext)} (implemented by sub classes)</p>
 46  
  *  <P>The component uses 2 main parameters for its configuration :
 47  
  *  <ul>
 48  
  *      <li>a keyword to be mapped to urls in the {@link
 49  
  *      org.acegisecurity.securechannel.ChannelProcessingFilter} configuration<br>
 50  
  *      default value provided by sub classes.</li>
 51  
  *      <li>and a thresold : used by the routine {@link
 52  
  *      #isContextValidConcerningHumanity(CaptchaSecurityContext)} to evaluate whether the {@link
 53  
  *      CaptchaSecurityContext} is valid default value = 0</li>
 54  
  *  </ul>
 55  
  *  </p>
 56  
  *
 57  
  * @author marc antoine Garrigue
 58  
  * @version $Id: CaptchaChannelProcessorTemplate.java 1496 2006-05-23 13:38:33Z benalex $
 59  
  */
 60  14
 public abstract class CaptchaChannelProcessorTemplate implements ChannelProcessor, InitializingBean {
 61  
     //~ Instance fields ================================================================================================
 62  
 
 63  
     private ChannelEntryPoint entryPoint;
 64  14
     protected Log logger = LogFactory.getLog(this.getClass());
 65  14
     private String keyword = null;
 66  14
     private int thresold = 0;
 67  
 
 68  
     //~ Methods ========================================================================================================
 69  
 
 70  
     /**
 71  
      * Verify if entryPoint and keyword are ok
 72  
      *
 73  
      * @throws Exception if not
 74  
      */
 75  
     public void afterPropertiesSet() throws Exception {
 76  4
         Assert.notNull(entryPoint, "entryPoint required");
 77  1
         Assert.hasLength(keyword, "keyword required");
 78  1
     }
 79  
 
 80  
     public void decide(FilterInvocation invocation, ConfigAttributeDefinition config)
 81  
         throws IOException, ServletException {
 82  9
         if ((invocation == null) || (config == null)) {
 83  1
             throw new IllegalArgumentException("Nulls cannot be provided");
 84  
         }
 85  
 
 86  8
         CaptchaSecurityContext context = null;
 87  8
         context = (CaptchaSecurityContext) SecurityContextHolder.getContext();
 88  
 
 89  8
         Iterator iter = config.getConfigAttributes();
 90  
 
 91  16
         while (iter.hasNext()) {
 92  8
             ConfigAttribute attribute = (ConfigAttribute) iter.next();
 93  
 
 94  8
             if (supports(attribute)) {
 95  5
                 logger.debug("supports this attribute : " + attribute);
 96  
 
 97  5
                 if (!isContextValidConcerningHumanity(context)) {
 98  2
                     logger.debug("context is not allowed to access ressource, redirect to captcha entry point");
 99  2
                     redirectToEntryPoint(invocation);
 100  
                 } else {
 101  3
                     logger.debug("has been successfully checked this keyword, increment request count");
 102  3
                     context.incrementHumanRestrictedRessoucesRequestsCount();
 103  
                 }
 104  
             } else {
 105  3
                 logger.debug("do not support this attribute");
 106  
             }
 107  8
         }
 108  8
     }
 109  
 
 110  
     public ChannelEntryPoint getEntryPoint() {
 111  2
         return entryPoint;
 112  
     }
 113  
 
 114  
     public String getKeyword() {
 115  3
         return keyword;
 116  
     }
 117  
 
 118  
     public int getThresold() {
 119  26
         return thresold;
 120  
     }
 121  
 
 122  
     abstract boolean isContextValidConcerningHumanity(CaptchaSecurityContext context);
 123  
 
 124  
     private void redirectToEntryPoint(FilterInvocation invocation)
 125  
         throws IOException, ServletException {
 126  2
         if (logger.isDebugEnabled()) {
 127  0
             logger.debug("context is not valid : redirecting to entry point");
 128  
         }
 129  
 
 130  2
         entryPoint.commence(invocation.getRequest(), invocation.getResponse());
 131  2
     }
 132  
 
 133  
     public void setEntryPoint(ChannelEntryPoint entryPoint) {
 134  5
         this.entryPoint = entryPoint;
 135  5
     }
 136  
 
 137  
     public void setKeyword(String keyword) {
 138  16
         this.keyword = keyword;
 139  16
     }
 140  
 
 141  
     public void setThresold(int thresold) {
 142  10
         this.thresold = thresold;
 143  10
     }
 144  
 
 145  
     public boolean supports(ConfigAttribute attribute) {
 146  12
         if ((attribute != null) && (keyword.equals(attribute.getAttribute()))) {
 147  7
             return true;
 148  
         } else {
 149  5
             return false;
 150  
         }
 151  
     }
 152  
 }