package ru.decipher.extraction.impl;
import ru.decipher.extraction.CallbackHandler;
import ru.decipher.provider.http.HttpProviderWithProxySupportFactory;
import ru.decipher.provider.http.HttpRequest;
import ru.decipher.provider.http.impl.SimpleHttpProviderFactory;
import ru.decipher.proxy.impl.HttpProxyConfig;
import ru.decipher.proxy.impl.LocalhostProxyConfig;
import java.util.concurrent.Future;
/**
* Provides basic functionality to get access to HTTP scrapping
* <p/>
* User: Alexander Paderin (apocarteres@gmail.com)
* Date: 1/26/14
* Time: 10:40 PM
*/
public class HttpProcessor<T extends HttpRequest> extends GeneralProcessor<T> {
private static final int DEFAULT_PROVIDER_TIMEOUT = 5000;
private HttpProviderWithProxySupportFactory<T> providerWithProxySupportFactory;
public HttpProcessor(int workers, String description) {
this(workers, DEFAULT_PROVIDER_TIMEOUT, description);
}
public HttpProcessor(int workers, int providerTimeout, String description) {
super(workers, description);
this.providerWithProxySupportFactory = new SimpleHttpProviderFactory<>(providerTimeout);
}
public Future<T> submit(T request, CallbackHandler<T> callback) throws Exception {
return submit(request, new LocalhostProxyConfig(), callback);
}
public Future<T> submit(T request, HttpProxyConfig proxy, CallbackHandler<T> callback) throws Exception {
GeneralTask<T, HttpProviderWithProxySupportFactory<T>> task = new HttpProxyTask<>(proxy);
task.setFactory(providerWithProxySupportFactory);
task.setRequest(request);
if (callback != null) {
task.registerHandler(callback);
}
return submit(task);
}
public void fetch(T request) throws Exception {
submit(request, null).get();
}
public void setProviderFactory(HttpProviderWithProxySupportFactory<T> providerWithProxySupportFactory) {
this.providerWithProxySupportFactory = providerWithProxySupportFactory;
}
}