1
|
package eu.dnetlib.dli.collector.plugin;
|
2
|
|
3
|
import org.antlr.stringtemplate.StringTemplate;
|
4
|
import org.apache.commons.io.IOUtils;
|
5
|
import org.apache.http.HttpEntity;
|
6
|
import org.apache.http.client.ResponseHandler;
|
7
|
import org.apache.http.client.config.RequestConfig;
|
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.HttpClientBuilder;
|
12
|
import org.apache.http.util.EntityUtils;
|
13
|
import org.junit.Ignore;
|
14
|
import org.junit.Test;
|
15
|
|
16
|
import java.io.IOException;
|
17
|
import java.io.InputStream;
|
18
|
|
19
|
public class CrossRefIteratorTest {
|
20
|
|
21
|
|
22
|
@Test
|
23
|
@Ignore
|
24
|
public void CrossRefIteratorTest() throws IOException {
|
25
|
final InputStream resourceAsStream = getClass().getResourceAsStream("/eu/dnetlib/dli/templates/Scholixv1.st");
|
26
|
|
27
|
|
28
|
final String template = IOUtils.toString(resourceAsStream);
|
29
|
final StringTemplate st = new StringTemplate(template);
|
30
|
|
31
|
CrossRefIterator ci = new CrossRefIterator(st);
|
32
|
int i =0;
|
33
|
while (ci.hasNext()){
|
34
|
ci.next();
|
35
|
|
36
|
i++;
|
37
|
if (i % 100 ==0 ) {
|
38
|
System.out.println("Read "+i);
|
39
|
}
|
40
|
}
|
41
|
}
|
42
|
|
43
|
|
44
|
private static ResponseHandler<String> responseHandler = response -> {
|
45
|
int status = response.getStatusLine().getStatusCode();
|
46
|
if (status >= 200 && status < 300) {
|
47
|
HttpEntity entity = response.getEntity();
|
48
|
return entity != null ? EntityUtils.toString(entity) : null;
|
49
|
} else {
|
50
|
System.out.println("Unexpected response status: " + status);
|
51
|
return null;
|
52
|
}
|
53
|
};
|
54
|
|
55
|
@Test
|
56
|
public void testGettingRequest() throws IOException {
|
57
|
final String url ="https://api.eventdata.crossref.org/v1/events/scholix?mailto=sandro.labruzzo@isti.cnr.it&rows=100";
|
58
|
int timeout = 300;
|
59
|
|
60
|
RequestConfig config = RequestConfig.custom()
|
61
|
.setConnectTimeout(timeout * 1000)
|
62
|
.setConnectionRequestTimeout(timeout * 1000)
|
63
|
.setSocketTimeout(timeout * 1000).build();
|
64
|
final CloseableHttpClient httpclient = HttpClientBuilder.create().setDefaultRequestConfig(config).build();
|
65
|
HttpGet httpGet = new HttpGet(url);
|
66
|
String s = httpclient.execute(httpGet, responseHandler);
|
67
|
|
68
|
System.out.println(s);
|
69
|
|
70
|
|
71
|
}
|
72
|
|
73
|
}
|