1
0
mirror of https://github.com/Rogiel/httpchannel synced 2025-12-06 07:32:50 +00:00

Implements CaptchaTrader service

This commit is contained in:
2012-01-17 12:28:22 -02:00
parent e943b08ede
commit 08d22a12fe
81 changed files with 2750 additions and 543 deletions

View File

@@ -0,0 +1,49 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>httpchannel-captcha</artifactId>
<groupId>com.rogiel.httpchannel</groupId>
<version>1.0.0</version>
<relativePath>..</relativePath>
</parent>
<groupId>com.rogiel.httpchannel.captcha</groupId>
<artifactId>httpchannel-captcha-captchatrader</artifactId>
<name>HttpChannel/Captcha/CaptchaTrader</name>
<description>This module provides an CaptchService that resolves image captchas.</description>
<dependencies>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.1.2</version>
<type>jar</type>
<scope>compile</scope>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.1.2</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1</version>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,207 @@
/**
*
*/
package com.captchatrader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import org.omg.CORBA.Any;
import com.captchatrader.exception.ApplicationKeyDisabledException;
import com.captchatrader.exception.CaptchaTraderException;
import com.captchatrader.exception.ConnectionLimitException;
import com.captchatrader.exception.DailyLimitException;
import com.captchatrader.exception.ImageTooLargeException;
import com.captchatrader.exception.IncorrectRespondsException;
import com.captchatrader.exception.InsuficientCreditsException;
import com.captchatrader.exception.InternalErrorException;
import com.captchatrader.exception.InvalidApplicationKeyException;
import com.captchatrader.exception.InvalidParametersException;
import com.captchatrader.exception.InvalidTicketException;
import com.captchatrader.exception.InvalidURLException;
import com.captchatrader.exception.InvalidUserException;
import com.captchatrader.exception.NotAnImageException;
import com.captchatrader.exception.SubmissionErrorException;
import com.captchatrader.exception.UserNotValidatedException;
/**
* @author <a href="http://www.rogiel.com">Rogiel</a>
*
*/
public class CaptchaTrader {
/**
* The {@link HttpClient} instance
*/
private final HttpClient client = new DefaultHttpClient();
private final JSONParser json = new JSONParser();
private final URI apiURL;
private final String applicationKey;
private final String username;
private final String password;
/**
* Creates a new API instance with the given application key
*
* @param applicationKey
* the key
*/
public CaptchaTrader(URI apiURL, String applicationKey, String username,
String password) {
this.apiURL = apiURL;
this.applicationKey = applicationKey;
this.username = username;
this.password = password;
}
/**
* Creates a new API instance with the given application key
*
* @param applicationKey
* the key
* @throws MalformedURLException
*/
public CaptchaTrader(String applicationKey, String username, String password) {
this(URI.create("http://api.captchatrader.com/"), applicationKey,
username, password);
}
/**
* Submit a CAPTCHA already hosted on an existing website.
*
* @param url
* The URL of the CAPTCHA image.
* @return The decoded CAPTCHA.
* @throws Any
* exceptions sent by the server.
*/
public ResolvedCaptcha submit(URL url) throws CaptchaTraderException,
IOException {
final URI requestUri = apiURL.resolve("submit");
final HttpPost request = new HttpPost(requestUri);
final MultipartEntity entity = new MultipartEntity();
entity.addPart("api_key", new StringBody(applicationKey));
entity.addPart("username", new StringBody(username));
entity.addPart("password", new StringBody(password));
entity.addPart("value", new StringBody(url.toString()));
request.setEntity(entity);
final List<Object> response = validate(execute(request));
return new ResolvedCaptcha(this, ((Long) response.get(0)).intValue(),
(String) response.get(1));
}
/**
* Responds if an CAPTCHA wes correctly answered or not
*
* @param captcha
* the CAPTCHA object
* @param state
* <code>true</code> if the CAPTCHA was correctly resolved
* @throws CaptchaTraderException
* any of the possible errors
*/
public void response(ResolvedCaptcha captcha, boolean state)
throws CaptchaTraderException, IOException {
final URI requestUri = apiURL.resolve("respond");
final HttpPost request = new HttpPost(requestUri);
final MultipartEntity entity = new MultipartEntity();
entity.addPart("is_correct", new StringBody(state ? "1" : "0"));
entity.addPart("username", new StringBody(username));
entity.addPart("password", new StringBody(password));
entity.addPart("ticket",
new StringBody(Integer.toString(captcha.getID())));
request.setEntity(entity);
validate(execute(request));
}
/**
* Checks the amount of resolutions available on a users account
*
* @return The decoded CAPTCHA.
* @throws Any
* exceptions sent by the server.
*/
public int getCredits() throws CaptchaTraderException, IOException {
return ((Number) validate(
execute(new HttpGet(apiURL.resolve("get_credits/username:"
+ username + "/password:" + password + "/")))).get(1))
.intValue();
}
private List<Object> validate(List<Object> response)
throws CaptchaTraderException {
if ((long) response.get(0) == -1) {
throw translateException((String) response.get(1));
}
return response;
}
@SuppressWarnings("unchecked")
private List<Object> execute(HttpUriRequest request) throws IOException {
final HttpResponse response = client.execute(request);
try {
return (List<Object>) json.parse(new InputStreamReader(response
.getEntity().getContent()));
} catch (IllegalStateException | ParseException e) {
throw new IOException(e);
}
}
private CaptchaTraderException translateException(String error) {
switch (error) {
case "API KEY DISABLED":
return new ApplicationKeyDisabledException();
case " CONNECTION LIMIT":
return new ConnectionLimitException();
case "DAILY LIMIT":
return new DailyLimitException();
case "IMAGE TOO LARGE":
return new ImageTooLargeException();
case "INSUFFICIENT CREDITS":
return new InsuficientCreditsException();
case "INTERNAL ERROR":
return new InternalErrorException();
case "INVALID API KEY":
return new InvalidApplicationKeyException();
case "INVALID PARAMETERS":
return new InvalidParametersException();
case "INVALID URL":
return new InvalidURLException();
case "INVALID USER":
return new InvalidUserException();
case "USER NOT VALIDATED":
return new UserNotValidatedException();
case "NOT AN IMAGE":
return new NotAnImageException();
case "SUBMISSION ERROR":
return new SubmissionErrorException();
case "INCORRECT REPORTS":
return new IncorrectRespondsException();
case "INVALID TICKET":
return new InvalidTicketException();
default:
return new CaptchaTraderException(error);
}
}
}

View File

@@ -0,0 +1,40 @@
/**
*
*/
package com.captchatrader;
import java.io.IOException;
import com.captchatrader.exception.CaptchaTraderException;
/**
* @author <a href="http://www.rogiel.com">Rogiel</a>
*
*/
public class ResolvedCaptcha {
private final CaptchaTrader api;
private final int id;
private final String answer;
public ResolvedCaptcha(CaptchaTrader api, int id, String answer) {
this.api = api;
this.id = id;
this.answer = answer;
}
public int getID() {
return id;
}
public String getAnswer() {
return answer;
}
public void valid() throws CaptchaTraderException, IOException {
api.response(this, true);
}
public void invalid() throws CaptchaTraderException, IOException {
api.response(this, true);
}
}

View File

@@ -0,0 +1,57 @@
/**
*
*/
package com.captchatrader.exception;
/**
* @author <a href="http://www.rogiel.com">Rogiel</a>
*
*/
public class ApplicationKeyDisabledException extends CaptchaTraderException {
private static final long serialVersionUID = 1L;
/**
*
*/
public ApplicationKeyDisabledException() {
// TODO Auto-generated constructor stub
}
/**
* @param message
*/
public ApplicationKeyDisabledException(String message) {
super(message);
// TODO Auto-generated constructor stub
}
/**
* @param cause
*/
public ApplicationKeyDisabledException(Throwable cause) {
super(cause);
// TODO Auto-generated constructor stub
}
/**
* @param message
* @param cause
*/
public ApplicationKeyDisabledException(String message, Throwable cause) {
super(message, cause);
// TODO Auto-generated constructor stub
}
/**
* @param message
* @param cause
* @param enableSuppression
* @param writableStackTrace
*/
public ApplicationKeyDisabledException(String message, Throwable cause,
boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
// TODO Auto-generated constructor stub
}
}

View File

@@ -0,0 +1,57 @@
/**
*
*/
package com.captchatrader.exception;
/**
* @author <a href="http://www.rogiel.com">Rogiel</a>
*
*/
public class CaptchaTraderException extends Exception {
private static final long serialVersionUID = 1L;
/**
*
*/
public CaptchaTraderException() {
// TODO Auto-generated constructor stub
}
/**
* @param message
*/
public CaptchaTraderException(String message) {
super(message);
// TODO Auto-generated constructor stub
}
/**
* @param cause
*/
public CaptchaTraderException(Throwable cause) {
super(cause);
// TODO Auto-generated constructor stub
}
/**
* @param message
* @param cause
*/
public CaptchaTraderException(String message, Throwable cause) {
super(message, cause);
// TODO Auto-generated constructor stub
}
/**
* @param message
* @param cause
* @param enableSuppression
* @param writableStackTrace
*/
public CaptchaTraderException(String message, Throwable cause,
boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
// TODO Auto-generated constructor stub
}
}

View File

@@ -0,0 +1,57 @@
/**
*
*/
package com.captchatrader.exception;
/**
* @author <a href="http://www.rogiel.com">Rogiel</a>
*
*/
public class ConnectionLimitException extends CaptchaTraderException {
private static final long serialVersionUID = 1L;
/**
*
*/
public ConnectionLimitException() {
// TODO Auto-generated constructor stub
}
/**
* @param message
*/
public ConnectionLimitException(String message) {
super(message);
// TODO Auto-generated constructor stub
}
/**
* @param cause
*/
public ConnectionLimitException(Throwable cause) {
super(cause);
// TODO Auto-generated constructor stub
}
/**
* @param message
* @param cause
*/
public ConnectionLimitException(String message, Throwable cause) {
super(message, cause);
// TODO Auto-generated constructor stub
}
/**
* @param message
* @param cause
* @param enableSuppression
* @param writableStackTrace
*/
public ConnectionLimitException(String message, Throwable cause,
boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
// TODO Auto-generated constructor stub
}
}

View File

@@ -0,0 +1,57 @@
/**
*
*/
package com.captchatrader.exception;
/**
* @author <a href="http://www.rogiel.com">Rogiel</a>
*
*/
public class DailyLimitException extends CaptchaTraderException {
private static final long serialVersionUID = 1L;
/**
*
*/
public DailyLimitException() {
// TODO Auto-generated constructor stub
}
/**
* @param message
*/
public DailyLimitException(String message) {
super(message);
// TODO Auto-generated constructor stub
}
/**
* @param cause
*/
public DailyLimitException(Throwable cause) {
super(cause);
// TODO Auto-generated constructor stub
}
/**
* @param message
* @param cause
*/
public DailyLimitException(String message, Throwable cause) {
super(message, cause);
// TODO Auto-generated constructor stub
}
/**
* @param message
* @param cause
* @param enableSuppression
* @param writableStackTrace
*/
public DailyLimitException(String message, Throwable cause,
boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
// TODO Auto-generated constructor stub
}
}

View File

@@ -0,0 +1,57 @@
/**
*
*/
package com.captchatrader.exception;
/**
* @author <a href="http://www.rogiel.com">Rogiel</a>
*
*/
public class ImageTooLargeException extends CaptchaTraderException {
private static final long serialVersionUID = 1L;
/**
*
*/
public ImageTooLargeException() {
// TODO Auto-generated constructor stub
}
/**
* @param message
*/
public ImageTooLargeException(String message) {
super(message);
// TODO Auto-generated constructor stub
}
/**
* @param cause
*/
public ImageTooLargeException(Throwable cause) {
super(cause);
// TODO Auto-generated constructor stub
}
/**
* @param message
* @param cause
*/
public ImageTooLargeException(String message, Throwable cause) {
super(message, cause);
// TODO Auto-generated constructor stub
}
/**
* @param message
* @param cause
* @param enableSuppression
* @param writableStackTrace
*/
public ImageTooLargeException(String message, Throwable cause,
boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
// TODO Auto-generated constructor stub
}
}

View File

@@ -0,0 +1,57 @@
/**
*
*/
package com.captchatrader.exception;
/**
* @author <a href="http://www.rogiel.com">Rogiel</a>
*
*/
public class IncorrectRespondsException extends CaptchaTraderException {
private static final long serialVersionUID = 1L;
/**
*
*/
public IncorrectRespondsException() {
// TODO Auto-generated constructor stub
}
/**
* @param message
*/
public IncorrectRespondsException(String message) {
super(message);
// TODO Auto-generated constructor stub
}
/**
* @param cause
*/
public IncorrectRespondsException(Throwable cause) {
super(cause);
// TODO Auto-generated constructor stub
}
/**
* @param message
* @param cause
*/
public IncorrectRespondsException(String message, Throwable cause) {
super(message, cause);
// TODO Auto-generated constructor stub
}
/**
* @param message
* @param cause
* @param enableSuppression
* @param writableStackTrace
*/
public IncorrectRespondsException(String message, Throwable cause,
boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
// TODO Auto-generated constructor stub
}
}

View File

@@ -0,0 +1,57 @@
/**
*
*/
package com.captchatrader.exception;
/**
* @author <a href="http://www.rogiel.com">Rogiel</a>
*
*/
public class InsuficientCreditsException extends CaptchaTraderException {
private static final long serialVersionUID = 1L;
/**
*
*/
public InsuficientCreditsException() {
// TODO Auto-generated constructor stub
}
/**
* @param message
*/
public InsuficientCreditsException(String message) {
super(message);
// TODO Auto-generated constructor stub
}
/**
* @param cause
*/
public InsuficientCreditsException(Throwable cause) {
super(cause);
// TODO Auto-generated constructor stub
}
/**
* @param message
* @param cause
*/
public InsuficientCreditsException(String message, Throwable cause) {
super(message, cause);
// TODO Auto-generated constructor stub
}
/**
* @param message
* @param cause
* @param enableSuppression
* @param writableStackTrace
*/
public InsuficientCreditsException(String message, Throwable cause,
boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
// TODO Auto-generated constructor stub
}
}

View File

@@ -0,0 +1,57 @@
/**
*
*/
package com.captchatrader.exception;
/**
* @author <a href="http://www.rogiel.com">Rogiel</a>
*
*/
public class InternalErrorException extends CaptchaTraderException {
private static final long serialVersionUID = 1L;
/**
*
*/
public InternalErrorException() {
// TODO Auto-generated constructor stub
}
/**
* @param message
*/
public InternalErrorException(String message) {
super(message);
// TODO Auto-generated constructor stub
}
/**
* @param cause
*/
public InternalErrorException(Throwable cause) {
super(cause);
// TODO Auto-generated constructor stub
}
/**
* @param message
* @param cause
*/
public InternalErrorException(String message, Throwable cause) {
super(message, cause);
// TODO Auto-generated constructor stub
}
/**
* @param message
* @param cause
* @param enableSuppression
* @param writableStackTrace
*/
public InternalErrorException(String message, Throwable cause,
boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
// TODO Auto-generated constructor stub
}
}

View File

@@ -0,0 +1,57 @@
/**
*
*/
package com.captchatrader.exception;
/**
* @author <a href="http://www.rogiel.com">Rogiel</a>
*
*/
public class InvalidApplicationKeyException extends CaptchaTraderException {
private static final long serialVersionUID = 1L;
/**
*
*/
public InvalidApplicationKeyException() {
// TODO Auto-generated constructor stub
}
/**
* @param message
*/
public InvalidApplicationKeyException(String message) {
super(message);
// TODO Auto-generated constructor stub
}
/**
* @param cause
*/
public InvalidApplicationKeyException(Throwable cause) {
super(cause);
// TODO Auto-generated constructor stub
}
/**
* @param message
* @param cause
*/
public InvalidApplicationKeyException(String message, Throwable cause) {
super(message, cause);
// TODO Auto-generated constructor stub
}
/**
* @param message
* @param cause
* @param enableSuppression
* @param writableStackTrace
*/
public InvalidApplicationKeyException(String message, Throwable cause,
boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
// TODO Auto-generated constructor stub
}
}

View File

@@ -0,0 +1,57 @@
/**
*
*/
package com.captchatrader.exception;
/**
* @author <a href="http://www.rogiel.com">Rogiel</a>
*
*/
public class InvalidParametersException extends CaptchaTraderException {
private static final long serialVersionUID = 1L;
/**
*
*/
public InvalidParametersException() {
// TODO Auto-generated constructor stub
}
/**
* @param message
*/
public InvalidParametersException(String message) {
super(message);
// TODO Auto-generated constructor stub
}
/**
* @param cause
*/
public InvalidParametersException(Throwable cause) {
super(cause);
// TODO Auto-generated constructor stub
}
/**
* @param message
* @param cause
*/
public InvalidParametersException(String message, Throwable cause) {
super(message, cause);
// TODO Auto-generated constructor stub
}
/**
* @param message
* @param cause
* @param enableSuppression
* @param writableStackTrace
*/
public InvalidParametersException(String message, Throwable cause,
boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
// TODO Auto-generated constructor stub
}
}

View File

@@ -0,0 +1,57 @@
/**
*
*/
package com.captchatrader.exception;
/**
* @author <a href="http://www.rogiel.com">Rogiel</a>
*
*/
public class InvalidTicketException extends CaptchaTraderException {
private static final long serialVersionUID = 1L;
/**
*
*/
public InvalidTicketException() {
// TODO Auto-generated constructor stub
}
/**
* @param message
*/
public InvalidTicketException(String message) {
super(message);
// TODO Auto-generated constructor stub
}
/**
* @param cause
*/
public InvalidTicketException(Throwable cause) {
super(cause);
// TODO Auto-generated constructor stub
}
/**
* @param message
* @param cause
*/
public InvalidTicketException(String message, Throwable cause) {
super(message, cause);
// TODO Auto-generated constructor stub
}
/**
* @param message
* @param cause
* @param enableSuppression
* @param writableStackTrace
*/
public InvalidTicketException(String message, Throwable cause,
boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
// TODO Auto-generated constructor stub
}
}

View File

@@ -0,0 +1,57 @@
/**
*
*/
package com.captchatrader.exception;
/**
* @author <a href="http://www.rogiel.com">Rogiel</a>
*
*/
public class InvalidURLException extends CaptchaTraderException {
private static final long serialVersionUID = 1L;
/**
*
*/
public InvalidURLException() {
// TODO Auto-generated constructor stub
}
/**
* @param message
*/
public InvalidURLException(String message) {
super(message);
// TODO Auto-generated constructor stub
}
/**
* @param cause
*/
public InvalidURLException(Throwable cause) {
super(cause);
// TODO Auto-generated constructor stub
}
/**
* @param message
* @param cause
*/
public InvalidURLException(String message, Throwable cause) {
super(message, cause);
// TODO Auto-generated constructor stub
}
/**
* @param message
* @param cause
* @param enableSuppression
* @param writableStackTrace
*/
public InvalidURLException(String message, Throwable cause,
boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
// TODO Auto-generated constructor stub
}
}

View File

@@ -0,0 +1,57 @@
/**
*
*/
package com.captchatrader.exception;
/**
* @author <a href="http://www.rogiel.com">Rogiel</a>
*
*/
public class InvalidUserException extends CaptchaTraderException {
private static final long serialVersionUID = 1L;
/**
*
*/
public InvalidUserException() {
// TODO Auto-generated constructor stub
}
/**
* @param message
*/
public InvalidUserException(String message) {
super(message);
// TODO Auto-generated constructor stub
}
/**
* @param cause
*/
public InvalidUserException(Throwable cause) {
super(cause);
// TODO Auto-generated constructor stub
}
/**
* @param message
* @param cause
*/
public InvalidUserException(String message, Throwable cause) {
super(message, cause);
// TODO Auto-generated constructor stub
}
/**
* @param message
* @param cause
* @param enableSuppression
* @param writableStackTrace
*/
public InvalidUserException(String message, Throwable cause,
boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
// TODO Auto-generated constructor stub
}
}

View File

@@ -0,0 +1,57 @@
/**
*
*/
package com.captchatrader.exception;
/**
* @author <a href="http://www.rogiel.com">Rogiel</a>
*
*/
public class NotAnImageException extends CaptchaTraderException {
private static final long serialVersionUID = 1L;
/**
*
*/
public NotAnImageException() {
// TODO Auto-generated constructor stub
}
/**
* @param message
*/
public NotAnImageException(String message) {
super(message);
// TODO Auto-generated constructor stub
}
/**
* @param cause
*/
public NotAnImageException(Throwable cause) {
super(cause);
// TODO Auto-generated constructor stub
}
/**
* @param message
* @param cause
*/
public NotAnImageException(String message, Throwable cause) {
super(message, cause);
// TODO Auto-generated constructor stub
}
/**
* @param message
* @param cause
* @param enableSuppression
* @param writableStackTrace
*/
public NotAnImageException(String message, Throwable cause,
boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
// TODO Auto-generated constructor stub
}
}

View File

@@ -0,0 +1,57 @@
/**
*
*/
package com.captchatrader.exception;
/**
* @author <a href="http://www.rogiel.com">Rogiel</a>
*
*/
public class SubmissionErrorException extends CaptchaTraderException {
private static final long serialVersionUID = 1L;
/**
*
*/
public SubmissionErrorException() {
// TODO Auto-generated constructor stub
}
/**
* @param message
*/
public SubmissionErrorException(String message) {
super(message);
// TODO Auto-generated constructor stub
}
/**
* @param cause
*/
public SubmissionErrorException(Throwable cause) {
super(cause);
// TODO Auto-generated constructor stub
}
/**
* @param message
* @param cause
*/
public SubmissionErrorException(String message, Throwable cause) {
super(message, cause);
// TODO Auto-generated constructor stub
}
/**
* @param message
* @param cause
* @param enableSuppression
* @param writableStackTrace
*/
public SubmissionErrorException(String message, Throwable cause,
boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
// TODO Auto-generated constructor stub
}
}

View File

@@ -0,0 +1,57 @@
/**
*
*/
package com.captchatrader.exception;
/**
* @author <a href="http://www.rogiel.com">Rogiel</a>
*
*/
public class UserNotValidatedException extends CaptchaTraderException {
private static final long serialVersionUID = 1L;
/**
*
*/
public UserNotValidatedException() {
// TODO Auto-generated constructor stub
}
/**
* @param message
*/
public UserNotValidatedException(String message) {
super(message);
// TODO Auto-generated constructor stub
}
/**
* @param cause
*/
public UserNotValidatedException(Throwable cause) {
super(cause);
// TODO Auto-generated constructor stub
}
/**
* @param message
* @param cause
*/
public UserNotValidatedException(String message, Throwable cause) {
super(message, cause);
// TODO Auto-generated constructor stub
}
/**
* @param message
* @param cause
* @param enableSuppression
* @param writableStackTrace
*/
public UserNotValidatedException(String message, Throwable cause,
boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
// TODO Auto-generated constructor stub
}
}

View File

@@ -0,0 +1,117 @@
/**
*
*/
package com.rogiel.httpchannel.captcha.impl;
import java.io.IOException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.captchatrader.CaptchaTrader;
import com.captchatrader.ResolvedCaptcha;
import com.captchatrader.exception.CaptchaTraderException;
import com.rogiel.httpchannel.captcha.ImageCaptcha;
import com.rogiel.httpchannel.captcha.ImageCaptchaService;
import com.rogiel.httpchannel.captcha.exception.AuthenticationCaptchaServiceException;
import com.rogiel.httpchannel.captcha.exception.InvalidCaptchaServiceException;
import com.rogiel.httpchannel.captcha.exception.UnsolvableCaptchaServiceException;
/**
* Implements CAPTCHA solving using CaptchaTrader.com service. If
* username-password authentication is not desired, send the API key as username
* and <code>null</code> password.
*
* @author <a href="http://www.rogiel.com">Rogiel</a>
*/
public class CaptchaTraderService implements ImageCaptchaService {
private static final Logger logger = LoggerFactory
.getLogger(CaptchaTraderService.class);
private static final String APP_KEY = "2acc44805ec208cc4d6b00c75a414996";
/**
* The current application key to be used
*/
private String currentApplicationKey = APP_KEY;
/**
* The CaptchaTrader.com API object
*/
private CaptchaTrader api;
@Override
public int authenticate(String username, String password)
throws AuthenticationCaptchaServiceException {
api = new CaptchaTrader(currentApplicationKey, username, password);
logger.debug("Authenticating into CaptchaTrader");
try {
// this will validate the account
return (int) Math.ceil(((double) api.getCredits()) / 10);
} catch (IOException | CaptchaTraderException e) {
throw new AuthenticationCaptchaServiceException(e);
}
}
@Override
public void solve(ImageCaptcha captcha)
throws UnsolvableCaptchaServiceException {
try {
logger.debug("Resolving CAPTCHA {}", captcha.getImageURL());
final ResolvedCaptcha resolved = api.submit(captcha.getImageURL());
captcha.setAnswer(resolved.getAnswer());
captcha.setAttachment(resolved);
logger.debug("CAPTCHA solved, answer is \"{}\"",
resolved.getAnswer());
} catch (IOException | CaptchaTraderException e) {
throw new UnsolvableCaptchaServiceException(e);
}
}
@Override
public void valid(ImageCaptcha captcha)
throws InvalidCaptchaServiceException {
final Object attachment = captcha.getAttachment();
if (attachment instanceof ResolvedCaptcha) {
try {
logger.debug("Notifying server that CAPTCHA {} is valid",
captcha);
((ResolvedCaptcha) attachment).valid();
} catch (CaptchaTraderException | IOException e) {
throw new InvalidCaptchaServiceException(e);
}
} else {
throw new InvalidCaptchaServiceException();
}
}
@Override
public void invalid(ImageCaptcha captcha)
throws InvalidCaptchaServiceException {
final Object attachment = captcha.getAttachment();
if (attachment instanceof ResolvedCaptcha) {
try {
logger.debug("Notifying server that CAPTCHA {} is invalid",
captcha);
((ResolvedCaptcha) attachment).invalid();
} catch (CaptchaTraderException | IOException e) {
throw new InvalidCaptchaServiceException(e);
}
} else {
throw new InvalidCaptchaServiceException();
}
}
/**
* Sets the CaptchaTrader API key. Each application should provide a key,
* otherwise the default "HttpChannel" key will be used.
*
* @param key
* the application key
*/
public void setApplicationKey(String key) {
if (key == null)
key = APP_KEY;
logger.debug("Setting new application key as {}", key);
currentApplicationKey = key;
}
}

View File

@@ -0,0 +1,42 @@
/**
*
*/
package com.captchatrader;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.Properties;
import org.junit.Test;
/**
* @author <a href="http://www.rogiel.com">Rogiel</a>
*
*/
public class CaptchaTraderTest {
/**
* Test method for
* {@link com.captchatrader.old.CaptchaTrader#respond(boolean)}.
*
* @throws Exception
*/
@Test
public void testRespond() throws Exception {
final Properties p = new Properties();
p.load(Files.newInputStream(
Paths.get("../../httpchannel-captcha/src/test/resources/captchatrader.properties"),
StandardOpenOption.READ));
final CaptchaTrader api = new CaptchaTrader(
"2acc44805ec208cc4d6b00c75a414996", p.getProperty("username"),
p.getProperty("password"));
final ResolvedCaptcha resolved = api
.submit(new URL(
"http://www.google.com/recaptcha/api/image?c=03AHJ_VusNSxAzZgs9OEvH79rOWOFDYXE2ElE5qkCr9kFU-ZU7gqy72tqEL3j_qCLYwdXgh4jaxU1iECISuUwt0zHbelni-lq8c7RVGSjUtJiMyHwlTTsG5CxWKIEus--yy3GPvwaW9l4N7hFnT57lLq272EOxcFDGYA"));
System.out.println(resolved);
resolved.valid();
}
}

View File

@@ -1,24 +0,0 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>httpchannel-capcha</artifactId>
<groupId>com.rogiel.httpchannel</groupId>
<version>1.0.0</version>
<relativePath>..</relativePath>
</parent>
<artifactId>httpchannel-captcha-recaptcha</artifactId>
<name>HttpChannel/CaptchaService/ReCaptcha</name>
<description>This module provides captcha resolving for Google ReCaptcha</description>
<dependencies>
<dependency>
<groupId>com.rogiel.httpchannel</groupId>
<artifactId>httpchannel-api</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>com.rogiel.httpchannel</groupId>
<artifactId>httpchannel-util</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
</project>

View File

@@ -1,36 +0,0 @@
/**
*
*/
package com.rogiel.httpchannel.captcha.impl;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.regex.Pattern;
import com.rogiel.httpchannel.util.PatternUtils;
/**
* @author <a href="http://www.rogiel.com">Rogiel</a>
*/
public class AjaxReCaptchaService extends BaseReCaptchaService {
private static final Pattern CAPTCHA_URL_PATTERN = Pattern
.compile("Recaptcha\\.create\\(\"([0-9A-z|_|\\-]*)\", ");
private static final String BASE_URL = "http://www.google.com/recaptcha/api/challenge?ajax=1&k=";
@Override
public ReCaptcha create(String content) throws MalformedURLException {
final String siteID = PatternUtils
.find(CAPTCHA_URL_PATTERN, content, 1);
try {
return super.create(get(BASE_URL + siteID).asString());
} catch (IOException e) {
return null;
}
}
@Override
public boolean resolve(ReCaptcha captcha) {
// not supported!
return false;
}
}

View File

@@ -1,34 +0,0 @@
/**
*
*/
package com.rogiel.httpchannel.captcha.impl;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.regex.Pattern;
import com.rogiel.httpchannel.captcha.AbstractImageCaptchaService;
import com.rogiel.httpchannel.util.PatternUtils;
/**
* @author <a href="http://www.rogiel.com">Rogiel</a>
*
*/
public class BaseReCaptchaService extends
AbstractImageCaptchaService<ReCaptcha> {
private static final Pattern CAPTCHA_IMAGE_PATTERN = Pattern
.compile("challenge : '(.*)'");
private static final String BASE_URL = "http://www.google.com/recaptcha/api/image?c=";
@Override
public ReCaptcha create(String content) throws MalformedURLException {
final String id = PatternUtils.find(CAPTCHA_IMAGE_PATTERN, content, 1);
return new ReCaptcha(new URL(BASE_URL + id), id);
}
@Override
public boolean resolve(ReCaptcha captcha) {
// not supported!
return false;
}
}

View File

@@ -1,41 +0,0 @@
/**
*
*/
package com.rogiel.httpchannel.captcha.impl;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.regex.Pattern;
import com.rogiel.httpchannel.util.htmlparser.HTMLPage;
/**
* @author <a href="http://www.rogiel.com">Rogiel</a>
*
*/
public class EmbeddedReCaptchaService extends BaseReCaptchaService {
private static final Pattern CAPTCHA_URL_PATTERN = Pattern
.compile("http://www\\.google\\.com/recaptcha/api/challenge\\?k=([0-9A-z|\\-]*)(&(.*))?");
@Override
public ReCaptcha create(String content) throws MalformedURLException {
try {
return create(HTMLPage.parse(content));
} catch (IOException e) {
return null;
}
}
public ReCaptcha create(HTMLPage page) throws IOException {
final String url = page.findScriptSrc(CAPTCHA_URL_PATTERN);
if (url == null)
return null;
return super.create(get(url).asString());
}
@Override
public boolean resolve(ReCaptcha captcha) {
// not supported!
return false;
}
}

View File

@@ -1,15 +0,0 @@
package com.rogiel.httpchannel.captcha.impl;
import java.net.URL;
import com.rogiel.httpchannel.captcha.AbstractImageCaptcha;
/**
* @author <a href="http://www.rogiel.com">Rogiel</a>
*
*/
public class ReCaptcha extends AbstractImageCaptcha {
public ReCaptcha(URL url, String ID) {
super(url, ID);
}
}

View File

@@ -1,25 +0,0 @@
/**
*
*/
package com.rogiel.httpchannel.captcha.impl;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import org.apache.commons.io.IOUtils;
import org.junit.Test;
/**
* @author <a href="http://www.rogiel.com">Rogiel</a>
*
*/
public class AjaxReCaptchaServiceTest {
@Test
public void test() throws MalformedURLException, IOException {
final String content = IOUtils.toString(new URL(
"http://www.uploadking.com/WM3PHD9JAY").openStream());
final AjaxReCaptchaService ajax = new AjaxReCaptchaService();
ajax.create(content);
}
}

View File

@@ -14,6 +14,13 @@
<description>This module provides implementations for captcha resolving</description>
<modules>
<module>httpchannel-captcha-recaptcha</module>
<module>httpchannel-captcha-captchatrader</module>
</modules>
<dependencies>
<dependency>
<groupId>com.rogiel.httpchannel</groupId>
<artifactId>httpchannel-api</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1 @@
/captchatrader.properties