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

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

Shift from OAI as a template, to separate IIIF based classes

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