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

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

Mostly code tidy-up. In IIIFServerBridge.java, edit to remove hard-wired path

File size: 6.6 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 static Logger logger = Logger.getLogger(org.greenstone.gsdl3.collection.IIIFCollection.class.getName());
58
59 /** does this collection provide the IIIF service */
60 protected boolean has_iiif = false;
61
62 /** a reference to the IIIFPMH service rack */
63 protected IIIFPMH iiif_service_rack = null;
64
65 /**
66 * Configures the collection.
67 *
68 * site_home and cluster_name must be set before configure is called.
69 *
70 * collection metadata is obtained, and services loaded.
71 *
72 * @return true/false on success/fail
73 */
74 public boolean configure()
75 {
76 if (this.site_home == null || this.cluster_name == null)
77 {
78 logger.error("Collection: site_home and collection_name must be set before configure called!");
79 return false;
80 }
81
82 macro_resolver.addMacro("_httpcollection_", this.site_http_address + "/collect/" + this.cluster_name);
83
84 Element coll_config_xml = loadCollConfigFile();
85 if (coll_config_xml == null) {
86 logger.error("Collection: couldn't configure collection: " + this.cluster_name + ", " + "Couldn't load collection config file");
87
88 return false;
89 }
90 Element build_config_xml = loadBuildConfigFile();
91 if (build_config_xml == null)
92 {
93 logger.error("Collection: couldn't configure collection: " + this.cluster_name + ", " + "Couldn't load build config file");
94
95 return false;
96 }
97 GSXSLT.modifyCollectionConfigForDebug(coll_config_xml);
98
99 // process the metadata and display items and default library params
100 super.configureLocalData(coll_config_xml);
101 super.configureLocalData(build_config_xml);
102 // get extra collection specific stuff
103 findAndLoadInfo(coll_config_xml, build_config_xml);
104
105 // load up the IIIFPMH serviceRack
106 configureServiceRacks(coll_config_xml, build_config_xml);
107
108 return true;
109
110 }
111
112 /**
113 * whether this collection has IIIFPMH services
114 */
115 public boolean hasIIIF()
116 {
117 return has_iiif;
118 }
119
120
121 /** add any extra info for collection from IIIFConfig.xml */
122 public boolean configureIIIF(Element iiif_config) {
123 // just pass the element to each service - should only be one
124 return this.iiif_service_rack.configureIIIF(iiif_config);
125 }
126
127 /** override this to only load up IIIFPMH serviceRack - don't need all the rest of them for iiif*/
128 protected boolean configureServiceRackList(Element service_rack_list, Element extra_info)
129 {
130 //logger.info("*** Away to call getNamedElement() for service_rack_list = " + XMLConverter.getPrettyString(service_rack_list));
131
132 // find the IIIFPMH service
133 Element iiif_service_xml = GSXML.getNamedElement(service_rack_list, GSXML.SERVICE_CLASS_ELEM, GSXML.NAME_ATT, "IIIFPMH");
134 if (iiif_service_xml == null) {
135 return false;
136 }
137
138 // the xml request to send to the serviceRack to query what services it provides
139 Document doc = XMLConverter.newDOM();
140 Element message = doc.createElement(GSXML.MESSAGE_ELEM);
141 Element request = GSXML.createBasicRequest(doc, GSXML.REQUEST_TYPE_DESCRIBE, "", new UserContext());
142 message.appendChild(request);
143
144 if(this.iiif_service_rack == null) {
145 logger.info("*** away to call constructor to IIIFPMH");
146 this.iiif_service_rack = new IIIFPMH();
147 }
148 this.iiif_service_rack.setSiteHome(this.site_home);
149 this.iiif_service_rack.setSiteAddress(this.site_http_address);
150 this.iiif_service_rack.setClusterName(this.cluster_name);
151 this.iiif_service_rack.setServiceCluster(this);
152 this.iiif_service_rack.setMessageRouter(this.router);
153
154 // pass the xml node to the service for configuration
155 logger.info("*** away to configure service rack IIIFPMH");
156
157 if (this.iiif_service_rack.configure(iiif_service_xml, extra_info)) {
158
159 // find out the supported service types for this service module
160 Node types = this.iiif_service_rack.process(message);
161 NodeList typenodes = ((Element) types).getElementsByTagName(GSXML.SERVICE_ELEM);
162
163 for (int j = 0; j < typenodes.getLength(); j++)
164 {
165 String service = ((Element) typenodes.item(j)).getAttribute(GSXML.NAME_ATT);
166
167 if (service_map.get(service) != null)
168 {
169 char extra = '0';
170 String new_service = service + extra;
171
172 while (service_map.get(new_service) != null)
173 {
174 extra++;
175 new_service = service + extra;
176 }
177 this.service_name_map.put(new_service, service);
178 service = new_service;
179 ((Element) typenodes.item(j)).setAttribute(GSXML.NAME_ATT, service);
180 }
181 this.service_map.put(service, this.iiif_service_rack);
182 // also add info to the ServiceInfo XML element
183 this.service_list.appendChild(this.desc_doc.importNode(typenodes.item(j), true));
184 }
185 has_iiif = true;
186 return true;
187 }
188
189 return false;
190 }
191}
Note: See TracBrowser for help on using the repository browser.