2020-04-27 14:52:31 +02:00
|
|
|
|
2019-10-24 11:36:59 +02:00
|
|
|
package eu.dnetlib.dhp.collection.plugin.oai;
|
2019-04-03 16:05:16 +02:00
|
|
|
|
|
|
|
import java.io.StringReader;
|
|
|
|
import java.io.UnsupportedEncodingException;
|
|
|
|
import java.net.URLEncoder;
|
|
|
|
import java.util.Iterator;
|
|
|
|
import java.util.Queue;
|
|
|
|
import java.util.concurrent.PriorityBlockingQueue;
|
2020-04-28 11:23:29 +02:00
|
|
|
|
2019-04-03 16:05:16 +02:00
|
|
|
import org.apache.commons.lang.StringUtils;
|
|
|
|
import org.dom4j.Document;
|
|
|
|
import org.dom4j.DocumentException;
|
|
|
|
import org.dom4j.Node;
|
|
|
|
import org.dom4j.io.SAXReader;
|
2021-02-03 12:33:41 +01:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
2019-04-03 16:05:16 +02:00
|
|
|
|
2021-02-12 12:31:02 +01:00
|
|
|
import eu.dnetlib.dhp.collection.CollectorException;
|
|
|
|
import eu.dnetlib.dhp.collection.CollectorPluginReport;
|
|
|
|
import eu.dnetlib.dhp.collection.HttpConnector2;
|
|
|
|
import eu.dnetlib.dhp.collection.XmlCleaner;
|
2020-04-28 11:23:29 +02:00
|
|
|
|
2019-04-03 16:05:16 +02:00
|
|
|
public class OaiIterator implements Iterator<String> {
|
|
|
|
|
2021-02-03 12:33:41 +01:00
|
|
|
private static final Logger log = LoggerFactory.getLogger(OaiIterator.class);
|
2020-04-27 14:52:31 +02:00
|
|
|
|
2021-02-05 19:18:05 +01:00
|
|
|
private final static String REPORT_PREFIX = "oai:";
|
|
|
|
|
2020-04-27 14:52:31 +02:00
|
|
|
private final Queue<String> queue = new PriorityBlockingQueue<>();
|
|
|
|
private final SAXReader reader = new SAXReader();
|
|
|
|
|
|
|
|
private final String baseUrl;
|
|
|
|
private final String set;
|
|
|
|
private final String mdFormat;
|
|
|
|
private final String fromDate;
|
|
|
|
private final String untilDate;
|
|
|
|
private String token;
|
|
|
|
private boolean started;
|
2021-02-04 17:24:52 +01:00
|
|
|
private final HttpConnector2 httpConnector;
|
2021-02-16 16:53:14 +01:00
|
|
|
private CollectorPluginReport report;
|
2020-04-27 14:52:31 +02:00
|
|
|
|
|
|
|
public OaiIterator(
|
|
|
|
final String baseUrl,
|
|
|
|
final String mdFormat,
|
|
|
|
final String set,
|
|
|
|
final String fromDate,
|
|
|
|
final String untilDate,
|
2021-02-04 17:24:52 +01:00
|
|
|
final HttpConnector2 httpConnector,
|
2021-02-16 16:53:14 +01:00
|
|
|
final CollectorPluginReport report) {
|
2020-04-27 14:52:31 +02:00
|
|
|
this.baseUrl = baseUrl;
|
|
|
|
this.mdFormat = mdFormat;
|
|
|
|
this.set = set;
|
|
|
|
this.fromDate = fromDate;
|
|
|
|
this.untilDate = untilDate;
|
|
|
|
this.started = false;
|
|
|
|
this.httpConnector = httpConnector;
|
2021-02-16 16:53:14 +01:00
|
|
|
this.report = report;
|
2020-04-27 14:52:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private void verifyStarted() {
|
|
|
|
if (!this.started) {
|
|
|
|
this.started = true;
|
|
|
|
try {
|
|
|
|
this.token = firstPage();
|
2021-01-28 09:51:17 +01:00
|
|
|
} catch (final CollectorException e) {
|
2020-04-27 14:52:31 +02:00
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean hasNext() {
|
|
|
|
synchronized (queue) {
|
|
|
|
verifyStarted();
|
|
|
|
return !queue.isEmpty();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String next() {
|
|
|
|
synchronized (queue) {
|
|
|
|
verifyStarted();
|
|
|
|
final String res = queue.poll();
|
|
|
|
while (queue.isEmpty() && token != null && !token.isEmpty()) {
|
|
|
|
try {
|
|
|
|
token = otherPages(token);
|
2021-01-28 09:51:17 +01:00
|
|
|
} catch (final CollectorException e) {
|
2020-04-27 14:52:31 +02:00
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void remove() {
|
|
|
|
}
|
|
|
|
|
2021-01-28 09:51:17 +01:00
|
|
|
private String firstPage() throws CollectorException {
|
2020-04-27 14:52:31 +02:00
|
|
|
try {
|
|
|
|
String url = baseUrl + "?verb=ListRecords&metadataPrefix=" + URLEncoder.encode(mdFormat, "UTF-8");
|
|
|
|
if (set != null && !set.isEmpty()) {
|
|
|
|
url += "&set=" + URLEncoder.encode(set, "UTF-8");
|
|
|
|
}
|
|
|
|
if (fromDate != null && fromDate.matches("\\d{4}-\\d{2}-\\d{2}")) {
|
|
|
|
url += "&from=" + URLEncoder.encode(fromDate, "UTF-8");
|
|
|
|
}
|
|
|
|
if (untilDate != null && untilDate.matches("\\d{4}-\\d{2}-\\d{2}")) {
|
|
|
|
url += "&until=" + URLEncoder.encode(untilDate, "UTF-8");
|
|
|
|
}
|
|
|
|
log.info("Start harvesting using url: " + url);
|
|
|
|
|
|
|
|
return downloadPage(url);
|
|
|
|
} catch (final UnsupportedEncodingException e) {
|
2021-02-16 16:53:14 +01:00
|
|
|
report.put(e.getClass().getName(), e.getMessage());
|
2021-01-28 09:51:17 +01:00
|
|
|
throw new CollectorException(e);
|
2020-04-27 14:52:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private String extractResumptionToken(final String xml) {
|
|
|
|
|
|
|
|
final String s = StringUtils.substringAfter(xml, "<resumptionToken");
|
|
|
|
if (s == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
final String result = StringUtils.substringBetween(s, ">", "</");
|
|
|
|
if (result == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return result.trim();
|
|
|
|
}
|
|
|
|
|
2021-01-28 09:51:17 +01:00
|
|
|
private String otherPages(final String resumptionToken) throws CollectorException {
|
2020-04-27 14:52:31 +02:00
|
|
|
try {
|
|
|
|
return downloadPage(
|
|
|
|
baseUrl
|
|
|
|
+ "?verb=ListRecords&resumptionToken="
|
|
|
|
+ URLEncoder.encode(resumptionToken, "UTF-8"));
|
|
|
|
} catch (final UnsupportedEncodingException e) {
|
2021-02-16 16:53:14 +01:00
|
|
|
report.put(e.getClass().getName(), e.getMessage());
|
2021-01-28 09:51:17 +01:00
|
|
|
throw new CollectorException(e);
|
2020-04-27 14:52:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-28 09:51:17 +01:00
|
|
|
private String downloadPage(final String url) throws CollectorException {
|
2020-04-27 14:52:31 +02:00
|
|
|
|
2021-02-16 16:53:14 +01:00
|
|
|
final String xml = httpConnector.getInputSource(url, report);
|
2020-04-27 14:52:31 +02:00
|
|
|
Document doc;
|
|
|
|
try {
|
|
|
|
doc = reader.read(new StringReader(xml));
|
|
|
|
} catch (final DocumentException e) {
|
2021-02-03 17:58:22 +01:00
|
|
|
log.warn("Error parsing xml, I try to clean it. {}", e.getMessage());
|
2021-02-16 16:53:14 +01:00
|
|
|
report.put(e.getClass().getName(), e.getMessage());
|
2020-04-27 14:52:31 +02:00
|
|
|
final String cleaned = XmlCleaner.cleanAllEntities(xml);
|
|
|
|
try {
|
|
|
|
doc = reader.read(new StringReader(cleaned));
|
|
|
|
} catch (final DocumentException e1) {
|
|
|
|
final String resumptionToken = extractResumptionToken(xml);
|
|
|
|
if (resumptionToken == null) {
|
2021-02-16 16:53:14 +01:00
|
|
|
report.put(e1.getClass().getName(), e1.getMessage());
|
2021-02-03 17:58:22 +01:00
|
|
|
throw new CollectorException("Error parsing cleaned document:\n" + cleaned, e1);
|
2020-04-27 14:52:31 +02:00
|
|
|
}
|
|
|
|
return resumptionToken;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
final Node errorNode = doc.selectSingleNode("/*[local-name()='OAI-PMH']/*[local-name()='error']");
|
|
|
|
if (errorNode != null) {
|
2021-02-05 19:18:05 +01:00
|
|
|
final String code = errorNode.valueOf("@code").trim();
|
|
|
|
if ("noRecordsMatch".equalsIgnoreCase(code)) {
|
2021-02-04 14:06:02 +01:00
|
|
|
final String msg = "noRecordsMatch for oai call : " + url;
|
|
|
|
log.warn(msg);
|
2021-02-16 16:53:14 +01:00
|
|
|
report.put(REPORT_PREFIX + code, msg);
|
2020-04-27 14:52:31 +02:00
|
|
|
return null;
|
|
|
|
} else {
|
2021-02-04 14:06:02 +01:00
|
|
|
final String msg = code + " - " + errorNode.getText();
|
2021-02-16 16:53:14 +01:00
|
|
|
report.put(REPORT_PREFIX + "error", msg);
|
2021-02-04 14:06:02 +01:00
|
|
|
throw new CollectorException(msg);
|
2020-04-27 14:52:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (final Object o : doc.selectNodes("//*[local-name()='ListRecords']/*[local-name()='record']")) {
|
|
|
|
queue.add(((Node) o).asXML());
|
|
|
|
}
|
|
|
|
|
|
|
|
return doc.valueOf("//*[local-name()='resumptionToken']");
|
|
|
|
}
|
2021-02-03 12:33:41 +01:00
|
|
|
|
2021-02-16 16:53:14 +01:00
|
|
|
public CollectorPluginReport getReport() {
|
|
|
|
return report;
|
2021-02-03 12:33:41 +01:00
|
|
|
}
|
2019-04-03 16:05:16 +02:00
|
|
|
}
|