Coverage Report - org.acegisecurity.captcha.CaptchaValidationProcessingFilter
 
Classes in this File Line Coverage Branch Coverage Complexity
CaptchaValidationProcessingFilter
87% 
100% 
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.captcha;
 17  
 
 18  
 import org.acegisecurity.context.SecurityContextHolder;
 19  
 
 20  
 import org.apache.commons.logging.Log;
 21  
 import org.apache.commons.logging.LogFactory;
 22  
 
 23  
 import org.springframework.beans.factory.InitializingBean;
 24  
 
 25  
 import java.io.IOException;
 26  
 
 27  
 import javax.servlet.*;
 28  
 import javax.servlet.http.HttpServletRequest;
 29  
 import javax.servlet.http.HttpSession;
 30  
 
 31  
 
 32  
 /**
 33  
  * Filter for web integration of the {@link CaptchaServiceProxy}. <br>
 34  
  * It basically intercept calls containing the specific validation parameter, use the {@link CaptchaServiceProxy} to
 35  
  * validate the request, and update the {@link CaptchaSecurityContext} if the request passed the validation. <br>
 36  
  * This Filter should be placed after the ContextIntegration filter and before the {@link
 37  
  * CaptchaChannelProcessorTemplate} filter in the filter stack in order to update the {@link CaptchaSecurityContext}
 38  
  * before the humanity verification routine occurs. <br>
 39  
  * This filter should only be used in conjunction with the {@link CaptchaSecurityContext}<br>
 40  
  *
 41  
  * @author marc antoine Garrigue
 42  
  * @version $Id: CaptchaValidationProcessingFilter.java 1784 2007-02-24 21:00:24Z luke_t $
 43  
  */
 44  3
 public class CaptchaValidationProcessingFilter implements InitializingBean, Filter {
 45  
     //~ Static fields/initializers =====================================================================================
 46  
 
 47  2
     protected static final Log logger = LogFactory.getLog(CaptchaValidationProcessingFilter.class);
 48  
 
 49  
     //~ Instance fields ================================================================================================
 50  
 
 51  
     private CaptchaServiceProxy captchaService;
 52  3
     private String captchaValidationParameter = "_captcha_parameter";
 53  
 
 54  
     //~ Methods ========================================================================================================
 55  
 
 56  
     public void afterPropertiesSet() throws Exception {
 57  3
         if (this.captchaService == null) {
 58  1
             throw new IllegalArgumentException("CaptchaServiceProxy must be defined ");
 59  
         }
 60  
 
 61  2
         if ((this.captchaValidationParameter == null) || "".equals(captchaValidationParameter)) {
 62  1
             throw new IllegalArgumentException("captchaValidationParameter must not be empty or null");
 63  
         }
 64  1
     }
 65  
 
 66  
     /**
 67  
      * Does nothing. We use IoC container lifecycle services instead.
 68  
      */
 69  0
     public void destroy() {}
 70  
 
 71  
     public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
 72  
         throws IOException, ServletException {
 73  4
         String captchaResponse = request.getParameter(captchaValidationParameter);
 74  
 
 75  4
         if ((request != null) && request instanceof HttpServletRequest && (captchaResponse != null)) {
 76  2
             logger.debug("captcha validation parameter found");
 77  
 
 78  
             // validate the request against CaptchaServiceProxy
 79  2
             boolean valid = false;
 80  
 
 81  2
             logger.debug("try to validate");
 82  
 
 83  
             //get session
 84  2
             HttpSession session = ((HttpServletRequest) request).getSession();
 85  
 
 86  2
             if (session != null) {
 87  2
                 String id = session.getId();
 88  2
                 valid = this.captchaService.validateReponseForId(id, captchaResponse);
 89  2
                 logger.debug("captchaServiceProxy says : request is valid = " + valid);
 90  
 
 91  2
                 if (valid) {
 92  1
                     logger.debug("update the context");
 93  1
                     ((CaptchaSecurityContext) SecurityContextHolder.getContext()).setHuman();
 94  
 
 95  
                     //logger.debug("retrieve original request from ")
 96  
                 } else {
 97  1
                     logger.debug("captcha test failed");
 98  
                 }
 99  2
             } else {
 100  0
                 logger.debug("no session found, user don't even ask a captcha challenge");
 101  
             }
 102  2
         } else {
 103  2
             logger.debug("captcha validation parameter not found, do nothing");
 104  
         }
 105  
 
 106  4
         if (logger.isDebugEnabled()) {
 107  0
             logger.debug("chain ...");
 108  
         }
 109  
 
 110  4
         chain.doFilter(request, response);
 111  4
     }
 112  
 
 113  
     public CaptchaServiceProxy getCaptchaService() {
 114  0
         return captchaService;
 115  
     }
 116  
 
 117  
     public String getCaptchaValidationParameter() {
 118  1
         return captchaValidationParameter;
 119  
     }
 120  
 
 121  
     /**
 122  
      * Does nothing. We use IoC container lifecycle services instead.
 123  
      *
 124  
      * @param filterConfig ignored
 125  
      *
 126  
      * @throws ServletException ignored
 127  
      */
 128  0
     public void init(FilterConfig filterConfig) throws ServletException {}
 129  
 
 130  
     public void setCaptchaService(CaptchaServiceProxy captchaService) {
 131  3
         this.captchaService = captchaService;
 132  3
     }
 133  
 
 134  
     public void setCaptchaValidationParameter(String captchaValidationParameter) {
 135  1
         this.captchaValidationParameter = captchaValidationParameter;
 136  1
     }
 137  
 }