source: main/trunk/greenstone3/src/java/org/greenstone/gsdl3/OAIServer.java@ 37518

Last change on this file since 37518 was 35362, checked in by kjdon, 3 years ago

Class.newInstance() is deprecated. Use getDeclaredConstructor().newInstance() instead

File size: 18.4 KB
Line 
1/*
2 * OAIServer.java
3 * Copyright (C) 2002 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.OAIMessageRouter;
37import org.greenstone.gsdl3.core.OAIReceptionist;
38import org.greenstone.gsdl3.util.GSConstants;
39import org.greenstone.gsdl3.util.GSParams;
40import org.greenstone.gsdl3.util.GSXML;
41import org.greenstone.gsdl3.util.OAIResumptionToken;
42import org.greenstone.gsdl3.util.OAIXML;
43import org.greenstone.gsdl3.util.XMLConverter;
44import org.w3c.dom.Document;
45import org.w3c.dom.Element;
46import org.w3c.dom.Node;
47
48/** a servlet to serve the OAI metadata harvesting - we are using servlets instead
49 * of cgi
50 * the init method is called only once - the first time the servlet classes
51 * are loaded. Each time a request comes in to the servlet, the session()
52 * method is called in a new thread (calls doGet/doPut etc)
53 * takes the verb= type args and builds a simple request to send to
54 * the oai receptionist, which returns a result in xml, conforming to the OAI-PMH
55 * protocol.
56 * @see Receptionist
57 */
58/**
59 * OAI server configuration instructions *
60 *
61 */
62public class OAIServer extends BaseGreenstoneServlet
63{
64
65 /** the receptionist to send messages to */
66 protected OAIReceptionist recept = null;
67 /**
68 * the default language - is specified by setting a servlet param, otherwise
69 * DEFAULT_LANG is used
70 */
71 protected String default_lang = null;
72 /**
73 * The default default - used if a default lang is not specified in the
74 * servlet params
75 */
76 protected final String DEFAULT_LANG = "en";
77
78 /** A HashSet which contains all the legal verbs. */
79 protected HashSet<String> verb_set = null;
80 /**
81 * A HashSet which contains all the legal oai keys in the key/value argument
82 * pair.
83 */
84 protected HashSet<String> param_set = null;
85 /**
86 * The name of the site with which we will finally be dealing, whether it is
87 * a local site or a remote site through a communicator.
88 */
89 protected String site = "";
90
91 // can be overriddden in OAIConfig.xml
92 // do we output the stylesheet processing instruction?
93 protected boolean use_oai_stylesheet = true;
94 protected String oai_stylesheet = "interfaces/oai/oai2.xsl";
95
96 // there is no getQueryString() method in the HttpServletRequest returned from doPost,
97 // since that is actually of type apache RequestFacade, and doesn't define such a method
98 protected String queryString = null;
99
100 static Logger logger = Logger.getLogger(org.greenstone.gsdl3.OAIServer.class.getName());
101
102 /**
103 * initialise the servlet
104 */
105 public void init(ServletConfig config) throws ServletException
106 {
107 // always call super.init, i.e., HttpServlet.;
108 super.init(config);
109 this.default_lang = config.getInitParameter(GSConstants.DEFAULT_LANG);
110 String servlet_url_name=config.getInitParameter("servlet_url");
111
112 initVerbs();
113 initParams();
114
115 String site_name = config.getInitParameter(GSConstants.SITE_NAME);
116 String remote_site_name = null;
117 String remote_site_type = null;
118 String remote_site_address = null;
119
120 if (site_name == null)
121 {
122 // no local site, try for communicator (remote site)
123 remote_site_name = config.getInitParameter("remote_site_name");
124 remote_site_type = config.getInitParameter("remote_site_type");
125 remote_site_address = config.getInitParameter("remote_site_address");
126 if (remote_site_name == null || remote_site_type == null || remote_site_address == null)
127 {
128 logger.error("initialisation paramters not all set!");
129 logger.error("if site_name is not set, then you must have remote_site_name, remote_site_type and remote_site_address set");
130 throw new UnavailableException("OAIServer: incorrect servlet parameters");
131 }
132 }
133
134 if (this.default_lang == null)
135 {
136 // choose english
137 this.default_lang = DEFAULT_LANG;
138 }
139
140
141 // Read in OAIConfig-xxx.xml (residing web/WEB-INF/classes/) and
142 //use it to configure the receptionist. And pass it to MR
143 Element oai_config = OAIXML.getOAIConfigXML(servlet_url_name);
144 if (oai_config == null)
145 {
146 logger.error("Fail to parse oai config file OAIConfig-"+servlet_url_name+".xml");
147 throw new UnavailableException("OAIServer: Couldn't parse OAIConfig-"+servlet_url_name+".xml");
148 }
149
150 // the receptionist -the servlet will talk to this
151 this.recept = new OAIReceptionist();
152
153 // the receptionist uses a OAIMessageRouter or Communicator to send its requests to. We either create a OAIMessageRouter here for the designated site (if site_name set), or we create a Communicator for a remote site. The is given to the Receptionist, and the servlet never talks to it again directly.
154 if (site_name != null)
155 {
156 //this site_name could consist of comma separated more than one site name.
157 String mr_name = (String) config.getInitParameter("messagerouter_class");
158 OAIMessageRouter message_router = null;
159 if (mr_name == null)
160 { // just use the normal MR *********
161 message_router = new OAIMessageRouter();
162 }
163 else
164 { // try the specified one
165 try
166 {
167 message_router = (OAIMessageRouter) Class.forName("org.greenstone.gsdl3.core." + mr_name).getDeclaredConstructor().newInstance();
168 }
169 catch (Exception e)
170 { // cant use this new one, so use normal one
171 logger.error("OAIServlet configure exception when trying to use a new OAIMessageRouter " + mr_name, e);
172 message_router = new OAIMessageRouter();
173 }
174 }
175
176 message_router.setSiteName(site_name);
177 // lots of work is done in this step; see OAIMessageRouter.java
178 message_router.setOAIConfig(oai_config);
179 if (!message_router.configure()) {
180 throw new UnavailableException("OAIServer: Couldn't configure OAIMessageRouter");
181 }
182 this.recept.setSiteName(site_name);
183 this.recept.setMessageRouter(message_router);
184
185 }
186 else
187 {
188 // talking to a remote site, create a communicator
189 Communicator communicator = null;
190 // we need to create the XML to configure the communicator
191 Document site_doc = XMLConverter.newDOM();
192 Element site_elem = site_doc.createElement(GSXML.SITE_ELEM);
193 site_elem.setAttribute(GSXML.TYPE_ATT, remote_site_type);
194 site_elem.setAttribute(GSXML.NAME_ATT, remote_site_name);
195 site_elem.setAttribute(GSXML.ADDRESS_ATT, remote_site_address);
196
197 if (remote_site_type.equals(GSXML.COMM_TYPE_SOAP_JAVA))
198 {
199 communicator = new SOAPCommunicator();
200 }
201 else
202 {
203 logger.error("OAIServlet.init Error: invalid Communicator type: " + remote_site_type);
204 throw new UnavailableException("OAIServer: invalid communicator type");
205 }
206
207 if (!communicator.configure(site_elem))
208 {
209 logger.error("OAIServlet.init Error: Couldn't configure communicator");
210 throw new UnavailableException("OAIServer: Couldn't configure communicator");
211 }
212 this.recept.setSiteName(remote_site_name);
213 this.recept.setMessageRouter(communicator);
214 }
215 // pass it to the receptionist
216 if (!this.recept.configure(oai_config)) {
217 logger.error("Couldn't configure receptionist");
218 throw new UnavailableException("OAIServer: Couldn't configure receptionist");
219 }
220 // also, we have something we want to get from here - useOAIStylesheet
221 this.configure(oai_config);
222
223 }//end of init()
224
225 private void configure(Element oai_config)
226 {
227 Element use_stylesheet_elem = (Element) GSXML.getChildByTagName(oai_config, OAIXML.USE_STYLESHEET);
228 if (use_stylesheet_elem != null)
229 {
230 String value = GSXML.getNodeText(use_stylesheet_elem);
231 if (value.equals("no"))
232 {
233 this.use_oai_stylesheet = false;
234 }
235 }
236 if (this.use_oai_stylesheet)
237 {
238 // now see if there is a custom stylesheet specified
239 Element stylesheet_elem = (Element) GSXML.getChildByTagName(oai_config, OAIXML.STYLESHEET);
240 if (stylesheet_elem != null)
241 {
242 String value = GSXML.getNodeText(stylesheet_elem);
243 if (!value.equals(""))
244 {
245 oai_stylesheet = value;
246 }
247 }
248
249 }
250 }
251
252 private void initVerbs()
253 {
254 verb_set = new HashSet<String>();
255 verb_set.add(OAIXML.GET_RECORD);
256 verb_set.add(OAIXML.LIST_RECORDS);
257 verb_set.add(OAIXML.LIST_IDENTIFIERS);
258 verb_set.add(OAIXML.LIST_SETS);
259 verb_set.add(OAIXML.LIST_METADATA_FORMATS);
260 verb_set.add(OAIXML.IDENTIFY);
261 }
262
263 private void initParams()
264 {
265 param_set = new HashSet<String>();
266 param_set.add(OAIXML.METADATA_PREFIX);
267 param_set.add(OAIXML.FROM);
268 param_set.add(OAIXML.UNTIL);
269 param_set.add(OAIXML.SET);
270 param_set.add(OAIXML.RESUMPTION_TOKEN);
271 param_set.add(OAIXML.IDENTIFIER);
272 }
273
274 private void logUsageInfo(HttpServletRequest request)
275 {
276 String usageInfo = "";
277
278 String query = (queryString == null) ? request.getQueryString() : queryString;
279
280 //logged info = general-info + session-info
281 usageInfo = request.getContextPath() + " " + //session id
282 request.getServletPath() + " " + //serlvet
283 "[" + query + "]" + " " + //the query string
284 "[" + usageInfo.trim() + "]" + " " + // params stored in a session
285 request.getRemoteAddr() + " " + //remote address
286 request.getHeader("user-agent") + " "; //the remote brower info
287
288 logger.info(usageInfo);
289 }
290
291 /**
292 * return true if the url is in the form of baseURL?verb=...,
293 */
294 private boolean validate(String query, String verb)
295 {
296 //Here in OAIServer, only the verbs are validated. All the validation for individual verb
297 // is taken in their doXXX() methods.
298 if (query == null || !query.startsWith(OAIXML.VERB + "="))
299 {
300 return false;
301 }
302 if (!verb_set.contains(verb))
303 {
304 return false;
305 }
306 return true;
307 }
308
309 private String getVerb(String query)
310 {
311 if (query == null)
312 return "";
313 int verb_start_index = query.indexOf("=") + 1;// first occurence of '='
314 int verb_end_index = query.indexOf("&");
315 if (verb_end_index == -1)
316 {
317 return query.substring(verb_start_index);
318 }
319 return query.substring(verb_start_index, verb_end_index);
320 }
321
322 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
323 {
324 logUsageInfo(request);
325
326 // oai always requires the content type be text/xml
327 request.setCharacterEncoding("UTF-8");
328 response.setContentType("text/xml;charset=UTF-8");
329 PrintWriter out = response.getWriter();
330
331 //
332 String lang = request.getParameter(GSParams.LANGUAGE);
333 if (lang == null || lang.equals(""))
334 {
335 // use the default
336 lang = this.default_lang;
337 }
338 //we don't get the baseURL from the http request because what we get might be different from the one known publicly due to local network redirection.
339 //For example, puka.cs.waikato.ac.nz vs www.greenstone.org
340 //String base_url = request.getRequestURL().toString();
341
342 // if called by doPost (if this was originally a POST request), var queryString would have been set
343 String query = (queryString == null) ? request.getQueryString() : queryString;
344 queryString = null; // reset member variable, else no doGet will work as long as the server remains running
345
346 if (query!=null && query.equals("reset")) {
347 logger.info("reset was called*******************");
348 out.println("<?xml version='1.0' encoding='UTF-8' ?>");
349 out.println(this.recept.process("<message><request reset='true'/></message>"));
350 return;
351 }
352 String[] pairs = (query == null) ? null : query.split("&");//split into key/value pairs
353
354 // besides "reset", the only other non-verb (non-OAI) requests allowed would be: (de)activate="collName"
355 if(pairs != null && pairs.length == 1) {
356 String command = pairs[0];
357 int index = command.indexOf('=');
358 if(index != -1) {
359 String collName = command.substring(index+1);
360 command = command.substring(0, index);
361 if(command.equals(GSXML.SYSTEM_TYPE_ACTIVATE)) {
362 logger.info("activating OAI collection " + collName + " was called*******************");
363 out.println("<?xml version='1.0' encoding='UTF-8' ?>");
364 out.println(this.recept.process("<message><request " + GSXML.SYSTEM_TYPE_ACTIVATE+"='"+collName+"'/></message>"));
365 return;
366
367 } else if(command.equals(GSXML.SYSTEM_TYPE_DEACTIVATE)) {
368 logger.info("deactivating OAI collection " + collName + " was called*******************");
369 out.println("<?xml version='1.0' encoding='UTF-8' ?>");
370 out.println(this.recept.process("<message><request " + GSXML.SYSTEM_TYPE_DEACTIVATE+"='"+collName+"'/></message>"));
371 return;
372 }
373 }
374 // any other format for activate/deactivate command in query is wrong, continue processing and fail with "badVerb" message:
375 }
376
377
378 String verb = getVerb(query);
379 Document response_doc = XMLConverter.newDOM();
380 Element xml_response = OAIXML.createBasicResponse(response_doc, verb, pairs);
381 Element verb_elem = null;
382
383 if (validate(query, verb) == false)
384 {
385 if (verb_set.contains(verb) == false)
386 {
387 logger.error(OAIXML.BAD_VERB + ": " + query);
388 verb_elem = OAIXML.createErrorElement(response_doc, OAIXML.BAD_VERB, OAIXML.ILLEGAL_OAI_VERB);
389 }
390 else
391 {
392 //must be something else other than bad verbs caused an error, so bad argument
393 logger.error(OAIXML.BAD_ARGUMENT + ": " + query);
394 verb_elem = OAIXML.createErrorElement(response_doc, OAIXML.BAD_ARGUMENT, "");
395 }
396 xml_response.appendChild(verb_elem);
397
398 out.println("<?xml version='1.0' encoding='UTF-8' ?>");
399 if (this.use_oai_stylesheet)
400 {
401 out.println("<?xml-stylesheet type='text/xsl' href='" + this.oai_stylesheet + "' ?>\n");
402 }
403 out.println(XMLConverter.getPrettyString(xml_response));
404 return;
405 }//end of if(validate
406
407 // The query is valid, we can now
408 // compose the request message to the receptionist
409 Document request_doc = XMLConverter.newDOM();
410 Element xml_message = request_doc.createElement(GSXML.MESSAGE_ELEM);
411 Element xml_request = request_doc.createElement(GSXML.REQUEST_ELEM);
412 // The type attribute is set to be 'oaiService' from OAIServer to OAIReceptionist.
413 //xml_request.setAttribute(GSXML.TYPE_ATT, OAIXML.OAI_SERVICE);
414 xml_request.setAttribute(GSXML.LANG_ATT, lang);
415 xml_request.setAttribute(GSXML.TO_ATT, verb);
416 addParams(xml_request, pairs);
417
418 //xml_request.setAttribute(GSXML.OUTPUT_ATT, output);????
419 xml_message.appendChild(xml_request);
420
421 Node xml_result = this.recept.process(xml_message);
422 if (xml_result == null)
423 {
424 logger.info("xml_result is null");
425 verb_elem = OAIXML.createErrorElement(response_doc, "Internal error", "");
426 xml_response.appendChild(verb_elem);
427 }
428 else
429 {
430
431 /**
432 * All response elements are in the form (with a corresponding verb
433 * name): <message> <response> <verb> ... <resumptionToken> .. this
434 * is optional! </resumptionToken> </verb> </response> </message>
435 */
436 Node res = GSXML.getChildByTagName(xml_result, GSXML.RESPONSE_ELEM);
437 if (res == null)
438 {
439 logger.info("response element in xml_result is null");
440 verb_elem = OAIXML.createErrorElement(response_doc, "Internal error", "");
441 }
442 else
443 {
444 verb_elem = GSXML.getFirstElementChild(res);
445 }
446
447 if ( verb_elem.getTagName().equals(OAIXML.ERROR))
448 {
449 xml_response.appendChild(response_doc.importNode(verb_elem, true));
450 }
451 else if (OAIXML.oai_version.equals(OAIXML.OAI_VERSION2)) {
452 xml_response.appendChild(response_doc.importNode(verb_elem, true));
453 }
454 else
455 {
456 GSXML.copyAllChildren(xml_response, verb_elem);
457 }
458 }
459 out.println("<?xml version='1.0' encoding='UTF-8' ?>");
460 if (this.use_oai_stylesheet)
461 {
462 out.println("<?xml-stylesheet type='text/xsl' href='" + this.oai_stylesheet + "' ?>\n");
463 }
464 out.println(XMLConverter.getPrettyString(xml_response));
465 return;
466 }
467
468 /** append parameter elements to the request sent to the receptionist */
469 public void addParams(Element request, String[] pairs)
470 {
471 Document doc = request.getOwnerDocument();
472 // no params apart from the verb
473 if (pairs == null || pairs.length < 2)
474 return;
475
476 /**
477 * the request xml is composed in the form: <request> <param name=.../>
478 * <param name=.../> </request> (No paramList element in between).
479 */
480 for (int i = 1; i < pairs.length; i++)
481 {
482 //the first pair in pairs is the verb=xxx
483 int index = pairs[i].indexOf("=");
484 if (index != -1)
485 { //just a double check
486 Element param = GSXML.createParameter(doc, pairs[i].substring(0, index), OAIXML.oaiDecode(pairs[i].substring(index + 1)));
487 request.appendChild(param);
488 }
489 }
490 }
491
492 // For OAI version 2.0, validation tests indicated that POST needs to be supported. Some
493 // modification was required in order to ensure that the request is passed intact to doGet()
494 public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
495 {
496
497 // the post method returns a wrapper of type RequestFacade by apache and there
498 // is no getQueryString() method defined for it. Therefore, need to work this out
499 // manually before calling doGet(request, response) so that doGet can work as before.
500
501 queryString = "";
502 Iterator parameter_entries = request.getParameterMap().entrySet().iterator();
503 while (parameter_entries.hasNext())
504 {
505 Map.Entry param_entry = (Map.Entry) parameter_entries.next();
506 String[] paramVals = (String[]) param_entry.getValue();
507 if (paramVals != null)
508 {
509 if (paramVals.length > 0)
510 {
511 logger.error("POST request received: " + param_entry.getKey() + " - " + paramVals[0]);
512 queryString = queryString + "&" + param_entry.getKey() + "=" + paramVals[0];
513 }
514 }
515 }
516 if (queryString.length() > 0)
517 {
518 queryString = queryString.substring(1);
519 //queryString = OAIXML.oaiEncode(queryString);
520 }
521 if (queryString.equals(""))
522 {
523 queryString = null;
524 }
525 doGet(request, response);
526 }
527
528
529 public void destroy()
530 {
531 recept.cleanUp();
532 }
533
534}
Note: See TracBrowser for help on using the repository browser.