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 | |
package fr.paris.lutece.plugins.captcha.modules.jcaptcha.service.sound; |
35 | |
|
36 | |
import java.security.SecureRandom; |
37 | |
import java.util.Locale; |
38 | |
import java.util.Random; |
39 | |
|
40 | |
import javax.sound.sampled.AudioInputStream; |
41 | |
|
42 | |
import com.octo.captcha.CaptchaException; |
43 | |
import com.octo.captcha.CaptchaQuestionHelper; |
44 | |
import com.octo.captcha.component.sound.wordtosound.WordToSound; |
45 | |
import com.octo.captcha.component.word.worddecorator.WordDecorator; |
46 | |
import com.octo.captcha.component.word.wordgenerator.WordGenerator; |
47 | |
import com.octo.captcha.sound.SoundCaptcha; |
48 | |
import com.octo.captcha.sound.SoundCaptchaFactory; |
49 | |
import com.octo.captcha.sound.gimpy.GimpySound; |
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
public class LuteceGimpySoundFactory extends SoundCaptchaFactory |
58 | |
{ |
59 | |
|
60 | |
|
61 | |
|
62 | |
public static final String MESSAGE_INVALID_EXCEPTION = "Invalid configuration for a "; |
63 | 0 | public static final String BUNDLE_QUESTION_KEY = GimpySound.class.getName( ); |
64 | |
private WordGenerator _wordGenerator; |
65 | |
private WordToSound _word2Sound; |
66 | 0 | private Random _myRandom = new SecureRandom( ); |
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
public LuteceGimpySoundFactory( WordGenerator thewordGenerator, WordToSound theword2Sound ) |
75 | 0 | { |
76 | 0 | if ( thewordGenerator == null ) |
77 | |
{ |
78 | 0 | throw new CaptchaException( MESSAGE_INVALID_EXCEPTION + "GimpySoundFactory : WordGenerator can't be null" ); |
79 | |
} |
80 | |
|
81 | 0 | if ( theword2Sound == null ) |
82 | |
{ |
83 | 0 | throw new CaptchaException( MESSAGE_INVALID_EXCEPTION + "GimpySoundFactory : Word2Sound can't be null" ); |
84 | |
} |
85 | |
|
86 | 0 | _wordGenerator = thewordGenerator; |
87 | 0 | _word2Sound = theword2Sound; |
88 | 0 | } |
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
public LuteceGimpySoundFactory( WordGenerator wordGenerator, WordToSound word2Sound, WordDecorator wordDecorator ) |
98 | 0 | { |
99 | 0 | if ( wordGenerator == null ) |
100 | |
{ |
101 | 0 | throw new CaptchaException( MESSAGE_INVALID_EXCEPTION + |
102 | |
"SpellingSoundFactory : WordGenerator can't be null" ); |
103 | |
} |
104 | |
|
105 | 0 | if ( word2Sound == null ) |
106 | |
{ |
107 | 0 | throw new CaptchaException( MESSAGE_INVALID_EXCEPTION + "SpellingSoundFactory : Word2Sound can't be null" ); |
108 | |
} |
109 | |
|
110 | 0 | if ( wordDecorator == null ) |
111 | |
{ |
112 | 0 | throw new CaptchaException( MESSAGE_INVALID_EXCEPTION + |
113 | |
"SpellingSoundFactory : wordAbstractor can't be null" ); |
114 | |
} |
115 | |
|
116 | 0 | _wordGenerator = wordGenerator; |
117 | 0 | _word2Sound = word2Sound; |
118 | 0 | } |
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
public WordToSound getWordToSound( ) |
125 | |
{ |
126 | 0 | return _word2Sound; |
127 | |
} |
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
public WordGenerator getWordGenerator( ) |
134 | |
{ |
135 | 0 | return _wordGenerator; |
136 | |
} |
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
public SoundCaptcha getSoundCaptcha( ) |
142 | |
{ |
143 | 0 | String word = _wordGenerator.getWord( getRandomLength( ), Locale.getDefault( ) ).toLowerCase( ); |
144 | 0 | AudioInputStream sound = _word2Sound.getSound( word ); |
145 | 0 | SoundCaptcha soundCaptcha = new GimpySound( getQuestion( Locale.getDefault( ) ), sound, word ); |
146 | |
|
147 | 0 | return soundCaptcha; |
148 | |
} |
149 | |
|
150 | |
|
151 | |
|
152 | |
|
153 | |
|
154 | |
public SoundCaptcha getSoundCaptcha( Locale locale ) |
155 | |
{ |
156 | 0 | String word = _wordGenerator.getWord( getRandomLength( ), locale ).toLowerCase( ); |
157 | 0 | AudioInputStream sound = _word2Sound.getSound( word, locale ); |
158 | 0 | SoundCaptcha soundCaptcha = new GimpySound( getQuestion( locale ), sound, word ); |
159 | |
|
160 | 0 | return soundCaptcha; |
161 | |
} |
162 | |
|
163 | |
|
164 | |
|
165 | |
|
166 | |
|
167 | |
|
168 | |
protected String getQuestion( Locale locale ) |
169 | |
{ |
170 | 0 | return CaptchaQuestionHelper.getQuestion( locale, BUNDLE_QUESTION_KEY ); |
171 | |
} |
172 | |
|
173 | |
|
174 | |
|
175 | |
|
176 | |
|
177 | |
protected Integer getRandomLength( ) |
178 | |
{ |
179 | |
Integer wordLength; |
180 | 0 | int range = getWordToSound( ).getMaxAcceptedWordLength( ) - getWordToSound( ).getMinAcceptedWordLength( ); |
181 | 0 | int randomRange = ( range != 0 ) ? _myRandom.nextInt( range + 1 ) : 0; |
182 | 0 | wordLength = Integer.valueOf( randomRange + getWordToSound( ).getMinAcceptedWordLength( ) ); |
183 | |
|
184 | 0 | return wordLength; |
185 | |
} |
186 | |
} |