View Javadoc
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  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      }
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          AppLogService.debug( LOGGER, "challenge captcha generation start" );
99  
100         HttpServletRequest request = (HttpServletRequest) req;
101         HttpServletResponse response = (HttpServletResponse) res;
102 
103         byte[] captchaChallengeSound = null;
104         ByteArrayOutputStream soundOutputStream = new ByteArrayOutputStream(  );
105 
106         try
107         {
108             String captchaIdSound = request.getSession(  ).getId(  );
109 
110             // grab bean
111             GenericManageableCaptchaService captcha = (GenericManageableCaptchaService) SpringContextService
112                     .getBean( SOUND_CAPTCHA_SERVICE_NAME );
113             AppLogService.info( "captcha : " + captcha );
114 
115             AudioInputStream challengeSound = captcha.getSoundChallengeForID( captchaIdSound, request.getLocale(  ) );
116             AudioSystem.write( challengeSound, AudioFileFormat.Type.WAVE, soundOutputStream );
117             soundOutputStream.flush(  );
118             soundOutputStream.close(  );
119         }
120         catch ( IllegalArgumentException e )
121         {
122             AppLogService.error( "exception : " + e.getMessage(  ), e );
123             response.sendError( HttpServletResponse.SC_NOT_FOUND );
124 
125             return;
126         }
127         catch ( CaptchaServiceException e )
128         {
129             AppLogService.error( "exception :" + e.getMessage(  ), e );
130             response.sendError( HttpServletResponse.SC_INTERNAL_SERVER_ERROR );
131 
132             return;
133         }
134 
135         // flush it in the response
136         captchaChallengeSound = soundOutputStream.toByteArray(  );
137         response.setHeader( "cache-control", "no-cache, no-store,must-revalidate,max-age=0" );
138         response.setHeader( "Content-Length", "" + captchaChallengeSound.length );
139         response.setHeader( "expires", "1" );
140         response.setContentType( "audio/x-wav" );
141 
142         ServletOutputStream responseOutputStream = response.getOutputStream(  );
143         responseOutputStream.write( captchaChallengeSound );
144         responseOutputStream.flush(  );
145         responseOutputStream.close(  );
146         AppLogService.debug( LOGGER, "captcha challenge generation end" );
147     }
148 
149     /**
150      * Destroy the filter
151      */
152     public void destroy(  )
153     {
154         // no-op
155     }
156 }