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