package br.com.netsoft.captcha;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonPropertyOrder({ "success", "challenge_ts", "hostname", "error-codes" })
public class GoogleResponse {
@JsonProperty("success")
private boolean success;
@JsonProperty("challenge_ts")
private String challengeTs;
@JsonProperty("hostname")
private String hostname;
@JsonProperty("error-codes")
private ErrorCode[] errorCodes;
static enum ErrorCode {
MissingSecret, InvalidSecret, MissingResponse, InvalidResponse;
private static Map<String, ErrorCode> errorsMap = new HashMap<String, ErrorCode>(4);
static {
errorsMap.put("missing-input-secret", MissingSecret);
errorsMap.put("invalid-input-secret", InvalidSecret);
errorsMap.put("missing-input-response", MissingResponse);
errorsMap.put("invalid-input-response", InvalidResponse);
}
@JsonCreator
public static ErrorCode forValue(final String value) {
return errorsMap.get(value.toLowerCase());
}
}
@JsonProperty("success")
public boolean isSuccess() {
return success;
}
@JsonProperty("success")
public void setSuccess(boolean success) {
this.success = success;
}
@JsonProperty("challenge_ts")
public String getChallengeTs() {
return challengeTs;
}
@JsonProperty("challenge_ts")
public void setChallengeTs(String challengeTs) {
this.challengeTs = challengeTs;
}
@JsonProperty("hostname")
public String getHostname() {
return hostname;
}
@JsonProperty("hostname")
public void setHostname(String hostname) {
this.hostname = hostname;
}
@JsonProperty("error-codes")
public void setErrorCodes(ErrorCode[] errorCodes) {
this.errorCodes = errorCodes;
}
@JsonProperty("error-codes")
public ErrorCode[] getErrorCodes() {
return errorCodes;
}
@JsonIgnore
public boolean hasClientError() {
final ErrorCode[] errors = getErrorCodes();
if (errors == null) {
return false;
}
for (final ErrorCode error : errors) {
switch (error) {
case InvalidResponse:
case MissingResponse:
return true;
default:
break;
}
}
return false;
}
@Override
public String toString() {
return "GoogleResponse{" + "success=" + success + ", challengeTs='" + challengeTs + '\'' + ", hostname='"
+ hostname + '\'' + ", errorCodes=" + Arrays.toString(errorCodes) + '}';
}
}