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.module.config;
8   
9   import javax.servlet.http.HttpServletRequest;
10  import java.util.ResourceBundle;
11  
12  /***
13   * Class that provides static utility method to use the Module configuration.
14   *
15   * @author <a href="mailto:mag@jcaptcha.net">Marc-Antoine Garrigue</a>
16   * @version 1.0
17   */
18  public class CaptchaModuleConfigHelper {
19  
20      /***
21       * method that get an id using the plugin configuration.<br/> It may be retrieved from the session via the getId()
22       * native method if the idType is set to 'session' or retrieved from the request using the 'idKey' parameter if the
23       * idType is set to 'generated';
24       */
25      public static String getId(HttpServletRequest httpServletRequest) {
26          String captchaID;
27          boolean generatedId = CaptchaModuleConfig.getInstance().getIdType().equals(CaptchaModuleConfig.ID_GENERATED) ?
28                  true : false;
29  
30          if (generatedId) {
31              //get it from the request
32              captchaID = httpServletRequest.getParameter(CaptchaModuleConfig.getInstance().getIdKey());
33  
34          } else {
35              //get captcha ID from the session!!
36              captchaID = httpServletRequest.getSession().getId();//theRequest.getParameter(captchaIDParameterName);
37          }
38          return captchaID;
39      }
40  
41      /***
42       * Method that return the fail or error message from the specified bundle or directly from the value specified
43       */
44  
45      public static String getMessage(HttpServletRequest httpServletRequest) {
46          String message = null;
47          boolean messageBundle =
48                  CaptchaModuleConfig.getInstance().getMessageType().equals(CaptchaModuleConfig.MESSAGE_TYPE_BUNDLE) ?
49                          true : false;
50          //get it from the bundle with the specified locale
51          if (messageBundle) {
52              ResourceBundle bundle = ResourceBundle.getBundle(CaptchaModuleConfig.getInstance().getMessageValue(),
53                      httpServletRequest.getLocale());
54              if (bundle != null) {
55                  message = bundle.getString(CaptchaModuleConfig.getInstance().getMessageKey());
56              }
57              //get it with no locale if still null
58              if (message == null) {
59                  bundle = ResourceBundle.getBundle(CaptchaModuleConfig.getInstance().getMessageValue());
60                  message = bundle.getString(CaptchaModuleConfig.getInstance().getMessageKey());
61              }
62  
63          } else {
64              //directly get the value
65              message = CaptchaModuleConfig.getInstance().getMessageValue();
66          }
67          return message;
68      }
69  
70  }