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.service.multitype;
8   
9   import java.awt.image.BufferedImage;
10  import java.util.Locale;
11  
12  import javax.sound.sampled.AudioInputStream;
13  
14  import com.octo.captcha.Captcha;
15  import com.octo.captcha.engine.CaptchaEngine;
16  import com.octo.captcha.image.ImageCaptcha;
17  import com.octo.captcha.service.AbstractManageableCaptchaService;
18  import com.octo.captcha.service.CaptchaServiceException;
19  import com.octo.captcha.service.captchastore.CaptchaStore;
20  import com.octo.captcha.service.captchastore.FastHashMapCaptchaStore;
21  import com.octo.captcha.sound.SoundCaptcha;
22  import com.octo.captcha.text.TextCaptcha;
23  
24  /***
25   * Generic and new default captchaService implementation. Can accept and serve any captcha type. <br> beware of class
26   * cast exception if you call the wrong typed getChallenge method!
27   *
28   * @author <a href="mailto:marc.antoine.garrigue@gmail.com">Marc-Antoine Garrigue</a>
29   * @version 1.0
30   */
31  public class GenericManageableCaptchaService extends AbstractManageableCaptchaService implements MultiTypeCaptchaService {
32  
33      /***
34       * Constructor with FastHashMapCaptchaStore
35       *
36       * @param captchaEngine the used engine. Use the {@link com.octo.captcha.engine.bufferedengine.BufferedEngineContainer}
37       *                      to enable buffered captcha generation
38       */
39      public GenericManageableCaptchaService(CaptchaEngine captchaEngine,
40                                             int minGuarantedStorageDelayInSeconds,
41                                             int maxCaptchaStoreSize,
42                                             int captchaStoreLoadBeforeGarbageCollection) {
43          this(new FastHashMapCaptchaStore(), captchaEngine, minGuarantedStorageDelayInSeconds,
44                  maxCaptchaStoreSize, captchaStoreLoadBeforeGarbageCollection);
45      }
46  
47      public GenericManageableCaptchaService(	CaptchaStore captchaStore, 
48      										CaptchaEngine captchaEngine,
49  								            int minGuarantedStorageDelayInSeconds,
50  								            int maxCaptchaStoreSize,
51  								            int captchaStoreLoadBeforeGarbageCollection) {
52      	super(captchaStore, captchaEngine, minGuarantedStorageDelayInSeconds,
53      			maxCaptchaStoreSize, captchaStoreLoadBeforeGarbageCollection);
54      }
55      
56      /***
57       * Method to retrive the image challenge corresponding to the given ticket.
58       *
59       * @param ID the ticket
60       *
61       * @return the challenge
62       *
63       * @throws com.octo.captcha.service.CaptchaServiceException
64       *          if the ticket is invalid
65       */
66      public BufferedImage getImageChallengeForID(String ID) throws CaptchaServiceException {
67          return (BufferedImage) this.getChallengeForID(ID);
68      }
69  
70      /***
71       * Method to retrive the image challenge corresponding to the given ticket.
72       *
73       * @param ID the ticket
74       *
75       * @return the challenge
76       *
77       * @throws com.octo.captcha.service.CaptchaServiceException
78       *          if the ticket is invalid
79       */
80      public BufferedImage getImageChallengeForID(String ID, Locale locale) throws CaptchaServiceException {
81          return (BufferedImage) this.getChallengeForID(ID, locale);
82      }
83  
84      /***
85       * Method to retrive the sound challenge corresponding to the given ticket.
86       *
87       * @param ID the ticket
88       *
89       * @return the challenge
90       *
91       * @throws com.octo.captcha.service.CaptchaServiceException
92       *          if the ticket is invalid
93       */
94      public AudioInputStream getSoundChallengeForID(String ID) throws CaptchaServiceException {
95          return (AudioInputStream) this.getChallengeForID(ID);
96      }
97  
98      /***
99       * Method to retrive the sound challenge corresponding to the given ticket.
100      *
101      * @param ID the ticket
102      *
103      * @return the challenge
104      *
105      * @throws com.octo.captcha.service.CaptchaServiceException
106      *          if the ticket is invalid
107      */
108     public AudioInputStream getSoundChallengeForID(String ID, Locale locale) throws CaptchaServiceException {
109         return (AudioInputStream) this.getChallengeForID(ID, locale);
110     }
111 
112     /***
113      * Method to retrive the text challenge corresponding to the given ticket.
114      *
115      * @param ID the ticket
116      *
117      * @return the challenge
118      *
119      * @throws com.octo.captcha.service.CaptchaServiceException
120      *          if the ticket is invalid
121      */
122     public String getTextChallengeForID(String ID) throws CaptchaServiceException {
123         return (String) this.getChallengeForID(ID);
124     }
125 
126     /***
127      * Method to retrieve the text challenge corresponding to the given ticket.
128      *
129      * @param ID the ticket
130      *
131      * @return the challenge
132      *
133      * @throws com.octo.captcha.service.CaptchaServiceException
134      *          if the ticket is invalid
135      */
136     public String getTextChallengeForID(String ID, Locale locale) throws CaptchaServiceException {
137         return (String) this.getChallengeForID(ID, locale);
138     }
139 
140     /***
141      * This method : Retrieve the challenge from the captcha Make and return a clone of the challenge Return the clone
142      * It has be design in order to let the service dipose the challenge of the captcha after rendering. It should be
143      * implemented for all captcha type (@see ImageCaptchaService implementations for exemple)
144      *
145      * @return a Challenge Clone
146      */
147     protected Object getChallengeClone(Captcha captcha) {
148         Class captchaClass = captcha.getClass();
149         if (ImageCaptcha.class.isAssignableFrom(captchaClass)) {
150             BufferedImage challenge = (BufferedImage) captcha.getChallenge();
151             BufferedImage clone = new BufferedImage(challenge.getWidth(), challenge.getHeight(), challenge.getType());
152             clone.getGraphics().drawImage(challenge, 0, 0, clone.getWidth(), clone.getHeight(), null);
153             clone.getGraphics().dispose();
154             return clone;
155         } else if (SoundCaptcha.class.isAssignableFrom(captchaClass)) {
156             AudioInputStream challenge = (AudioInputStream) captcha.getChallenge();
157             AudioInputStream clone = new AudioInputStream(challenge, challenge.getFormat(), challenge.getFrameLength());
158             return clone;
159         } else if (TextCaptcha.class.isAssignableFrom(captchaClass)) {
160             return String.valueOf(captcha.getChallenge());
161         } else {
162             throw new CaptchaServiceException("Unknown captcha type," +
163                     " can't clone challenge captchaClass:'" + captcha.getClass() + "'");
164         }
165 
166 
167     }
168 
169 
170 }