Project

General

Profile

1 45395 michele.ar
package eu.dnetlib.msro.workers.aggregation.collect.plugins.split;
2
3
import java.io.BufferedInputStream;
4
import java.io.IOException;
5
6
import org.apache.http.HttpStatus;
7
import org.apache.http.client.methods.CloseableHttpResponse;
8
import org.apache.http.client.methods.HttpGet;
9
import org.apache.http.impl.client.CloseableHttpClient;
10
import org.apache.http.impl.client.HttpClients;
11 45405 michele.ar
import org.springframework.stereotype.Component;
12 45395 michele.ar
13 45405 michele.ar
import eu.dnetlib.msro.workers.aggregation.collect.plugins.DnetCollectorParam;
14
import eu.dnetlib.msro.workers.aggregation.collect.plugins.DnetCollectorPlugin;
15 47707 michele.ar
import eu.dnetlib.msro.workflows.nodes.collect.CollectException;
16 45405 michele.ar
17
@Component
18
@DnetCollectorPlugin(value = "http", parameters = {
19
		@DnetCollectorParam("splitOnElement")
20
})
21 45395 michele.ar
public class HttpCollectorPlugin extends AbstractSplittedRecordPlugin {
22
23
	@Override
24
	protected BufferedInputStream getBufferedInputStream(final String baseUrl) throws CollectException {
25 45405 michele.ar
		try (CloseableHttpClient client = HttpClients.createDefault()) {
26 45395 michele.ar
			final HttpGet method = new HttpGet(baseUrl);
27 45405 michele.ar
			try (CloseableHttpResponse response = client.execute(method)) {
28
				final int statusCode = response.getStatusLine().getStatusCode();
29 45395 michele.ar
30 45405 michele.ar
				if (HttpStatus.SC_OK != statusCode) { throw new CollectException("Error " + statusCode + " dowloading url: " + baseUrl); }
31 45395 michele.ar
32
				return new BufferedInputStream(response.getEntity().getContent());
33
			}
34 45405 michele.ar
		} catch (final IOException e) {
35 45395 michele.ar
			throw new CollectException("Error dowloading url: " + baseUrl);
36
		}
37
	}
38
39
}