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 | */
|
---|
19 | package org.greenstone.gsdl3;
|
---|
20 |
|
---|
21 | import java.io.IOException;
|
---|
22 | import java.io.PrintWriter;
|
---|
23 | import java.util.HashSet;
|
---|
24 | import java.util.Iterator;
|
---|
25 | import java.util.Map;
|
---|
26 |
|
---|
27 | import javax.servlet.ServletConfig;
|
---|
28 | import javax.servlet.ServletException;
|
---|
29 | import javax.servlet.UnavailableException;
|
---|
30 | import javax.servlet.http.HttpServletRequest;
|
---|
31 | import javax.servlet.http.HttpServletResponse;
|
---|
32 |
|
---|
33 | import org.apache.log4j.Logger;
|
---|
34 | import org.greenstone.gsdl3.comms.Communicator;
|
---|
35 | import org.greenstone.gsdl3.comms.SOAPCommunicator;
|
---|
36 | import org.greenstone.gsdl3.core.IIIFMessageRouter;
|
---|
37 | import org.greenstone.gsdl3.core.IIIFReceptionist;
|
---|
38 | import org.greenstone.gsdl3.util.GSConstants;
|
---|
39 | import org.greenstone.gsdl3.util.GSParams;
|
---|
40 | import org.greenstone.gsdl3.util.GSXML;
|
---|
41 | import org.greenstone.gsdl3.util.IIIFXML;
|
---|
42 | import org.greenstone.gsdl3.util.XMLConverter;
|
---|
43 | import org.w3c.dom.Document;
|
---|
44 | import org.w3c.dom.Element;
|
---|
45 | import 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 | */
|
---|
58 | public 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 | String result = "";
|
---|
173 |
|
---|
174 | String[] pairs = new String[2];
|
---|
175 | pairs[0] = "verb=GetRecord";
|
---|
176 | pairs[1] = "identifier="+identifier;
|
---|
177 |
|
---|
178 | String verb = "GetRecord";
|
---|
179 | Document response_doc = XMLConverter.newDOM();
|
---|
180 | Element xml_response = IIIFXML.createBasicResponse(response_doc, verb, pairs);
|
---|
181 | Element verb_elem = null;
|
---|
182 |
|
---|
183 | // compose the request message to the receptionist
|
---|
184 | Document request_doc = XMLConverter.newDOM();
|
---|
185 | Element xml_message = request_doc.createElement(GSXML.MESSAGE_ELEM);
|
---|
186 | Element xml_request = request_doc.createElement(GSXML.REQUEST_ELEM);
|
---|
187 | // The type attribute is set to be 'oaiService' from OAIServer to OAIReceptionist.
|
---|
188 | ////xml_request.setAttribute(GSXML.TYPE_ATT, OAIXML.OAI_SERVICE); // ****
|
---|
189 | //xml_request.setAttribute(GSXML.LANG_ATT, lang);
|
---|
190 | xml_request.setAttribute(GSXML.TO_ATT, verb);
|
---|
191 | addParams(xml_request, pairs);
|
---|
192 |
|
---|
193 | //xml_request.setAttribute(GSXML.OUTPUT_ATT, output);????
|
---|
194 | xml_message.appendChild(xml_request);
|
---|
195 |
|
---|
196 | Node xml_result = this.recept.process(xml_message);
|
---|
197 | if (xml_result == null)
|
---|
198 | {
|
---|
199 | logger.info("xml_result is null");
|
---|
200 | verb_elem = IIIFXML.createErrorElement(response_doc, "Internal error", "");
|
---|
201 | xml_response.appendChild(verb_elem);
|
---|
202 | }
|
---|
203 | else
|
---|
204 | {
|
---|
205 |
|
---|
206 | //
|
---|
207 | // All response elements are in the form (with a corresponding verb
|
---|
208 | // name): <message> <response> <verb> ... <resumptionToken> .. this
|
---|
209 | // is optional! </resumptionToken> </verb> </response> </message>
|
---|
210 | //
|
---|
211 | Node res = GSXML.getChildByTagName(xml_result, GSXML.RESPONSE_ELEM);
|
---|
212 | if (res == null)
|
---|
213 | {
|
---|
214 | logger.info("response element in xml_result is null");
|
---|
215 | verb_elem = IIIFXML.createErrorElement(response_doc, "Internal error", "");
|
---|
216 | }
|
---|
217 | else {
|
---|
218 | System.err.println("*** res (verb_elem) = " + XMLConverter.getPrettyString(res));
|
---|
219 |
|
---|
220 | verb_elem = GSXML.getFirstElementChild(res); // GetRecord
|
---|
221 | Node record_node = GSXML.getFirstElementChild(verb_elem); // record
|
---|
222 | Element metadata_list_elem = (Element)GSXML.getChildByTagName(record_node,"metadata"); // metadata
|
---|
223 |
|
---|
224 | System.err.println("*** metadata_list_elem = " + XMLConverter.getPrettyString(metadata_list_elem));
|
---|
225 |
|
---|
226 | Element assocfilepath_metadata_elem = (Element)GSXML.getChildByTagName(metadata_list_elem,"assocfilepath");
|
---|
227 | String assocfilepath_metadata_val = GSXML.getNodeText(assocfilepath_metadata_elem);
|
---|
228 |
|
---|
229 | Element image_metadata_elem = (Element)GSXML.getChildByTagName(metadata_list_elem,"Image");
|
---|
230 | String image_metadata_val = GSXML.getNodeText(image_metadata_elem);
|
---|
231 |
|
---|
232 | result = assocfilepath_metadata_val + "/" + image_metadata_val;
|
---|
233 | }
|
---|
234 |
|
---|
235 | // ******
|
---|
236 | xml_response.appendChild(response_doc.importNode(verb_elem, true));
|
---|
237 | /*
|
---|
238 | if ( verb_elem.getTagName().equals(IIIFXML.ERROR))
|
---|
239 | {
|
---|
240 | xml_response.appendChild(response_doc.importNode(verb_elem, true));
|
---|
241 | }
|
---|
242 | else if (IIIFXML.iiif_version.equals(IIIFXML.IIIF_VERSION2)) {
|
---|
243 | xml_response.appendChild(response_doc.importNode(verb_elem, true));
|
---|
244 | }
|
---|
245 | else
|
---|
246 | {
|
---|
247 | GSXML.copyAllChildren(xml_response, verb_elem);
|
---|
248 | }*/
|
---|
249 | }
|
---|
250 | /*
|
---|
251 | out.println("<?xml version='1.0' encoding='UTF-8' ?>");
|
---|
252 | if (this.use_oai_stylesheet)
|
---|
253 | {
|
---|
254 | out.println("<?xml-stylesheet type='text/xsl' href='" + this.oai_stylesheet + "' ?>\n");
|
---|
255 | }
|
---|
256 | out.println(XMLConverter.getPrettyString(xml_response));
|
---|
257 | */
|
---|
258 |
|
---|
259 | //return XMLConverter.getPrettyString(xml_response);
|
---|
260 | return result;
|
---|
261 | //return xml_response;
|
---|
262 | }
|
---|
263 |
|
---|
264 | /** append parameter elements to the request sent to the receptionist */
|
---|
265 | public void addParams(Element request, String[] pairs)
|
---|
266 | {
|
---|
267 | Document doc = request.getOwnerDocument();
|
---|
268 | // no params apart from the verb
|
---|
269 | if (pairs == null || pairs.length < 2)
|
---|
270 | return;
|
---|
271 |
|
---|
272 | /**
|
---|
273 | * the request xml is composed in the form: <request> <param name=.../>
|
---|
274 | * <param name=.../> </request> (No paramList element in between).
|
---|
275 | */
|
---|
276 | for (int i = 1; i < pairs.length; i++)
|
---|
277 | {
|
---|
278 | //the first pair in pairs is the verb=xxx
|
---|
279 | int index = pairs[i].indexOf("=");
|
---|
280 | if (index != -1)
|
---|
281 | { //just a double check
|
---|
282 | Element param = GSXML.createParameter(doc, pairs[i].substring(0, index), IIIFXML.iiifDecode(pairs[i].substring(index + 1)));
|
---|
283 | request.appendChild(param);
|
---|
284 | }
|
---|
285 | }
|
---|
286 | }
|
---|
287 |
|
---|
288 |
|
---|
289 | public void destroy()
|
---|
290 | {
|
---|
291 | recept.cleanUp();
|
---|
292 | }
|
---|
293 |
|
---|
294 | }
|
---|