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.image;
35
36 import java.awt.image.BufferedImage;
37 import java.security.SecureRandom;
38 import java.util.Locale;
39 import java.util.Random;
40
41 import com.octo.captcha.CaptchaException;
42 import com.octo.captcha.CaptchaQuestionHelper;
43 import com.octo.captcha.component.image.wordtoimage.WordToImage;
44 import com.octo.captcha.component.word.wordgenerator.WordGenerator;
45 import com.octo.captcha.image.ImageCaptcha;
46 import com.octo.captcha.image.gimpy.Gimpy;
47
48
49
50
51
52 public class LuteceGimpyImageFactory extends com.octo.captcha.image.ImageCaptchaFactory
53 {
54 public static final String BUNDLE_QUESTION_KEY = Gimpy.class.getName( );
55 private Random _myRandom = new SecureRandom( );
56 private WordToImage _wordToImage;
57 private WordGenerator _wordGenerator;
58
59
60
61
62
63
64
65
66 public LuteceGimpyImageFactory( WordGenerator generator, WordToImage word2image )
67 {
68 if ( word2image == null )
69 {
70 throw new CaptchaException( "Invalid configuration" + " for a GimpyFactory : WordToImage can't be null" );
71 }
72
73 if ( generator == null )
74 {
75 throw new CaptchaException( "Invalid configuration" + " for a GimpyFactory : WordGenerator can't be null" );
76 }
77
78 _wordToImage = word2image;
79 _wordGenerator = generator;
80 }
81
82
83
84
85
86
87 public ImageCaptcha getImageCaptcha( )
88 {
89 return getImageCaptcha( Locale.getDefault( ) );
90 }
91
92
93
94
95
96 public WordToImage getWordToImage( )
97 {
98 return _wordToImage;
99 }
100
101
102
103
104
105 public WordGenerator getWordGenerator( )
106 {
107 return _wordGenerator;
108 }
109
110
111
112
113
114
115
116
117
118 public ImageCaptcha getImageCaptcha( Locale locale )
119 {
120
121 Integer wordLength = getRandomLength( );
122
123 String word = getWordGenerator( ).getWord( wordLength, locale );
124
125 BufferedImage image = null;
126
127 try
128 {
129 image = getWordToImage( ).getImage( word );
130 }
131 catch( Throwable e )
132 {
133 throw new CaptchaException( e );
134 }
135
136 ImageCaptcha captcha = new LuteceGimpyImage( CaptchaQuestionHelper.getQuestion( locale, BUNDLE_QUESTION_KEY ), image, word.toLowerCase( ) );
137
138 return captcha;
139 }
140
141
142
143
144
145 protected Integer getRandomLength( )
146 {
147 Integer wordLength;
148 int range = getWordToImage( ).getMaxAcceptedWordLength( ) - getWordToImage( ).getMinAcceptedWordLength( );
149 int randomRange = ( range != 0 ) ? _myRandom.nextInt( range + 1 ) : 0;
150 wordLength = Integer.valueOf( randomRange + getWordToImage( ).getMinAcceptedWordLength( ) );
151
152 return wordLength;
153 }
154 }