source: gs3-extensions/iiif-servlet/trunk/src/gsdl-src/java/org/greenstone/gsdl3/util/IIIFXML.java@ 32883

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

Code tidy up

File size: 10.4 KB
Line 
1/*
2 * IIIFXML.java
3 * Copyright (C) 2019 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 */
19package org.greenstone.gsdl3.util;
20
21import org.greenstone.util.GlobalProperties;
22
23import org.w3c.dom.*;
24
25import java.io.*;
26import java.net.*;
27import java.util.*;
28import java.text.DateFormat;
29import java.text.SimpleDateFormat;
30
31// import file Logger.java
32import org.apache.log4j.*;
33
34/** these constants are used for the IIIF service */
35public class IIIFXML {
36
37 static Logger logger = Logger.getLogger(org.greenstone.gsdl3.util.IIIFXML.class.getName());
38
39 // the leading keyword of IIIF protocol // ****
40 public static final String VERB = "verb";
41
42 // Possible states for non-IIIF/non-verb activate/deactivate requests
43 public static final int DEACTIVATION = 0;
44 public static final int ACTIVATION = 1;
45
46 // one valid iiif verb // ****
47 public static final String GET_RECORD = "GetRecord";
48
49 // iiif request parameters
50 public static final String IDENTIFIER = "identifier";
51
52 // Error element and code att
53 public static final String ERROR = "error";
54 public static final String CODE = "code";
55
56 // IIIF error codes
57 public static final String ID_DOES_NOT_EXIST = "idDoesNotExist";
58 public static final String BAD_ARGUMENT = "badArgument";
59 public static final String BAD_VERB = "badVerb";
60
61 // General
62 public static final String IIIF_PMH = "IIIF-PMH"; // ****
63 //public static final String RESPONSE_DATE = "responseDate"; // ****
64 public static final String REQUEST = "request";
65
66 // Identify data
67 // OAI replacing => IIIF, maybe want baseURL and oai-identifier
68 public static final String BASE_URL = "baseURL";
69
70 // record response data
71 public static final String RECORD = "record";
72 public static final String HEADER = "header";
73 public static final String METADATA = "metadata";
74
75 public static final String OID = "OID"; // ****
76
77 public static final String IIIF_SERVICE_RACK = "IIIFPMH";
78
79 /*************************above are final values****************************/
80
81 public static Element iiif_config_elem = null;
82
83 public static final String iiif_version = "2.1"; // ****
84 public static String baseURL = "";
85
86 /** Converter for parsing files and creating Elements */
87 public static XMLConverter converter = new XMLConverter();
88
89 public static String[] special_char = {"/", "?", "#", "=", "&", ":", ";", " ", "%", "+"};
90 public static String[] escape_sequence = {"%2F", "%3F", "%23", "%3D", "%26", "%3A", "%3B", "%20", "%25", "%2B"};
91
92 public static String getIIIFVersion() {
93 return iiif_version;
94 }
95
96 public static String getBaseURL() {
97 return baseURL;
98 }
99
100 /** Read in IIIFConfig.xml (residing web/WEB-INF/classes/) and use it to configure the receptionist etc.
101 * the baseURL variables are also set in here.
102 * The init() method is also called in here. */
103 public static Element getIIIFConfigXML() {
104
105 File iiif_config_file = null;
106
107 try {
108 URL iiif_config_url = Class.forName("org.greenstone.gsdl3.IIIFServerBridge").getClassLoader().getResource("IIIFConfig.xml");
109 if (iiif_config_url == null) {
110 logger.error("couldn't find IIIFConfig.xml via class loader");
111 return null;
112 }
113 iiif_config_file = new File(iiif_config_url.toURI());
114 if (!iiif_config_file.exists()) {
115 logger.error(" IIIF config file: "+iiif_config_file.getPath()+" not found!");
116 return null;
117 }
118 } catch(Exception e) {
119 logger.error("couldn't find IIIFConfig.xml "+e.getMessage());
120 return null;
121 }
122
123 Document iiif_config_doc = converter.getDOM(iiif_config_file, "utf-8");
124 if (iiif_config_doc != null) {
125 iiif_config_elem = iiif_config_doc.getDocumentElement();
126 } else {
127 logger.error("Failed to parse IIIF config file IIIFConfig.xml.");
128 return null;
129 }
130
131 // initialize baseURL
132 Element base_url_elem = (Element)GSXML.getChildByTagName(iiif_config_elem, BASE_URL);
133 baseURL = GSXML.getNodeText(base_url_elem);
134
135 return iiif_config_elem;
136 }
137
138
139 /** TODO: returns a basic response (loosely based on OAI XML message)
140 *
141 */
142 public static Element createBasicResponse(Document doc, String verb, String[] pairs) { // ****
143
144 Element response = createResponseHeader(doc, verb);
145
146 //set the responseDate and request elements accordingly
147 Element request_elem = (Element)GSXML.getChildByTagName(response, REQUEST);
148 if (verb.equals("")) {
149 request_elem.setAttribute(VERB, verb);
150 }
151 int num_pairs = (pairs==null)? 0 : pairs.length;
152 for (int i=num_pairs - 1; i>=0; i--) {
153 int index = pairs[i].indexOf("=");
154 if (index != -1) {
155 String[] strs = pairs[i].split("=");
156 if(strs != null && strs.length == 2) {
157 request_elem.setAttribute(strs[0], iiifDecode(strs[1]));
158 }
159 }
160 }//end of for()
161
162 GSXML.setNodeText(request_elem, baseURL);
163
164 return response;
165 }
166
167 /** @param error_code the value of the code attribute
168 * @param error_text the node text of the error element
169 * @return an iiif error <message><response><error>
170 */
171 public static Element createErrorMessage(String error_code, String error_text) {
172 Document doc = converter.newDOM();
173 Element message = doc.createElement(GSXML.MESSAGE_ELEM);
174 Element resp = doc.createElement(GSXML.RESPONSE_ELEM);
175 message.appendChild(resp);
176 Element error = createErrorElement(doc, error_code, error_text);
177 resp.appendChild(error);
178 return message;
179 }
180
181 /** @param error_code the value of the code attribute
182 * @param error_text the node text of the error element
183 * @return an iiif error <response><error>
184 */
185 public static Element createErrorResponse(String error_code, String error_text) {
186 Document doc = converter.newDOM();
187 Element resp = doc.createElement(GSXML.RESPONSE_ELEM);
188 Element error = createErrorElement(doc, error_code, error_text);
189 resp.appendChild(error);
190 return resp;
191 }
192
193 /** @param error_code the value of the code attribute
194 * @param error_text the node text of the error element
195 * @return an iiif error <error>
196 */
197 public static Element createErrorElement(Document doc, String error_code, String error_text) {
198 Element error = doc.createElement(ERROR);
199 error.setAttribute(CODE, error_code);
200 GSXML.setNodeText(error, error_text);
201 return error;
202 }
203
204 // This is the response message sent when there's a request to activate/deactivate a non-IIIF collection
205 // A request to activate a non-existent/non-IIIF collection is not invalid, it's just that we won't process it.
206 // So we still return status code OK (OK status code is needed for servercontrol.pm of activate.pl to recognise
207 // that the command had been "successful" when it runs de/activate).
208 public static Element createDeActivationOfNonIIIFCollResponse(int activationState, String collname) {
209 Document doc = converter.newDOM();
210 Element response = doc.createElement(GSXML.RESPONSE_ELEM);
211 response.setAttribute("status", "OK");
212 String message = "collection: " + collname + " is not enabled for IIIF.";
213 if(activationState == ACTIVATION) {
214 message += " Not attempting to activate it.";
215 } else {
216 message += " Not attempting to deactivate it.";
217 }
218 GSXML.setNodeText(response, message);
219 return response;
220 }
221
222 // The response message sent when a request comes in to activate/deactivate a proper IIIF collection.
223 public static Element createActivationStateResponse(boolean success, int activationState, String collname) {
224 Document doc = converter.newDOM();
225 Element response = doc.createElement(GSXML.RESPONSE_ELEM);
226 if (success) {
227 response.setAttribute("status", "OK");
228 if(activationState == ACTIVATION) {
229 GSXML.setNodeText(response, "collection: " + collname + " activated");
230 } else {
231 GSXML.setNodeText(response, "collection: " + collname + " deactivated");
232 }
233 } else {
234 response.setAttribute("status", "FAIL");
235 if(activationState == ACTIVATION) {
236 GSXML.setNodeText(response, "Failed to activate collection " + collname);
237 } else {
238 GSXML.setNodeText(response, "Failed to deactivate collection " + collname);
239 }
240 }
241 return response;
242 }
243
244 public static Element createResetResponse(boolean success) {
245 Document doc = converter.newDOM();
246 Element response = doc.createElement(GSXML.RESPONSE_ELEM);
247 if (success) {
248 response.setAttribute("status", "OK");
249 GSXML.setNodeText(response, "Reset IIIFServerBridge successfully");
250 } else {
251 response.setAttribute("status", "FAIL");
252 GSXML.setNodeText(response, "Failed to reset IIIFServerBridge");
253 }
254 return response;
255 }
256 /** convert the escaped sequences (eg, '%3A') of those special characters back to their
257 * original form (eg, ':').
258 */
259 public static String iiifDecode(String escaped_str) {
260 logger.info("iiifDecode() " +escaped_str);
261 for (int i=0; i<special_char.length; i++) {
262 if (escaped_str.indexOf(escape_sequence[i]) != -1) {
263 escaped_str = escaped_str.replaceAll(escape_sequence[i], special_char[i]);
264 }
265 }
266 return escaped_str;
267 }
268 /** convert those special characters (eg, ':') to their
269 * escaped sequences (eg, '%3A').
270 */
271 /*
272 public static String iiifEncode(String original_str) { // ****
273 logger.info("iiifEncode() " + original_str);
274 for (int i=0; i<special_char.length; i++) {
275 if (original_str.indexOf(special_char[i]) != -1) {
276 original_str = original_str.replaceAll(special_char[i], escape_sequence[i]);
277 }
278 }
279 return original_str;
280 }
281 */
282
283 public static Element createResponseHeader(Document response_doc, String verb) {
284 String tag_name = IIIF_PMH;
285 Element iiif = response_doc.createElement(tag_name);
286 Element req = response_doc.createElement(REQUEST);
287 iiif.appendChild(req);
288
289 return iiif;
290 }
291}
292
293
294
295
296
297
Note: See TracBrowser for help on using the repository browser.