1 |
42181
|
sandro.lab
|
package eu.dnetlib.oai;
|
2 |
26600
|
sandro.lab
|
|
3 |
|
|
import java.text.SimpleDateFormat;
|
4 |
42184
|
michele.ar
|
import java.util.Date;
|
5 |
|
|
import java.util.HashMap;
|
6 |
|
|
import java.util.Iterator;
|
7 |
|
|
import java.util.Map;
|
8 |
26600
|
sandro.lab
|
import java.util.Map.Entry;
|
9 |
42184
|
michele.ar
|
import java.util.TimeZone;
|
10 |
|
|
|
11 |
26600
|
sandro.lab
|
import javax.servlet.http.HttpServletRequest;
|
12 |
|
|
import javax.servlet.http.HttpServletResponse;
|
13 |
|
|
|
14 |
42184
|
michele.ar
|
import org.apache.commons.lang3.StringUtils;
|
15 |
26600
|
sandro.lab
|
import org.apache.commons.logging.Log;
|
16 |
|
|
import org.apache.commons.logging.LogFactory;
|
17 |
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
18 |
|
|
import org.springframework.stereotype.Controller;
|
19 |
|
|
import org.springframework.ui.ModelMap;
|
20 |
|
|
import org.springframework.web.bind.annotation.ModelAttribute;
|
21 |
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
22 |
|
|
|
23 |
42184
|
michele.ar
|
import eu.dnetlib.oai.core.AbstractOAICore;
|
24 |
|
|
import eu.dnetlib.oai.info.ListRecordsInfo;
|
25 |
|
|
import eu.dnetlib.oai.info.RecordInfo;
|
26 |
|
|
import eu.dnetlib.rmi.provision.OaiPublisherException;
|
27 |
|
|
|
28 |
26600
|
sandro.lab
|
/**
|
29 |
|
|
* OAI Servlet.
|
30 |
42181
|
sandro.lab
|
*
|
31 |
26600
|
sandro.lab
|
* @author michele
|
32 |
|
|
*/
|
33 |
|
|
@Controller
|
34 |
|
|
public final class OAIController {
|
35 |
|
|
|
36 |
|
|
/**
|
37 |
|
|
* logger.
|
38 |
|
|
*/
|
39 |
|
|
private static final Log log = LogFactory.getLog(OAIController.class); // NOPMD by marko on 11/24/08 5:02 PM
|
40 |
|
|
/**
|
41 |
|
|
* Default content type.
|
42 |
|
|
*/
|
43 |
|
|
private static final String DEFAULT_CONTENT_TYPE = "text/xml;charset=utf-8";
|
44 |
|
|
/**
|
45 |
|
|
* OAI servlet core. Most of the logic is here
|
46 |
|
|
*/
|
47 |
|
|
@Autowired
|
48 |
|
|
private AbstractOAICore core;
|
49 |
|
|
@Autowired
|
50 |
|
|
private OAIProperties oaiProperties;
|
51 |
|
|
|
52 |
39969
|
alessia.ba
|
@RequestMapping("/oai/clearCaches.do")
|
53 |
|
|
public void clearOaiCaches() throws OaiPublisherException {
|
54 |
42184
|
michele.ar
|
this.core.getMdFormatsCache().removeAll();
|
55 |
|
|
this.core.setCurrentDBFromIS();
|
56 |
|
|
this.core.getLookupClient().evictCaches();
|
57 |
39969
|
alessia.ba
|
}
|
58 |
|
|
|
59 |
26600
|
sandro.lab
|
@RequestMapping("/oai/oai.do")
|
60 |
|
|
public String oai(final ModelMap map, final HttpServletRequest request, final HttpServletResponse response) throws Exception {
|
61 |
|
|
response.setContentType(OAIController.DEFAULT_CONTENT_TYPE);
|
62 |
|
|
String theVerb = "";
|
63 |
|
|
try {
|
64 |
|
|
final Map<String, String> params = cleanParameters(request.getParameterMap());
|
65 |
42184
|
michele.ar
|
if (params == null) { return oaiError(OAIError.badArgument, map); }
|
66 |
26600
|
sandro.lab
|
theVerb = params.get("verb");
|
67 |
42184
|
michele.ar
|
final OAI_VERBS requestedVerb = OAI_VERBS.getVerb(theVerb);
|
68 |
26600
|
sandro.lab
|
switch (requestedVerb) {
|
69 |
|
|
case IDENTIFY:
|
70 |
|
|
return oaiIdentify(params, map);
|
71 |
|
|
case LIST_METADATA_FORMATS:
|
72 |
|
|
return oaiListMetadataFormats(params, map);
|
73 |
|
|
case LIST_SETS:
|
74 |
|
|
return oaiListSets(params, map);
|
75 |
|
|
case GET_RECORD:
|
76 |
|
|
return oaiGetRecord(params, map);
|
77 |
|
|
case LIST_IDENTIFIERS:
|
78 |
|
|
return oaiListIdentifiersOrRecords(params, map);
|
79 |
|
|
case LIST_RECORDS:
|
80 |
|
|
return oaiListIdentifiersOrRecords(params, map);
|
81 |
|
|
default:
|
82 |
34802
|
alessia.ba
|
return oaiError(OAIError.badVerb, map);
|
83 |
26600
|
sandro.lab
|
}
|
84 |
|
|
} catch (final CannotDisseminateFormatException e) {
|
85 |
35490
|
alessia.ba
|
log.debug("ERROR", e);
|
86 |
34802
|
alessia.ba
|
return oaiError(OAIError.cannotDisseminateFormat, theVerb, map);
|
87 |
26600
|
sandro.lab
|
} catch (final NoRecordsMatchException e) {
|
88 |
35490
|
alessia.ba
|
log.debug("ERROR", e);
|
89 |
34802
|
alessia.ba
|
return oaiError(OAIError.noRecordsMatch, theVerb, map);
|
90 |
26600
|
sandro.lab
|
} catch (final BadResumptionTokenException e) {
|
91 |
35490
|
alessia.ba
|
log.debug("ERROR", e);
|
92 |
34802
|
alessia.ba
|
return oaiError(OAIError.badResumptionToken, theVerb, map);
|
93 |
26600
|
sandro.lab
|
} catch (final Exception e) {
|
94 |
29814
|
alessia.ba
|
log.error("ERROR", e);
|
95 |
26600
|
sandro.lab
|
return oaiError(e, map);
|
96 |
|
|
}
|
97 |
|
|
}
|
98 |
|
|
|
99 |
|
|
private Map<String, String> cleanParameters(final Map<?, ?> startParams) {
|
100 |
|
|
final HashMap<String, String> params = new HashMap<String, String>();
|
101 |
|
|
final Iterator<?> iter = startParams.entrySet().iterator();
|
102 |
|
|
while (iter.hasNext()) {
|
103 |
|
|
final Entry<?, ?> entry = (Entry<?, ?>) iter.next();
|
104 |
|
|
final String key = entry.getKey().toString();
|
105 |
|
|
final String[] arr = (String[]) entry.getValue();
|
106 |
42184
|
michele.ar
|
if (arr.length == 0) { return null; }
|
107 |
26600
|
sandro.lab
|
final String value = arr[0];
|
108 |
|
|
if (key.equals("verb")) {
|
109 |
|
|
params.put("verb", value);
|
110 |
|
|
} else if (key.equals("from")) {
|
111 |
|
|
params.put("from", value);
|
112 |
|
|
} else if (key.equals("until")) {
|
113 |
|
|
params.put("until", value);
|
114 |
|
|
} else if (key.equals("metadataPrefix")) {
|
115 |
|
|
params.put("metadataPrefix", value);
|
116 |
|
|
} else if (key.equals("identifier")) {
|
117 |
|
|
params.put("identifier", value);
|
118 |
|
|
} else if (key.equals("set")) {
|
119 |
|
|
params.put("set", value);
|
120 |
|
|
} else if (key.equals("resumptionToken")) {
|
121 |
|
|
params.put("resumptionToken", value);
|
122 |
42184
|
michele.ar
|
} else {
|
123 |
|
|
return null;
|
124 |
|
|
}
|
125 |
26600
|
sandro.lab
|
}
|
126 |
|
|
return params;
|
127 |
|
|
}
|
128 |
|
|
|
129 |
|
|
private String oaiIdentify(final Map<String, String> params, final ModelMap map) throws Exception {
|
130 |
|
|
String verb = null;
|
131 |
|
|
if (params.containsKey("verb")) {
|
132 |
|
|
verb = params.get("verb");
|
133 |
|
|
params.remove("verb");
|
134 |
|
|
}
|
135 |
42184
|
michele.ar
|
if (params.entrySet().size() > 0) { return oaiError(OAIError.badArgument, verb, map); }
|
136 |
26600
|
sandro.lab
|
return "oai/OAI_Identify";
|
137 |
|
|
}
|
138 |
|
|
|
139 |
|
|
private String oaiGetRecord(final Map<String, String> params, final ModelMap map) throws Exception {
|
140 |
|
|
String verb = null;
|
141 |
|
|
String prefix = null;
|
142 |
|
|
String identifier = null;
|
143 |
|
|
if (params.containsKey("verb")) {
|
144 |
|
|
verb = params.get("verb");
|
145 |
|
|
params.remove("verb");
|
146 |
|
|
}
|
147 |
|
|
if (params.containsKey("metadataPrefix")) {
|
148 |
|
|
prefix = params.get("metadataPrefix");
|
149 |
|
|
params.remove("metadataPrefix");
|
150 |
42184
|
michele.ar
|
} else {
|
151 |
|
|
return oaiError(OAIError.badArgument, verb, map);
|
152 |
|
|
}
|
153 |
26600
|
sandro.lab
|
if (params.containsKey("identifier")) {
|
154 |
|
|
identifier = params.get("identifier");
|
155 |
|
|
params.remove("identifier");
|
156 |
42184
|
michele.ar
|
} else {
|
157 |
|
|
return oaiError(OAIError.badArgument, verb, map);
|
158 |
|
|
}
|
159 |
|
|
if (params.entrySet().size() > 0) { return oaiError(OAIError.badArgument, verb, map); }
|
160 |
38446
|
alessia.ba
|
this.core.setCurrentDBFromIS();
|
161 |
42184
|
michele.ar
|
final RecordInfo record = this.core.getInfoRecord(identifier, prefix);
|
162 |
|
|
if (record == null) { return oaiError(OAIError.idDoesNotExist, map); }
|
163 |
34803
|
alessia.ba
|
map.addAttribute("record", record);
|
164 |
26600
|
sandro.lab
|
|
165 |
|
|
return "oai/OAI_GetRecord";
|
166 |
|
|
}
|
167 |
|
|
|
168 |
|
|
private String oaiListIdentifiersOrRecords(final Map<String, String> params, final ModelMap map) throws Exception {
|
169 |
|
|
OAI_VERBS verb = null;
|
170 |
|
|
String metadataPrefix = null;
|
171 |
|
|
String resumptionToken = null;
|
172 |
|
|
String set = null;
|
173 |
|
|
String from = null;
|
174 |
|
|
String until = null;
|
175 |
|
|
|
176 |
|
|
if (params.containsKey("verb")) {
|
177 |
|
|
verb = OAI_VERBS.getVerb(params.get("verb"));
|
178 |
|
|
params.remove("verb");
|
179 |
|
|
}
|
180 |
|
|
|
181 |
|
|
if (params.containsKey("resumptionToken")) {
|
182 |
|
|
resumptionToken = params.get("resumptionToken");
|
183 |
|
|
params.remove("resumptionToken");
|
184 |
38446
|
alessia.ba
|
this.core.setCurrentDBFromIS();
|
185 |
26600
|
sandro.lab
|
} else {
|
186 |
38446
|
alessia.ba
|
this.core.setCurrentDBFromIS();
|
187 |
26600
|
sandro.lab
|
if (params.containsKey("metadataPrefix")) {
|
188 |
|
|
metadataPrefix = params.get("metadataPrefix");
|
189 |
|
|
params.remove("metadataPrefix");
|
190 |
42184
|
michele.ar
|
} else {
|
191 |
|
|
return oaiError(OAIError.badArgument, verb.toString(), map);
|
192 |
|
|
}
|
193 |
26600
|
sandro.lab
|
if (params.containsKey("from")) {
|
194 |
|
|
from = params.get("from");
|
195 |
|
|
params.remove("from");
|
196 |
|
|
}
|
197 |
|
|
if (params.containsKey("until")) {
|
198 |
|
|
until = params.get("until");
|
199 |
|
|
params.remove("until");
|
200 |
|
|
}
|
201 |
|
|
if (params.containsKey("set")) {
|
202 |
|
|
set = params.get("set");
|
203 |
|
|
params.remove("set");
|
204 |
|
|
}
|
205 |
|
|
}
|
206 |
42184
|
michele.ar
|
if (params.entrySet().size() > 0) { return oaiError(OAIError.badArgument, verb.toString(), map); }
|
207 |
|
|
if (StringUtils.isNotBlank(set) && !this.core.existSet(set)) { return oaiError(OAIError.badArgument, verb.toString(), map); }
|
208 |
26600
|
sandro.lab
|
|
209 |
|
|
boolean onlyIdentifiers = true;
|
210 |
|
|
if (verb == OAI_VERBS.LIST_RECORDS) {
|
211 |
|
|
onlyIdentifiers = false;
|
212 |
|
|
}
|
213 |
|
|
|
214 |
|
|
ListRecordsInfo infos;
|
215 |
|
|
|
216 |
|
|
if (StringUtils.isBlank(resumptionToken)) {
|
217 |
42184
|
michele.ar
|
infos = this.core.listRecords(onlyIdentifiers, metadataPrefix, set, from, until);
|
218 |
26600
|
sandro.lab
|
} else {
|
219 |
42184
|
michele.ar
|
infos = this.core.listRecords(onlyIdentifiers, resumptionToken);
|
220 |
26600
|
sandro.lab
|
}
|
221 |
|
|
|
222 |
|
|
map.addAttribute("info", infos);
|
223 |
|
|
|
224 |
42184
|
michele.ar
|
if (verb == OAI_VERBS.LIST_RECORDS) { return "oai/OAI_ListRecords"; }
|
225 |
26600
|
sandro.lab
|
|
226 |
|
|
return "oai/OAI_ListIdentifiers";
|
227 |
|
|
}
|
228 |
|
|
|
229 |
|
|
private String oaiListSets(final Map<String, String> params, final ModelMap map) throws Exception {
|
230 |
|
|
String verb = null;
|
231 |
|
|
if (params.containsKey("verb")) {
|
232 |
|
|
verb = params.get("verb");
|
233 |
|
|
params.remove("verb");
|
234 |
|
|
}
|
235 |
42184
|
michele.ar
|
if (params.entrySet().size() > 0) { return oaiError(OAIError.badArgument, verb, map); }
|
236 |
38446
|
alessia.ba
|
this.core.setCurrentDBFromIS();
|
237 |
42184
|
michele.ar
|
map.addAttribute("sets", this.core.listSets());
|
238 |
26600
|
sandro.lab
|
return "oai/OAI_ListSets";
|
239 |
|
|
}
|
240 |
|
|
|
241 |
|
|
private String oaiListMetadataFormats(final Map<String, String> params, final ModelMap map) throws Exception {
|
242 |
|
|
String id = null;
|
243 |
|
|
String verb = null;
|
244 |
|
|
if (params.containsKey("verb")) {
|
245 |
|
|
verb = params.get("verb");
|
246 |
|
|
params.remove("verb");
|
247 |
|
|
}
|
248 |
|
|
if (params.containsKey("identifier")) {
|
249 |
|
|
id = params.get("identifier");
|
250 |
|
|
params.remove("identifier");
|
251 |
|
|
}
|
252 |
42184
|
michele.ar
|
if (params.entrySet().size() > 0) { return oaiError(OAIError.badArgument, verb, map); }
|
253 |
38446
|
alessia.ba
|
this.core.setCurrentDBFromIS();
|
254 |
42184
|
michele.ar
|
map.addAttribute("formats", this.core.listMetadataFormats());
|
255 |
26600
|
sandro.lab
|
if (id != null) {
|
256 |
|
|
map.addAttribute("identifier", id);
|
257 |
|
|
return "oai/OAI_ListMetadataFormats_withid";
|
258 |
|
|
}
|
259 |
|
|
return "oai/OAI_ListMetadataFormats";
|
260 |
|
|
}
|
261 |
|
|
|
262 |
34802
|
alessia.ba
|
private String oaiError(final OAIError errCode, final String verb, final ModelMap map) throws Exception {
|
263 |
42184
|
michele.ar
|
if (StringUtils.isBlank(verb)) { return oaiError(errCode, map); }
|
264 |
26600
|
sandro.lab
|
map.addAttribute("verb", verb);
|
265 |
34802
|
alessia.ba
|
map.addAttribute("errcode", errCode.name());
|
266 |
|
|
map.addAttribute("errmsg", errCode.getMessage());
|
267 |
26600
|
sandro.lab
|
return "oai/OAI_Error";
|
268 |
|
|
}
|
269 |
|
|
|
270 |
34802
|
alessia.ba
|
private String oaiError(final OAIError errCode, final ModelMap map) throws Exception {
|
271 |
|
|
map.addAttribute("errcode", errCode.name());
|
272 |
|
|
map.addAttribute("errmsg", errCode.getMessage());
|
273 |
26600
|
sandro.lab
|
return "oai/OAI_Error_noverb";
|
274 |
|
|
}
|
275 |
|
|
|
276 |
|
|
private String oaiError(final Exception e, final ModelMap map) throws Exception {
|
277 |
|
|
map.addAttribute("errcode", "InternalException");
|
278 |
|
|
map.addAttribute("errmsg", e.getMessage());
|
279 |
|
|
return "oai/OAI_Error_noverb";
|
280 |
|
|
}
|
281 |
|
|
|
282 |
|
|
/*
|
283 |
|
|
* Default Attribute
|
284 |
|
|
*/
|
285 |
|
|
@ModelAttribute("url")
|
286 |
|
|
public String url(final HttpServletRequest request) {
|
287 |
42184
|
michele.ar
|
final String forwardedUrl = request.getHeader(getForwardedUrlHeaderName());
|
288 |
|
|
final String baseURL = getBaseUrl();
|
289 |
|
|
if (StringUtils.isNotBlank(baseURL)) {
|
290 |
|
|
return baseURL;
|
291 |
|
|
} else if (StringUtils.isNotBlank(forwardedUrl)) {
|
292 |
|
|
return forwardedUrl;
|
293 |
|
|
} else {
|
294 |
|
|
return request.getRequestURL() + "";
|
295 |
|
|
}
|
296 |
26600
|
sandro.lab
|
}
|
297 |
|
|
|
298 |
|
|
/**
|
299 |
|
|
* Model attribute for the date of response.
|
300 |
|
|
* <p>
|
301 |
|
|
* According to OAI-PMH protocol, it must be in UTC with format YYYY-MM-DDThh:mm:ssZ, where Z is the time zone ('Z' for "Zulu Time",
|
302 |
|
|
* i.e. UTC).
|
303 |
|
|
* </p>
|
304 |
|
|
* <p>
|
305 |
|
|
* See http://www.openarchives.org/OAI/openarchivesprotocol.html#Dates
|
306 |
|
|
* </p>
|
307 |
|
|
*/
|
308 |
|
|
@ModelAttribute("date")
|
309 |
|
|
public String date() {
|
310 |
42184
|
michele.ar
|
final SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
|
311 |
26600
|
sandro.lab
|
formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
|
312 |
42184
|
michele.ar
|
final String date = formatter.format(new Date());
|
313 |
26600
|
sandro.lab
|
return date.replace("+0000", "Z");
|
314 |
|
|
}
|
315 |
|
|
|
316 |
|
|
@ModelAttribute("repoName")
|
317 |
|
|
public String getRepoName() {
|
318 |
42184
|
michele.ar
|
return this.oaiProperties.getRepoName();
|
319 |
26600
|
sandro.lab
|
}
|
320 |
|
|
|
321 |
|
|
@ModelAttribute("email")
|
322 |
|
|
public String getRepoEmail() {
|
323 |
42184
|
michele.ar
|
return this.oaiProperties.getRepoEmail();
|
324 |
26600
|
sandro.lab
|
}
|
325 |
|
|
|
326 |
|
|
@ModelAttribute("earliestDatestamp")
|
327 |
|
|
public String getEarliestDatestamp() {
|
328 |
42184
|
michele.ar
|
return this.oaiProperties.getEarliestDatestamp();
|
329 |
26600
|
sandro.lab
|
}
|
330 |
|
|
|
331 |
|
|
public String getBaseUrl() {
|
332 |
42184
|
michele.ar
|
return this.oaiProperties.getBaseUrl();
|
333 |
26600
|
sandro.lab
|
}
|
334 |
|
|
|
335 |
|
|
public String getForwardedUrlHeaderName() {
|
336 |
42184
|
michele.ar
|
return this.oaiProperties.getForwardedUrlHeaderName();
|
337 |
26600
|
sandro.lab
|
}
|
338 |
|
|
|
339 |
|
|
@ModelAttribute("deletedRecord")
|
340 |
|
|
public String getDeletedRecordSupport() {
|
341 |
42184
|
michele.ar
|
return this.oaiProperties.getDeletedRecordSupport();
|
342 |
26600
|
sandro.lab
|
}
|
343 |
|
|
|
344 |
|
|
@ModelAttribute("granularity")
|
345 |
|
|
public String getDateGranularity() {
|
346 |
42184
|
michele.ar
|
return this.oaiProperties.getDateGranularity();
|
347 |
26600
|
sandro.lab
|
}
|
348 |
42181
|
sandro.lab
|
|
349 |
|
|
public enum OAI_VERBS {
|
350 |
|
|
IDENTIFY, LIST_IDENTIFIERS, LIST_RECORDS, LIST_METADATA_FORMATS, LIST_SETS, GET_RECORD, UNSUPPORTED_VERB;
|
351 |
|
|
|
352 |
|
|
public static OAI_VERBS getVerb(final String theVerb) {
|
353 |
42184
|
michele.ar
|
if (StringUtils.isBlank(theVerb)) { return UNSUPPORTED_VERB; }
|
354 |
|
|
if (theVerb.equalsIgnoreCase("Identify")) { return IDENTIFY; }
|
355 |
|
|
if (theVerb.equalsIgnoreCase("ListIdentifiers")) { return LIST_IDENTIFIERS; }
|
356 |
|
|
if (theVerb.equalsIgnoreCase("ListRecords")) { return LIST_RECORDS; }
|
357 |
|
|
if (theVerb.equalsIgnoreCase("ListMetadataFormats")) { return LIST_METADATA_FORMATS; }
|
358 |
|
|
if (theVerb.equalsIgnoreCase("ListSets")) { return LIST_SETS; }
|
359 |
|
|
if (theVerb.equalsIgnoreCase("GetRecord")) { return GET_RECORD; }
|
360 |
|
|
if (theVerb.equalsIgnoreCase("listidentifiers")) { return LIST_IDENTIFIERS; }
|
361 |
42181
|
sandro.lab
|
return UNSUPPORTED_VERB;
|
362 |
|
|
|
363 |
|
|
}
|
364 |
|
|
}
|
365 |
|
|
|
366 |
|
|
public enum DELETED_SUPPORT {
|
367 |
|
|
NO, TRANSIENT, PERSISTENT;
|
368 |
|
|
|
369 |
|
|
@Override
|
370 |
|
|
public String toString() {
|
371 |
|
|
switch (this) {
|
372 |
|
|
case TRANSIENT:
|
373 |
|
|
return "transient";
|
374 |
|
|
case PERSISTENT:
|
375 |
|
|
return "persistent";
|
376 |
|
|
default:
|
377 |
|
|
return "no";
|
378 |
|
|
}
|
379 |
|
|
}
|
380 |
|
|
|
381 |
|
|
}
|
382 |
26600
|
sandro.lab
|
}
|