1
|
package eu.dnetlib.data.collector.plugins.datacite;
|
2
|
|
3
|
|
4
|
import java.io.ByteArrayOutputStream;
|
5
|
import java.io.IOException;
|
6
|
import java.net.URL;
|
7
|
import java.util.ArrayDeque;
|
8
|
import java.util.Iterator;
|
9
|
import java.util.Objects;
|
10
|
import java.util.Queue;
|
11
|
import java.util.zip.DataFormatException;
|
12
|
import java.util.zip.Inflater;
|
13
|
|
14
|
import com.google.gson.Gson;
|
15
|
import com.google.gson.GsonBuilder;
|
16
|
import eu.dnetlib.data.collector.plugins.datacite.schema.DataciteSchema;
|
17
|
import eu.dnetlib.data.collector.plugins.datacite.schema.Result;
|
18
|
import org.apache.commons.codec.binary.Base64;
|
19
|
import org.apache.commons.io.IOUtils;
|
20
|
import org.apache.commons.lang3.StringUtils;
|
21
|
|
22
|
public class DataciteESIterator implements Iterator<String> {
|
23
|
|
24
|
|
25
|
private final long timestamp;
|
26
|
|
27
|
private String scrollId;
|
28
|
|
29
|
private Queue<String> currentPage;
|
30
|
|
31
|
private final Gson g = new GsonBuilder().create();
|
32
|
|
33
|
private String baseURL;
|
34
|
|
35
|
private static final String START_PATH = "new_scan";
|
36
|
private static final String NEXT_PATH = "scan/%s";
|
37
|
|
38
|
|
39
|
public DataciteESIterator(long timestamp, String baseUrl) throws Exception {
|
40
|
this.timestamp = timestamp;
|
41
|
this.baseURL = baseUrl;
|
42
|
currentPage = new ArrayDeque<>();
|
43
|
startRequest();
|
44
|
}
|
45
|
|
46
|
private static String decompression(final Result r) {
|
47
|
try {
|
48
|
byte[] byteArray = Base64.decodeBase64(r.getBody().getBytes());
|
49
|
Inflater decompresser = new Inflater();
|
50
|
decompresser.setInput(byteArray);
|
51
|
ByteArrayOutputStream bos = new ByteArrayOutputStream(byteArray.length);
|
52
|
byte[] buffer = new byte[8192];
|
53
|
while (!decompresser.finished()) {
|
54
|
int size = decompresser.inflate(buffer);
|
55
|
bos.write(buffer, 0, size);
|
56
|
}
|
57
|
byte[] unzippeddata = bos.toByteArray();
|
58
|
decompresser.end();
|
59
|
|
60
|
return new String(unzippeddata);
|
61
|
} catch (DataFormatException e) {
|
62
|
return null;
|
63
|
}
|
64
|
|
65
|
}
|
66
|
|
67
|
private void fillQueue(final String hits) {
|
68
|
if (StringUtils.isBlank(hits) || "[]".equalsIgnoreCase(hits.trim()))
|
69
|
return;
|
70
|
try {
|
71
|
DataciteSchema datacitepage = g.fromJson(hits, DataciteSchema.class);
|
72
|
this.scrollId = datacitepage.getScrollId();
|
73
|
datacitepage.getResult().stream().map(DataciteESIterator::decompression).filter(Objects::nonNull).forEach(this.currentPage::add);
|
74
|
} catch (Throwable e) {
|
75
|
System.out.println(hits);
|
76
|
e.printStackTrace();
|
77
|
}
|
78
|
}
|
79
|
|
80
|
private void startRequest() throws Exception {
|
81
|
String url = baseURL+"/"+START_PATH;
|
82
|
final URL startUrl = new URL(timestamp >0 ? url + "?timestamp="+timestamp : url);
|
83
|
fillQueue(IOUtils.toString(startUrl.openStream()));
|
84
|
}
|
85
|
|
86
|
private void getNextPage() throws IOException {
|
87
|
String url = baseURL+"/"+NEXT_PATH;
|
88
|
final URL startUrl = new URL(String.format(url,scrollId));
|
89
|
fillQueue(IOUtils.toString(startUrl.openStream()));
|
90
|
}
|
91
|
|
92
|
|
93
|
@Override
|
94
|
public boolean hasNext() {
|
95
|
return currentPage.size() >0;
|
96
|
}
|
97
|
|
98
|
@Override
|
99
|
public String next() {
|
100
|
|
101
|
if (currentPage.size() == 0) {
|
102
|
|
103
|
return null;
|
104
|
}
|
105
|
|
106
|
String nextItem = currentPage.remove();
|
107
|
if (currentPage.size() == 0) {
|
108
|
try {
|
109
|
getNextPage();
|
110
|
} catch (Throwable e) {
|
111
|
throw new RuntimeException(e);
|
112
|
}
|
113
|
}
|
114
|
|
115
|
return nextItem;
|
116
|
}
|
117
|
|
118
|
public String getBaseURL() {
|
119
|
return baseURL;
|
120
|
}
|
121
|
|
122
|
public void setBaseURL(final String baseURL) {
|
123
|
this.baseURL = baseURL;
|
124
|
}
|
125
|
}
|