Project

General

Profile

1
package eu.dnetlib.validator.service.impls.providers;
2

    
3
import java.io.IOException;
4
import java.io.StringReader;
5
import java.net.MalformedURLException;
6
import java.net.URL;
7

    
8
import javax.xml.parsers.DocumentBuilder;
9
import javax.xml.parsers.DocumentBuilderFactory;
10
import javax.xml.parsers.ParserConfigurationException;
11

    
12
import org.w3c.dom.Document;
13
import org.xml.sax.InputSource;
14
import org.xml.sax.SAXException;
15

    
16
import eu.dnetlib.validator.engine.data.DataException;
17
import eu.dnetlib.validator.engine.data.Provider;
18
import eu.dnetlib.validator.engine.data.ResultSet;
19
import eu.dnetlib.validator.engine.execution.ValidationObject;
20
import eu.dnetlib.validator.service.impls.valobjs.XMLTextValidationObject;
21

    
22
/**
23
 * A provider that retrieves the XML response created by sending a verb request to an OAI-PMH repository.
24
 * 
25
 * @author Manos Karvounis
26
 * 
27
 */
28
public class OAIPMHSinglePageVerbProvider extends Provider {
29
	
30
	private static final long serialVersionUID = -8398496197308270467L;
31

    
32
	private int timeout;
33
	private int delay;
34
	private int retryDelay;
35
	private int retryEfforts;
36
	
37
	public static final String BASEURL = "BASEURL";
38
	
39
	/**
40
	 * The verb request to be issued.
41
	 * Note that you should not include the "?verb=" part, but if you need additional parameters, you should include them as part of the verb.
42
	 * E.g., "GetRecord&metadataPrefix=oai_dc&identifier=..."
43
	 */
44
	public static final String VERB="VERB";
45
	
46
	/**
47
	 * The maximum time to wait for a response from the repository (in millis)
48
	 */
49
//	public static final String TIMEOUT = "TIMEOUT";
50
	/**
51
	 * How much time to wait between consecutive HTTP requests to the repository
52
	 * (in millis).
53
	 */
54
//	public static final String DELAY = "DELAY";
55
	/**
56
	 * How much to wait if an HTTP request fails before trying again by
57
	 * resending the request.
58
	 */
59
//	public static final String RETRY_DELAY = "RETRY_DELAY";
60
	/**
61
	 * If an HTTP requests fails, how many times to try to resend the request.
62
	 */
63
//	public static final String RETRY_EFFORTS = "RETRY_EFFORTS";
64
	
65
	public OAIPMHSinglePageVerbProvider() {
66
		super(2);
67
	}
68

    
69
	@Override
70
	public ResultSet<ValidationObject> getValidationObjects() throws ProviderException {
71
		return new OAIPMHVerbResultSet();
72
	}
73

    
74
	@Override
75
	public ResultSet<String> getValidationObjectIds() throws ProviderException {
76
		throw new UnsupportedOperationException();
77
	}
78

    
79
	@Override
80
	public ValidationObject getValidationObject(String valObjId) throws ProviderException {
81
		throw new UnsupportedOperationException();
82
	}
83
	
84
	private class OAIPMHVerbResultSet implements ResultSet<ValidationObject> {
85

    
86
		protected DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
87
		protected DocumentBuilder builder;
88
		private URLStreamer streamer = new URLStreamer();
89
		
90
		private Document doc = null;
91
		boolean finished = false;
92
		
93
		public OAIPMHVerbResultSet() {
94
			super();
95
			try {
96
				builder = factory.newDocumentBuilder();
97
			} catch (ParserConfigurationException e) {
98
				log.error("", e);
99
			}
100
		}
101
		
102
		@Override
103
		public boolean next() throws DataException {
104
			if(!finished) {
105
				String surl = pros.getProperty(BASEURL) + "?verb="+pros.getProperty(VERB);
106
				String response = null;
107
				
108
				log.debug("Issuing request: "+surl);
109
				
110
				try {
111
					response = streamer.getResponse(new URL(surl), timeout, delay, retryDelay, retryEfforts);
112
				} catch (NumberFormatException e) {
113
					log.error("", e);
114
					throw new DataException();
115
				} catch (MalformedURLException e) {
116
					log.error("", e);
117
					throw new DataException();
118
				} catch (IOException e) {
119
					log.error("", e);
120
					throw new DataException();
121
				}
122
				
123
				InputSource is = new InputSource(new StringReader(response));
124
				try {
125
					doc = builder.parse(is);
126
				} catch (SAXException e) {
127
					log.error("", e);
128
					throw new DataException();
129
				} catch (IOException e) {
130
					log.error("", e);
131
					throw new DataException();
132
				}
133
				finished = true;
134
				return true;
135
			}
136
			return false;
137
		}
138

    
139
		@Override
140
		public ValidationObject get() throws DataException {
141
			XMLTextValidationObject obj = new XMLTextValidationObject(doc);
142
			obj.setId(pros.getProperty(VERB)+"@"+pros.getProperty(BASEURL));
143
			
144
			log.debug("Getting object with id "+obj.getId());
145
			
146
			return obj;
147
		}
148

    
149
		@Override
150
		public String getError() {
151
			// TODO Auto-generated method stub
152
			return null;
153
		}
154
		
155
	}
156

    
157
	@Override
158
	public ResultSet<ValidationObject> getValidationObjects(String entity)
159
			throws ProviderException {
160
		// TODO Auto-generated method stub
161
		return null;
162
	}
163

    
164
	public Integer getTimeout() {
165
		return timeout;
166
	}
167

    
168
	public void setTimeout(Integer timeout) {
169
		this.timeout = timeout;
170
	}
171

    
172
	public Integer getDelay() {
173
		return delay;
174
	}
175

    
176
	public void setDelay(Integer delay) {
177
		this.delay = delay;
178
	}
179

    
180
	public Integer getRetryDelay() {
181
		return retryDelay;
182
	}
183

    
184
	public void setRetryDelay(Integer retryDelay) {
185
		this.retryDelay = retryDelay;
186
	}
187

    
188
	public Integer getRetryEfforts() {
189
		return retryEfforts;
190
	}
191

    
192
	public void setRetryEfforts(Integer retryEfforts) {
193
		this.retryEfforts = retryEfforts;
194
	}
195

    
196
}
(8-8/11)