1
|
package eu.dnetlib.data.collector.plugins.oai;
|
2
|
|
3
|
import java.io.StringReader;
|
4
|
import java.io.UnsupportedEncodingException;
|
5
|
import java.net.URLEncoder;
|
6
|
import java.util.Iterator;
|
7
|
import java.util.Map;
|
8
|
import java.util.Queue;
|
9
|
import java.util.concurrent.PriorityBlockingQueue;
|
10
|
|
11
|
import com.google.common.collect.Maps;
|
12
|
|
13
|
import org.apache.commons.lang3.StringUtils;
|
14
|
import org.apache.commons.logging.Log;
|
15
|
import org.apache.commons.logging.LogFactory;
|
16
|
import org.dom4j.Document;
|
17
|
import org.dom4j.DocumentException;
|
18
|
import org.dom4j.Node;
|
19
|
import org.dom4j.io.SAXReader;
|
20
|
|
21
|
import eu.dnetlib.data.collector.plugins.HttpConnector;
|
22
|
import eu.dnetlib.data.collector.rmi.CollectorServiceException;
|
23
|
|
24
|
import eu.dnetlib.data.collector.plugins.oai.engine.XmlCleaner;
|
25
|
|
26
|
public class OaiIterator implements Iterator<String> {
|
27
|
|
28
|
private static final Log log = LogFactory.getLog(OaiIterator.class); // NOPMD by marko on 11/24/08 5:02 PM
|
29
|
|
30
|
private Queue<String> queue = new PriorityBlockingQueue<String>();
|
31
|
private SAXReader reader = new SAXReader();
|
32
|
|
33
|
private String baseUrl;
|
34
|
private String set;
|
35
|
private String mdFormat;
|
36
|
private String fromDate;
|
37
|
private String untilDate;
|
38
|
private String token;
|
39
|
private boolean started;
|
40
|
private HttpConnector httpConnector;
|
41
|
private Map<String, String> requestHeader;
|
42
|
|
43
|
public OaiIterator(final String baseUrl, final String mdFormat, final String set, final String fromDate, final String untilDate, final HttpConnector httpConnector) {
|
44
|
this.baseUrl = baseUrl;
|
45
|
this.mdFormat = mdFormat;
|
46
|
this.set = set;
|
47
|
this.fromDate = fromDate;
|
48
|
this.untilDate = untilDate;
|
49
|
this.started = false;
|
50
|
this.httpConnector = httpConnector;
|
51
|
this.requestHeader = requestHeader != null ? requestHeader : Maps.newHashMap();
|
52
|
}
|
53
|
|
54
|
private void verifyStarted() {
|
55
|
if (!this.started) {
|
56
|
this.started = true;
|
57
|
try {
|
58
|
this.token = firstPage();
|
59
|
} catch (CollectorServiceException e) {
|
60
|
throw new RuntimeException(e);
|
61
|
}
|
62
|
}
|
63
|
}
|
64
|
|
65
|
@Override
|
66
|
public boolean hasNext() {
|
67
|
synchronized (queue) {
|
68
|
verifyStarted();
|
69
|
return !queue.isEmpty();
|
70
|
}
|
71
|
}
|
72
|
|
73
|
@Override
|
74
|
public String next() {
|
75
|
synchronized (queue) {
|
76
|
verifyStarted();
|
77
|
final String res = queue.poll();
|
78
|
while (queue.isEmpty() && (token != null) && !token.isEmpty()) {
|
79
|
try {
|
80
|
token = otherPages(token);
|
81
|
} catch (CollectorServiceException e) {
|
82
|
throw new RuntimeException(e);
|
83
|
}
|
84
|
}
|
85
|
return res;
|
86
|
}
|
87
|
}
|
88
|
|
89
|
@Override
|
90
|
public void remove() {}
|
91
|
|
92
|
private String firstPage() throws CollectorServiceException {
|
93
|
try {
|
94
|
String url = baseUrl + "?verb=ListRecords&metadataPrefix=" + URLEncoder.encode(mdFormat,"UTF-8");
|
95
|
if ((set != null) && !set.isEmpty()) {
|
96
|
url += "&set=" + URLEncoder.encode(set,"UTF-8");
|
97
|
}
|
98
|
// ISO8601 , http://www.openarchives.org/OAI/openarchivesprotocol.html#Dates , eg. 1957-03-20T20:30:00Z
|
99
|
if ((fromDate != null) && (fromDate.matches("\\d{4}-\\d{2}-\\d{2}") || fromDate.matches("\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}Z")) ) {
|
100
|
url += "&from=" + URLEncoder.encode(fromDate,"UTF-8");
|
101
|
}
|
102
|
if ((untilDate != null) && (untilDate.matches("\\d{4}-\\d{2}-\\d{2}") || untilDate.matches("\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}Z")) ) {
|
103
|
url += "&until=" + URLEncoder.encode(untilDate,"UTF-8");
|
104
|
}
|
105
|
log.info("Start harvesting using url: " + url);
|
106
|
|
107
|
return downloadPage(url);
|
108
|
} catch(UnsupportedEncodingException e) {
|
109
|
throw new CollectorServiceException(e);
|
110
|
}
|
111
|
}
|
112
|
|
113
|
private String extractResumptionToken(final String xml) {
|
114
|
|
115
|
final String s = StringUtils.substringAfter(xml, "<resumptionToken");
|
116
|
if (s == null){
|
117
|
return null;
|
118
|
}
|
119
|
|
120
|
final String result = StringUtils.substringBetween(s, ">", "</");
|
121
|
if (result == null)
|
122
|
return null;
|
123
|
return result.trim();
|
124
|
|
125
|
|
126
|
}
|
127
|
|
128
|
private String otherPages(final String resumptionToken) throws CollectorServiceException {
|
129
|
try {
|
130
|
return downloadPage(baseUrl + "?verb=ListRecords&resumptionToken=" + URLEncoder.encode(resumptionToken,"UTF-8"));
|
131
|
} catch (UnsupportedEncodingException e) {
|
132
|
throw new CollectorServiceException(e);
|
133
|
}
|
134
|
}
|
135
|
|
136
|
private String downloadPage(final String url) throws CollectorServiceException {
|
137
|
|
138
|
final String xml = httpConnector.getInputSource(url);
|
139
|
Document doc;
|
140
|
try {
|
141
|
doc = reader.read(new StringReader(xml));
|
142
|
} catch (DocumentException e) {
|
143
|
log.warn("Error parsing xml, I try to clean it", e);
|
144
|
final String cleaned = XmlCleaner.cleanAllEntities(xml);
|
145
|
try {
|
146
|
doc = reader.read(new StringReader(cleaned));
|
147
|
} catch (DocumentException e1) {
|
148
|
final String resumptionToken = extractResumptionToken(xml);
|
149
|
if (resumptionToken == null)
|
150
|
throw new CollectorServiceException("Error parsing cleaned document", e1);
|
151
|
return resumptionToken;
|
152
|
}
|
153
|
}
|
154
|
|
155
|
final Node errorNode = doc.selectSingleNode("/*[local-name()='OAI-PMH']/*[local-name()='error']");
|
156
|
if (errorNode != null) {
|
157
|
final String code = errorNode.valueOf("@code");
|
158
|
if ("noRecordsMatch".equalsIgnoreCase(code.trim())) {
|
159
|
log.warn("noRecordsMatch for oai call: " + url);
|
160
|
return null;
|
161
|
} else {
|
162
|
throw new CollectorServiceException(code + " - " + errorNode.getText());
|
163
|
}
|
164
|
}
|
165
|
int added_records = 0;
|
166
|
for (Object o : doc.selectNodes("//*[local-name()='ListRecords']/*[local-name()='record']")) {
|
167
|
queue.add(((Node) o).asXML());
|
168
|
added_records++;
|
169
|
}
|
170
|
if(added_records == 0){
|
171
|
throw new RuntimeException("OAI Iterator Exception: Empty records list for " + url);
|
172
|
}
|
173
|
|
174
|
return doc.valueOf("//*[local-name()='resumptionToken']");
|
175
|
|
176
|
}
|
177
|
|
178
|
}
|