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 |
|
---|
48 | /** the configure file of this receptionist passed from the iiif restlet. */
|
---|
49 | protected Element iiif_config = null;
|
---|
50 |
|
---|
51 | /** the message router that the Receptionist and Actions will talk to */
|
---|
52 | protected ModuleInterface mr = null;
|
---|
53 |
|
---|
54 |
|
---|
55 | public IIIFReceptionist() {
|
---|
56 |
|
---|
57 | }
|
---|
58 |
|
---|
59 | public void cleanUp() {
|
---|
60 | if (this.mr != null) {
|
---|
61 |
|
---|
62 | this.mr.cleanUp();
|
---|
63 | }
|
---|
64 | }
|
---|
65 |
|
---|
66 | public void setSiteName(String site_name) {
|
---|
67 | this.site_name = site_name;
|
---|
68 | }
|
---|
69 |
|
---|
70 | /** sets the message router - it should already be created and
|
---|
71 | * configured in the init() of a servlet/restlet (IIIFServerBridge, for example) before being passed to the receptionist*/
|
---|
72 | public void setMessageRouter(ModuleInterface mr) {
|
---|
73 | this.mr = mr;
|
---|
74 | }
|
---|
75 |
|
---|
76 | /** configures the receptionist */
|
---|
77 | public boolean configure(Element config) {
|
---|
78 |
|
---|
79 | if (this.mr==null) {
|
---|
80 | logger.error(" message routers must be set before calling IIIF configure");
|
---|
81 | return false;
|
---|
82 | }
|
---|
83 | if (config == null) {
|
---|
84 | logger.error(" IIIF configure file is null");
|
---|
85 | return false;
|
---|
86 | }
|
---|
87 | iiif_config = config;
|
---|
88 |
|
---|
89 | return true;
|
---|
90 | }
|
---|
91 |
|
---|
92 |
|
---|
93 | protected void resetMessageRouter() {
|
---|
94 | // we just need to send a configure request to MR
|
---|
95 | Document doc = XMLConverter.newDOM();
|
---|
96 | Element mr_request_message = doc.createElement(GSXML.MESSAGE_ELEM);
|
---|
97 | Element mr_request = GSXML.createBasicRequest(doc, GSXML.REQUEST_TYPE_SYSTEM, "", null);
|
---|
98 | mr_request_message.appendChild(mr_request);
|
---|
99 |
|
---|
100 | Element system = doc.createElement(GSXML.SYSTEM_ELEM);
|
---|
101 | mr_request.appendChild(system);
|
---|
102 | system.setAttribute(GSXML.TYPE_ATT, GSXML.SYSTEM_TYPE_CONFIGURE);
|
---|
103 |
|
---|
104 | Element response = (Element) this.mr.process(mr_request_message);
|
---|
105 | logger.info("*** configure response = "+XMLConverter.getPrettyString(response));
|
---|
106 | }
|
---|
107 |
|
---|
108 | /** process using strings - just calls process using Elements */
|
---|
109 | public String process(String xml_in) {
|
---|
110 |
|
---|
111 | Node message_node = XMLConverter.getDOM(xml_in);
|
---|
112 | Node page = process(message_node);
|
---|
113 | return XMLConverter.getString(page);
|
---|
114 | }
|
---|
115 |
|
---|
116 | //Compose a message/response element used to send back to the IIIFServerBridge servlet.
|
---|
117 | //This method is only used within IIIFReceptionist
|
---|
118 | private Element getMessage(Document doc, Element e) {
|
---|
119 | Element msg = doc.createElement(GSXML.MESSAGE_ELEM);
|
---|
120 | Element response = doc.createElement(GSXML.RESPONSE_ELEM);
|
---|
121 | msg.appendChild(response);
|
---|
122 | response.appendChild(e);
|
---|
123 | return msg;
|
---|
124 | }
|
---|
125 |
|
---|
126 | /** process - produce xml data in response to a request
|
---|
127 | * if something goes wrong, it returns null -
|
---|
128 | */
|
---|
129 | public Node process(Node message_node) {
|
---|
130 | logger.info("*** IIIFReceptionist received request");
|
---|
131 |
|
---|
132 | Element message = GSXML.nodeToElement(message_node);
|
---|
133 | logger.info("*** " + XMLConverter.getString(message));
|
---|
134 |
|
---|
135 | // check that its a correct message tag
|
---|
136 | if (!message.getTagName().equals(GSXML.MESSAGE_ELEM)) {
|
---|
137 | logger.error(" Invalid message. GSDL message should start with <"+GSXML.MESSAGE_ELEM+">, instead it starts with:"+message.getTagName()+".");
|
---|
138 | return IIIFXML.createErrorMessage(IIIFXML.BAD_ARGUMENT, "Internal messaging error");
|
---|
139 | }
|
---|
140 |
|
---|
141 | // get the request out of the message - assume that there is only one
|
---|
142 | Element request = (Element)GSXML.getChildByTagName(message, GSXML.REQUEST_ELEM);
|
---|
143 | if (request == null) {
|
---|
144 | logger.error(" message had no request!");
|
---|
145 | return IIIFXML.createErrorMessage(IIIFXML.BAD_ARGUMENT, "Internal messaging error");
|
---|
146 | }
|
---|
147 |
|
---|
148 | // special case, reset=true for reloading the MR and recept data
|
---|
149 | String reset = request.getAttribute("reset");
|
---|
150 | if (!reset.equals("")) {
|
---|
151 | resetMessageRouter();
|
---|
152 | return IIIFXML.createResetResponse(true);
|
---|
153 | }
|
---|
154 |
|
---|
155 |
|
---|
156 | //At this stage, the value of 'to' attribute of the request must be the 'verb'
|
---|
157 | //The only thing that the iiif receptionist can be sure is that these verbs are valid, nothing else.
|
---|
158 | String verb = request.getAttribute(GSXML.TO_ATT);
|
---|
159 |
|
---|
160 | if (verb.equals(IIIFXML.GET_RECORD)) {
|
---|
161 | return doGetRecord(request);
|
---|
162 | }
|
---|
163 |
|
---|
164 | // should never get here as verbs were checked in IIIFServerBridge
|
---|
165 | return IIIFXML.createErrorMessage(IIIFXML.BAD_VERB, "Unexpected things happened");
|
---|
166 |
|
---|
167 | }
|
---|
168 |
|
---|
169 | private Element doGetRecord(Element req){
|
---|
170 | logger.info("");
|
---|
171 |
|
---|
172 | Document doc = XMLConverter.newDOM();
|
---|
173 | Element get_record = doc.createElement(IIIFXML.GET_RECORD);
|
---|
174 |
|
---|
175 | //HashSet<String> valid_strs = new HashSet<String>(); // ****
|
---|
176 | //valid_strs.add(IIIFXML.IDENTIFIER);
|
---|
177 | //valid_strs.add(IIIFXML.METADATA_PREFIX);
|
---|
178 |
|
---|
179 | NodeList params = GSXML.getChildrenByTagName(req, GSXML.PARAM_ELEM);
|
---|
180 | HashMap<String, String> param_map = GSXML.getParamMap(params);
|
---|
181 |
|
---|
182 | // Any need to check all params are valid, like OAI?? // ****
|
---|
183 |
|
---|
184 | String identifier = param_map.get(IIIFXML.IDENTIFIER);
|
---|
185 |
|
---|
186 | // get the names
|
---|
187 | String[] strs = identifier.split(":", 2);
|
---|
188 | if(strs == null || strs.length < 2) {
|
---|
189 | logger.error("identifier is not in the form coll:id" + identifier);
|
---|
190 | return IIIFXML.createErrorMessage(IIIFXML.ID_DOES_NOT_EXIST, "");
|
---|
191 | }
|
---|
192 | String coll_name = strs[0];
|
---|
193 | String oid = strs[1];
|
---|
194 |
|
---|
195 | //re-organize the request element
|
---|
196 | // reset the 'to' attribute
|
---|
197 | String verb = req.getAttribute(GSXML.TO_ATT);
|
---|
198 | req.setAttribute(GSXML.TO_ATT, coll_name + "/" + verb);
|
---|
199 | // reset the identifier element
|
---|
200 | Element param = GSXML.getNamedElement(req, GSXML.PARAM_ELEM, GSXML.NAME_ATT, IIIFXML.IDENTIFIER);
|
---|
201 | if (param != null) {
|
---|
202 | param.setAttribute(GSXML.NAME_ATT, IIIFXML.OID);
|
---|
203 | param.setAttribute(GSXML.VALUE_ATT, oid);
|
---|
204 | }
|
---|
205 |
|
---|
206 | //Now send the request to the message router to process
|
---|
207 | Element msg = doc.createElement(GSXML.MESSAGE_ELEM);
|
---|
208 | msg.appendChild(doc.importNode(req, true));
|
---|
209 | Node result_node = mr.process(msg);
|
---|
210 | return GSXML.nodeToElement(result_node);
|
---|
211 | }
|
---|
212 | }
|
---|
213 |
|
---|
214 |
|
---|