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 com.octo.captcha.module.CaptchaModuleException;
10  
11  import java.util.ResourceBundle;
12  
13  /***
14   * Configuration base class for modules.
15   *
16   * @author <a href="mailto:mag@jcaptcha.net">Marc-Antoine Garrigue</a>
17   * @version 1.0
18   */
19  public class CaptchaModuleConfig {
20  
21  
22      private static CaptchaModuleConfig instance = new CaptchaModuleConfig();
23  
24      public static CaptchaModuleConfig getInstance() {
25          return instance;
26      }
27  
28      /***
29       * Set the message type to be a bundle
30       */
31      public static final String MESSAGE_TYPE_BUNDLE = "bundle";
32  
33      /***
34       * Set the id to be generated
35       */
36      public static final String ID_GENERATED = "generated";
37      /***
38       * Set the message type to be a text
39       */
40      public static final String MESSAGE_TYPE_TEXT = "text";
41      /***
42       * Set the id to be retrieved from session id
43       */
44      public static final String ID_SESSION = "session";
45      /***
46       * Set the default JMX registration name
47       */
48      public static final String JMX_REGISTERING_NAME =
49              "com.octo.captcha.module.struts:object=CaptchaServicePlugin";
50  
51  
52      private CaptchaModuleConfig() {
53      }
54  
55      private Boolean registerToMbean = Boolean.FALSE;
56  
57      private String responseKey = "jcaptcha_response";
58  
59      private String serviceClass = "com.octo.captcha.service.image.DefaultManageableImageCaptchaService";
60  
61      private String messageType = com.octo.captcha.module.config.CaptchaModuleConfig.MESSAGE_TYPE_TEXT;
62  
63      private String messageValue = "You failed the jcaptcha test";
64  
65      private String messageKey = "jcaptcha_fail";
66  
67      private String idType = com.octo.captcha.module.config.CaptchaModuleConfig.ID_SESSION;
68  
69      private String idKey = "jcaptcha_id";
70  
71      /***
72       * @return The key parameter name, default jcatpcha_id
73       */
74      public String getIdKey() {
75          return idKey;
76      }
77  
78      public void setIdKey(String idKey) {
79          this.idKey = idKey;
80      }
81  
82      /***
83       * @return The message type parameter value, default text
84       */
85  
86      public String getMessageType() {
87          return messageType;
88      }
89  
90      public void setMessageType(String messageType) {
91          this.messageType = messageType;
92      }
93  
94      /***
95       * @return The message parameter value, default "you failed the captcha test"
96       */
97      public String getMessageValue() {
98          return messageValue;
99      }
100 
101     public void setMessageValue(String messageValue) {
102         this.messageValue = messageValue;
103     }
104 
105     /***
106      * @return The message parameter key, default "jcaptcha_fail"
107      */
108     public String getMessageKey() {
109         return messageKey;
110     }
111 
112     public void setMessageKey(String messageKey) {
113         this.messageKey = messageKey;
114     }
115 
116     /***
117      * @return The id generation type, default "session"
118      */
119     public String getIdType() {
120         return idType;
121     }
122 
123     public void setIdType(String idType) {
124         this.idType = idType;
125     }
126 
127     /***
128      * @return The jcaptcha service class name, default "jcaptcha_fail"
129      */
130     public String getServiceClass() {
131         return serviceClass;
132     }
133 
134     public void setServiceClass(String serviceClass) {
135         this.serviceClass = serviceClass;
136     }
137 
138     public String getResponseKey() {
139         return responseKey;
140     }
141 
142     public void setResponseKey(String responseKey) {
143         this.responseKey = responseKey;
144     }
145 
146     public Boolean getRegisterToMbean() {
147         return registerToMbean;
148     }
149 
150     public void setRegisterToMbean(Boolean registerToMbean) {
151         this.registerToMbean = registerToMbean;
152     }
153 
154 
155     public void validate() {
156 
157         //verify values
158         if (!(com.octo.captcha.module.config.CaptchaModuleConfig.MESSAGE_TYPE_TEXT.equals(messageType) || com.octo.captcha.module.config.CaptchaModuleConfig.MESSAGE_TYPE_BUNDLE.equals(messageType)))
159             throw new com.octo.captcha.service.CaptchaServiceException("messageType can " +
160                     "only be set to '" + com.octo.captcha.module.config.CaptchaModuleConfig.MESSAGE_TYPE_TEXT + "' or '" + com.octo.captcha.module.config.CaptchaModuleConfig.MESSAGE_TYPE_BUNDLE + "'");
161 
162         if (!(com.octo.captcha.module.config.CaptchaModuleConfig.ID_SESSION.equals(idType) || com.octo.captcha.module.config.CaptchaModuleConfig.ID_GENERATED.equals(idType)))
163             throw new com.octo.captcha.service.CaptchaServiceException("idType can " +
164                     "only be set to '" + com.octo.captcha.module.config.CaptchaModuleConfig.ID_SESSION + "' or '" + com.octo.captcha.module.config.CaptchaModuleConfig.ID_GENERATED + "'");
165 
166         if (messageValue == null) throw new CaptchaModuleException("messageValue cannot be null");
167 
168         if (messageKey == null || "".equals(messageKey))
169             throw new CaptchaModuleException("messageKey cannot be null or empty");
170 
171         if (responseKey == null || "".equals(responseKey))
172             throw new CaptchaModuleException("responseKey cannot be null or empty");
173 
174         if ((idType.equals(com.octo.captcha.module.config.CaptchaModuleConfig.ID_GENERATED)) && (idKey == null || "".equals(idKey)))
175             throw new com.octo.captcha.service.CaptchaServiceException("idKey cannot be null or empty when id is generated (ie idType='" + com.octo.captcha.module.config.CaptchaModuleConfig.ID_GENERATED + "'");
176 
177         //if message is in a bundle, try to load it
178         if (this.messageType.equals(com.octo.captcha.module.config.CaptchaModuleConfig.MESSAGE_TYPE_BUNDLE)) {
179             ResourceBundle bundle = ResourceBundle.getBundle(getMessageValue());
180             if (bundle == null) {
181                 throw new CaptchaModuleException("can't initialize module config with a unfound bundle : "
182                         + "resource bundle " + getMessageValue() + " has  not been found");
183             } else {
184                 if (bundle.getString(getMessageKey()) == null) {
185                     throw new CaptchaModuleException("can't initialize module config with a unfound message : "
186                             + "resource bundle " + getMessageValue() + " has  no key named :" + getMessageKey());
187                 }
188             }
189 
190         }
191 
192         // try to create the CaptchaService
193         try {
194             Class.forName(serviceClass).newInstance();
195         } catch (Throwable e) {
196             e.printStackTrace();
197             throw new CaptchaModuleException("Error during Service Class initialization", e);
198         }
199 
200 
201     }
202 }