1
|
package eu.dnetlib.data.collector.plugins.eosc;
|
2
|
|
3
|
import org.apache.commons.lang3.math.NumberUtils;
|
4
|
import org.apache.http.client.methods.CloseableHttpResponse;
|
5
|
import org.apache.http.client.methods.HttpGet;
|
6
|
import org.apache.http.impl.client.CloseableHttpClient;
|
7
|
import org.apache.http.impl.client.HttpClients;
|
8
|
import org.dom4j.Document;
|
9
|
import org.dom4j.DocumentHelper;
|
10
|
import org.dom4j.Element;
|
11
|
import org.dom4j.Node;
|
12
|
import org.dom4j.io.SAXReader;
|
13
|
|
14
|
import eu.dnetlib.data.collector.plugin.AbstractCollectorPlugin;
|
15
|
import eu.dnetlib.data.collector.rmi.CollectorServiceException;
|
16
|
import eu.dnetlib.data.collector.rmi.InterfaceDescriptor;
|
17
|
|
18
|
public class EoscServicesPlugin extends AbstractCollectorPlugin {
|
19
|
|
20
|
@SuppressWarnings("unchecked")
|
21
|
@Override
|
22
|
public Iterable<String> collect(final InterfaceDescriptor interfaceDescriptor, final String fromDate, final String untilDate)
|
23
|
throws CollectorServiceException {
|
24
|
|
25
|
final String baseUrl = interfaceDescriptor.getBaseUrl();
|
26
|
final long maxProviders = NumberUtils.toLong(interfaceDescriptor.getParams().get("maxProviders"), 10000);
|
27
|
|
28
|
return () -> httpCall(baseUrl + "/provider/all?quantity=" + maxProviders)
|
29
|
.selectNodes("/Paging/results/results")
|
30
|
.stream()
|
31
|
.map(o -> processProvider(baseUrl, (Node) o))
|
32
|
.iterator();
|
33
|
}
|
34
|
|
35
|
private String processProvider(final String baseUrl, final Node nodeProv) {
|
36
|
final String orgId = nodeProv.valueOf("./id");
|
37
|
|
38
|
final Document newDoc = DocumentHelper.createDocument();
|
39
|
final Element newRoot = DocumentHelper.createElement("record");
|
40
|
|
41
|
newDoc.setRootElement(newRoot);
|
42
|
|
43
|
nodeProv.setName("organization");
|
44
|
newRoot.add(nodeProv.detach());
|
45
|
|
46
|
final Document docSrvs = httpCall(baseUrl + "/provider/services/" + orgId);
|
47
|
for (final Object o : docSrvs.selectNodes("/List/item")) {
|
48
|
final Node nodeSrv = (Node) o;
|
49
|
nodeSrv.setName("service");
|
50
|
newRoot.add(nodeSrv.detach());
|
51
|
}
|
52
|
|
53
|
return newDoc.asXML();
|
54
|
}
|
55
|
|
56
|
private Document httpCall(final String url) {
|
57
|
final SAXReader reader = new SAXReader();
|
58
|
|
59
|
final HttpGet req = new HttpGet(url);
|
60
|
req.addHeader("Accept", "application/xml");
|
61
|
|
62
|
try (final CloseableHttpClient client = HttpClients.createDefault()) {
|
63
|
try (final CloseableHttpResponse response = client.execute(req)) {
|
64
|
return reader.read(response.getEntity().getContent());
|
65
|
}
|
66
|
} catch (final Exception e) {
|
67
|
throw new RuntimeException(e);
|
68
|
}
|
69
|
}
|
70
|
|
71
|
}
|