View Javadoc
1   /*
2    * Copyright (c) 2002-2014, Mairie de Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
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   * Factories for Gimpies. Built on top of WordGenerator and WordToImage. It uses thoses interfaces to build an
51   * ImageCaptha answered by a String and for which the question is : Spell the word.
52   */
53  public class LuteceGimpyImageFactory extends com.octo.captcha.image.ImageCaptchaFactory
54  {
55      public static final String BUNDLE_QUESTION_KEY = Gimpy.class.getName(  );
56      private Random _myRandom = new SecureRandom(  );
57      private WordToImage _wordToImage;
58      private WordGenerator _wordGenerator;
59  
60      /**
61       *
62       * @param generator the generator
63       * @param word2image the word to image
64       */
65      public LuteceGimpyImageFactory( WordGenerator generator, WordToImage word2image )
66      {
67          if ( word2image == null )
68          {
69              throw new CaptchaException( "Invalid configuration" + " for a GimpyFactory : WordToImage can't be null" );
70          }
71  
72          if ( generator == null )
73          {
74              throw new CaptchaException( "Invalid configuration" + " for a GimpyFactory : WordGenerator can't be null" );
75          }
76  
77          _wordToImage = word2image;
78          _wordGenerator = generator;
79      }
80  
81      /**
82       * gimpies are ImageCaptcha
83       *
84       * @return the image captcha with default locale
85       */
86      public ImageCaptcha getImageCaptcha(  )
87      {
88          return getImageCaptcha( Locale.getDefault(  ) );
89      }
90  
91      /**
92       *
93       * @return the word to the image
94       */
95      public WordToImage getWordToImage(  )
96      {
97          return _wordToImage;
98      }
99  
100     /**
101      *
102      * @return the word generator
103      */
104     public WordGenerator getWordGenerator(  )
105     {
106         return _wordGenerator;
107     }
108 
109     /**
110      * gimpies are ImageCaptcha
111      *
112      * @param locale the locale
113      *
114      * @return a pixCaptcha with the question :"spell the word"
115      */
116     public ImageCaptcha getImageCaptcha( Locale locale )
117     {
118         //length
119         Integer wordLength = getRandomLength(  );
120 
121         String word = getWordGenerator(  ).getWord( wordLength, locale );
122 
123         BufferedImage image = null;
124 
125         try
126         {
127             image = getWordToImage(  ).getImage( word );
128         }
129         catch ( Throwable e )
130         {
131             throw new CaptchaException( e );
132         }
133 
134         ImageCaptcha captcha = new LuteceGimpyImage( CaptchaQuestionHelper.getQuestion( locale, BUNDLE_QUESTION_KEY ),
135                 image, word.toLowerCase(  ) );
136 
137         return captcha;
138     }
139 
140     /**
141      *
142      * @return a random length for the word image
143      */
144     protected Integer getRandomLength(  )
145     {
146         Integer wordLength;
147         int range = getWordToImage(  ).getMaxAcceptedWordLength(  ) - getWordToImage(  ).getMinAcceptedWordLength(  );
148         int randomRange = ( range != 0 ) ? _myRandom.nextInt( range + 1 ) : 0;
149         wordLength = Integer.valueOf( randomRange + getWordToImage( ).getMinAcceptedWordLength( ) );
150 
151         return wordLength;
152     }
153 }