1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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
67
68
69
70
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
80
81 public void init( FilterConfig config ) throws ServletException
82 {
83 }
84
85
86
87
88
89
90
91
92
93
94
95
96
97
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
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
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
153
154 public void destroy( )
155 {
156
157 }
158 }