[32707] | 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;
|
---|
[32842] | 30 | import org.greenstone.gsdl3.collection.IIIFCollection;
|
---|
[32707] | 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;
|
---|
[32842] | 38 | import org.greenstone.gsdl3.util.IIIFXML;
|
---|
[32707] | 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 | *
|
---|
[32842] | 50 | * A simplified version of MessageRouter for IIIFServerBridge. Only loads up collections that have IIIF services.
|
---|
[32707] | 51 | */
|
---|
| 52 | public class IIIFMessageRouter extends MessageRouter
|
---|
| 53 | {
|
---|
| 54 |
|
---|
| 55 | static Logger logger = Logger.getLogger(org.greenstone.gsdl3.core.IIIFMessageRouter.class.getName());
|
---|
| 56 |
|
---|
[32842] | 57 | public Element iiif_config = null;
|
---|
[32707] | 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 |
|
---|
[32842] | 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
|
---|
[32707] | 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 |
|
---|
[32842] | 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)
|
---|
[32707] | 104 | {
|
---|
[32842] | 105 | logger.error("Couldn't load in IIIFConfig.xml");
|
---|
[32707] | 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();
|
---|
[32842] | 131 | // use our special IIIFCollection - this will only load in IIIF services
|
---|
| 132 | IIIFCollection c = new IIIFCollection();
|
---|
[32707] | 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);
|
---|
[32842] | 144 | if (!c.hasIIIF()) {
|
---|
| 145 | logger.info ("collection "+col_name+" has no IIIF services. Not keeping it loaded");
|
---|
[32707] | 146 | return false;
|
---|
| 147 | }
|
---|
[32842] | 148 | if (!c.configureIIIF(this.iiif_config)) {
|
---|
| 149 | logger.info("couldn't configure the collection : "+col_name +" with the iiif config info");
|
---|
[32707] | 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);
|
---|
[32842] | 156 | /*
|
---|
[32707] | 157 | e.setAttribute(OAIXML.LASTMODIFIED, "" + c.getLastmodified());
|
---|
| 158 | e.setAttribute(OAIXML.EARLIEST_DATESTAMP, "" + c.getEarliestDatestamp());
|
---|
| 159 | e.setAttribute(OAIXML.EARLIEST_OAI_DATESTAMP, "" + c.getEarliestOAIDatestamp());
|
---|
[32842] | 160 | */
|
---|
[32707] | 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 | */
|
---|
[32842] | 179 | /*
|
---|
[32707] | 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 |
|
---|
[32842] | 197 | */
|
---|
[32707] | 198 | }
|
---|