View Javadoc

1   /*
2    * JCaptcha, the open source java framework for captcha definition and integration
3    * Copyright (c)  2007 jcaptcha.net. All Rights Reserved.
4    * See the LICENSE.txt file distributed with this package.
5    */
6   
7   package com.octo.captcha.text.math;
8   
9   import com.octo.captcha.CaptchaQuestionHelper;
10  import com.octo.captcha.text.TextCaptcha;
11  import com.octo.captcha.text.TextCaptchaFactory;
12  
13  import java.security.SecureRandom;
14  import java.util.Locale;
15  import java.util.Random;
16  
17  /***
18   * The simpliest text captcha<br/> <b>Do not use this in production!!!</b>
19   *
20   * @author <a href="mailto:marc.antoine.garrigue@gmail.com">Marc-Antoine Garrigue</a>
21   * @version 1.0
22   */
23  public class MathCaptchaFactory extends TextCaptchaFactory {
24  
25      private static final String BUNDLE_QUESTION_KEY = MathCaptcha.class.getName();
26  
27      Random myRamdom = new SecureRandom();
28  
29      public MathCaptchaFactory() {
30      }
31  
32      /***
33       * TextCaptcha.
34       *
35       * @return a Text captcha
36       */
37      public TextCaptcha getTextCaptcha() {
38          return getTextCaptcha(Locale.getDefault());
39      }
40  
41      /***
42       * a TextCaptcha.
43       *
44       * @return a localized TextCaptcha
45       */
46      public TextCaptcha getTextCaptcha(Locale locale) {
47          //build the challenge;
48          //get 2 int
49          int one = myRamdom.nextInt(50);
50          int two = myRamdom.nextInt(50);
51          TextCaptcha captcha = new MathCaptcha(getQuestion(locale), one + "+" + two, String.valueOf(one + two));
52  
53          return captcha;
54      }
55  
56      protected String getQuestion(Locale locale) {
57          return CaptchaQuestionHelper.getQuestion(locale, BUNDLE_QUESTION_KEY);
58      }
59  
60  
61  }