Project

General

Profile

1
package eu.dnetlib.data.collector.plugins;
2

    
3
import java.io.BufferedInputStream;
4
import java.io.ByteArrayInputStream;
5
import java.io.IOException;
6
import java.io.InputStream;
7

    
8
import eu.dnetlib.rmi.data.CollectorServiceException;
9
import org.apache.commons.io.IOUtils;
10
import org.apache.http.HttpStatus;
11
import org.apache.http.client.methods.CloseableHttpResponse;
12
import org.apache.http.client.methods.HttpGet;
13
import org.apache.http.impl.client.CloseableHttpClient;
14
import org.apache.http.impl.client.HttpClients;
15

    
16
public class HttpCollectorPlugin extends AbstractSplittedRecordPlugin {
17

    
18
	@Override
19
	protected BufferedInputStream getBufferedInputStream(final String baseUrl) throws CollectorServiceException {
20
		final HttpGet method = new HttpGet(baseUrl);
21

    
22
		try(CloseableHttpResponse response = HttpClients.createDefault().execute(method)) {
23

    
24
			int responseCode = response.getStatusLine().getStatusCode();
25

    
26
			if (HttpStatus.SC_OK != responseCode) {
27
				throw new CollectorServiceException("Error " + responseCode + " dowloading url: " + baseUrl);
28
			}
29

    
30
			byte[] content = IOUtils.toByteArray(response.getEntity().getContent());
31

    
32
			try(InputStream in = new ByteArrayInputStream(content)) {
33
				return new BufferedInputStream(in);
34
			}
35
		} catch (IOException e) {
36
			throw new CollectorServiceException("Error dowloading url: " + baseUrl);
37
		}
38
	}
39

    
40
}
(7-7/8)