public void validate(FacesContext facesContext, UIComponent uiComponent, Object value) throws ValidatorException {
if (value != null) {
LiferayFacesContext liferayFacesContext = LiferayFacesContext.getInstance();
try {
PortletRequest portletRequest = liferayFacesContext.getPortletRequest();
String userCaptchaTextValue = value.toString();
String correctCaptchaTextValue = (String) liferayFacesContext.getSessionAttribute(
WEB_KEYS_CAPTCHA_TEXT);
CaptchaPortletRequest captchaPortletRequest = new CaptchaPortletRequest(portletRequest,
userCaptchaTextValue);
// The CaptchaUtil.check(PortletRequest) method will ultimately call
// portletRequest.getParameter("captchaText") and so we have to pass a CaptchaPortletRequest to handle
// that. This is because the string "captchaText" is hard-coded in the liferay-ui:captcha JSP.
CaptchaUtil.check(captchaPortletRequest);
// Liferay Captcha implementations like SimpleCaptchaUtil will remove the "CAPTCHA_TEXT" session
// attribute when calling the Capatcha.check(PortletRequest) method. But this will cause a problem
// if we're using an Ajaxified input field. As a workaround, set the value of the attribute again.
liferayFacesContext.setSessionAttribute(WEB_KEYS_CAPTCHA_TEXT, correctCaptchaTextValue);
}
catch (CaptchaTextException e) {
String key = "text-verification-failed";
String summary = liferayFacesContext.getMessage(key);
FacesMessage facesMessage = new FacesMessage(FacesMessage.SEVERITY_ERROR, summary, summary);
throw new ValidatorException(facesMessage);
}
catch (CaptchaMaxChallengesException e) {
String key = "maximum-number-of-captcha-attempts-exceeded";
String summary = liferayFacesContext.getMessage(key);
FacesMessage facesMessage = new FacesMessage(FacesMessage.SEVERITY_ERROR, summary, summary);
throw new ValidatorException(facesMessage);
}
catch (Exception e) {
logger.error(e);
String key = "an-unexpected-error-occurred";
String summary = liferayFacesContext.getMessage(key);
FacesMessage facesMessage = new FacesMessage(FacesMessage.SEVERITY_ERROR, summary, summary);
throw new ValidatorException(facesMessage);
}
}
}