source: main/trunk/greenstone3/src/java/org/greenstone/gsdl3/core/OAIReceptionist.java@ 29270

Last change on this file since 29270 was 29270, checked in by kjdon, 10 years ago

removed some old stuff

File size: 41.3 KB
Line 
1/*
2 * OAIReceptionist.java
3 * Copyright (C) 2012 New Zealand Digital Library, http://www.nzdl.org
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20package org.greenstone.gsdl3.core;
21
22import org.greenstone.gsdl3.util.*;
23import org.greenstone.gsdl3.action.*;
24// XML classes
25import org.w3c.dom.Node;
26import org.w3c.dom.NodeList;
27import org.w3c.dom.Document;
28import org.w3c.dom.Element;
29
30// other java classes
31import java.io.File;
32import java.util.*;
33
34import org.apache.log4j.*;
35
36/** a Receptionist, used for oai metadata response xml generation.
37 * This receptionist talks to the message router directly,
38 * instead of via any action, hence no action map is needed.
39 * @see the basic Receptionist
40 */
41public class OAIReceptionist implements ModuleInterface {
42
43 static Logger logger = Logger.getLogger(org.greenstone.gsdl3.core.OAIReceptionist.class.getName());
44
45 /** Instead of a config_params object, only a site_name is needed by oai receptionist. */
46 protected String site_name = null;
47 /** The unique repository identifier */
48 protected String repository_id = null;
49
50 /** the configure file of this receptionist passed from the oai servlet. */
51 protected Element oai_config = null;
52
53 /** contained in the OAIConfig.xml deciding whether the resumptionToken should be in use */
54 protected int resume_after = -1 ;
55
56 /** the message router that the Receptionist and Actions will talk to */
57 protected ModuleInterface mr = null;
58
59 // Some of the data/responses will not change while the servlet is running, so
60 // we can cache them
61
62 /** A list of all the collections available to this OAI server */
63 protected Element collection_list = null;
64 /** a vector of the names, for convenience */
65 protected Vector<String> collection_name_list = null;
66 /** If this is true, then there are no OAI enabled collections, so can always return noRecordsMatch (after validating the request params) */
67 protected boolean noRecordsMatch = false;
68
69 /** A set of all known 'sets' */
70 protected HashSet<String> set_set = null;
71
72 protected boolean has_super_colls = false;
73 /** a hash of super set-> collection list */
74 protected HashMap<String, Vector<String>> super_coll_map = null;
75 /** store the super coll elements for convenience */
76 HashMap<String, Element> super_coll_data = null;
77 /** store the metadata formats ??????*/
78 /** The identify response */
79 protected Element identify_response = null;
80 /** The list set response */
81 protected Element listsets_response = null;
82 /** the list metadata formats response */
83 protected Element listmetadataformats_response = null;
84
85 public OAIReceptionist() {
86
87 }
88
89 public void cleanUp() {
90 if (this.mr != null) {
91
92 this.mr.cleanUp();
93 }
94 OAIResumptionToken.saveTokensToFile();
95 }
96
97 public void setSiteName(String site_name) {
98 this.site_name = site_name;
99 }
100 /** sets the message router - it should already be created and
101 * configured in the init() of a servlet (OAIServer, for example) before being passed to the receptionist*/
102 public void setMessageRouter(ModuleInterface mr) {
103 this.mr = mr;
104 }
105
106 /** configures the receptionist */
107 public boolean configure(Element config) {
108
109 if (this.mr==null) {
110 logger.error(" message routers must be set before calling oai configure");
111 return false;
112 }
113 if (config == null) {
114 logger.error(" oai configure file is null");
115 return false;
116 }
117 oai_config = config;
118 resume_after = getResumeAfter();
119
120 repository_id = getRepositoryIdentifier();
121 configureSuperSetInfo();
122 if (!configureSetInfo()) {
123 // there are no sets
124 logger.error("No sets (collections) available for OAI");
125 return false;
126 }
127
128 // load in tokens from OAIResumptionToken.xml, and then clear out any
129 // expired ones.
130 OAIResumptionToken.init();
131 OAIResumptionToken.clearExpiredTokens();
132
133 return true;
134 }
135
136 // assuming that sets are static. If collections change then the servlet
137 // should be restarted.
138 private boolean configureSuperSetInfo() {
139 // do we have any super colls listed in web/WEB-INF/classes/OAIConfig.xml?
140 // Will be like
141 // <oaiSuperSet>
142 // <SetSpec>xxx</SetSpec>
143 // <setName>xxx</SetName>
144 // <SetDescription>xxx</setDescription>
145 // </oaiSuperSet>
146 // The super set is listed in OAIConfig, and collections themselves state
147 // whether they are part of the super set or not.
148 NodeList super_coll_list = this.oai_config.getElementsByTagName(OAIXML.OAI_SUPER_SET);
149 this.super_coll_data = new HashMap<String, Element>();
150 if (super_coll_list.getLength() > 0) {
151 this.has_super_colls = true;
152 for (int i=0; i<super_coll_list.getLength(); i++) {
153 Element super_coll = (Element)super_coll_list.item(i);
154 Element set_spec = (Element)GSXML.getChildByTagName(super_coll, OAIXML.SET_SPEC);
155 if (set_spec != null) {
156 String name = GSXML.getNodeText(set_spec);
157 if (!name.equals("")) {
158 this.super_coll_data.put(name, super_coll);
159 logger.error("adding in super coll "+name);
160 }
161 }
162 }
163
164 if (this.super_coll_data.size()==0) {
165 this.has_super_colls = false;
166 }
167 }
168 if (this.has_super_colls == true) {
169 this.super_coll_map = new HashMap<String, Vector<String>>();
170 }
171 return true;
172
173 }
174 private boolean configureSetInfo() {
175 this.set_set = new HashSet<String>();
176
177 // First, we get a list of all the OAI enabled collections
178 // We get this by sending a listSets request to the MR
179 Document doc = XMLConverter.newDOM();
180 Element message = doc.createElement(GSXML.MESSAGE_ELEM);
181
182 Element request = GSXML.createBasicRequest(doc, OAIXML.OAI_SET_LIST, "", null);
183 message.appendChild(request);
184 Node msg_node = mr.process(message);
185
186 if (msg_node == null) {
187 logger.error("returned msg_node from mr is null");
188 return false;
189 }
190 Element resp = (Element)GSXML.getChildByTagName(msg_node, GSXML.RESPONSE_ELEM);
191 Element coll_list = (Element)GSXML.getChildByTagName(resp, GSXML.COLLECTION_ELEM + GSXML.LIST_MODIFIER);
192 if (coll_list == null) {
193 logger.error("coll_list is null");
194 return false;
195 }
196
197 this.collection_list = (Element)doc.importNode(coll_list, true);
198
199 // go through and store a list of collection names for convenience
200 // also create a 'to' attribute for the next request to the MR, which
201 // is a ListSets request to each collection
202 Node child = this.collection_list.getFirstChild();
203 if (child == null) {
204 logger.error("collection list has no children");
205 noRecordsMatch = true;
206 return false;
207 }
208
209 this.collection_name_list = new Vector<String>();
210 StringBuffer to = new StringBuffer();
211 boolean first = true;
212 while (child != null) {
213 if (child.getNodeName().equals(GSXML.COLLECTION_ELEM)) {
214 String coll_id =((Element) child).getAttribute(GSXML.NAME_ATT);
215 this.collection_name_list.add(coll_id);
216 if (!first) {
217 to.append(',');
218 }
219 first = false;
220 to.append(coll_id+"/"+OAIXML.LIST_SETS);
221 }
222 child = child.getNextSibling();
223 }
224 if (first) {
225 // we haven't found any collections
226 logger.error("found no collection elements in collectionList");
227 noRecordsMatch = true;
228 return false;
229 }
230 Document listsets_doc = XMLConverter.newDOM();
231 Element listsets_element = listsets_doc.createElement(OAIXML.LIST_SETS);
232 this.listsets_response = getMessage(listsets_doc, listsets_element);
233
234 // Now, for each collection, get a list of all its sets
235 // might include subsets (classifiers) or super colls
236 // We'll reuse the first message, changing its type and to atts
237 request.setAttribute(GSXML.TYPE_ATT, "");
238 request.setAttribute(GSXML.TO_ATT, to.toString());
239 // send to MR
240 msg_node = mr.process(message);
241 logger.error(XMLConverter.getPrettyString(msg_node));
242 NodeList response_list = ((Element)msg_node).getElementsByTagName(GSXML.RESPONSE_ELEM);
243 for (int c=0; c<response_list.getLength(); c++) {
244 // for each collection's response
245 Element response = (Element)response_list.item(c);
246 String coll_name = GSPath.getFirstLink(response.getAttribute(GSXML.FROM_ATT));
247 logger.error("coll from response "+coll_name);
248 NodeList set_list = response.getElementsByTagName(OAIXML.SET);
249 for (int j=0; j<set_list.getLength(); j++) {
250 // now check if it a super collection
251 Element set = (Element)set_list.item(j);
252 String set_spec = GSXML.getNodeText((Element)GSXML.getChildByTagName(set, OAIXML.SET_SPEC));
253 logger.error("set spec = "+set_spec);
254 // this may change if we add site name back in
255 // setSpecs will be collname or collname:subset or supercollname
256 if (set_spec.indexOf(":")==-1 && ! set_spec.equals(coll_name)) {
257 // it must be a super coll spec
258 logger.error("found super coll, "+set_spec);
259 // check that it is a valid one from config
260 if (this.has_super_colls == true && this.super_coll_data.containsKey(set_spec)) {
261 Vector <String> subcolls = this.super_coll_map.get(set_spec);
262 if (subcolls == null) {
263 logger.error("its new!!");
264 // not in there yet
265 subcolls = new Vector<String>();
266 this.set_set.add(set_spec);
267 this.super_coll_map.put(set_spec, subcolls);
268 // the first time a supercoll is mentioned, add into the set list
269 logger.error("finding the set info "+XMLConverter.getPrettyString(this.super_coll_data.get(set_spec)));
270 listsets_element.appendChild(GSXML.duplicateWithNewName(listsets_doc, this.super_coll_data.get(set_spec), OAIXML.SET, true));
271 }
272 // add this collection to the list for the super coll
273 subcolls.add(coll_name);
274 }
275 } else { // its either the coll itself or a subcoll
276 // add in the set
277 listsets_element.appendChild(listsets_doc.importNode(set, true));
278 this.set_set.add(set_spec);
279 }
280 } // for each set in the collection
281 } // for each OAI enabled collection
282 return true;
283 }
284
285 protected void resetMessageRouter() {
286 // we just need to send a configure request to MR
287 Document doc = XMLConverter.newDOM();
288 Element mr_request_message = doc.createElement(GSXML.MESSAGE_ELEM);
289 Element mr_request = GSXML.createBasicRequest(doc, GSXML.REQUEST_TYPE_SYSTEM, "", null);
290 mr_request_message.appendChild(mr_request);
291
292 Element system = doc.createElement(GSXML.SYSTEM_ELEM);
293 mr_request.appendChild(system);
294 system.setAttribute(GSXML.TYPE_ATT, GSXML.SYSTEM_TYPE_CONFIGURE);
295
296 Element response = (Element) this.mr.process(mr_request_message);
297 logger.error("configure response = "+XMLConverter.getPrettyString(response));
298 }
299 /** process using strings - just calls process using Elements */
300 public String process(String xml_in) {
301
302 Node message_node = XMLConverter.getDOM(xml_in);
303 Node page = process(message_node);
304 return XMLConverter.getString(page);
305 }
306
307 //Compose a message/response element used to send back to the OAIServer servlet.
308 //This method is only used within OAIReceptionist
309 private Element getMessage(Document doc, Element e) {
310 Element msg = doc.createElement(GSXML.MESSAGE_ELEM);
311 Element response = doc.createElement(GSXML.RESPONSE_ELEM);
312 msg.appendChild(response);
313 response.appendChild(e);
314 return msg;
315 }
316
317 /** process - produce xml data in response to a request
318 * if something goes wrong, it returns null -
319 */
320 public Node process(Node message_node) {
321 logger.error("OAIReceptionist received request");
322
323 Element message = GSXML.nodeToElement(message_node);
324 logger.error(XMLConverter.getString(message));
325
326 // check that its a correct message tag
327 if (!message.getTagName().equals(GSXML.MESSAGE_ELEM)) {
328 logger.error(" Invalid message. GSDL message should start with <"+GSXML.MESSAGE_ELEM+">, instead it starts with:"+message.getTagName()+".");
329 return OAIXML.createErrorMessage(OAIXML.BAD_ARGUMENT, "Internal messaging error");
330 }
331
332 // get the request out of the message - assume that there is only one
333 Element request = (Element)GSXML.getChildByTagName(message, GSXML.REQUEST_ELEM);
334 if (request == null) {
335 logger.error(" message had no request!");
336 return OAIXML.createErrorMessage(OAIXML.BAD_ARGUMENT, "Internal messaging error");
337 }
338
339 // special case, reset=true for reloading the MR and recept data
340 String reset = request.getAttribute("reset");
341 if (!reset.equals("")) {
342 resetMessageRouter();
343 configureSetInfo();
344 return OAIXML.createResetResponse(true);
345 }
346
347
348 //At this stage, the value of 'to' attribute of the request must be the 'verb'
349 //The only thing that the oai receptionist can be sure is that these verbs are valid, nothing else.
350 String verb = request.getAttribute(GSXML.TO_ATT);
351 if (verb.equals(OAIXML.IDENTIFY)) {
352 return doIdentify();
353 }
354 if (verb.equals(OAIXML.LIST_METADATA_FORMATS)) {
355 return doListMetadataFormats(request);
356 }
357 if (verb.equals(OAIXML.LIST_SETS)) {
358 // we have composed the list sets response on init
359 // Note this means that list sets never uses resumption tokens
360 return this.listsets_response;
361 }
362 if (verb.equals(OAIXML.GET_RECORD)) {
363 return doGetRecord(request);
364 }
365 if (verb.equals(OAIXML.LIST_IDENTIFIERS)) {
366 return doListIdentifiersOrRecords(request,OAIXML.LIST_IDENTIFIERS , OAIXML.HEADER);
367 }
368 if (verb.equals(OAIXML.LIST_RECORDS)) {
369 return doListIdentifiersOrRecords(request, OAIXML.LIST_RECORDS, OAIXML.RECORD);
370 }
371 // should never get here as verbs were checked in OAIServer
372 return OAIXML.createErrorMessage(OAIXML.BAD_VERB, "Unexpected things happened");
373
374 }
375
376
377 private int getResumeAfter() {
378 Element resume_after = (Element)GSXML.getChildByTagName(oai_config, OAIXML.RESUME_AFTER);
379 if(resume_after != null) return Integer.parseInt(GSXML.getNodeText(resume_after));
380 return -1;
381 }
382 private String getRepositoryIdentifier() {
383 Element ri = (Element)GSXML.getChildByTagName(oai_config, OAIXML.REPOSITORY_IDENTIFIER);
384 if (ri != null) {
385 return GSXML.getNodeText(ri);
386 }
387 return "";
388 }
389
390
391 /** if the param_map contains strings other than those in valid_strs, return false;
392 * otherwise true.
393 */
394 private boolean areAllParamsValid(HashMap<String, String> param_map, HashSet<String> valid_strs) {
395 ArrayList<String> param_list = new ArrayList<String>(param_map.keySet());
396 for(int i=0; i<param_list.size(); i++) {
397 logger.error("param, key = "+param_list.get(i)+", value = "+param_map.get(param_list.get(i)));
398 if (valid_strs.contains(param_list.get(i)) == false) {
399 return false;
400 }
401 }
402 return true;
403 }
404
405 private Element doListIdentifiersOrRecords(Element req, String verb, String record_type) {
406 // options: from, until, set, metadataPrefix, resumptionToken
407 // exceptions: badArgument, badResumptionToken, cannotDisseminateFormat, noRecordMatch, and noSetHierarchy
408 HashSet<String> valid_strs = new HashSet<String>();
409 valid_strs.add(OAIXML.FROM);
410 valid_strs.add(OAIXML.UNTIL);
411 valid_strs.add(OAIXML.SET);
412 valid_strs.add(OAIXML.METADATA_PREFIX);
413 valid_strs.add(OAIXML.RESUMPTION_TOKEN);
414
415 Document result_doc = XMLConverter.newDOM();
416 Element result_element = result_doc.createElement(verb);
417 boolean result_token_needed = false; // does this result need to include a
418 // resumption token
419
420 NodeList params = GSXML.getChildrenByTagName(req, GSXML.PARAM_ELEM);
421
422 HashMap<String, String> param_map = GSXML.getParamMap(params);
423
424 // are all the params valid?
425 if (!areAllParamsValid(param_map, valid_strs)) {
426 logger.error("One of the params is invalid");
427 return OAIXML.createErrorMessage(OAIXML.BAD_ARGUMENT, "There was an invalid parameter");
428 // TODO, need to tell the user which one was invalid ??
429 }
430
431 // Do we have a resumption token??
432 String token = null;
433 String from = null;
434 String until = null;
435 boolean set_requested = false;
436 String set_spec_str = null;
437 String prefix_value = null;
438 int cursor = 0;
439 int current_cursor = 0;
440 String current_set = null;
441 long initial_time = 0;
442
443 int total_size = -1; // we are only going to set this in resumption
444 // token if it is easy to work out, i.e. not sending extra requests to
445 // MR just to calculate total size
446
447 if(param_map.containsKey(OAIXML.RESUMPTION_TOKEN)) {
448 // Is it an error to have other arguments? Do we need to check to make sure that resumptionToken is the only arg??
449 // validate resumptionToken
450 token = param_map.get(OAIXML.RESUMPTION_TOKEN);
451 logger.info("has resumptionToken " + token);
452 if(OAIResumptionToken.isValidToken(token) == false) {
453 logger.error("token is not valid");
454 return OAIXML.createErrorMessage(OAIXML.BAD_RESUMPTION_TOKEN, "");
455 }
456 result_token_needed = true; // we always need to send a token back if we have started with one. It may be empty if we are returning the end of the list
457 // initialise the request params from the stored token data
458 HashMap<String, String> token_data = OAIResumptionToken.getTokenData(token);
459 from = token_data.get(OAIXML.FROM);
460 until = token_data.get(OAIXML.UNTIL);
461 set_spec_str = token_data.get(OAIXML.SET);
462 if (set_spec_str != null) {
463 set_requested = true;
464 }
465 prefix_value = token_data.get(OAIXML.METADATA_PREFIX);
466 current_set = token_data.get(OAIResumptionToken.CURRENT_SET);
467 try {
468 cursor = Integer.parseInt(token_data.get(OAIXML.CURSOR));
469 cursor = cursor + resume_after; // increment cursor
470 current_cursor = Integer.parseInt(token_data.get(OAIResumptionToken.CURRENT_CURSOR));
471 initial_time = Long.parseLong(token_data.get(OAIResumptionToken.INITIAL_TIME));
472 } catch (NumberFormatException e) {
473 logger.error("tried to parse int from cursor data and failed");
474 }
475
476 // check that the collections/sets haven't changed since the token was issued
477 if (collectionsChangedSinceTime(set_spec_str, initial_time)) {
478 logger.error("one of the collections in set "+set_spec_str+" has changed since token issued. Expiring the token");
479 OAIResumptionToken.expireToken(token);
480 return OAIXML.createErrorMessage(OAIXML.BAD_RESUMPTION_TOKEN, "Repository data has changed since this token was issued. Resend original request");
481 }
482 }
483 else {
484 // no resumption token, lets check the other params
485 // there must be a metadataPrefix
486 if (!param_map.containsKey(OAIXML.METADATA_PREFIX)) {
487 logger.error("metadataPrefix param required");
488 return OAIXML.createErrorMessage(OAIXML.BAD_ARGUMENT, "metadataPrefix param required");
489 }
490
491 //if there are any date params, check they're of the right format
492 from = param_map.get(OAIXML.FROM);
493 if(from != null) {
494 Date from_date = OAIXML.getDate(from);
495 if(from_date == null) {
496 logger.error("invalid date: " + from);
497 return OAIXML.createErrorMessage(OAIXML.BAD_ARGUMENT, "invalid format for "+ OAIXML.FROM);
498 }
499 }
500 until = param_map.get(OAIXML.UNTIL);
501 if(until != null) {
502 Date until_date = OAIXML.getDate(until);
503 if(until_date == null) {
504 logger.error("invalid date: " + until);
505 return OAIXML.createErrorMessage(OAIXML.BAD_ARGUMENT, "invalid format for "+ OAIXML.UNTIL);
506 }
507 }
508 if(from != null && until != null) { // check they are of the same date-time format (granularity)
509 if(from.length() != until.length()) {
510 logger.error("The request has different granularities (date-time formats) for the From and Until date parameters.");
511 return OAIXML.createErrorMessage(OAIXML.BAD_ARGUMENT, "The request has different granularities (date-time formats) for the From and Until date parameters.");
512 }
513 }
514
515 // check the set arg is a set we know about
516 set_requested = param_map.containsKey(OAIXML.SET);
517 set_spec_str = null;
518 if(set_requested == true) {
519 set_spec_str = param_map.get(OAIXML.SET);
520 if (!this.set_set.contains(set_spec_str)) {
521 // the set is not one we know about
522 logger.error("requested set is not found in this repository");
523 return OAIXML.createErrorMessage(OAIXML.BAD_ARGUMENT, "invalid set parameter");
524
525 }
526 }
527 // Is the metadataPrefix arg one this repository supports?
528 prefix_value = param_map.get(OAIXML.METADATA_PREFIX);
529 if (repositorySupportsMetadataPrefix(prefix_value) == false) {
530 logger.error("requested metadataPrefix is not found in OAIConfig.xml");
531 return OAIXML.createErrorMessage(OAIXML.CANNOT_DISSEMINATE_FORMAT, "metadata format "+prefix_value+" not supported by this repository");
532 }
533
534 } // else no resumption token, check other params
535
536 // Whew. Now we have validated the params, we can work on doing the actual
537 // request
538
539
540 Document doc = XMLConverter.newDOM();
541 Element mr_msg = doc.createElement(GSXML.MESSAGE_ELEM);
542 Element mr_req = doc.createElement(GSXML.REQUEST_ELEM);
543 // TODO does this need a type???
544 mr_msg.appendChild(mr_req);
545
546 // copy in the from/until params if there
547 if (from != null) {
548 mr_req.appendChild(GSXML.createParameter(doc, OAIXML.FROM, from));
549 }
550 if (until != null) {
551 mr_req.appendChild(GSXML.createParameter(doc, OAIXML.UNTIL, until));
552 }
553 // add metadataPrefix
554 mr_req.appendChild(GSXML.createParameter(doc, OAIXML.METADATA_PREFIX, prefix_value));
555
556 // do we have a set???
557 // if no set, we send to all collections in the collection list
558 // if super set, we send to all collections in super set list
559 // if a single collection, send to it
560 // if a subset, send to the collection
561 Vector<String> current_coll_list = getCollectionListForSet(set_spec_str);
562 boolean single_collection = false;
563 if (current_coll_list.size() == 1) {
564 single_collection = true;
565 }
566 if (set_spec_str != null && set_spec_str.indexOf(":") != -1) {
567 // we have a subset - add the set param back in
568 mr_req.appendChild(GSXML.createParameter(doc, OAIXML.SET, set_spec_str));
569 }
570
571 int num_collected_records = 0;
572 int start_point = current_cursor; // may not be 0 if we are using a resumption token
573 String resumption_collection = "";
574 boolean empty_result_token = false; // if we are sending the last part of a list, then the token value will be empty
575
576 // iterate through the list of collections and send the request to each
577
578 int start_coll=0;
579 if (current_set != null) {
580 // we are resuming a previous request, need to locate the first collection
581 for (int i=0; i<current_coll_list.size(); i++) {
582 if (current_set.equals(current_coll_list.get(i))) {
583 start_coll = i;
584 break;
585 }
586 }
587 }
588
589 for (int i=start_coll; i<current_coll_list.size(); i++) {
590 String current_coll = current_coll_list.get(i);
591 mr_req.setAttribute(GSXML.TO_ATT, current_coll+"/"+verb);
592
593 Element result = (Element)mr.process(mr_msg);
594 logger.error(verb+ " result for coll "+current_coll);
595 logger.error(XMLConverter.getPrettyString(result));
596 if (result == null) {
597 logger.info("message router returns null");
598 // do what??? carry on? fail??
599 return OAIXML.createErrorMessage("Internal service returns null", "");
600 }
601 Element res = (Element)GSXML.getChildByTagName(result, GSXML.RESPONSE_ELEM);
602 if(res == null) {
603 logger.info("response element in xml_result is null");
604 return OAIXML.createErrorMessage("Internal service returns null", "");
605 }
606 NodeList record_list = res.getElementsByTagName(record_type);
607 int num_records = record_list.getLength();
608 if(num_records == 0) {
609 logger.info("message router returns 0 records for coll "+current_coll);
610 continue; // try the next collection
611 }
612 if (single_collection) {
613 total_size = num_records;
614 }
615 int records_to_add = (resume_after > 0 ? resume_after - num_collected_records : num_records);
616 if (records_to_add > (num_records-start_point)) {
617 records_to_add = num_records-start_point;
618 }
619 addRecordsToList(result_doc, result_element, record_list, start_point, records_to_add);
620 num_collected_records += records_to_add;
621
622 // do we need to stop here, and do we need to issue a resumption token?
623 if (resume_after > 0 && num_collected_records == resume_after) {
624 // we have finished collecting records at the moment.
625 // but are we conincidentally at the end? or are there more to go?
626 if (records_to_add < (num_records - start_point)) {
627 // we have added less than this collection had
628 start_point += records_to_add;
629 resumption_collection = current_coll;
630 result_token_needed = true;
631 }
632 else {
633 // we added all this collection had to offer
634 // is there another collection in the list??
635 if (i<current_coll_list.size()-1) {
636 result_token_needed = true;
637 start_point = 0;
638 resumption_collection = current_coll_list.get(i+1);
639 }
640 else {
641 // we have finished one collection and there are no more collection
642 // if we need to send a resumption token (in this case, only because we started with one, then it will be empty
643 logger.error("at end of list, need empty result token");
644 empty_result_token = true;
645 }
646 }
647 break;
648 }
649 start_point = 0; // only the first one will have start non-zero, if we
650 // have a resumption token
651
652 } // for each collection
653
654 if (num_collected_records ==0) {
655 // there were no matching results
656 return OAIXML.createErrorMessage(OAIXML.NO_RECORDS_MATCH, "");
657 }
658
659 if (num_collected_records < resume_after) {
660 // we have been through all collections, and there are no more
661 // if we need a result token - only because we started with one, so we need to send an empty one, then make sure everyone knows we are just sending an empty one
662 if (result_token_needed) {
663 empty_result_token = true;
664 }
665 }
666
667 if (result_token_needed) {
668 // we need a resumption token
669 if (empty_result_token) {
670 logger.error("have empty result token");
671 token = "";
672 } else {
673 if (token != null) {
674 // we had a token for this request, we can just update it
675 token = OAIResumptionToken.updateToken(token, ""+cursor, resumption_collection, ""+start_point);
676 } else {
677 // we are generating a new one
678 token = OAIResumptionToken.createAndStoreResumptionToken(set_spec_str, prefix_value, from, until, ""+cursor, resumption_collection, ""+start_point );
679 }
680 }
681
682 // result token XML
683 long expiration_date = -1;
684 if (empty_result_token) {
685 // we know how many records in total as we have sent them all
686 total_size = cursor+num_collected_records;
687 } else {
688 // non-empty token, set the expiration date
689 expiration_date = OAIResumptionToken.getExpirationDate(token);
690 }
691 Element token_elem = OAIXML.createResumptionTokenElement(result_doc, token, total_size, cursor, expiration_date);
692 // OAIXML.addToken(token_elem); // store it
693 result_element.appendChild(token_elem); // add to the result
694 }
695
696
697 return getMessage(result_doc, result_element);
698 }
699
700 private Vector<String> getCollectionListForSet(String set) {
701 if (set == null) {
702 // no set requested, need the complete collection list
703 return this.collection_name_list;
704 }
705 if (has_super_colls && super_coll_map.containsKey(set)) {
706 return super_coll_map.get(set);
707 }
708
709 Vector<String> coll_list = new Vector<String>();
710 if (set.indexOf(":") != -1) {
711 String col_name = set.substring(0, set.indexOf(":"));
712 coll_list.add(col_name);
713 }
714 else {
715 coll_list.add(set);
716 }
717 return coll_list;
718 }
719 private void addRecordsToList(Document doc, Element result_element, NodeList
720 record_list, int start_point, int num_records) {
721 int end_point = start_point + num_records;
722 for (int i=start_point; i<end_point; i++) {
723 result_element.appendChild(doc.importNode(record_list.item(i), true));
724 }
725 }
726
727 private Element collectAll(Element result, Element msg, String verb, String elem_name) {
728 if(result == null) {
729 //in the first round, result is null
730 return msg;
731 }
732 Element res_in_result = (Element)GSXML.getChildByTagName(result, GSXML.RESPONSE_ELEM);
733 if(res_in_result == null) { // return the results of all other collections accumulated so far
734 return msg;
735 }
736 Element verb_elem = (Element)GSXML.getChildByTagName(res_in_result, verb);
737 if(msg == null) {
738 return result;
739 }
740
741 //e.g., get all <record> elements from the returned message. There may be none of
742 //such element, for example, the collection service returned an error message
743 NodeList elem_list = msg.getElementsByTagName(elem_name);
744
745 for (int i=0; i<elem_list.getLength(); i++) {
746 verb_elem.appendChild(res_in_result.getOwnerDocument().importNode(elem_list.item(i), true));
747 }
748 return result;
749 }
750
751
752 /** there are three possible exception conditions: bad argument, idDoesNotExist, and noMetadataFormat.
753 * The first one is handled here, and the last two are processed by OAIPMH.
754 */
755 private Element doListMetadataFormats(Element req) {
756 //if the verb is ListMetadataFormats, there could be only one parameter: identifier
757 //, or there is no parameter; otherwise it is an error
758 //logger.info("" + XMLConverter.getString(msg));
759
760 NodeList params = GSXML.getChildrenByTagName(req, GSXML.PARAM_ELEM);
761 Element param = null;
762 Document lmf_doc = XMLConverter.newDOM();
763 if(params.getLength() == 0) {
764 //this is requesting metadata formats for the whole repository
765 //read the oaiConfig.xml file, return the metadata formats specified there.
766 if (this.listmetadataformats_response != null) {
767 // we have already created it
768 return this.listmetadataformats_response;
769 }
770
771 Element list_metadata_formats = lmf_doc.createElement(OAIXML.LIST_METADATA_FORMATS);
772 // get all the formats out of oai_config
773 NodeList formats = oai_config.getElementsByTagName(OAIXML.METADATA_FORMAT);
774 if (formats.getLength() ==0) {
775 logger.error("OAIConfig.xml must contain the supported metadata formats");
776 // TODO this is internal error, what to do???
777 return getMessage(lmf_doc, list_metadata_formats);
778 }
779
780 for(int i=0; i<formats.getLength(); i++) {
781 Element f = OAIXML.getMetadataFormatShort(lmf_doc, (Element)formats.item(i));
782 list_metadata_formats.appendChild(f);
783 }
784 this.listmetadataformats_response = getMessage(lmf_doc, list_metadata_formats);
785 return this.listmetadataformats_response;
786
787 }
788
789 if (params.getLength() > 1) {
790 //Bad argument. Can't be more than one parameters for ListMetadataFormats verb
791 return OAIXML.createErrorMessage(OAIXML.BAD_ARGUMENT, "");
792 }
793
794 // This is a request for the metadata of a particular item with an identifier
795 /**the request xml is in the form: <request>
796 * <param name=.../>
797 * </request>
798 *And there is a param element and one element only. (No paramList element in between).
799 */
800 param = (Element)params.item(0);
801 String param_name = param.getAttribute(GSXML.NAME_ATT);
802 String identifier = "";
803 if (!param_name.equals(OAIXML.IDENTIFIER)) {
804 //Bad argument
805 return OAIXML.createErrorMessage(OAIXML.BAD_ARGUMENT, "");
806 }
807
808 identifier = param.getAttribute(GSXML.VALUE_ATT);
809 // the identifier is in the form: <coll_name>:<OID>
810 // so it must contain at least one ':' characters
811 // (the oid itself may contain : chars)
812 String[] strs = identifier.split(":", 2);
813 if(strs.length != 2) {
814 logger.error("identifier is not in the form coll:id" + identifier);
815 return OAIXML.createErrorMessage(OAIXML.ID_DOES_NOT_EXIST, "");
816 }
817
818 // send request to message router
819 // get the names
820 String coll_name = strs[0];
821 String oid = strs[1];
822
823 Document msg_doc = XMLConverter.newDOM();
824 Element message = msg_doc.createElement(GSXML.MESSAGE_ELEM);
825 String verb = req.getAttribute(GSXML.TO_ATT);
826 String new_to = coll_name + "/" + verb;
827 Element request = GSXML.createBasicRequest(msg_doc, "oai???", new_to, null);
828 message.appendChild(request);
829 // add the id param
830 GSXML.addParameterToList(request, OAIXML.OID, oid);
831
832 //Now send the request to the message router to process
833 Node result_node = mr.process(message);
834 return GSXML.nodeToElement(result_node);
835 }
836
837
838
839
840 private void copyNamedElementfromConfig(Element to_elem, String element_name) {
841 Element original_element = (Element)GSXML.getChildByTagName(oai_config, element_name);
842 if(original_element != null) {
843 GSXML.copyNode(to_elem, original_element);
844 }
845 }
846
847
848 private Element doIdentify() {
849 //The validation for this verb has been done in OAIServer.validate(). So no bother here.
850 logger.info("");
851 if (this.identify_response != null) {
852 // we have already created it
853 return getMessage(this.identify_response.getOwnerDocument(), this.identify_response);
854 }
855 Document doc = XMLConverter.newDOM();
856 Element identify = doc.createElement(OAIXML.IDENTIFY);
857 //do the repository name
858 copyNamedElementfromConfig(identify, OAIXML.REPOSITORY_NAME);
859 //do the baseurl
860 copyNamedElementfromConfig(identify, OAIXML.BASE_URL);
861 //do the protocol version
862 copyNamedElementfromConfig(identify, OAIXML.PROTOCOL_VERSION);
863
864 //There can be more than one admin email according to the OAI specification
865 NodeList admin_emails = GSXML.getChildrenByTagName(oai_config, OAIXML.ADMIN_EMAIL);
866 int num_admin = 0;
867 Element from_admin_email = null;
868 if (admin_emails != null) {
869 num_admin = admin_emails.getLength();
870 }
871 for (int i=0; i<num_admin; i++) {
872 GSXML.copyNode(identify, admin_emails.item(i));
873 }
874
875 //do the earliestDatestamp
876 //send request to mr to search through the earliest datestamp amongst all oai collections in the repository.
877 //ask the message router for a list of oai collections
878 //NodeList oai_coll = getOAICollectionList();
879 long earliestDatestamp = getEarliestDateStamp(collection_list);
880 String earliestDatestamp_str = OAIXML.getTime(earliestDatestamp);
881 Element earliestDatestamp_elem = doc.createElement(OAIXML.EARLIEST_DATESTAMP);
882 GSXML.setNodeText(earliestDatestamp_elem, earliestDatestamp_str);
883 identify.appendChild(earliestDatestamp_elem);
884
885 //do the deletedRecord
886 copyNamedElementfromConfig(identify, OAIXML.DELETED_RECORD);
887 //do the granularity
888 copyNamedElementfromConfig(identify, OAIXML.GRANULARITY);
889
890 // output the oai identifier
891 Element description = doc.createElement(OAIXML.DESCRIPTION);
892 identify.appendChild(description);
893 // TODO, make this a valid id
894 Element oaiIdentifier = OAIXML.createOAIIdentifierXML(doc, repository_id, "lucene-jdbm-demo", "ec159e");
895 description.appendChild(oaiIdentifier);
896
897 // if there are any oaiInfo metadata, add them in too.
898 Element info = (Element)GSXML.getChildByTagName(oai_config, OAIXML.OAI_INFO);
899 if (info != null) {
900 NodeList meta = GSXML.getChildrenByTagName(info, OAIXML.METADATA);
901 if (meta != null && meta.getLength() > 0) {
902 Element gsdl = OAIXML.createGSDLElement(doc);
903 description.appendChild(gsdl);
904 for (int m = 0; m<meta.getLength(); m++) {
905 GSXML.copyNode(gsdl, meta.item(m));
906 }
907
908 }
909 }
910 this.identify_response = identify;
911 return getMessage(doc, identify);
912 }
913 /** split the identifier into <collection + OID> as an array
914 It has already been checked that the 'identifier' contains at least one ':'
915 */
916
917 /** validate if the specified metadata prefix value is supported by the repository
918 * by checking it in the OAIConfig.xml
919 */
920 private boolean repositorySupportsMetadataPrefix(String prefix_value) {
921 NodeList prefix_list = oai_config.getElementsByTagName(OAIXML.METADATA_PREFIX);
922
923 for(int i=0; i<prefix_list.getLength(); i++) {
924 if(prefix_value.equals(GSXML.getNodeText((Element)prefix_list.item(i)).trim() )) {
925 return true;
926 }
927 }
928 return false;
929 }
930 private Element doGetRecord(Element req){
931 logger.info("");
932 /** arguments:
933 identifier: required
934 metadataPrefix: required
935 * Exceptions: badArgument; cannotDisseminateFormat; idDoesNotExist
936 */
937 Document doc = XMLConverter.newDOM();
938 Element get_record = doc.createElement(OAIXML.GET_RECORD);
939
940 HashSet<String> valid_strs = new HashSet<String>();
941 valid_strs.add(OAIXML.IDENTIFIER);
942 valid_strs.add(OAIXML.METADATA_PREFIX);
943
944 NodeList params = GSXML.getChildrenByTagName(req, GSXML.PARAM_ELEM);
945 HashMap<String, String> param_map = GSXML.getParamMap(params);
946
947 if(!areAllParamsValid(param_map, valid_strs) ||
948 params.getLength() == 0 ||
949 param_map.containsKey(OAIXML.IDENTIFIER) == false ||
950 param_map.containsKey(OAIXML.METADATA_PREFIX) == false ) {
951 logger.error("must have the metadataPrefix/identifier parameter.");
952 return OAIXML.createErrorMessage(OAIXML.BAD_ARGUMENT, "");
953 }
954
955 String prefix = param_map.get(OAIXML.METADATA_PREFIX);
956 String identifier = param_map.get(OAIXML.IDENTIFIER);
957
958 // verify the metadata prefix
959 if (repositorySupportsMetadataPrefix(prefix) == false) {
960 logger.error("requested prefix is not found in OAIConfig.xml");
961 return OAIXML.createErrorMessage(OAIXML.CANNOT_DISSEMINATE_FORMAT, "");
962 }
963
964 // get the names
965 String[] strs = identifier.split(":", 2);
966 if(strs == null || strs.length < 2) {
967 logger.error("identifier is not in the form coll:id" + identifier);
968 return OAIXML.createErrorMessage(OAIXML.ID_DOES_NOT_EXIST, "");
969 }
970 //String name_of_site = strs[0];
971 String coll_name = strs[0];
972 String oid = strs[1];
973
974 //re-organize the request element
975 // reset the 'to' attribute
976 String verb = req.getAttribute(GSXML.TO_ATT);
977 req.setAttribute(GSXML.TO_ATT, coll_name + "/" + verb);
978 // reset the identifier element
979 Element param = GSXML.getNamedElement(req, GSXML.PARAM_ELEM, GSXML.NAME_ATT, OAIXML.IDENTIFIER);
980 if (param != null) {
981 param.setAttribute(GSXML.NAME_ATT, OAIXML.OID);
982 param.setAttribute(GSXML.VALUE_ATT, oid);
983 }
984
985 //Now send the request to the message router to process
986 Element msg = doc.createElement(GSXML.MESSAGE_ELEM);
987 msg.appendChild(doc.importNode(req, true));
988 Node result_node = mr.process(msg);
989 return GSXML.nodeToElement(result_node);
990 }
991
992 // See OAIConfig.xml
993 // dynamically works out what the earliestDateStamp is, since it varies by collection
994 // returns this time in *milliseconds*.
995 protected long getEarliestDateStamp(Element oai_coll_list) {
996 // config earliest datstamp
997 long config_datestamp = 0;
998 Element config_datestamp_elem = (Element)GSXML.getChildByTagName(this.oai_config, OAIXML.EARLIEST_DATESTAMP);
999 if (config_datestamp_elem != null) {
1000 String datest = GSXML.getNodeText(config_datestamp_elem);
1001 config_datestamp = OAIXML.getTime(datest);
1002 if (config_datestamp == -1) {
1003 config_datestamp = 0;
1004 }
1005 }
1006 //do the earliestDatestamp
1007 long current_time = System.currentTimeMillis();
1008 long earliestDatestamp = current_time;
1009 NodeList oai_coll = oai_coll_list.getElementsByTagName(GSXML.COLLECTION_ELEM);
1010 int oai_coll_size = oai_coll.getLength();
1011 if (oai_coll_size == 0) {
1012 logger.info("returned oai collection list is empty. Setting repository earliestDatestamp to be the earliest datestamp from OAIConfig.xml, or 1970-01-01 if not specified.");
1013 return config_datestamp;
1014 }
1015 // the earliestDatestamp is now stored as a metadata element in the collection's buildConfig.xml file
1016 // we get the earliestDatestamp among the collections
1017 for(int i=0; i<oai_coll_size; i++) {
1018 long coll_earliestDatestamp = Long.parseLong(((Element)oai_coll.item(i)).getAttribute(OAIXML.EARLIEST_DATESTAMP));
1019 if (coll_earliestDatestamp == 0) {
1020 // try last modified
1021 coll_earliestDatestamp = Long.parseLong(((Element)oai_coll.item(i)).getAttribute(OAIXML.LAST_MODIFIED));
1022 }
1023 if (coll_earliestDatestamp > 0) {
1024 earliestDatestamp = (earliestDatestamp > coll_earliestDatestamp)? coll_earliestDatestamp : earliestDatestamp;
1025 }
1026 }
1027 if (earliestDatestamp == current_time) {
1028 logger.info("no collection had a real datestamp, using value from OAIConfig");
1029 return config_datestamp;
1030 }
1031 return earliestDatestamp;
1032 }
1033
1034 private boolean collectionsChangedSinceTime(String set_spec_str, long initial_time) {
1035
1036 // we need to look though all collections in the set to see if any have last modified dates > initial_time
1037 Vector<String> set_coll_list = getCollectionListForSet(set_spec_str);
1038
1039 Node child = this.collection_list.getFirstChild();
1040 while (child != null) {
1041 if (child.getNodeName().equals(GSXML.COLLECTION_ELEM)) {
1042 String coll_id =((Element) child).getAttribute(GSXML.NAME_ATT);
1043 if (set_coll_list.contains(coll_id)) {
1044 long last_modified = Long.parseLong(((Element)child).getAttribute(OAIXML.LAST_MODIFIED));
1045 if (initial_time < last_modified) {
1046 return true;
1047 }
1048 }
1049 }
1050 child = child.getNextSibling();
1051 }
1052 return false;
1053
1054 }
1055
1056}
1057
1058
Note: See TracBrowser for help on using the repository browser.