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.image.gimpy;
8   
9   import com.octo.captcha.image.ImageCaptcha;
10  
11  import java.awt.image.BufferedImage;
12  import java.io.Serializable;
13  
14  /***
15   * <p>A Gimpy is an ImageCaptcha. It is also the most common captcha.</p> <ul> <li>Challenge type : image</li>
16   * <li>Response type : String</li> <li>Description : An image of a distorded word is shown. User have to recognize the
17   * word and to submit it.</li> </ul>
18   *
19   * @author <a href="mailto:mag@jcaptcha.net">Marc-Antoine Garrigue</a>
20   * @version 1.0
21   */
22  public class Gimpy extends ImageCaptcha implements Serializable {
23  
24  	private String response;
25      private boolean caseSensitive=true;
26  
27  
28      Gimpy(String question, BufferedImage challenge, String response, boolean caseSensitive) {
29          super(question, challenge);
30          this.response = response;
31          this.caseSensitive=caseSensitive;
32      }
33  
34      Gimpy(String question, BufferedImage challenge, String response) {
35          this(question, challenge, response, true);
36      }
37  
38      /***
39       * Validation routine from the CAPTCHA interface. this methods verify if the response is not null and a String and
40       * then compares the given response to the internal string.
41       *
42       * @return true if the given response equals the internal response, false otherwise.
43       */
44      public final Boolean validateResponse(final Object response) {
45          return (null != response && response instanceof String)
46                  ? validateResponse((String) response) : Boolean.FALSE;
47      }
48  
49      /***
50       * Very simple validation routine that compares the given response to the internal string.
51       *
52       * @return true if the given response equals the internal response, false otherwise.
53       */
54      private final Boolean validateResponse(final String response) {
55          return caseSensitive? response.equals(this.response) : response.equalsIgnoreCase(this.response);
56      }
57  
58  }