Coverage Report - fr.paris.lutece.plugins.captcha.modules.jcaptcha.service.image.ImageCaptchaFilter
 
Classes in this File Line Coverage Branch Coverage Complexity
ImageCaptchaFilter
0 %
0/34
N/A
2,333
 
 1  
 /*
 2  
  * Copyright (c) 2002-2014, Mairie de Paris
 3  
  * All rights reserved.
 4  
  *
 5  
  * Redistribution and use in source and binary forms, with or without
 6  
  * modification, are permitted provided that the following conditions
 7  
  * are met:
 8  
  *
 9  
  *  1. Redistributions of source code must retain the above copyright notice
 10  
  *     and the following disclaimer.
 11  
  *
 12  
  *  2. Redistributions in binary form must reproduce the above copyright notice
 13  
  *     and the following disclaimer in the documentation and/or other materials
 14  
  *     provided with the distribution.
 15  
  *
 16  
  *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
 17  
  *     contributors may be used to endorse or promote products derived from
 18  
  *     this software without specific prior written permission.
 19  
  *
 20  
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 21  
  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 22  
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 23  
  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
 24  
  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 25  
  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 26  
  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 27  
  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 28  
  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 29  
  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 30  
  * POSSIBILITY OF SUCH DAMAGE.
 31  
  *
 32  
  * License 1.0
 33  
  */
 34  
 
 35  
 /**
 36  
  * Nom du Fichier : $RCSfile: CaptchaServlet.java,v $
 37  
  * Version CVS : $Revision: 1.6 $
 38  
  * Auteur : A.Floquet
 39  
  * Description : Servlet de génération de l'image captcha.
 40  
  *
 41  
  */
 42  
 package fr.paris.lutece.plugins.captcha.modules.jcaptcha.service.image;
 43  
 
 44  
 import fr.paris.lutece.portal.service.spring.SpringContextService;
 45  
 import fr.paris.lutece.portal.service.util.AppLogService;
 46  
 
 47  
 import java.awt.image.BufferedImage;
 48  
 import java.io.ByteArrayOutputStream;
 49  
 import java.io.IOException;
 50  
 
 51  
 import javax.imageio.ImageIO;
 52  
 import javax.servlet.Filter;
 53  
 import javax.servlet.FilterChain;
 54  
 import javax.servlet.FilterConfig;
 55  
 import javax.servlet.ServletException;
 56  
 import javax.servlet.ServletOutputStream;
 57  
 import javax.servlet.ServletRequest;
 58  
 import javax.servlet.ServletResponse;
 59  
 import javax.servlet.http.HttpServletRequest;
 60  
 import javax.servlet.http.HttpServletResponse;
 61  
 
 62  
 import com.octo.captcha.service.CaptchaServiceException;
 63  
 import com.octo.captcha.service.multitype.GenericManageableCaptchaService;
 64  
 
 65  
 
 66  
 /**
 67  
  * Generate Jcapcha test.
 68  
  */
 69  0
 public class ImageCaptchaFilter implements Filter
 70  
 {
 71  
     private static final String PARAMETER_SESSION_IMAGE_CAPTCHA = "imageCaptcha";
 72  
     private static final String LOGGER = "lutece.captcha";
 73  
     private static final String IMAGE_TYPE = "jpeg";
 74  
     private static final long serialVersionUID = -1806578484091247923L;
 75  
 
 76  
     /**
 77  
      * {@inheritDoc}
 78  
      */
 79  
     public void init( FilterConfig filterConfig ) throws ServletException
 80  
     {
 81  
         // This would be a good place to collect a parameterized
 82  
         // default encoding type.  For brevity, we're going to
 83  
         // use a hard-coded value in this example.
 84  0
     }
 85  
 
 86  
     /**
 87  
      * Apply the filter
 88  
      * @param req The HTTP request
 89  
      * @param res The HTTP response
 90  
      * @param filterChain The Filter Chain
 91  
      * @throws IOException If an error occured
 92  
      * @throws ServletException If an error occured
 93  
      */
 94  
     public void doFilter( ServletRequest req, ServletResponse res, FilterChain filterChain )
 95  
         throws IOException, ServletException
 96  
     {
 97  0
         HttpServletRequest request = (HttpServletRequest) req;
 98  0
         HttpServletResponse response = (HttpServletResponse) res;
 99  
 
 100  0
         AppLogService.debug( LOGGER, "challenge captcha generation start" );
 101  
 
 102  0
         byte[] captchaChallengeImage = null;
 103  
 
 104  
         // the output stream to render the captcha image as jpeg into
 105  0
         ByteArrayOutputStream jpegOutputStream = new ByteArrayOutputStream(  );
 106  
 
 107  
         try
 108  
         {
 109  
             // grab bean
 110  
             // MultiTypeCaptchaService captcha = (MultiTypeCaptchaService) SpringContextService.getPluginBean("jcaptcha", "imageCaptchaService");
 111  0
             GenericManageableCaptchaService captcha = (GenericManageableCaptchaService) SpringContextService.getBean( "jcaptcha.imageCaptchaService" );
 112  0
             AppLogService.info( "captcha : " + captcha );
 113  
 
 114  
             // get the session id that will identify the generated captcha.
 115  
             // the same id must be used to validate the response, the session id
 116  
             // is a good candidate!
 117  0
             BufferedImage challengeImage = captcha.getImageChallengeForID( request.getSession(  ).getId(  ),
 118  0
                     request.getLocale(  ) );
 119  
 
 120  0
             ImageIO.write( challengeImage, IMAGE_TYPE, jpegOutputStream );
 121  
             // a jpeg encoder
 122  
             /*JPEGImageEncoder jpegEncoder = JPEGCodec.createJPEGEncoder( jpegOutputStream );
 123  
             jpegEncoder.encode( challengeImage );*/
 124  
         }
 125  0
         catch ( IllegalArgumentException e )
 126  
         {
 127  0
             AppLogService.error( "exception :" + e.getMessage(  ), e );
 128  0
             response.sendError( HttpServletResponse.SC_NOT_FOUND );
 129  
 
 130  0
             return;
 131  
         }
 132  0
         catch ( CaptchaServiceException e )
 133  
         {
 134  0
             AppLogService.error( "exception :" + e.getMessage(  ), e );
 135  0
             response.sendError( HttpServletResponse.SC_INTERNAL_SERVER_ERROR );
 136  
 
 137  0
             return;
 138  0
         }
 139  
 
 140  0
         captchaChallengeImage = jpegOutputStream.toByteArray(  );
 141  0
         request.getSession(  ).setAttribute( PARAMETER_SESSION_IMAGE_CAPTCHA, captchaChallengeImage );
 142  
         // flush it in the response
 143  0
         response.setHeader( "cache-control", "no-cache, no-store,must-revalidate,max-age=0" );
 144  0
         response.setHeader( "pragma", "no-store, no-cache" );
 145  0
         response.setHeader( "expires", "1" );
 146  0
         response.setContentType( "image/jpeg" );
 147  
 
 148  0
         ServletOutputStream responseOutputStream = response.getOutputStream(  );
 149  0
         responseOutputStream.write( captchaChallengeImage );
 150  0
         responseOutputStream.flush(  );
 151  0
         responseOutputStream.close(  );
 152  0
         AppLogService.debug( LOGGER, "challenge captcha generation end" );
 153  0
     }
 154  
 
 155  
     /**
 156  
      * Destroy the filter
 157  
      */
 158  
     public void destroy(  )
 159  
     {
 160  
         // no-op
 161  0
     }
 162  
 }