01: /*
02: * (C) Copyright 2000 - 2006 Nabh Information Systems, Inc.
03: *
04: * This program is free software; you can redistribute it and/or
05: * modify it under the terms of the GNU General Public License
06: * as published by the Free Software Foundation; either version 2
07: * of the License, or (at your option) any later version.
08: *
09: * This program is distributed in the hope that it will be useful,
10: * but WITHOUT ANY WARRANTY; without even the implied warranty of
11: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12: * GNU General Public License for more details.
13: *
14: * You should have received a copy of the GNU General Public License
15: * along with this program; if not, write to the Free Software
16: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17: *
18: */
19: package com.nabhinc.portlet.login;
20:
21: import javax.portlet.PortletException;
22: import javax.portlet.PortletRequest;
23: import javax.portlet.PortletSession;
24:
25: import org.w3c.dom.Element;
26:
27: import com.nabhinc.portal.login.LoginConstants;
28: import com.nabhinc.portal.model.PortalConfiguration;
29: import com.nabhinc.portlet.mvcportlet.core.ControllerPortletConfig;
30: import com.nabhinc.portlet.mvcportlet.core.RequestConfig;
31: import com.nabhinc.portlet.mvcportlet.validator.BaseValidator;
32: import com.octo.captcha.Captcha;
33:
34: /**
35: * Validate provided captcha answer (if required). A base parameter which
36: * indicates the captcha answer validation is required must be provided.
37: *
38: * @author Wirawan Chokry
39: * (c) 2006 Nabh Information Systems, Inc. All Rights Reserved.
40: */
41: public class CaptchaAnswerValidator extends BaseValidator {
42:
43: public void init(Element config, ControllerPortletConfig conConfig)
44: throws PortletException {
45: super .init(config, conConfig);
46: }
47:
48: public boolean validate(String fieldValue, PortletRequest request,
49: RequestConfig config) {
50:
51: if (!PortalConfiguration.getInstance()
52: .isRegistrationCaptchaEnabled())
53: return true;
54:
55: String answer = request
56: .getParameter(LoginConstants.CAPTCHA_ANSWER_PARAM);
57: if (answer == null || "".equals(answer))
58: return false;
59: Captcha expectedAnswer = (Captcha) request.getPortletSession()
60: .getAttribute(LoginConstants.CAPTCHA_ANSWER_PARAM,
61: PortletSession.APPLICATION_SCOPE);
62: if (expectedAnswer == null) { // Session expired ??
63: return false;
64: }
65: Boolean result = expectedAnswer.validateResponse(answer);
66: if (result.equals(Boolean.FALSE))
67: return false;
68:
69: return true;
70: }
71:
72: }
|