View Javadoc
1   /*
2    * Copyright (c) 2002-2021, City of 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   * Factories for Gimpies. Built on top of WordGenerator and WordToImage. It uses thoses interfaces to build an ImageCaptha answered by a String and for which
50   * the question is : Spell the word.
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       * @param generator
62       *            the generator
63       * @param word2image
64       *            the word to image
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       * gimpies are ImageCaptcha
84       *
85       * @return the image captcha with default locale
86       */
87      public ImageCaptcha getImageCaptcha( )
88      {
89          return getImageCaptcha( Locale.getDefault( ) );
90      }
91  
92      /**
93       *
94       * @return the word to the image
95       */
96      public WordToImage getWordToImage( )
97      {
98          return _wordToImage;
99      }
100 
101     /**
102      *
103      * @return the word generator
104      */
105     public WordGenerator getWordGenerator( )
106     {
107         return _wordGenerator;
108     }
109 
110     /**
111      * gimpies are ImageCaptcha
112      *
113      * @param locale
114      *            the locale
115      *
116      * @return a pixCaptcha with the question :"spell the word"
117      */
118     public ImageCaptcha getImageCaptcha( Locale locale )
119     {
120         // length
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      * @return a random length for the word image
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 }