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