1 | /*
|
---|
2 | * IIIFMessageRouter.java
|
---|
3 | * Copyright (C) 2018 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.core;
|
---|
20 |
|
---|
21 | import java.io.File;
|
---|
22 | import java.net.Authenticator;
|
---|
23 | import java.net.PasswordAuthentication;
|
---|
24 | import java.util.HashMap;
|
---|
25 | import java.util.Map;
|
---|
26 | import java.util.Iterator;
|
---|
27 |
|
---|
28 | import org.apache.commons.lang3.StringUtils;
|
---|
29 | import org.apache.log4j.Logger;
|
---|
30 | import org.greenstone.gsdl3.collection.IIIFCollection;
|
---|
31 | import org.greenstone.gsdl3.collection.ServiceCluster;
|
---|
32 | import org.greenstone.gsdl3.comms.Communicator;
|
---|
33 | import org.greenstone.gsdl3.comms.SOAPCommunicator;
|
---|
34 | import org.greenstone.gsdl3.service.ServiceRack;
|
---|
35 | import org.greenstone.gsdl3.util.GSFile;
|
---|
36 | import org.greenstone.gsdl3.util.GSPath;
|
---|
37 | import org.greenstone.gsdl3.util.GSXML;
|
---|
38 | import org.greenstone.gsdl3.util.IIIFXML;
|
---|
39 | import org.greenstone.gsdl3.util.UserContext;
|
---|
40 | import org.greenstone.gsdl3.util.XMLConverter;
|
---|
41 | import org.greenstone.util.GlobalProperties;
|
---|
42 | import org.w3c.dom.Document;
|
---|
43 | import org.w3c.dom.Element;
|
---|
44 | import org.w3c.dom.Node;
|
---|
45 | import org.w3c.dom.NodeList;
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * The hub of a Greenstone IIIF image server/bridge.
|
---|
49 | *
|
---|
50 | * A simplified version of MessageRouter for IIIFServerBridge. Only loads up collections that have IIIF services.
|
---|
51 | */
|
---|
52 | public class IIIFMessageRouter extends MessageRouter
|
---|
53 | {
|
---|
54 |
|
---|
55 | static Logger logger = Logger.getLogger(org.greenstone.gsdl3.core.IIIFMessageRouter.class.getName());
|
---|
56 |
|
---|
57 | public Element iiif_config = null;
|
---|
58 | //***************************************************************
|
---|
59 | // public methods
|
---|
60 | //***************************************************************
|
---|
61 |
|
---|
62 | /** constructor */
|
---|
63 | public IIIFMessageRouter()
|
---|
64 | {
|
---|
65 | }
|
---|
66 |
|
---|
67 | /**
|
---|
68 | * read thru own site config file - create services and connect to sites
|
---|
69 | */
|
---|
70 | protected boolean configureLocalSite()
|
---|
71 | {
|
---|
72 |
|
---|
73 | // this may be a reconfigure, so clean up the old moduleMap
|
---|
74 | cleanUpModuleMapEntire();
|
---|
75 |
|
---|
76 | // ****
|
---|
77 | // for oai, we don't do anything with the site config
|
---|
78 | // file. But we'll read it in and keep it in case need
|
---|
79 | // it later, e.g. for replace elements when retrieving
|
---|
80 | // metadata - which I don't think has been implemented
|
---|
81 | File configFile = new File(GSFile.siteConfigFile(this.site_home));
|
---|
82 |
|
---|
83 | if (!configFile.exists())
|
---|
84 | {
|
---|
85 | logger.error(" site config file: " + configFile.getPath() + " not found!");
|
---|
86 | return false;
|
---|
87 | }
|
---|
88 |
|
---|
89 | Document config_doc = XMLConverter.getDOM(configFile);
|
---|
90 | if (config_doc == null)
|
---|
91 | {
|
---|
92 | logger.error(" couldn't parse site config file: " + configFile.getPath());
|
---|
93 | return false;
|
---|
94 | }
|
---|
95 |
|
---|
96 | this.config_info = config_doc.getDocumentElement();
|
---|
97 |
|
---|
98 | // this is the receptionist's IFFFConfig.xml.
|
---|
99 | // Need to rethink how the MR gets this this if we
|
---|
100 | // ever talk to remote site, and whether it should be
|
---|
101 | // using it anyway
|
---|
102 | this.iiif_config = IIIFXML.getIIIFConfigXML();
|
---|
103 | if (this.iiif_config == null)
|
---|
104 | {
|
---|
105 | logger.error("Couldn't load in IIIFConfig.xml");
|
---|
106 | return false;
|
---|
107 | }
|
---|
108 | Document doc = XMLConverter.newDOM();
|
---|
109 | // load up the collections
|
---|
110 | this.collection_list = doc.createElement(GSXML.COLLECTION_ELEM + GSXML.LIST_MODIFIER);
|
---|
111 | configureCollections();
|
---|
112 |
|
---|
113 | return true;
|
---|
114 |
|
---|
115 | }
|
---|
116 |
|
---|
117 |
|
---|
118 | /**
|
---|
119 | * creates and configures a new collection if this is done for a
|
---|
120 | * reconfigure, the collection should be deactivated first.
|
---|
121 | *
|
---|
122 | * @param col_name
|
---|
123 | * the name of the collection
|
---|
124 | * @return true if collection created ok
|
---|
125 | */
|
---|
126 | protected boolean activateCollectionByName(String col_name)
|
---|
127 | {
|
---|
128 |
|
---|
129 | logger.info("Activating collection: " + col_name + ".");
|
---|
130 | Document doc = this.collection_list.getOwnerDocument();
|
---|
131 | // use our special IIIFCollection - this will only load in IIIF services
|
---|
132 | IIIFCollection c = new IIIFCollection();
|
---|
133 |
|
---|
134 | c.setCollectionName(col_name);
|
---|
135 | c.setSiteHome(this.site_home);
|
---|
136 | c.setSiteAddress(this.site_http_address);
|
---|
137 | c.setMessageRouter(this);
|
---|
138 | if (!c.configure()) {
|
---|
139 | logger.error("Couldn't configure collection: " + col_name + ".");
|
---|
140 | return false;
|
---|
141 | }
|
---|
142 |
|
---|
143 | logger.info("have just configured collection " + col_name);
|
---|
144 | if (!c.hasIIIF()) {
|
---|
145 | logger.info ("collection "+col_name+" has no IIIF services. Not keeping it loaded");
|
---|
146 | return false;
|
---|
147 | }
|
---|
148 | if (!c.configureIIIF(this.iiif_config)) {
|
---|
149 | logger.info("couldn't configure the collection : "+col_name +" with the iiif config info");
|
---|
150 | return false;
|
---|
151 | }
|
---|
152 | // add to list of collections
|
---|
153 | this.module_map.put(col_name, c);
|
---|
154 | Element e = doc.createElement(GSXML.COLLECTION_ELEM);
|
---|
155 | e.setAttribute(GSXML.NAME_ATT, col_name);
|
---|
156 | /*
|
---|
157 | e.setAttribute(OAIXML.LASTMODIFIED, "" + c.getLastmodified());
|
---|
158 | e.setAttribute(OAIXML.EARLIEST_DATESTAMP, "" + c.getEarliestDatestamp());
|
---|
159 | e.setAttribute(OAIXML.EARLIEST_OAI_DATESTAMP, "" + c.getEarliestOAIDatestamp());
|
---|
160 | */
|
---|
161 | this.collection_list.appendChild(e);
|
---|
162 | return true;
|
---|
163 |
|
---|
164 | }
|
---|
165 |
|
---|
166 |
|
---|
167 |
|
---|
168 | //*****************************************************************
|
---|
169 | // auxiliary process methods
|
---|
170 | //*****************************************************************
|
---|
171 |
|
---|
172 | /**
|
---|
173 | * handles requests made to the MessageRouter itself
|
---|
174 | *
|
---|
175 | * @param req
|
---|
176 | * - the request Element- <request>
|
---|
177 | * @return the result Element - should be <response>
|
---|
178 | */
|
---|
179 | /*
|
---|
180 | protected Element processMessage(Element req) {
|
---|
181 | Document doc = XMLConverter.newDOM();
|
---|
182 | String type = req.getAttribute(GSXML.TYPE_ATT);
|
---|
183 | Element response = doc.createElement(GSXML.RESPONSE_ELEM);
|
---|
184 | response.setAttribute(GSXML.FROM_ATT, "");
|
---|
185 |
|
---|
186 | if (type.equals(OAIXML.OAI_SET_LIST))
|
---|
187 | {
|
---|
188 | logger.info("oaiSetList request received");
|
---|
189 | //this is the oai receptionist asking for a list of oai-support collections
|
---|
190 | response.setAttribute(GSXML.TYPE_ATT, OAIXML.OAI_SET_LIST);
|
---|
191 | response.appendChild(doc.importNode(this.collection_list, true));
|
---|
192 | return response;
|
---|
193 | }
|
---|
194 | return super.processMessage(req);
|
---|
195 | }
|
---|
196 |
|
---|
197 | */
|
---|
198 | }
|
---|