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

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

Code tidy up

File size: 8.3 KB
Line 
1/*
2 * IIIFPMH.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.service;
20
21// Greenstone classes
22import org.greenstone.gsdl3.core.GSException;
23import org.greenstone.gsdl3.util.GSXML;
24import org.greenstone.gsdl3.util.IIIFXML;
25import org.greenstone.gsdl3.util.OID;
26import org.greenstone.gsdl3.util.GSFile;
27import org.greenstone.gsdl3.util.XMLConverter;
28
29import org.greenstone.gsdl3.util.SimpleCollectionDatabase;
30import org.greenstone.gsdl3.util.DBInfo;
31// XML classes
32import org.w3c.dom.Document;
33import org.w3c.dom.Element;
34import org.w3c.dom.NodeList;
35
36// General Java classes
37import java.io.File;
38import java.util.StringTokenizer;
39import java.util.Vector;
40import java.util.Set;
41import java.util.Iterator;
42import java.util.ArrayList;
43import java.util.Date;
44import java.util.HashMap;
45import java.util.HashSet;
46import java.util.Map.Entry;
47
48import org.apache.log4j.Logger;
49
50/** Implements the IIIF service for GS3 collections.
51 * Digs into each collection's database to study the metadata
52 * and from that determine what file in a document's assocdir
53 * can be used to provide a source image for the doc
54 *
55 */
56
57public class IIIFPMH extends ServiceRack {
58
59 static Logger logger = Logger.getLogger(org.greenstone.gsdl3.service.IIIFPMH.class.getName());
60
61 protected SimpleCollectionDatabase coll_db = null;
62
63 protected String site_name = "";
64 protected String coll_name = "";
65
66 // set this up during configure
67 protected Element list_sets_response = null;
68
69 protected String index_stem = ""; // ****
70 protected String infodb_type = "";
71
72 /** constructor */
73 public IIIFPMH() {
74 }
75
76 public void cleanUp() {
77 super.cleanUp();//??
78
79 if(this.coll_db != null) {
80 this.coll_db.closeDatabase();
81 this.coll_db = null;
82 }
83 }
84
85 /** configure this service
86 info is the IIIFPMH service rack from collectionConfig.xml, and
87 extra_info is buildConfig.xml */
88 public boolean configure(Element info, Element extra_info) {
89 if (!super.configure(info, extra_info)){
90 logger.info("Configuring IIIFPMH(extends ServiceRack) returns false.");
91 return false;
92 }
93
94 //get the names from ServiceRack.java
95 this.site_name = this.router.getSiteName();
96 this.coll_name = this.cluster_name;
97
98 logger.info("Configuring IIIFPMH...");
99
100 this.config_info = info;
101
102 // the index stem is either specified in the buildConfig.xml file (extra_info) or uses the collection name
103 Element metadata_list = (Element) GSXML.getChildByTagName(extra_info, GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER);
104
105 // Is indexStem needed for IIIF // ****
106 if (metadata_list != null) {
107
108 Element index_stem_elem = (Element) GSXML.getNamedElement(metadata_list, GSXML.METADATA_ELEM, GSXML.NAME_ATT, "indexStem");
109
110 if (index_stem_elem != null) {
111 this.index_stem = GSXML.getNodeText(index_stem_elem);
112 }
113
114 Element infodb_type_elem = (Element) GSXML.getNamedElement(metadata_list, GSXML.METADATA_ELEM, GSXML.NAME_ATT, "infodbType");
115 if (infodb_type_elem != null) {
116 this.infodb_type = GSXML.getNodeText(infodb_type_elem);
117 }
118
119 }
120
121 if (index_stem == null || index_stem.equals("")) {
122 this.index_stem = this.cluster_name; // index_stem is the name of the db in indext/text, it is <colname>.<db>
123 }
124 if (infodb_type == null || infodb_type.equals("")) {
125 this.infodb_type = "gdbm"; // the default
126 }
127
128 Element get_record = this.desc_doc.createElement(GSXML.SERVICE_ELEM);
129 get_record.setAttribute(GSXML.NAME_ATT, IIIFXML.GET_RECORD);
130 get_record.setAttribute(GSXML.TYPE_ATT, "iiif");
131 this.short_service_info.appendChild(get_record);
132
133 return true;
134 }
135
136 public boolean configureIIIF(Element iiif_config_elem) {
137
138 // Open the coll db db databases and store handles to them
139 coll_db = new SimpleCollectionDatabase(infodb_type);
140 if (!coll_db.databaseOK()) {
141 logger.error("Couldn't create the collection database of type "+infodb_type);
142 return false;
143 }
144
145 // Open databases for querying
146 String coll_db_file = GSFile.collectionDatabaseFile(this.site_home, this.cluster_name, index_stem, infodb_type);
147 if (!this.coll_db.openDatabase(coll_db_file, SimpleCollectionDatabase.READ)) {
148 logger.error("Could not open collection database!");
149 return false;
150 }
151
152 return true;
153 }
154
155 /** returns a specific service description */
156 public Element getServiceDescription(Document doc, String service_id, String lang, String subset) {
157
158 if (service_id.equals(IIIFXML.GET_RECORD)) {
159 Element get_record = doc.createElement(GSXML.SERVICE_ELEM);
160 get_record.setAttribute(GSXML.NAME_ATT, IIIFXML.GET_RECORD);
161 get_record.setAttribute(GSXML.TYPE_ATT, "iiif");
162 return get_record;
163 }
164
165 return null;
166 }
167
168 /** returns the actual record element used in the IIIF GetRecord response */
169 protected Element processGetRecord(Element req) {
170 /** arguments:
171 identifier: required
172 * Exceptions: badArgument; cannotDisseminateFormat; idDoesNotExist
173 */
174 NodeList params = GSXML.getChildrenByTagName(req, GSXML.PARAM_ELEM);
175 HashMap<String, String> param_map = GSXML.getParamMap(params);
176
177 Document doc = XMLConverter.newDOM();
178
179 String oid = param_map.get(IIIFXML.OID); // TODO should this be identifier???
180
181 // Get a DBInfo object of the identifier; if this identifier is not present in the database,
182 // null is returned.
183 DBInfo info = this.coll_db.getInfo(oid);
184 if (info == null) {
185 logger.error("OID: " + oid + " is not present in the collection index database.");
186 return IIIFXML.createErrorResponse(IIIFXML.ID_DOES_NOT_EXIST, "");
187 }
188
189
190 // ****
191 Element get_record_response = doc.createElement(GSXML.RESPONSE_ELEM);
192 Element get_record = doc.createElement(IIIFXML.GET_RECORD);
193 get_record_response.appendChild(get_record);
194 Element record = doc.createElement(IIIFXML.RECORD);
195 //compose the header element
196 record.appendChild(createHeaderElement(doc, oid));
197 //compose the metadata element
198 record.appendChild(createMetadataElement(doc, info));
199 get_record.appendChild(record);
200 return get_record_response;
201 }
202
203
204 /** create the metadata element used when processing GetRecord request
205 */
206
207 protected Element createMetadataElement(Document doc, DBInfo info) {
208 // the <metadata> element
209 Element metadata_message = doc.createElement(IIIFXML.METADATA);
210
211 addFirstMetadata(metadata_message, "assocfilepath", info);
212 addFirstMetadata(metadata_message, "Image", info);
213
214 return metadata_message;
215 }
216
217 /** a simple addMetadata where we look for meta_name metadata, and add as that name*/
218
219 protected void addFirstMetadata(Element meta_list_elem, String meta_name, DBInfo info) {
220 Vector<String> values = info.getMultiInfo(meta_name);
221 if (values != null && values.size()>0) {
222 addMetadataElement(meta_list_elem, meta_name, values.get(0));
223
224 }
225 }
226
227
228 /** create the actual metadata element for the list */
229
230 protected void addMetadataElement(Element meta_list_elem, String name, String value) {
231
232 Element meta = GSXML.createTextElement(meta_list_elem.getOwnerDocument(), name, value);
233 meta_list_elem.appendChild(meta);
234 }
235
236
237 /** create a header element used when processing requests like GetRecord
238 */
239 protected Element createHeaderElement(Document doc, String oid) {
240
241 Element header = doc.createElement(IIIFXML.HEADER);
242
243 Element identifier = doc.createElement(IIIFXML.IDENTIFIER);
244 GSXML.setNodeText(identifier, coll_name + ":" + oid); // **** OID
245 header.appendChild(identifier);
246
247 return header;
248 }
249
250}
251
252
Note: See TracBrowser for help on using the repository browser.