Revision 61048
Added by Alessia Bardi almost 3 years ago
modules/dnet-data-services/branches/saxonHE/src/main/java/eu/dnetlib/data/collector/plugins/HttpCollectorPlugin.java | ||
---|---|---|
1 | 1 |
package eu.dnetlib.data.collector.plugins; |
2 | 2 |
|
3 | 3 |
import java.io.BufferedInputStream; |
4 |
import java.io.ByteArrayInputStream; |
|
4 | 5 |
import java.io.IOException; |
6 |
import java.io.InputStream; |
|
7 |
|
|
5 | 8 |
import eu.dnetlib.rmi.data.CollectorServiceException; |
9 |
import org.apache.commons.io.IOUtils; |
|
6 | 10 |
import org.apache.http.HttpStatus; |
7 | 11 |
import org.apache.http.client.methods.CloseableHttpResponse; |
8 | 12 |
import org.apache.http.client.methods.HttpGet; |
... | ... | |
13 | 17 |
|
14 | 18 |
@Override |
15 | 19 |
protected BufferedInputStream getBufferedInputStream(final String baseUrl) throws CollectorServiceException { |
16 |
try(CloseableHttpClient client = HttpClients.createDefault()) { |
|
17 |
final HttpGet method = new HttpGet(baseUrl); |
|
18 |
try(CloseableHttpResponse response = client.execute(method)) { |
|
19 |
int statusCode = response.getStatusLine().getStatusCode(); |
|
20 |
final HttpGet method = new HttpGet(baseUrl); |
|
20 | 21 |
|
21 |
if (HttpStatus.SC_OK != statusCode) { |
|
22 |
throw new CollectorServiceException("Error " + statusCode + " downloading url: " + baseUrl); |
|
23 |
} |
|
22 |
try(CloseableHttpResponse response = HttpClients.createDefault().execute(method)) { |
|
24 | 23 |
|
25 |
return new BufferedInputStream(response.getEntity().getContent()); |
|
24 |
int responseCode = response.getStatusLine().getStatusCode(); |
|
25 |
|
|
26 |
if (HttpStatus.SC_OK != responseCode) { |
|
27 |
throw new CollectorServiceException("Error " + responseCode + " dowloading url: " + baseUrl); |
|
26 | 28 |
} |
29 |
|
|
30 |
byte[] content = IOUtils.toByteArray(response.getEntity().getContent()); |
|
31 |
|
|
32 |
try(InputStream in = new ByteArrayInputStream(content)) { |
|
33 |
return new BufferedInputStream(in); |
|
34 |
} |
|
27 | 35 |
} catch (IOException e) { |
28 |
throw new CollectorServiceException("Error downloading url: " + baseUrl);
|
|
36 |
throw new CollectorServiceException("Error dowloading url: " + baseUrl); |
|
29 | 37 |
} |
30 | 38 |
} |
31 | 39 |
|
Also available in: Unified diff
Updated to the latest version on dnet45 codebase: the stream is closed, need to copy the content