Revision 42936
Added by Claudio Atzori almost 8 years ago
HttpCollectorPlugin.java | ||
---|---|---|
1 | 1 |
package eu.dnetlib.data.collector.plugins; |
2 | 2 |
|
3 | 3 |
import java.io.BufferedInputStream; |
4 |
|
|
4 |
import java.io.IOException; |
|
5 | 5 |
import eu.dnetlib.rmi.data.CollectorServiceException; |
6 |
import org.apache.commons.httpclient.HttpClient; |
|
7 |
import org.apache.commons.httpclient.HttpMethod; |
|
8 |
import org.apache.commons.httpclient.HttpStatus; |
|
9 |
import org.apache.commons.httpclient.methods.GetMethod; |
|
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; |
|
10 | 11 |
|
11 | 12 |
public class HttpCollectorPlugin extends AbstractSplittedRecordPlugin { |
12 | 13 |
|
13 | 14 |
@Override |
14 | 15 |
protected BufferedInputStream getBufferedInputStream(final String baseUrl) throws CollectorServiceException { |
15 |
try { |
|
16 |
final HttpClient client = new HttpClient();
|
|
17 |
final HttpMethod method = new GetMethod(baseUrl);
|
|
18 |
final int responseCode = client.executeMethod(method);
|
|
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();
|
|
19 | 20 |
|
20 |
if (HttpStatus.SC_OK != responseCode) { throw new CollectorServiceException("Error " + responseCode + " dowloading url: " + baseUrl); } |
|
21 |
return new BufferedInputStream(method.getResponseBodyAsStream()); |
|
22 |
} catch (Exception e) { |
|
21 |
if (HttpStatus.SC_OK != statusCode) { |
|
22 |
throw new CollectorServiceException("Error " + statusCode + " dowloading url: " + baseUrl); |
|
23 |
} |
|
24 |
|
|
25 |
return new BufferedInputStream(response.getEntity().getContent()); |
|
26 |
} |
|
27 |
} catch (IOException e) { |
|
23 | 28 |
throw new CollectorServiceException("Error dowloading url: " + baseUrl); |
24 | 29 |
} |
25 | 30 |
} |
Also available in: Unified diff
adapted to org.apache.httpcomponents:httpclient:4.4.1