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.HttpConnector;
18
import eu.dnetlib.data.collector.rmi.CollectorServiceException;
19

    
20
import eu.dnetlib.data.collector.plugins.oai.engine.XmlCleaner;
21

    
22
public class OaiIterator implements Iterator<String> {
23

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

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

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

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

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

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

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

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

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

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

    
150
}
(2-2/3)