1
|
package eu.dnetlib.data.collector.plugins.oaisets;
|
2
|
|
3
|
import java.io.StringReader;
|
4
|
import java.util.Iterator;
|
5
|
import java.util.Queue;
|
6
|
import java.util.Set;
|
7
|
import java.util.concurrent.PriorityBlockingQueue;
|
8
|
|
9
|
import org.apache.commons.logging.Log;
|
10
|
import org.apache.commons.logging.LogFactory;
|
11
|
import org.dom4j.Document;
|
12
|
import org.dom4j.DocumentException;
|
13
|
import org.dom4j.Element;
|
14
|
import org.dom4j.Node;
|
15
|
import org.dom4j.io.SAXReader;
|
16
|
|
17
|
import com.google.common.collect.Sets;
|
18
|
|
19
|
import eu.dnetlib.data.collector.plugins.HttpConnector;
|
20
|
import eu.dnetlib.data.collector.plugins.oai.engine.XmlCleaner;
|
21
|
import eu.dnetlib.data.collector.rmi.CollectorServiceException;
|
22
|
|
23
|
public class OaiSetsIterator implements Iterator<String> {
|
24
|
|
25
|
private static final Log log = LogFactory.getLog(OaiSetsIterator.class);
|
26
|
|
27
|
private Queue<String> queue = new PriorityBlockingQueue<String>();
|
28
|
private SAXReader reader = new SAXReader();
|
29
|
|
30
|
private String baseUrl;
|
31
|
|
32
|
private String token;
|
33
|
private boolean started;
|
34
|
private HttpConnector httpConnector;
|
35
|
|
36
|
private Set<String> setsAlreadySeen = Sets.newHashSet();
|
37
|
|
38
|
public OaiSetsIterator(final String baseUrl, final HttpConnector httpConnector) {
|
39
|
this.baseUrl = baseUrl;
|
40
|
this.started = false;
|
41
|
this.httpConnector = httpConnector;
|
42
|
}
|
43
|
|
44
|
private void verifyStarted() {
|
45
|
if (!this.started) {
|
46
|
this.started = true;
|
47
|
try {
|
48
|
this.token = firstPage();
|
49
|
} catch (CollectorServiceException e) {
|
50
|
throw new RuntimeException(e);
|
51
|
}
|
52
|
}
|
53
|
}
|
54
|
|
55
|
@Override
|
56
|
public boolean hasNext() {
|
57
|
synchronized (queue) {
|
58
|
verifyStarted();
|
59
|
return !queue.isEmpty();
|
60
|
}
|
61
|
}
|
62
|
|
63
|
@Override
|
64
|
public String next() {
|
65
|
synchronized (queue) {
|
66
|
verifyStarted();
|
67
|
final String res = queue.poll();
|
68
|
while (queue.isEmpty() && (token != null) && !token.isEmpty()) {
|
69
|
try {
|
70
|
token = otherPages(token);
|
71
|
} catch (CollectorServiceException e) {
|
72
|
throw new RuntimeException(e);
|
73
|
}
|
74
|
}
|
75
|
return res;
|
76
|
}
|
77
|
}
|
78
|
|
79
|
@Override
|
80
|
public void remove() {}
|
81
|
|
82
|
private String firstPage() throws CollectorServiceException {
|
83
|
final String url = baseUrl + "?verb=ListSets";
|
84
|
log.info("Start harvesting using url: " + url);
|
85
|
return downloadPage(url);
|
86
|
}
|
87
|
|
88
|
private String otherPages(final String resumptionToken) throws CollectorServiceException {
|
89
|
return downloadPage(baseUrl + "?verb=ListSets&resumptionToken=" + resumptionToken);
|
90
|
}
|
91
|
|
92
|
private String downloadPage(final String url) throws CollectorServiceException {
|
93
|
|
94
|
final String xml = httpConnector.getInputSource(url);
|
95
|
|
96
|
Document doc;
|
97
|
try {
|
98
|
doc = reader.read(new StringReader(xml));
|
99
|
} catch (DocumentException e) {
|
100
|
log.warn("Error parsing xml, I try to clean it", e);
|
101
|
final String cleaned = XmlCleaner.cleanAllEntities(xml);
|
102
|
try {
|
103
|
doc = reader.read(new StringReader(cleaned));
|
104
|
} catch (DocumentException e1) {
|
105
|
throw new CollectorServiceException("Error parsing cleaned document", e1);
|
106
|
}
|
107
|
}
|
108
|
|
109
|
final Node errorNode = doc.selectSingleNode("/*[local-name()='OAI-PMH']/*[local-name()='error']");
|
110
|
if (errorNode != null) {
|
111
|
final String code = errorNode.valueOf("@code");
|
112
|
if ("noRecordsMatch".equalsIgnoreCase(code.trim())) {
|
113
|
log.warn("noRecordsMatch for oai call: " + url);
|
114
|
return null;
|
115
|
} else throw new CollectorServiceException(code + " - " + errorNode.getText());
|
116
|
}
|
117
|
|
118
|
boolean sawAllSets = true;
|
119
|
for (Object o : doc.selectNodes("//*[local-name()='ListSets']/*[local-name()='set']")) {
|
120
|
String set = ((Element) o).valueOf("./*[local-name()='setSpec']");
|
121
|
if (!setsAlreadySeen.contains(set)) {
|
122
|
sawAllSets = false;
|
123
|
setsAlreadySeen.add(set);
|
124
|
queue.add(((Node) o).asXML());
|
125
|
}
|
126
|
}
|
127
|
if (sawAllSets) {
|
128
|
log.warn("URL " + baseUrl + " keeps returning the same OAI sets. Please contact the repo admin.");
|
129
|
return null;
|
130
|
} else return doc.valueOf("//*[local-name()='resumptionToken']");
|
131
|
}
|
132
|
}
|