source: gs3-extensions/iiif-servlet/trunk/src/gsdl-src/java/org/greenstone/gsdl3/IIIFServerBridge.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: 10.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 ////GlobalProperties.loadGlobalProperties(config.getServletContext().getRealPath(""));
94
95 org.greenstone.util.GlobalProperties.loadGlobalProperties("");
96 //org.greenstone.util.GlobalProperties.loadGlobalProperties(config.getServletContext().getRealPath(""));
97 //org.greenstone.util.GlobalProperties.loadGlobalProperties("/home/osboxes/research/code/greenstone3-svn/web");
98 java.io.InputStream in = Class.forName("org.greenstone.util.GlobalProperties").getClassLoader().getResourceAsStream("global.properties");
99
100 //String gsdl3_writablehome = System.getProperty("gsdl3.writablehome");
101 String tomcat_context = System.getProperty("tomcat.context");
102 System.err.println("*** in = " + in);
103 //System.err.println("*** gsdl3.writablehome = " + gsdl3_writablehome);
104 System.err.println("*** tomcat.context = " + tomcat_context);
105
106 // the receptionist -the servlet will talk to this
107 this.recept = new IIIFReceptionist();
108
109 //this site_name could consist of comma separated more than one site name.
110 IIIFMessageRouter message_router = new IIIFMessageRouter();
111
112 message_router.setSiteName(site_name);
113 // lots of work is done in this step; see IIIFMessageRouter.java
114 if (!message_router.configure()) {
115 throw new UnavailableException("IIIFServerBridge: Couldn't configure IIIFMessageRouter");
116 }
117 this.recept.setSiteName(site_name);
118 this.recept.setMessageRouter(message_router);
119
120 configure();
121 } // end of init()
122
123 public void remote_init(String remote_site_name, String remote_site_type, String remote_site_address) throws UnavailableException
124 {
125 if (remote_site_name == null || remote_site_type == null || remote_site_address == null)
126 {
127 logger.error("Initialisation paramters not all set!");
128 logger.error("You must have remote_site_name, remote_site_type and remote_site_address set");
129 throw new UnavailableException("IIIFServerBridge: incorrect remote connection parameters specified");
130 }
131
132 // the receptionist -the servlet will talk to this
133 this.recept = new IIIFReceptionist();
134
135 // talking to a remote site, create a communicator
136 Communicator communicator = null;
137 // we need to create the XML to configure the communicator
138 Document site_doc = XMLConverter.newDOM();
139 Element site_elem = site_doc.createElement(GSXML.SITE_ELEM);
140 site_elem.setAttribute(GSXML.TYPE_ATT, remote_site_type);
141 site_elem.setAttribute(GSXML.NAME_ATT, remote_site_name);
142 site_elem.setAttribute(GSXML.ADDRESS_ATT, remote_site_address);
143
144 if (remote_site_type.equals(GSXML.COMM_TYPE_SOAP_JAVA))
145 {
146 communicator = new SOAPCommunicator();
147 }
148 else
149 {
150 logger.error("IIIFServerBridge.init Error: invalid Communicator type: " + remote_site_type);
151 throw new UnavailableException("IIIFServerBridge: invalid communicator type");
152 }
153
154 if (!communicator.configure(site_elem))
155 {
156 logger.error("IIIFServerBridge.init Error: Couldn't configure communicator");
157 throw new UnavailableException("IIIFServerBridge: Couldn't configure communicator");
158 }
159 this.recept.setSiteName(remote_site_name);
160 this.recept.setMessageRouter(communicator);
161
162 configure();
163 } // end of remote_init()
164
165
166 public String doGetDocumentMessage(String identifier)
167 {
168 // oai always requires the content type be text/xml
169 //request.setCharacterEncoding("UTF-8");
170 //response.setContentType("text/xml;charset=UTF-8");
171 //PrintWriter out = response.getWriter();
172
173 String[] pairs = new String[2];
174 pairs[0] = "verb=GetRecord";
175 pairs[1] = "identifier="+identifier;
176
177 String verb = "GetRecord";
178 Document response_doc = XMLConverter.newDOM();
179 Element xml_response = IIIFXML.createBasicResponse(response_doc, verb, pairs);
180 Element verb_elem = null;
181
182 // compose the request message to the receptionist
183 Document request_doc = XMLConverter.newDOM();
184 Element xml_message = request_doc.createElement(GSXML.MESSAGE_ELEM);
185 Element xml_request = request_doc.createElement(GSXML.REQUEST_ELEM);
186 // The type attribute is set to be 'oaiService' from OAIServer to OAIReceptionist.
187 ////xml_request.setAttribute(GSXML.TYPE_ATT, OAIXML.OAI_SERVICE);
188 //xml_request.setAttribute(GSXML.LANG_ATT, lang);
189 xml_request.setAttribute(GSXML.TO_ATT, verb);
190 addParams(xml_request, pairs);
191
192 //xml_request.setAttribute(GSXML.OUTPUT_ATT, output);????
193 xml_message.appendChild(xml_request);
194
195 Node xml_result = this.recept.process(xml_message);
196 if (xml_result == null)
197 {
198 logger.info("xml_result is null");
199 verb_elem = IIIFXML.createErrorElement(response_doc, "Internal error", "");
200 xml_response.appendChild(verb_elem);
201 }
202 else
203 {
204
205 //
206 // All response elements are in the form (with a corresponding verb
207 // name): <message> <response> <verb> ... <resumptionToken> .. this
208 // is optional! </resumptionToken> </verb> </response> </message>
209 //
210 Node res = GSXML.getChildByTagName(xml_result, GSXML.RESPONSE_ELEM);
211 if (res == null)
212 {
213 logger.info("response element in xml_result is null");
214 verb_elem = IIIFXML.createErrorElement(response_doc, "Internal error", "");
215 }
216 else
217 {
218 verb_elem = GSXML.getFirstElementChild(res);
219 }
220
221 // ******
222 xml_response.appendChild(response_doc.importNode(verb_elem, true));
223 /*
224 if ( verb_elem.getTagName().equals(IIIFXML.ERROR))
225 {
226 xml_response.appendChild(response_doc.importNode(verb_elem, true));
227 }
228 else if (IIIFXML.iiif_version.equals(IIIFXML.IIIF_VERSION2)) {
229 xml_response.appendChild(response_doc.importNode(verb_elem, true));
230 }
231 else
232 {
233 GSXML.copyAllChildren(xml_response, verb_elem);
234 }*/
235 }
236 /*
237 out.println("<?xml version='1.0' encoding='UTF-8' ?>");
238 if (this.use_oai_stylesheet)
239 {
240 out.println("<?xml-stylesheet type='text/xsl' href='" + this.oai_stylesheet + "' ?>\n");
241 }
242 out.println(XMLConverter.getPrettyString(xml_response));
243 */
244
245 return XMLConverter.getPrettyString(xml_response);
246 }
247
248 /** append parameter elements to the request sent to the receptionist */
249 public void addParams(Element request, String[] pairs)
250 {
251 Document doc = request.getOwnerDocument();
252 // no params apart from the verb
253 if (pairs == null || pairs.length < 2)
254 return;
255
256 /**
257 * the request xml is composed in the form: <request> <param name=.../>
258 * <param name=.../> </request> (No paramList element in between).
259 */
260 for (int i = 1; i < pairs.length; i++)
261 {
262 //the first pair in pairs is the verb=xxx
263 int index = pairs[i].indexOf("=");
264 if (index != -1)
265 { //just a double check
266 Element param = GSXML.createParameter(doc, pairs[i].substring(0, index), IIIFXML.oaiDecode(pairs[i].substring(index + 1)));
267 request.appendChild(param);
268 }
269 }
270 }
271
272
273 public void destroy()
274 {
275 recept.cleanUp();
276 }
277
278}
Note: See TracBrowser for help on using the repository browser.