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 | |
|
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 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
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 | |
|
82 | |
|
83 | |
public void init( FilterConfig config ) throws ServletException |
84 | |
{ |
85 | 0 | } |
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
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 | |
|
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 | |
|
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 | |
|
151 | |
|
152 | |
public void destroy( ) |
153 | |
{ |
154 | |
|
155 | 0 | } |
156 | |
} |