source: gs3-extensions/iiif-servlet/trunk/src/gsdl-src/java/org/greenstone/gsdl3/core/IIIFReceptionist.java@ 32707

Last change on this file since 32707 was 32707, checked in by davidb, 5 years ago

Files needed for GS3-Cantaloupe bridge result from second round of testing

File size: 16.1 KB
Line 
1/*
2 * IIIFReceptionist.java
3 * Copyright (C) 2018 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 IIIF image server support.
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 IIIFReceptionist implements ModuleInterface {
42
43 static Logger logger = Logger.getLogger(org.greenstone.gsdl3.core.IIIFReceptionist.class.getName());
44
45 /** Instead of a config_params object, only a site_name is needed by iiif 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 iiif restlet. */
51 protected Element oai_config = null;
52
53 /** the message router that the Receptionist and Actions will talk to */
54 protected ModuleInterface mr = null;
55
56 // Some of the data/responses will not change while the servlet is running, so
57 // we can cache them
58
59 /** A list of all the collections available to this IIIF image server */
60 protected Element collection_list = null;
61 /** a vector of the names, for convenience */
62 protected Vector<String> collection_name_list = null;
63 /** If this is true, then there are no OAI enabled collections, so can always return
64 noRecordsMatch (after validating the request params) */
65 protected boolean noRecordsMatch = false;
66
67 /** A set of all known 'sets' */
68 protected HashSet<String> set_set = null;
69
70 protected boolean has_super_colls = false;
71 /** a hash of super set-> collection list */
72 protected HashMap<String, Vector<String>> super_coll_map = null;
73 /** store the super coll elements for convenience */
74 HashMap<String, Element> super_coll_data = null;
75 /** store the metadata formats ??????*/
76 /** The identify response */
77 protected Element identify_response = null;
78 /** The list set response */
79 protected Element listsets_response = null;
80 /** the list metadata formats response */
81 protected Element listmetadataformats_response = null;
82
83 public IIIFReceptionist() {
84
85 }
86
87 public void cleanUp() {
88 if (this.mr != null) {
89
90 this.mr.cleanUp();
91 }
92 }
93
94 public void setSiteName(String site_name) {
95 this.site_name = site_name;
96 }
97 /** sets the message router - it should already be created and
98 * configured in the init() of a servlet/restlet (OAIServer, for example) before being passed to the receptionist*/
99 public void setMessageRouter(ModuleInterface mr) {
100 this.mr = mr;
101 }
102
103 /** configures the receptionist */
104 public boolean configure(Element config) {
105
106 if (this.mr==null) {
107 logger.error(" message routers must be set before calling IIIF configure");
108 return false;
109 }
110 if (config == null) {
111 logger.error(" IIIF configure file is null");
112 return false;
113 }
114 oai_config = config;
115
116 repository_id = getRepositoryIdentifier();
117
118 if (!configureSetInfo()) {
119 // there are no sets
120 logger.error("No sets (collections) available for IIIF");
121 return false;
122 }
123
124 return true;
125 }
126
127 private boolean configureSetInfo() {
128 this.set_set = new HashSet<String>();
129
130 // First, we get a list of all the OAI enabled collections
131 // We get this by sending a listSets request to the MR
132 Document doc = XMLConverter.newDOM();
133 Element message = doc.createElement(GSXML.MESSAGE_ELEM);
134
135 Element request = GSXML.createBasicRequest(doc, OAIXML.OAI_SET_LIST, "", null);
136 message.appendChild(request);
137 Node msg_node = mr.process(message);
138
139 if (msg_node == null) {
140 logger.error("returned msg_node from mr is null");
141 return false;
142 }
143 Element resp = (Element)GSXML.getChildByTagName(msg_node, GSXML.RESPONSE_ELEM);
144 Element coll_list = (Element)GSXML.getChildByTagName(resp, GSXML.COLLECTION_ELEM + GSXML.LIST_MODIFIER);
145 if (coll_list == null) {
146 logger.error("coll_list is null");
147 return false;
148 }
149
150 this.collection_list = (Element)doc.importNode(coll_list, true);
151
152 // go through and store a list of collection names for convenience
153 // also create a 'to' attribute for the next request to the MR, which
154 // is a ListSets request to each collection
155 Node child = this.collection_list.getFirstChild();
156 if (child == null) {
157 logger.error("collection list has no children");
158 noRecordsMatch = true;
159 return false;
160 }
161
162 this.collection_name_list = new Vector<String>();
163 StringBuffer to = new StringBuffer();
164 boolean first = true;
165 while (child != null) {
166 if (child.getNodeName().equals(GSXML.COLLECTION_ELEM)) {
167 String coll_id =((Element) child).getAttribute(GSXML.NAME_ATT);
168 this.collection_name_list.add(coll_id);
169 if (!first) {
170 to.append(',');
171 }
172 first = false;
173 to.append(coll_id+"/"+OAIXML.LIST_SETS);
174 }
175 child = child.getNextSibling();
176 }
177 if (first) {
178 // we haven't found any collections
179 logger.error("found no collection elements in collectionList");
180 noRecordsMatch = true;
181 return false;
182 }
183 Document listsets_doc = XMLConverter.newDOM();
184 Element listsets_element = listsets_doc.createElement(OAIXML.LIST_SETS);
185 this.listsets_response = getMessage(listsets_doc, listsets_element);
186
187 // Now, for each collection, get a list of all its sets
188 // might include subsets (classifiers) or super colls
189 // We'll reuse the first message, changing its type and to atts
190 request.setAttribute(GSXML.TYPE_ATT, "");
191 request.setAttribute(GSXML.TO_ATT, to.toString());
192 // send to MR
193 msg_node = mr.process(message);
194 //logger.info("*** " + XMLConverter.getPrettyString(msg_node));
195 NodeList response_list = ((Element)msg_node).getElementsByTagName(GSXML.RESPONSE_ELEM);
196 for (int c=0; c<response_list.getLength(); c++) {
197 // for each collection's response
198 Element response = (Element)response_list.item(c);
199 String coll_name = GSPath.getFirstLink(response.getAttribute(GSXML.FROM_ATT));
200 logger.info("*** coll from response "+coll_name);
201 NodeList set_list = response.getElementsByTagName(OAIXML.SET);
202 for (int j=0; j<set_list.getLength(); j++) {
203 // now check if it a super collection
204 Element set = (Element)set_list.item(j);
205 String set_spec = GSXML.getNodeText((Element)GSXML.getChildByTagName(set, OAIXML.SET_SPEC));
206 logger.info("*** set spec = "+set_spec);
207 // this may change if we add site name back in
208 // setSpecs will be collname or collname:subset or supercollname
209 if (set_spec.indexOf(":")==-1 && ! set_spec.equals(coll_name)) {
210 // it must be a super coll spec
211 logger.info("*** found super coll, "+set_spec);
212 // check that it is a valid one from config
213 if (this.has_super_colls == true && this.super_coll_data.containsKey(set_spec)) {
214 Vector <String> subcolls = this.super_coll_map.get(set_spec);
215 if (subcolls == null) {
216 logger.info("*** its new!!");
217 // not in there yet
218 subcolls = new Vector<String>();
219 this.set_set.add(set_spec);
220 this.super_coll_map.put(set_spec, subcolls);
221 // the first time a supercoll is mentioned, add into the set list
222 logger.info("*** finding the set info "+XMLConverter.getPrettyString(this.super_coll_data.get(set_spec)));
223 listsets_element.appendChild(GSXML.duplicateWithNewName(listsets_doc, this.super_coll_data.get(set_spec), OAIXML.SET, true));
224 }
225 // add this collection to the list for the super coll
226 subcolls.add(coll_name);
227 }
228 } else { // its either the coll itself or a subcoll
229 // add in the set
230 listsets_element.appendChild(listsets_doc.importNode(set, true));
231 this.set_set.add(set_spec);
232 }
233 } // for each set in the collection
234 } // for each OAI enabled collection
235 return true;
236 }
237
238 protected void resetMessageRouter() {
239 // we just need to send a configure request to MR
240 Document doc = XMLConverter.newDOM();
241 Element mr_request_message = doc.createElement(GSXML.MESSAGE_ELEM);
242 Element mr_request = GSXML.createBasicRequest(doc, GSXML.REQUEST_TYPE_SYSTEM, "", null);
243 mr_request_message.appendChild(mr_request);
244
245 Element system = doc.createElement(GSXML.SYSTEM_ELEM);
246 mr_request.appendChild(system);
247 system.setAttribute(GSXML.TYPE_ATT, GSXML.SYSTEM_TYPE_CONFIGURE);
248
249 Element response = (Element) this.mr.process(mr_request_message);
250 logger.info("*** configure response = "+XMLConverter.getPrettyString(response));
251 }
252
253 /** process using strings - just calls process using Elements */
254 public String process(String xml_in) {
255
256 Node message_node = XMLConverter.getDOM(xml_in);
257 Node page = process(message_node);
258 return XMLConverter.getString(page);
259 }
260
261 //Compose a message/response element used to send back to the OAIServer servlet.
262 //This method is only used within OAIReceptionist
263 private Element getMessage(Document doc, Element e) {
264 Element msg = doc.createElement(GSXML.MESSAGE_ELEM);
265 Element response = doc.createElement(GSXML.RESPONSE_ELEM);
266 msg.appendChild(response);
267 response.appendChild(e);
268 return msg;
269 }
270
271 /** process - produce xml data in response to a request
272 * if something goes wrong, it returns null -
273 */
274 public Node process(Node message_node) {
275 logger.info("*** IIIFReceptionist received request");
276
277 Element message = GSXML.nodeToElement(message_node);
278 logger.info("*** " + XMLConverter.getString(message));
279
280 // check that its a correct message tag
281 if (!message.getTagName().equals(GSXML.MESSAGE_ELEM)) {
282 logger.error(" Invalid message. GSDL message should start with <"+GSXML.MESSAGE_ELEM+">, instead it starts with:"+message.getTagName()+".");
283 return OAIXML.createErrorMessage(OAIXML.BAD_ARGUMENT, "Internal messaging error");
284 }
285
286 // get the request out of the message - assume that there is only one
287 Element request = (Element)GSXML.getChildByTagName(message, GSXML.REQUEST_ELEM);
288 if (request == null) {
289 logger.error(" message had no request!");
290 return OAIXML.createErrorMessage(OAIXML.BAD_ARGUMENT, "Internal messaging error");
291 }
292
293 // special case, reset=true for reloading the MR and recept data
294 String reset = request.getAttribute("reset");
295 if (!reset.equals("")) {
296 resetMessageRouter();
297 configureSetInfo();
298 return OAIXML.createResetResponse(true);
299 }
300
301
302 //At this stage, the value of 'to' attribute of the request must be the 'verb'
303 //The only thing that the oai receptionist can be sure is that these verbs are valid, nothing else.
304 String verb = request.getAttribute(GSXML.TO_ATT);
305 if (verb.equals(OAIXML.IDENTIFY)) {
306 return doIdentify();
307 }
308 if (verb.equals(OAIXML.GET_RECORD)) {
309 return doGetRecord(request);
310 }
311
312 // should never get here as verbs were checked in OAIServer
313 return OAIXML.createErrorMessage(OAIXML.BAD_VERB, "Unexpected things happened");
314
315 }
316
317 private String getRepositoryIdentifier() {
318 Element ri = (Element)GSXML.getChildByTagName(oai_config, OAIXML.REPOSITORY_IDENTIFIER);
319 if (ri != null) {
320 return GSXML.getNodeText(ri);
321 }
322 return "";
323 }
324
325
326 private void copyNamedElementfromConfig(Element to_elem, String element_name) {
327 Element original_element = (Element)GSXML.getChildByTagName(oai_config, element_name);
328 if(original_element != null) {
329 GSXML.copyNode(to_elem, original_element);
330 }
331 }
332
333 private Element doIdentify() {
334 //The validation for this verb has been done in OAIServer.validate(). So no bother here.
335 logger.info("");
336 if (this.identify_response != null) {
337 // we have already created it
338 return getMessage(this.identify_response.getOwnerDocument(), this.identify_response);
339 }
340 Document doc = XMLConverter.newDOM();
341 Element identify = doc.createElement(OAIXML.IDENTIFY);
342 //do the repository name
343 copyNamedElementfromConfig(identify, OAIXML.REPOSITORY_NAME);
344 //do the baseurl
345 copyNamedElementfromConfig(identify, OAIXML.BASE_URL);
346 //do the protocol version
347 copyNamedElementfromConfig(identify, OAIXML.PROTOCOL_VERSION);
348
349 //There can be more than one admin email according to the OAI specification
350 NodeList admin_emails = GSXML.getChildrenByTagName(oai_config, OAIXML.ADMIN_EMAIL);
351 int num_admin = 0;
352 Element from_admin_email = null;
353 if (admin_emails != null) {
354 num_admin = admin_emails.getLength();
355 }
356 for (int i=0; i<num_admin; i++) {
357 GSXML.copyNode(identify, admin_emails.item(i));
358 }
359
360 /* IIIF does not have the equivalent of oai earliestDatestamp */
361
362 // output the oai identifier
363 Element description = doc.createElement(OAIXML.DESCRIPTION);
364 identify.appendChild(description);
365 // TODO, make this a valid id
366 Element oaiIdentifier = OAIXML.createOAIIdentifierXML(doc, repository_id, "lucene-jdbm-demo", "ec159e");
367 description.appendChild(oaiIdentifier);
368
369 // if there are any oaiInfo metadata, add them in too.
370 Element info = (Element)GSXML.getChildByTagName(oai_config, OAIXML.OAI_INFO);
371 if (info != null) {
372 NodeList meta = GSXML.getChildrenByTagName(info, OAIXML.METADATA);
373 if (meta != null && meta.getLength() > 0) {
374 Element gsdl = OAIXML.createGSDLElement(doc);
375 description.appendChild(gsdl);
376 for (int m = 0; m<meta.getLength(); m++) {
377 GSXML.copyNode(gsdl, meta.item(m));
378 }
379
380 }
381 }
382 this.identify_response = identify;
383 return getMessage(doc, identify);
384 }
385
386 private Element doGetRecord(Element req){
387 logger.info("");
388 /** arguments:
389 identifier: required
390 metadataPrefix: required
391 * Exceptions: badArgument; cannotDisseminateFormat; idDoesNotExist
392 */
393 Document doc = XMLConverter.newDOM();
394 Element get_record = doc.createElement(OAIXML.GET_RECORD);
395
396 HashSet<String> valid_strs = new HashSet<String>();
397 valid_strs.add(OAIXML.IDENTIFIER);
398 valid_strs.add(OAIXML.METADATA_PREFIX);
399
400 NodeList params = GSXML.getChildrenByTagName(req, GSXML.PARAM_ELEM);
401 HashMap<String, String> param_map = GSXML.getParamMap(params);
402
403 // Any need to check all params are valid, like OAI??
404
405 String identifier = param_map.get(OAIXML.IDENTIFIER);
406
407 // get the names
408 String[] strs = identifier.split(":", 2);
409 if(strs == null || strs.length < 2) {
410 logger.error("identifier is not in the form coll:id" + identifier);
411 return OAIXML.createErrorMessage(OAIXML.ID_DOES_NOT_EXIST, "");
412 }
413 //String name_of_site = strs[0];
414 String coll_name = strs[0];
415 String oid = strs[1];
416
417 //re-organize the request element
418 // reset the 'to' attribute
419 String verb = req.getAttribute(GSXML.TO_ATT);
420 req.setAttribute(GSXML.TO_ATT, coll_name + "/" + verb);
421 // reset the identifier element
422 Element param = GSXML.getNamedElement(req, GSXML.PARAM_ELEM, GSXML.NAME_ATT, OAIXML.IDENTIFIER);
423 if (param != null) {
424 param.setAttribute(GSXML.NAME_ATT, OAIXML.OID);
425 param.setAttribute(GSXML.VALUE_ATT, oid);
426 }
427
428 //Now send the request to the message router to process
429 Element msg = doc.createElement(GSXML.MESSAGE_ELEM);
430 msg.appendChild(doc.importNode(req, true));
431 Node result_node = mr.process(msg);
432 return GSXML.nodeToElement(result_node);
433 }
434
435
436}
437
438
Note: See TracBrowser for help on using the repository browser.