source: gs3-extensions/iiif-servlet/trunk/src/gsdl-src/java/org/greenstone/gsdl3/collection/IIIFCollection.java@ 32843

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

Shift from OAI as a template, to separate IIIF based classes

File size: 7.7 KB
Line 
1/*
2 * IIIFCollection.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.collection;
20
21import java.io.BufferedReader;
22import java.io.BufferedWriter;
23import java.io.File;
24import java.io.FileReader;
25import java.io.FileWriter;
26import java.io.IOException;
27import java.io.PrintWriter;
28import java.io.StringWriter;
29import java.util.ArrayList;
30import java.util.HashMap;
31
32import org.apache.commons.lang3.StringUtils;
33import org.apache.log4j.Logger;
34import org.greenstone.gsdl3.core.ModuleInterface;
35import org.greenstone.gsdl3.service.ServiceRack;
36import org.greenstone.gsdl3.service.IIIFPMH;
37import org.greenstone.gsdl3.util.GSFile;
38import org.greenstone.gsdl3.util.GSXML;
39import org.greenstone.gsdl3.util.GSXSLT;
40import org.greenstone.gsdl3.util.IIIFXML;
41import org.greenstone.gsdl3.util.SimpleMacroResolver;
42import org.greenstone.gsdl3.util.UserContext;
43import org.greenstone.gsdl3.util.XMLConverter;
44import org.greenstone.gsdl3.util.XMLTransformer;
45import org.w3c.dom.Document;
46import org.w3c.dom.Element;
47import org.w3c.dom.Node;
48import org.w3c.dom.NodeList;
49
50/**
51 * Represents a collection for the IIIFServerBridge. This is a cut down version of Collection, as we
52 * only want to load the IIIFPMH service rack, not any of the others
53 *
54 */
55public class IIIFCollection extends Collection
56{
57
58 static Logger logger = Logger.getLogger(org.greenstone.gsdl3.collection.IIIFCollection.class.getName());
59
60 /** does this collection provide the IIIF service */
61 protected boolean has_iiif = false;
62
63 /** earliest datestamp of an OAI collection. Also used to work out the earliest datetimestamp of the entire OAI repository */
64 /*
65 protected long earliestOAIDatestamp = 0;
66 */
67
68 /** a reference to the IIIFPMH service rack */
69 protected IIIFPMH iiif_service_rack = null;
70
71 /**
72 * Configures the collection.
73 *
74 * site_home and cluster_name must be set before configure is called.
75 *
76 * collection metadata is obtained, and services loaded.
77 *
78 * @return true/false on success/fail
79 */
80 public boolean configure()
81 {
82 if (this.site_home == null || this.cluster_name == null)
83 {
84 logger.error("Collection: site_home and collection_name must be set before configure called!");
85 return false;
86 }
87
88 macro_resolver.addMacro("_httpcollection_", this.site_http_address + "/collect/" + this.cluster_name);
89
90 Element coll_config_xml = loadCollConfigFile();
91 if (coll_config_xml == null) {
92 logger.error("Collection: couldn't configure collection: " + this.cluster_name + ", " + "Couldn't load collection config file");
93
94 return false;
95 }
96 Element build_config_xml = loadBuildConfigFile();
97 if (build_config_xml == null)
98 {
99 logger.error("Collection: couldn't configure collection: " + this.cluster_name + ", " + "Couldn't load build config file");
100
101 return false;
102 }
103 GSXSLT.modifyCollectionConfigForDebug(coll_config_xml);
104
105 // process the metadata and display items and default library params
106 super.configureLocalData(coll_config_xml);
107 super.configureLocalData(build_config_xml);
108 // get extra collection specific stuff
109 findAndLoadInfo(coll_config_xml, build_config_xml);
110
111 // load up the IIIFPMH serviceRack
112 configureServiceRacks(coll_config_xml, build_config_xml);
113
114 return true;
115
116 }
117
118 // NOTE:
119 // Calling cleanUp() on IIIFPMH object iiif_service_rack will
120 // close any open dbs handles on module deactivation by
121 // MESSAGEROUTER (no deactivate yet called by
122 // IIIFMessageRouter)
123
124 // But the IIIFPMH object's cleanUp() is already called by
125 // superclass ServiceCluster, which goes around calling
126 // cleanUp() on all services/ServiceRacks.
127
128 /**
129 * whether this collection has IIIFPMH services
130 */
131 public boolean hasIIIF()
132 {
133 return has_iiif;
134 }
135
136 /**
137 * The earliesttimestamp entry in the oai-inf.db representing when the collection was created.
138 * Used by the OAIReceptionist
139 */
140 /*
141 public long getEarliestOAIDatestamp()
142 {
143 return earliestOAIDatestamp;
144 }
145 */
146
147 /** add any extra info for collection from IIIFConfig.xml */
148 public boolean configureIIIF(Element iiif_config) {
149 // just pass the element to each service - should only be one
150 return this.iiif_service_rack.configureIIIF(iiif_config);
151 }
152
153 /** override this to only load up IIIFPMH serviceRack - don't need all the rest of them for iiif*/
154 protected boolean configureServiceRackList(Element service_rack_list, Element extra_info)
155 {
156
157 // find the IIIFPMH service
158 Element iiif_service_xml = GSXML.getNamedElement(service_rack_list, GSXML.SERVICE_CLASS_ELEM, GSXML.NAME_ATT, "IIIFPMH");
159 if (iiif_service_xml == null) {
160 return false;
161 }
162
163 // the xml request to send to the serviceRack to query what services it provides
164 Document doc = XMLConverter.newDOM();
165 Element message = doc.createElement(GSXML.MESSAGE_ELEM);
166 Element request = GSXML.createBasicRequest(doc, GSXML.REQUEST_TYPE_DESCRIBE, "", new UserContext());
167 message.appendChild(request);
168
169 if(this.iiif_service_rack == null) {
170 logger.info("*** away to call constructor to IIIFPMH");
171 this.iiif_service_rack = new IIIFPMH();
172 }
173 this.iiif_service_rack.setSiteHome(this.site_home);
174 this.iiif_service_rack.setSiteAddress(this.site_http_address);
175 this.iiif_service_rack.setClusterName(this.cluster_name);
176 this.iiif_service_rack.setServiceCluster(this);
177 this.iiif_service_rack.setMessageRouter(this.router);
178
179 // pass the xml node to the service for configuration
180 logger.info("*** away to configure service rack IIIFPMH");
181
182 if (this.iiif_service_rack.configure(iiif_service_xml, extra_info)) {
183
184 /*
185 // once we've configured the IIIFPMH service, we can use the OAIPMH service to
186 // retrieve the earliest timestamp of this OAI collection from the oai-inf db
187 long earliestTimestamp = this.oai_service_rack.getEarliestTimestamp();
188 if(earliestTimestamp == -1) {
189 this.earliestOAIDatestamp = -1;
190 logger.warn("No OAI timestamp for collection " + this.cluster_name);
191 } else {
192 this.earliestOAIDatestamp = earliestTimestamp; // milliseconds
193 }
194 */
195
196 // find out the supported service types for this service module
197 Node types = this.iiif_service_rack.process(message);
198 NodeList typenodes = ((Element) types).getElementsByTagName(GSXML.SERVICE_ELEM);
199
200 for (int j = 0; j < typenodes.getLength(); j++)
201 {
202 String service = ((Element) typenodes.item(j)).getAttribute(GSXML.NAME_ATT);
203
204 if (service_map.get(service) != null)
205 {
206 char extra = '0';
207 String new_service = service + extra;
208
209 while (service_map.get(new_service) != null)
210 {
211 extra++;
212 new_service = service + extra;
213 }
214 this.service_name_map.put(new_service, service);
215 service = new_service;
216 ((Element) typenodes.item(j)).setAttribute(GSXML.NAME_ATT, service);
217 }
218 this.service_map.put(service, this.iiif_service_rack);
219 // also add info to the ServiceInfo XML element
220 this.service_list.appendChild(this.desc_doc.importNode(typenodes.item(j), true));
221 }
222 has_iiif = true;
223 return true;
224 }
225
226
227 return false;
228 }
229
230}
Note: See TracBrowser for help on using the repository browser.