source: gs3-extensions/iiif-servlet/trunk/src/gsdl-src/java/org/greenstone/gsdl3/IIIFServerBridge.java@ 32883

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

Code tidy up

File size: 9.0 KB
Line 
1/*
2 * IIIFServerBridge.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 */
19package org.greenstone.gsdl3;
20
21import java.io.IOException;
22import java.io.PrintWriter;
23import java.util.HashSet;
24import java.util.Iterator;
25import java.util.Map;
26
27import javax.servlet.ServletConfig;
28import javax.servlet.ServletException;
29import javax.servlet.UnavailableException;
30import javax.servlet.http.HttpServletRequest;
31import javax.servlet.http.HttpServletResponse;
32
33import org.apache.log4j.Logger;
34import org.greenstone.gsdl3.comms.Communicator;
35import org.greenstone.gsdl3.comms.SOAPCommunicator;
36import org.greenstone.gsdl3.core.IIIFMessageRouter;
37import org.greenstone.gsdl3.core.IIIFReceptionist;
38import org.greenstone.gsdl3.util.GSConstants;
39import org.greenstone.gsdl3.util.GSParams;
40import org.greenstone.gsdl3.util.GSXML;
41import org.greenstone.gsdl3.util.IIIFXML;
42import org.greenstone.gsdl3.util.XMLConverter;
43import org.w3c.dom.Document;
44import org.w3c.dom.Element;
45import org.w3c.dom.Node;
46
47/** a class the serve as a bridge between the Cantaloupe IIIF image server and
48 * Greenstone collections. Loosely based on OAIServer
49 *
50 * the init method is called only once - the first time the bridge is established
51 * then doGet() each time a document image request is made
52 * @see Receptionist
53 */
54/**
55 * IIIF server configuration instructions *
56 *
57 */
58public class IIIFServerBridge
59{
60 /** the receptionist to send messages to */
61 protected IIIFReceptionist recept = null;
62
63 /**
64 * The name of the site with which we will finally be dealing, whether it is
65 * a local site or a remote site through a communicator.
66 */
67 protected String site = "";
68
69 static Logger logger = Logger.getLogger(org.greenstone.gsdl3.IIIFServerBridge.class.getName());
70
71 private void configure() throws UnavailableException
72 {
73 // Read in IIIFConfig.xml (residing web/WEB-INF/classes/) and
74 //use it to configure the receptionist.
75 Element iiif_config = IIIFXML.getIIIFConfigXML();
76 if (iiif_config == null)
77 {
78 logger.error("Fail to parse IIIF config file IIIFConfig.xml.");
79 throw new UnavailableException("IIIFServerBridge: Couldn't parse IIIFConfig.xml");
80 }
81 // pass it to the receptionist
82 if (!this.recept.configure(iiif_config)) {
83 logger.error("Couldn't configure IIIF receptionist");
84 throw new UnavailableException("IIIFServerBridge: Couldn't configure receptionist");
85 }
86 }
87
88 /**
89 * initialise the class
90 */
91 public void init(String site_name) throws UnavailableException, Exception
92 {
93 org.greenstone.util.GlobalProperties.loadGlobalProperties("");
94 java.io.InputStream in = Class.forName("org.greenstone.util.GlobalProperties").getClassLoader().getResourceAsStream("global.properties");
95
96 String tomcat_context = System.getProperty("tomcat.context");
97
98 // the receptionist -the servlet will talk to this
99 this.recept = new IIIFReceptionist();
100
101 //this site_name could consist of comma separated more than one site name.
102 IIIFMessageRouter message_router = new IIIFMessageRouter();
103
104 message_router.setSiteName(site_name);
105 // lots of work is done in this step; see IIIFMessageRouter.java
106 if (!message_router.configure()) {
107 throw new UnavailableException("IIIFServerBridge: Couldn't configure IIIFMessageRouter");
108 }
109 this.recept.setSiteName(site_name);
110 this.recept.setMessageRouter(message_router);
111
112 configure();
113 } // end of init()
114
115 /*
116 Written but never used/tested
117 */
118
119 public void remote_init(String remote_site_name, String remote_site_type, String remote_site_address) throws UnavailableException
120 {
121 if (remote_site_name == null || remote_site_type == null || remote_site_address == null)
122 {
123 logger.error("Initialisation paramters not all set!");
124 logger.error("You must have remote_site_name, remote_site_type and remote_site_address set");
125 throw new UnavailableException("IIIFServerBridge: incorrect remote connection parameters specified");
126 }
127
128 // the receptionist -the servlet will talk to this
129 this.recept = new IIIFReceptionist();
130
131 // talking to a remote site, create a communicator
132 Communicator communicator = null;
133 // we need to create the XML to configure the communicator
134 Document site_doc = XMLConverter.newDOM();
135 Element site_elem = site_doc.createElement(GSXML.SITE_ELEM);
136 site_elem.setAttribute(GSXML.TYPE_ATT, remote_site_type);
137 site_elem.setAttribute(GSXML.NAME_ATT, remote_site_name);
138 site_elem.setAttribute(GSXML.ADDRESS_ATT, remote_site_address);
139
140 if (remote_site_type.equals(GSXML.COMM_TYPE_SOAP_JAVA))
141 {
142 communicator = new SOAPCommunicator();
143 }
144 else
145 {
146 logger.error("IIIFServerBridge.init Error: invalid Communicator type: " + remote_site_type);
147 throw new UnavailableException("IIIFServerBridge: invalid communicator type");
148 }
149
150 if (!communicator.configure(site_elem))
151 {
152 logger.error("IIIFServerBridge.init Error: Couldn't configure communicator");
153 throw new UnavailableException("IIIFServerBridge: Couldn't configure communicator");
154 }
155 this.recept.setSiteName(remote_site_name);
156 this.recept.setMessageRouter(communicator);
157
158 configure();
159 } // end of remote_init()
160
161
162 public String doGetDocumentMessage(String identifier)
163 {
164 String result = "";
165
166 String[] pairs = new String[2];
167 pairs[0] = "verb=GetRecord";
168 pairs[1] = "identifier="+identifier;
169
170 String verb = "GetRecord";
171 Document response_doc = XMLConverter.newDOM();
172 //Element xml_response = IIIFXML.createBasicResponse(response_doc, verb, pairs);
173 Element verb_elem = null;
174
175 // compose the request message to the receptionist
176 Document request_doc = XMLConverter.newDOM();
177 Element xml_message = request_doc.createElement(GSXML.MESSAGE_ELEM);
178 Element xml_request = request_doc.createElement(GSXML.REQUEST_ELEM);
179 xml_request.setAttribute(GSXML.TO_ATT, verb);
180 addParams(xml_request, pairs);
181
182 xml_message.appendChild(xml_request);
183
184 Node xml_result = this.recept.process(xml_message);
185 if (xml_result == null)
186 {
187 logger.info("xml_result is null");
188 verb_elem = IIIFXML.createErrorElement(response_doc, "Internal error", "");
189 //xml_response.appendChild(verb_elem);
190 }
191 else
192 {
193
194 //
195 // All response elements are in the form (with a corresponding verb
196 // name): <message> <response> <verb> ... </verb> </response> </message>
197 //
198 Node res = GSXML.getChildByTagName(xml_result, GSXML.RESPONSE_ELEM);
199 if (res == null)
200 {
201 logger.info("response element in xml_result is null");
202 verb_elem = IIIFXML.createErrorElement(response_doc, "Internal error", "");
203 }
204 else {
205 verb_elem = GSXML.getFirstElementChild(res); // GetRecord
206 Node record_node = GSXML.getFirstElementChild(verb_elem); // record
207 Element metadata_list_elem = (Element)GSXML.getChildByTagName(record_node,"metadata"); // metadata
208
209 Element assocfilepath_metadata_elem = (Element)GSXML.getChildByTagName(metadata_list_elem,"assocfilepath");
210 String assocfilepath_metadata_val = GSXML.getNodeText(assocfilepath_metadata_elem);
211
212 Element image_metadata_elem = (Element)GSXML.getChildByTagName(metadata_list_elem,"Image");
213 String image_metadata_val = GSXML.getNodeText(image_metadata_elem);
214
215 result = assocfilepath_metadata_val + "/" + image_metadata_val;
216 }
217
218 //xml_response.appendChild(response_doc.importNode(verb_elem, true));
219 }
220 return result;
221 }
222
223 /** append parameter elements to the request sent to the receptionist */
224 public void addParams(Element request, String[] pairs)
225 {
226 Document doc = request.getOwnerDocument();
227 // no params apart from the verb
228 if (pairs == null || pairs.length < 2)
229 return;
230
231 /**
232 * the request xml is composed in the form: <request> <param name=.../>
233 * <param name=.../> </request> (No paramList element in between).
234 */
235 for (int i = 1; i < pairs.length; i++)
236 {
237 //the first pair in pairs is the verb=xxx
238 int index = pairs[i].indexOf("=");
239 if (index != -1)
240 { //just a double check
241 Element param = GSXML.createParameter(doc, pairs[i].substring(0, index), IIIFXML.iiifDecode(pairs[i].substring(index + 1)));
242 request.appendChild(param);
243 }
244 }
245 }
246
247
248 public void destroy()
249 {
250 recept.cleanUp();
251 }
252
253}
Note: See TracBrowser for help on using the repository browser.