Project

General

Profile

1
package eu.dnetlib.msro.workers.aggregation.collect.plugins.split;
2

    
3
import java.io.BufferedInputStream;
4
import java.io.IOException;
5

    
6
import eu.dnetlib.msro.workers.aggregation.collect.CollectException;
7
import org.apache.http.HttpStatus;
8
import org.apache.http.client.methods.CloseableHttpResponse;
9
import org.apache.http.client.methods.HttpGet;
10
import org.apache.http.impl.client.CloseableHttpClient;
11
import org.apache.http.impl.client.HttpClients;
12

    
13
public class HttpCollectorPlugin extends AbstractSplittedRecordPlugin {
14

    
15
	@Override
16
	protected BufferedInputStream getBufferedInputStream(final String baseUrl) throws CollectException {
17
		try(CloseableHttpClient client = HttpClients.createDefault()) {
18
			final HttpGet method = new HttpGet(baseUrl);
19
			try(CloseableHttpResponse response = client.execute(method)) {
20
				int statusCode = response.getStatusLine().getStatusCode();
21

    
22
				if (HttpStatus.SC_OK != statusCode) {
23
					throw new CollectException("Error " + statusCode + " dowloading url: " + baseUrl);
24
				}
25

    
26
				return new BufferedInputStream(response.getEntity().getContent());
27
			}
28
		} catch (IOException e) {
29
			throw new CollectException("Error dowloading url: " + baseUrl);
30
		}
31
	}
32

    
33
}
(5-5/5)