76 lines
2.3 KiB
Java
76 lines
2.3 KiB
Java
/*********************************************************************************************************************************
|
|
* Autorskie Prawa Majątkowe - Moose Spółka z ograniczoną odpowiedzialnością
|
|
*
|
|
* Copyright 2017 Moose Spółka z ograniczoną odpowiedzialnością
|
|
********************************************************************************************************************************/
|
|
|
|
package eu.mooseinc.dell.form.service.impl;
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
import eu.mooseinc.dell.form.service.CaptchaService;
|
|
import java.io.IOException;
|
|
import java.util.Map;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
import org.apache.http.client.fluent.Request;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
/**
|
|
* Implementacja serwisu do obsługi captcha.
|
|
*/
|
|
@Service
|
|
public class CaptchaServiceImpl implements CaptchaService {
|
|
/**
|
|
* Logger zdarzeń.
|
|
*/
|
|
private static final Logger logger = LoggerFactory.getLogger(CaptchaServiceImpl.class);
|
|
|
|
@Value("${captcha.secret}")
|
|
private String secret;
|
|
|
|
@Value("${captcha.url}")
|
|
private String url;
|
|
|
|
@Value("${captcha.timeout}")
|
|
private Integer timeout;
|
|
|
|
@Override
|
|
public boolean isCaptchaValid(final String captcha) {
|
|
if (StringUtils.isEmpty(captcha))
|
|
return false;
|
|
final String urlWithParams = createUrlWithParams(captcha);
|
|
try {
|
|
final String respons =
|
|
Request.Get(urlWithParams).connectTimeout(timeout).socketTimeout(timeout).execute().returnContent()
|
|
.asString();
|
|
ObjectMapper mapper = new ObjectMapper();
|
|
final Map map = mapper.readValue(respons, Map.class);
|
|
return (boolean) map.get("success");
|
|
}
|
|
catch (final IOException e) {
|
|
logger.warn("Wystąpił błąd podczas potwierdzenia captcha", e);
|
|
return true;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Tworzy adres z parametrami.
|
|
*
|
|
* @param captcha captcha użytkownika.
|
|
*
|
|
* @return adres z parametrami.
|
|
*/
|
|
private String createUrlWithParams(final String captcha) {
|
|
final StringBuilder urlWithParams = new StringBuilder();
|
|
urlWithParams.append(url);
|
|
urlWithParams.append("?secret=");
|
|
urlWithParams.append(secret);
|
|
urlWithParams.append("&response=");
|
|
urlWithParams.append(captcha);
|
|
return urlWithParams.toString();
|
|
}
|
|
}
|
|
|