Project

General

Profile

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

    
3
import eu.dnetlib.data.collector.rmi.CollectorServiceException;
4
import org.apache.commons.io.IOUtils;
5
import org.apache.http.HttpStatus;
6
import org.apache.http.client.methods.CloseableHttpResponse;
7
import org.apache.http.client.methods.HttpGet;
8
import org.apache.http.impl.client.HttpClients;
9

    
10
import java.io.BufferedInputStream;
11
import java.io.ByteArrayInputStream;
12
import java.io.IOException;
13
import java.io.InputStream;
14

    
15
public class HttpCollectorPlugin extends AbstractSplittedRecordPlugin {
16

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

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

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

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

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

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

    
39
}
(7-7/8)