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

org.loom.appengine.AppEngineRecaptchaServiceImpl Maven / Gradle / Ivy

The newest version!
package org.loom.appengine;

import static com.google.appengine.api.urlfetch.FetchOptions.Builder.disallowTruncate;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.Map;

import javax.inject.Singleton;

import org.loom.addons.recaptcha.AbstractRecaptchaService;
import org.loom.addons.recaptcha.RecaptchaService;

import com.google.appengine.api.urlfetch.HTTPMethod;
import com.google.appengine.api.urlfetch.HTTPRequest;
import com.google.appengine.api.urlfetch.HTTPResponse;
import com.google.appengine.api.urlfetch.URLFetchService;
import com.google.appengine.api.urlfetch.URLFetchServiceFactory;

/**
 * {@link RecaptchaService} implementation to use the AppEngine API 
 * @author icoloma
 */
@Singleton
public class AppEngineRecaptchaServiceImpl extends AbstractRecaptchaService {

	private URLFetchService urlFetch = URLFetchServiceFactory.getURLFetchService();
	
	@Override
	protected byte[] getValidationResult(Map params) {
		try {
			// concatenate parameter values
			StringBuilder builder = new StringBuilder();
			for (Map.Entry entry : params.entrySet()) {
				if (builder.length() > 0) {
					builder.append('&');
				}
				builder.append(entry.getKey()).append('=').append(URLEncoder.encode(entry.getValue(), "UTF-8"));
			}
			
			// launch request
			HTTPRequest request = new HTTPRequest(VERIFY_URL, HTTPMethod.POST, disallowTruncate());
			request.setPayload(params.toString().getBytes("UTF-8"));
			HTTPResponse httpResponse = urlFetch.fetch(request);
			
			// the format of the response message is at
			// http://recaptcha.net/apidocs/captcha/
			return httpResponse.getContent();
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy