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.image;
8   
9   import com.octo.captcha.Captcha;
10  import com.octo.captcha.service.AbstractManageableCaptchaService;
11  import com.octo.captcha.service.CaptchaServiceException;
12  import com.octo.captcha.service.captchastore.CaptchaStore;
13  
14  import java.awt.image.BufferedImage;
15  import java.util.Locale;
16  
17  /***
18   * Base implementation of the ImageCaptchaService.
19   *
20   * @author <a href="mailto:mag@jcaptcha.net">Marc-Antoine Garrigue</a>
21   * @version 1.0
22   */
23  public abstract class AbstractManageableImageCaptchaService extends AbstractManageableCaptchaService
24          implements ImageCaptchaService {
25  
26      protected AbstractManageableImageCaptchaService(CaptchaStore captchaStore,
27                                                      com.octo.captcha.engine.CaptchaEngine captchaEngine,
28                                                      int minGuarantedStorageDelayInSeconds,
29                                                      int maxCaptchaStoreSize,
30                                                      int captchaStoreLoadBeforeGarbageCollection) {
31          super(captchaStore, captchaEngine,
32                  minGuarantedStorageDelayInSeconds, maxCaptchaStoreSize,
33                  captchaStoreLoadBeforeGarbageCollection);
34      }
35  
36      /***
37       * Method to retrive the image challenge corresponding to the given ticket.
38       *
39       * @param ID the ticket
40       *
41       * @return the challenge
42       *
43       * @throws com.octo.captcha.service.CaptchaServiceException
44       *          if the ticket is invalid
45       */
46      public BufferedImage getImageChallengeForID(String ID) throws CaptchaServiceException {
47          return (BufferedImage) this.getChallengeForID(ID);
48      }
49  
50  
51      /***
52       * Method to retrive the image challenge corresponding to the given ticket.
53       *
54       * @param ID the ticket
55       *
56       * @return the challenge
57       *
58       * @throws com.octo.captcha.service.CaptchaServiceException
59       *          if the ticket is invalid
60       */
61      public BufferedImage getImageChallengeForID(String ID, Locale locale) throws CaptchaServiceException {
62          return (BufferedImage) this.getChallengeForID(ID, locale);
63      }
64  
65      /***
66       * This method must be implemented by sublcasses and : Retrieve the challenge from the captcha Make and return a
67       * clone of the challenge Return the clone It has be design in order to let the service dipose the challenge of the
68       * captcha after rendering. It should be implemented for all captcha type (@see ImageCaptchaService implementations
69       * for exemple)
70       *
71       * @return a Challenge Clone
72       */
73      protected Object getChallengeClone(Captcha captcha) {
74          BufferedImage challenge = (BufferedImage) captcha.getChallenge();
75          BufferedImage clone = new BufferedImage(challenge.getWidth(), challenge.getHeight(), challenge.getType());
76  
77          clone.getGraphics().drawImage(challenge, 0, 0, clone.getWidth(), clone.getHeight(), null);
78          clone.getGraphics().dispose();
79  
80  
81          return clone;
82      }
83  }