Example usage for com.liferay.portal.kernel.captcha CaptchaTextException CaptchaTextException

List of usage examples for com.liferay.portal.kernel.captcha CaptchaTextException CaptchaTextException

Introduction

In this page you can find the example usage for com.liferay.portal.kernel.captcha CaptchaTextException CaptchaTextException.

Prototype

public CaptchaTextException() 

Source Link

Usage

From source file:org.gfbio.ContactFormToHelpdeskPortlet.java

License:Open Source License

protected boolean validateChallenge(HttpServletRequest request) throws CaptchaException {

    String reCaptchaResponse = ParamUtil.getString(request, "g-recaptcha-response");
    //_log.info("validate Challenge response"+reCaptchaResponse);
    Http.Options options = new Http.Options();

    try {//from   w w  w.  ja va 2s.  c o m
        options.addPart("secret", PropsUtil.get("google.secretkey"));
    } catch (Exception se) {
        _log.error(se, se);
    }

    options.addPart("remoteip", request.getRemoteAddr());
    options.addPart("response", reCaptchaResponse);
    options.setLocation(PropsUtil.get("google.urlverify"));
    options.setPost(true);

    String content = null;

    try {
        content = HttpUtil.URLtoString(options);
        //_log.info("Content :"+content);
    } catch (IOException ioe) {
        _log.error(ioe, ioe);

        throw new CaptchaTextException();
    }

    if (content == null) {
        _log.error("reCAPTCHA did not return a result");

        throw new CaptchaTextException();
    }

    try {
        JSONObject jsonObject = JSONFactoryUtil.createJSONObject(content);

        String success = jsonObject.getString("success");

        if (StringUtil.equalsIgnoreCase(success, "true")) {
            return true;
        }

        JSONArray jsonArray = jsonObject.getJSONArray("error-codes");

        if ((jsonArray == null) || (jsonArray.length() == 0)) {
            return false;
        }

        StringBundler sb = new StringBundler(jsonArray.length() * 2 - 1);

        for (int i = 0; i < jsonArray.length(); i++) {
            sb.append(jsonArray.getString(i));

            if (i < (jsonArray.length() - 1)) {
                sb.append(StringPool.COMMA_AND_SPACE);
            }
        }

        _log.error("reCAPTCHA encountered an error: " + sb.toString());

        return false;
    } catch (JSONException jsone) {
        _log.error("reCAPTCHA did not return a valid result: " + content);

        throw new CaptchaTextException();
    }
}

From source file:org.politaktiv.captcha.MathCaptcha.java

License:Apache License

public void check(HttpServletRequest request) throws CaptchaException {
    if (!isEnabled(request)) {
        return;/*from   w ww .  j a v  a 2 s.  c o  m*/
    }

    HttpSession session = request.getSession();

    String captchaText = (String) session.getAttribute(CAPTCHA_TEXT);

    if (captchaText == null) {
        /*
        _log.error(
           "Captcha text is null. User " + request.getRemoteUser() +
              " may be trying to circumvent the captcha.");
        */

        throw new CaptchaTextException();
    }

    if (!captchaText.equals(ParamUtil.getString(request, "captchaText"))) {
        if ((CAPTCHA_MAX_CHALLENGES > 0) && (Validator.isNotNull(request.getRemoteUser()))) {

            Integer count = (Integer) session.getAttribute(CAPTCHA_COUNT);

            if (count == null) {
                count = new Integer(1);
            } else {
                count = new Integer(count.intValue() + 1);
            }

            session.setAttribute(CAPTCHA_COUNT, count);
        }

        throw new CaptchaTextException();
    }

    /*
    if (_log.isDebugEnabled()) {
       _log.debug("Captcha text is valid");
    }
    */

    session.removeAttribute(CAPTCHA_TEXT);
}

From source file:org.politaktiv.captcha.MathCaptcha.java

License:Apache License

public void check(PortletRequest portletRequest) throws CaptchaException {
    if (!isEnabled(portletRequest)) {
        return;/*from  www. j av a  2 s  .c o m*/
    }

    PortletSession portletSession = portletRequest.getPortletSession();

    String captchaText = (String) portletSession.getAttribute(CAPTCHA_TEXT);

    if (captchaText == null) {
        /*
        _log.error(
           "Captcha text is null. User " + portletRequest.getRemoteUser() +
              " may be trying to circumvent the captcha.");
        */
        throw new CaptchaTextException();
    }

    if (!captchaText.equals(ParamUtil.getString(portletRequest, "captchaText"))) {

        if ((CAPTCHA_MAX_CHALLENGES > 0) && (Validator.isNotNull(portletRequest.getRemoteUser()))) {

            Integer count = (Integer) portletSession.getAttribute(CAPTCHA_COUNT);

            if (count == null) {
                count = new Integer(1);
            } else {
                count = new Integer(count.intValue() + 1);
            }

            portletSession.setAttribute(CAPTCHA_COUNT, count);
        }

        throw new CaptchaTextException();
    }

    /*
    if (_log.isDebugEnabled()) {
       _log.debug("Captcha text is valid");
    }
    */

    portletSession.removeAttribute(CAPTCHA_TEXT);
}