Coverage Report - fr.paris.lutece.plugins.captcha.modules.jcaptcha.service.sound.SoundCaptchaFilter
 
Classes in this File Line Coverage Branch Coverage Complexity
SoundCaptchaFilter
0 %
0/36
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.sound;
 43  
 
 44  
 import fr.paris.lutece.portal.service.spring.SpringContextService;
 45  
 import fr.paris.lutece.portal.service.util.AppLogService;
 46  
 
 47  
 import java.io.ByteArrayOutputStream;
 48  
 import java.io.IOException;
 49  
 
 50  
 import javax.servlet.Filter;
 51  
 import javax.servlet.FilterChain;
 52  
 import javax.servlet.FilterConfig;
 53  
 import javax.servlet.ServletException;
 54  
 import javax.servlet.ServletOutputStream;
 55  
 import javax.servlet.ServletRequest;
 56  
 import javax.servlet.ServletResponse;
 57  
 import javax.servlet.http.HttpServletRequest;
 58  
 import javax.servlet.http.HttpServletResponse;
 59  
 import javax.sound.sampled.AudioFileFormat;
 60  
 import javax.sound.sampled.AudioInputStream;
 61  
 import javax.sound.sampled.AudioSystem;
 62  
 
 63  
 import com.octo.captcha.service.CaptchaServiceException;
 64  
 import com.octo.captcha.service.multitype.GenericManageableCaptchaService;
 65  
 
 66  
 
 67  
 /**
 68  
  * Generation captcha class
 69  
  *
 70  
  * This class invok captcha service from applicationContext and get a challenge.
 71  
  * The challenge response temp stored in service map, with user session id key.<br>
 72  
  * this servlet throw generated sound, wav formatted.
 73  
  */
 74  0
 public class SoundCaptchaFilter implements Filter
 75  
 {
 76  
     private static final String SOUND_CAPTCHA_SERVICE_NAME = "jcaptcha.soundCaptchaService";
 77  
     private static final String LOGGER = "lutece.captcha";
 78  
     private static final long serialVersionUID = -1806578484091247923L;
 79  
 
 80  
     /**
 81  
      * {@inheritDoc}
 82  
      */
 83  
     public void init( FilterConfig config ) throws ServletException
 84  
     {
 85  0
     }
 86  
 
 87  
     /**
 88  
      * Apply the filter
 89  
      * @param req The HTTP request
 90  
      * @param res The HTTP response
 91  
      * @param filterChain The Filter Chain
 92  
      * @throws IOException If an error occured
 93  
      * @throws ServletException If an error occured
 94  
      */
 95  
     public void doFilter( ServletRequest req, ServletResponse res, FilterChain filterChain )
 96  
         throws IOException, ServletException
 97  
     {
 98  0
         AppLogService.debug( LOGGER, "challenge captcha generation start" );
 99  
 
 100  0
         HttpServletRequest request = (HttpServletRequest) req;
 101  0
         HttpServletResponse response = (HttpServletResponse) res;
 102  
 
 103  0
         byte[] captchaChallengeSound = null;
 104  0
         ByteArrayOutputStream soundOutputStream = new ByteArrayOutputStream(  );
 105  
 
 106  
         try
 107  
         {
 108  0
             String captchaIdSound = request.getSession(  ).getId(  );
 109  
 
 110  
             // grab bean
 111  0
             GenericManageableCaptchaService captcha = (GenericManageableCaptchaService) SpringContextService
 112  0
                     .getBean( SOUND_CAPTCHA_SERVICE_NAME );
 113  0
             AppLogService.info( "captcha : " + captcha );
 114  
 
 115  0
             AudioInputStream challengeSound = captcha.getSoundChallengeForID( captchaIdSound, request.getLocale(  ) );
 116  0
             AudioSystem.write( challengeSound, AudioFileFormat.Type.WAVE, soundOutputStream );
 117  0
             soundOutputStream.flush(  );
 118  0
             soundOutputStream.close(  );
 119  
         }
 120  0
         catch ( IllegalArgumentException e )
 121  
         {
 122  0
             AppLogService.error( "exception : " + e.getMessage(  ), e );
 123  0
             response.sendError( HttpServletResponse.SC_NOT_FOUND );
 124  
 
 125  0
             return;
 126  
         }
 127  0
         catch ( CaptchaServiceException e )
 128  
         {
 129  0
             AppLogService.error( "exception :" + e.getMessage(  ), e );
 130  0
             response.sendError( HttpServletResponse.SC_INTERNAL_SERVER_ERROR );
 131  
 
 132  0
             return;
 133  0
         }
 134  
 
 135  
         // flush it in the response
 136  0
         captchaChallengeSound = soundOutputStream.toByteArray(  );
 137  0
         response.setHeader( "cache-control", "no-cache, no-store,must-revalidate,max-age=0" );
 138  0
         response.setHeader( "Content-Length", "" + captchaChallengeSound.length );
 139  0
         response.setHeader( "expires", "1" );
 140  0
         response.setContentType( "audio/x-wav" );
 141  
 
 142  0
         ServletOutputStream responseOutputStream = response.getOutputStream(  );
 143  0
         responseOutputStream.write( captchaChallengeSound );
 144  0
         responseOutputStream.flush(  );
 145  0
         responseOutputStream.close(  );
 146  0
         AppLogService.debug( LOGGER, "captcha challenge generation end" );
 147  0
     }
 148  
 
 149  
     /**
 150  
      * Destroy the filter
 151  
      */
 152  
     public void destroy(  )
 153  
     {
 154  
         // no-op
 155  0
     }
 156  
 }