1
|
package eu.dnetlib.data.collector.plugins.fairsharing;
|
2
|
|
3
|
import java.util.Iterator;
|
4
|
import java.util.Queue;
|
5
|
import java.util.concurrent.PriorityBlockingQueue;
|
6
|
|
7
|
import org.apache.commons.io.IOUtils;
|
8
|
import org.apache.commons.logging.Log;
|
9
|
import org.apache.commons.logging.LogFactory;
|
10
|
import org.apache.http.client.methods.CloseableHttpResponse;
|
11
|
import org.apache.http.client.methods.HttpGet;
|
12
|
import org.apache.http.impl.client.CloseableHttpClient;
|
13
|
import org.apache.http.impl.client.HttpClients;
|
14
|
import org.json.JSONObject;
|
15
|
import org.json.XML;
|
16
|
|
17
|
import eu.dnetlib.data.collector.rmi.CollectorServiceException;
|
18
|
|
19
|
public class FairSharingIterator implements Iterator<String> {
|
20
|
|
21
|
private static final Log log = LogFactory.getLog(FairSharingIterator.class); // NOPMD by marko on 11/24/08 5:02 PM
|
22
|
|
23
|
private final Queue<String> queue = new PriorityBlockingQueue<>();
|
24
|
|
25
|
private final String baseUrl;
|
26
|
private final String authCode;
|
27
|
private final int pageSize;
|
28
|
|
29
|
private String nextUrl;
|
30
|
private boolean started;
|
31
|
|
32
|
public FairSharingIterator(final String baseUrl, final String authCode, final int pageSize) {
|
33
|
this.baseUrl = baseUrl;
|
34
|
this.authCode = authCode;
|
35
|
this.pageSize = pageSize;
|
36
|
this.started = false;
|
37
|
}
|
38
|
|
39
|
private void verifyStarted() {
|
40
|
if (!this.started) {
|
41
|
this.started = true;
|
42
|
try {
|
43
|
final String url = baseUrl + "/fairsharing_records/?page%5Bnumber%5D=1&page%5Bsize%5D=" + pageSize;
|
44
|
this.nextUrl = downloadPage(url);
|
45
|
} catch (final CollectorServiceException e) {
|
46
|
throw new RuntimeException(e);
|
47
|
}
|
48
|
}
|
49
|
}
|
50
|
|
51
|
@Override
|
52
|
public boolean hasNext() {
|
53
|
synchronized (queue) {
|
54
|
verifyStarted();
|
55
|
return !queue.isEmpty();
|
56
|
}
|
57
|
}
|
58
|
|
59
|
@Override
|
60
|
public String next() {
|
61
|
synchronized (queue) {
|
62
|
verifyStarted();
|
63
|
final String res = queue.poll();
|
64
|
while (queue.isEmpty() && nextUrl != null && !nextUrl.isEmpty()) {
|
65
|
try {
|
66
|
nextUrl = downloadPage(nextUrl);
|
67
|
} catch (final CollectorServiceException e) {
|
68
|
throw new RuntimeException(e);
|
69
|
}
|
70
|
}
|
71
|
return res;
|
72
|
}
|
73
|
}
|
74
|
|
75
|
@Override
|
76
|
public void remove() {}
|
77
|
|
78
|
private String downloadPage(final String url) throws CollectorServiceException {
|
79
|
log.debug("Fetching url: " + url);
|
80
|
|
81
|
final HttpGet req = new HttpGet(url);
|
82
|
req.addHeader("Accept", "application/json");
|
83
|
req.addHeader("Content-Type", "application/json");
|
84
|
req.addHeader("Authorization", "Bearer " + authCode);
|
85
|
|
86
|
try (final CloseableHttpClient client = HttpClients.createDefault()) {
|
87
|
try (final CloseableHttpResponse response = client.execute(req)) {
|
88
|
final String content = IOUtils.toString(response.getEntity().getContent());
|
89
|
final JSONObject obj = new JSONObject(content);
|
90
|
|
91
|
obj.getJSONArray("data")
|
92
|
.forEach(x -> queue.add(XML.toString(x, "record")));
|
93
|
|
94
|
final JSONObject links = obj.getJSONObject("links");
|
95
|
|
96
|
return links.isNull("next") ? null : links.getString("next");
|
97
|
}
|
98
|
} catch (final Exception e) {
|
99
|
throw new CollectorServiceException("Error perfoming call fro login", e);
|
100
|
}
|
101
|
}
|
102
|
|
103
|
}
|