Project

General

Profile

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.Queue;
8
import java.util.concurrent.PriorityBlockingQueue;
9

    
10
import org.apache.commons.logging.Log;
11
import org.apache.commons.logging.LogFactory;
12
import org.dom4j.Document;
13
import org.dom4j.DocumentException;
14
import org.dom4j.Node;
15
import org.dom4j.io.SAXReader;
16

    
17
import eu.dnetlib.data.collector.plugins.oai.engine.HttpConnector;
18
import eu.dnetlib.data.collector.plugins.oai.engine.XmlCleaner;
19
import eu.dnetlib.data.collector.rmi.CollectorServiceException;
20

    
21
public class OaiIterator implements Iterator<String> {
22

    
23
	private static final Log log = LogFactory.getLog(OaiIterator.class); // NOPMD by marko on 11/24/08 5:02 PM
24

    
25
	private Queue<String> queue = new PriorityBlockingQueue<String>();
26
	private SAXReader reader = new SAXReader();
27

    
28
	private String baseUrl;
29
	private String set;
30
	private String mdFormat;
31
	private String fromDate;
32
	private String untilDate;
33
	private String token;
34
	private boolean started;
35
	private HttpConnector httpConnector;
36

    
37
	public OaiIterator(final String baseUrl, final String mdFormat, final String set, final String fromDate, final String untilDate, final HttpConnector httpConnector) {
38
		this.baseUrl = baseUrl;
39
		this.mdFormat = mdFormat;
40
		this.set = set;
41
		this.fromDate = fromDate;
42
		this.untilDate = untilDate;
43
		this.started = false;
44
		this.httpConnector = httpConnector;
45
	}
46
	
47
	private void verifyStarted() {
48
		if (!this.started) {
49
			this.started = true;
50
			try {
51
				this.token = firstPage();
52
			} catch (CollectorServiceException e) {
53
				throw new RuntimeException(e);
54
			}
55
		}
56
	}
57

    
58
	@Override
59
	public boolean hasNext() {
60
		synchronized (queue) {
61
			verifyStarted();
62
			return !queue.isEmpty();
63
		}
64
	}
65

    
66
	@Override
67
	public String next() {
68
		synchronized (queue) {
69
			verifyStarted();
70
			final String res = queue.poll();
71
			while (queue.isEmpty() && (token != null) && !token.isEmpty()) {
72
				try {
73
					token = otherPages(token);
74
				} catch (CollectorServiceException e) {
75
					throw new RuntimeException(e);
76
				}
77
			}
78
			return res;
79
		}
80
	}
81

    
82
	@Override
83
	public void remove() {}
84

    
85
	private String firstPage() throws CollectorServiceException {
86
		try {
87
			String url = baseUrl + "?verb=ListRecords&metadataPrefix=" + URLEncoder.encode(mdFormat,"UTF-8");
88
			if ((set != null) && !set.isEmpty()) {
89
				url += "&set=" + URLEncoder.encode(set,"UTF-8");
90
			}
91
			if ((fromDate != null) && fromDate.matches("\\d{4}-\\d{2}-\\d{2}")) {
92
				url += "&from=" + URLEncoder.encode(fromDate,"UTF-8");
93
			}
94
			if ((untilDate != null) && untilDate.matches("\\d{4}-\\d{2}-\\d{2}")) {
95
				url += "&until=" + URLEncoder.encode(untilDate,"UTF-8");
96
			}
97
			log.info("Start harvesting using url: " + url);
98
	
99
			return downloadPage(url);
100
		} catch(UnsupportedEncodingException e) {
101
			throw new CollectorServiceException(e);
102
		}
103
	}
104

    
105
	private String otherPages(final String resumptionToken) throws CollectorServiceException {
106
		try {
107
			return downloadPage(baseUrl + "?verb=ListRecords&resumptionToken=" + URLEncoder.encode(resumptionToken,"UTF-8"));
108
		} catch (UnsupportedEncodingException e) {
109
			throw new CollectorServiceException(e);
110
		}
111
	}
112

    
113
	private String downloadPage(final String url) throws CollectorServiceException {
114
	
115
		final String xml = httpConnector.getInputSource(url);
116
		
117
		Document doc;
118
		try {
119
			doc = reader.read(new StringReader(xml));
120
		} catch (DocumentException e) {
121
			log.warn("Error parsing xml, I try to clean it: " + xml, e);
122
			final String cleaned = XmlCleaner.cleanAllEntities(xml);
123
			try {
124
				doc = reader.read(new StringReader(cleaned));
125
			} catch (DocumentException e1) {
126
				throw new CollectorServiceException("Error parsing cleaned document:" + cleaned, e1);
127
			}
128
		}
129
		
130
		final Node errorNode = doc.selectSingleNode("/*[local-name()='OAI-PMH']/*[local-name()='error']");
131
		if (errorNode != null) {
132
			final String code = errorNode.valueOf("@code");
133
			if ("noRecordsMatch".equalsIgnoreCase(code.trim())) {
134
				log.warn("noRecordsMatch for oai call: " + url);
135
				return null;
136
			} else {
137
				throw new CollectorServiceException(code + " - " + errorNode.getText());
138
			}
139
		}
140
		
141
		for (Object o : doc.selectNodes("//*[local-name()='ListRecords']/*[local-name()='record']")) {
142
			queue.add(((Node) o).asXML());
143
		}
144
	
145
		return doc.valueOf("//*[local-name()='resumptionToken']");
146
		
147
	}
148

    
149
}
(2-2/3)