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