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.sound.gimpy;
8   
9   import java.security.SecureRandom;
10  import java.util.Locale;
11  import java.util.Random;
12  
13  import javax.sound.sampled.AudioInputStream;
14  
15  import com.octo.captcha.CaptchaException;
16  import com.octo.captcha.CaptchaQuestionHelper;
17  import com.octo.captcha.component.sound.wordtosound.WordToSound;
18  import com.octo.captcha.component.word.wordgenerator.WordGenerator;
19  import com.octo.captcha.sound.SoundCaptcha;
20  import com.octo.captcha.sound.SoundCaptchaFactory;
21  
22  /***
23   * @author Gandin Mathieu
24   * @author Benoit Doumas
25   * @version 1.1
26   */
27  public class GimpySoundFactory extends SoundCaptchaFactory {
28  
29      private WordGenerator wordGenerator;
30  
31      private WordToSound word2Sound;
32  
33      private Random myRandom = new SecureRandom();
34  
35      /***
36       * The bundle question key for CaptchaQuestionHelper
37       */
38      public static final String BUNDLE_QUESTION_KEY = GimpySound.class.getName();
39  
40      /***
41       * Construct a GimpySoundFactory from a word generator component and a wordtosound component
42       *
43       * @param thewordGenerator component
44       * @param theword2Sound    component
45       */
46      public GimpySoundFactory(WordGenerator thewordGenerator, WordToSound theword2Sound) {
47          if (thewordGenerator == null) {
48              throw new CaptchaException("Invalid configuration for a "
49                      + "GimpySoundFactory : WordGenerator can't be null");
50          }
51          if (theword2Sound == null) {
52              throw new CaptchaException("Invalid configuration for a "
53                      + "GimpySoundFactory : Word2Sound can't be null");
54          }
55          this.wordGenerator = thewordGenerator;
56          this.word2Sound = theword2Sound;
57      }
58  
59      public WordToSound getWordToSound() {
60          return this.word2Sound;
61      }
62  
63      public WordGenerator getWordGenerator() {
64          return this.wordGenerator;
65      }
66  
67      /***
68       * @return a Sound Captcha
69       */
70      public SoundCaptcha getSoundCaptcha() {
71          String word = this.wordGenerator.getWord(getRandomLength(), Locale.getDefault());
72          AudioInputStream sound = this.word2Sound.getSound(word);
73          SoundCaptcha soundCaptcha = new GimpySound(getQuestion(Locale
74                  .getDefault()), sound, word);
75          return soundCaptcha;
76      }
77  
78      /***
79       * @param locale the locale
80       * @return a localized sound captcha
81       */
82      public SoundCaptcha getSoundCaptcha(Locale locale) {
83          String word = this.wordGenerator.getWord(getRandomLength(), locale);
84          AudioInputStream sound = this.word2Sound.getSound(word, locale);
85          SoundCaptcha soundCaptcha = new GimpySound(getQuestion(locale), sound, word);
86          return soundCaptcha;
87      }
88  
89      protected String getQuestion(Locale locale) {
90          return CaptchaQuestionHelper.getQuestion(locale, BUNDLE_QUESTION_KEY);
91      }
92  
93      protected Integer getRandomLength() {
94          Integer wordLength;
95          int range = getWordToSound().getMaxAcceptedWordLength()
96                  - getWordToSound().getMinAcceptedWordLength();
97          int randomRange = range != 0 ? myRandom.nextInt(range + 1) : 0;
98          wordLength = new Integer(randomRange + getWordToSound().getMinAcceptedWordLength());
99          return wordLength;
100     }
101 
102 }