All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.nixer.nixerplugin.captcha.recaptcha.RecaptchaRestClient Maven / Gradle / Ivy

There is a newer version: 0.1.1.3
Show newest version
package io.nixer.nixerplugin.captcha.recaptcha;

import java.net.URI;
import java.net.URISyntaxException;

import io.nixer.nixerplugin.captcha.error.CaptchaErrors;
import org.apache.http.client.utils.URIBuilder;
import org.springframework.util.Assert;
import org.springframework.web.client.RestOperations;

/**
 * Client API for Google's Recaptcha verification endpoint
 * @see Developer Guid
 */
public class RecaptchaRestClient implements RecaptchaClient {

    private final RestOperations restTemplate;
    private final String verifyUrl;
    private final String recaptchaSecret;

    public RecaptchaRestClient(final RestOperations restTemplate, final RecaptchaProperties config) {
        Assert.notNull(restTemplate, "RestTemplate must not be null");
        this.restTemplate = restTemplate;

        Assert.notNull(config, "RecaptchaProperties must not be null");
        this.verifyUrl = config.getVerifyUrl();
        this.recaptchaSecret = config.getKey().getSecret();
    }

    public RecaptchaVerifyResponse call(final String captcha) {
        try {
            final URI url = captchaUri(captcha);
            return restTemplate.getForObject(url, RecaptchaVerifyResponse.class);
        } catch (Exception e) {
            throw CaptchaErrors.serviceFailure("Failed calling captcha verify", e);
        }
    }

    private URI captchaUri(final String captcha) throws URISyntaxException {
        return new URIBuilder(verifyUrl)
                .addParameter("secret", recaptchaSecret)
                .addParameter("response", captcha)
                .build();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy