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 | */
|
---|
19 | package org.greenstone.gsdl3.collection;
|
---|
20 |
|
---|
21 | import java.io.BufferedReader;
|
---|
22 | import java.io.BufferedWriter;
|
---|
23 | import java.io.File;
|
---|
24 | import java.io.FileReader;
|
---|
25 | import java.io.FileWriter;
|
---|
26 | import java.io.IOException;
|
---|
27 | import java.io.PrintWriter;
|
---|
28 | import java.io.StringWriter;
|
---|
29 | import java.util.ArrayList;
|
---|
30 | import java.util.HashMap;
|
---|
31 |
|
---|
32 | import org.apache.commons.lang3.StringUtils;
|
---|
33 | import org.apache.log4j.Logger;
|
---|
34 | import org.greenstone.gsdl3.core.ModuleInterface;
|
---|
35 | import org.greenstone.gsdl3.service.ServiceRack;
|
---|
36 | import org.greenstone.gsdl3.service.IIIFPMH;
|
---|
37 | import org.greenstone.gsdl3.util.GSFile;
|
---|
38 | import org.greenstone.gsdl3.util.GSXML;
|
---|
39 | import org.greenstone.gsdl3.util.GSXSLT;
|
---|
40 | import org.greenstone.gsdl3.util.IIIFXML;
|
---|
41 | import org.greenstone.gsdl3.util.SimpleMacroResolver;
|
---|
42 | import org.greenstone.gsdl3.util.UserContext;
|
---|
43 | import org.greenstone.gsdl3.util.XMLConverter;
|
---|
44 | import org.greenstone.gsdl3.util.XMLTransformer;
|
---|
45 | import org.w3c.dom.Document;
|
---|
46 | import org.w3c.dom.Element;
|
---|
47 | import org.w3c.dom.Node;
|
---|
48 | import 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 | */
|
---|
55 | public 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 | // find the IIIFPMH service
|
---|
131 | Element iiif_service_xml = GSXML.getNamedElement(service_rack_list, GSXML.SERVICE_CLASS_ELEM, GSXML.NAME_ATT, "IIIFPMH");
|
---|
132 | if (iiif_service_xml == null) {
|
---|
133 | return false;
|
---|
134 | }
|
---|
135 |
|
---|
136 | // the xml request to send to the serviceRack to query what services it provides
|
---|
137 | Document doc = XMLConverter.newDOM();
|
---|
138 | Element message = doc.createElement(GSXML.MESSAGE_ELEM);
|
---|
139 | Element request = GSXML.createBasicRequest(doc, GSXML.REQUEST_TYPE_DESCRIBE, "", new UserContext());
|
---|
140 | message.appendChild(request);
|
---|
141 |
|
---|
142 | if(this.iiif_service_rack == null) {
|
---|
143 | this.iiif_service_rack = new IIIFPMH();
|
---|
144 | }
|
---|
145 | this.iiif_service_rack.setSiteHome(this.site_home);
|
---|
146 | this.iiif_service_rack.setSiteAddress(this.site_http_address);
|
---|
147 | this.iiif_service_rack.setClusterName(this.cluster_name);
|
---|
148 | this.iiif_service_rack.setServiceCluster(this);
|
---|
149 | this.iiif_service_rack.setMessageRouter(this.router);
|
---|
150 |
|
---|
151 | // pass the xml node to the service for configuration
|
---|
152 | if (this.iiif_service_rack.configure(iiif_service_xml, extra_info)) {
|
---|
153 |
|
---|
154 | // find out the supported service types for this service module
|
---|
155 | Node types = this.iiif_service_rack.process(message);
|
---|
156 | NodeList typenodes = ((Element) types).getElementsByTagName(GSXML.SERVICE_ELEM);
|
---|
157 |
|
---|
158 | for (int j = 0; j < typenodes.getLength(); j++)
|
---|
159 | {
|
---|
160 | String service = ((Element) typenodes.item(j)).getAttribute(GSXML.NAME_ATT);
|
---|
161 |
|
---|
162 | if (service_map.get(service) != null)
|
---|
163 | {
|
---|
164 | char extra = '0';
|
---|
165 | String new_service = service + extra;
|
---|
166 |
|
---|
167 | while (service_map.get(new_service) != null)
|
---|
168 | {
|
---|
169 | extra++;
|
---|
170 | new_service = service + extra;
|
---|
171 | }
|
---|
172 | this.service_name_map.put(new_service, service);
|
---|
173 | service = new_service;
|
---|
174 | ((Element) typenodes.item(j)).setAttribute(GSXML.NAME_ATT, service);
|
---|
175 | }
|
---|
176 | this.service_map.put(service, this.iiif_service_rack);
|
---|
177 | // also add info to the ServiceInfo XML element
|
---|
178 | this.service_list.appendChild(this.desc_doc.importNode(typenodes.item(j), true));
|
---|
179 | }
|
---|
180 | has_iiif = true;
|
---|
181 | return true;
|
---|
182 | }
|
---|
183 |
|
---|
184 | return false;
|
---|
185 | }
|
---|
186 | }
|
---|