[14207] | 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 | */
|
---|
| 19 | package org.greenstone.gsdl3;
|
---|
| 20 |
|
---|
[25717] | 21 | import java.io.IOException;
|
---|
| 22 | import java.io.PrintWriter;
|
---|
[14207] | 23 | import java.util.HashSet;
|
---|
[23913] | 24 | import java.util.Iterator;
|
---|
| 25 | import java.util.Map;
|
---|
[14207] | 26 |
|
---|
[25717] | 27 | import javax.servlet.ServletConfig;
|
---|
| 28 | import javax.servlet.ServletException;
|
---|
[28852] | 29 | import javax.servlet.UnavailableException;
|
---|
[25717] | 30 | import javax.servlet.http.HttpServletRequest;
|
---|
| 31 | import javax.servlet.http.HttpServletResponse;
|
---|
[14207] | 32 |
|
---|
[25717] | 33 | import org.apache.log4j.Logger;
|
---|
| 34 | import org.greenstone.gsdl3.comms.Communicator;
|
---|
| 35 | import org.greenstone.gsdl3.comms.SOAPCommunicator;
|
---|
[28985] | 36 | import org.greenstone.gsdl3.core.OAIMessageRouter;
|
---|
[25717] | 37 | import org.greenstone.gsdl3.core.OAIReceptionist;
|
---|
| 38 | import org.greenstone.gsdl3.util.GSConstants;
|
---|
| 39 | import org.greenstone.gsdl3.util.GSParams;
|
---|
| 40 | import org.greenstone.gsdl3.util.GSXML;
|
---|
[28854] | 41 | import org.greenstone.gsdl3.util.OAIResumptionToken;
|
---|
[25717] | 42 | import org.greenstone.gsdl3.util.OAIXML;
|
---|
| 43 | import org.greenstone.gsdl3.util.XMLConverter;
|
---|
| 44 | import org.w3c.dom.Document;
|
---|
| 45 | import org.w3c.dom.Element;
|
---|
| 46 | import org.w3c.dom.Node;
|
---|
| 47 |
|
---|
[14207] | 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 *
|
---|
[25717] | 60 | *
|
---|
[14207] | 61 | */
|
---|
[25717] | 62 | public class OAIServer extends BaseGreenstoneServlet
|
---|
| 63 | {
|
---|
[14207] | 64 |
|
---|
[25717] | 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";
|
---|
[21779] | 77 |
|
---|
[25717] | 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 = "";
|
---|
[14207] | 90 |
|
---|
[28852] | 91 | // can be overriddden in OAIConfig.xml
|
---|
[25717] | 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 |
|
---|
| 111 | initVerbs();
|
---|
| 112 | initParams();
|
---|
| 113 |
|
---|
| 114 | String site_name = config.getInitParameter(GSConstants.SITE_NAME);
|
---|
| 115 | String remote_site_name = null;
|
---|
| 116 | String remote_site_type = null;
|
---|
| 117 | String remote_site_address = null;
|
---|
| 118 |
|
---|
| 119 | if (site_name == null)
|
---|
| 120 | {
|
---|
| 121 | // no local site, try for communicator (remote site)
|
---|
| 122 | remote_site_name = config.getInitParameter("remote_site_name");
|
---|
| 123 | remote_site_type = config.getInitParameter("remote_site_type");
|
---|
| 124 | remote_site_address = config.getInitParameter("remote_site_address");
|
---|
| 125 | if (remote_site_name == null || remote_site_type == null || remote_site_address == null)
|
---|
| 126 | {
|
---|
[28852] | 127 | logger.error("initialisation paramters not all set!");
|
---|
| 128 | logger.error("if site_name is not set, then you must have remote_site_name, remote_site_type and remote_site_address set");
|
---|
| 129 | throw new UnavailableException("OAIServer: incorrect servlet parameters");
|
---|
[25717] | 130 | }
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 | if (this.default_lang == null)
|
---|
| 134 | {
|
---|
| 135 | // choose english
|
---|
| 136 | this.default_lang = DEFAULT_LANG;
|
---|
| 137 | }
|
---|
| 138 |
|
---|
| 139 | // the receptionist -the servlet will talk to this
|
---|
| 140 | this.recept = new OAIReceptionist();
|
---|
[28981] | 141 |
|
---|
[28985] | 142 | // 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 teh Receptionist, and the servlet never talks to it again.directly.
|
---|
[25717] | 143 | if (site_name != null)
|
---|
| 144 | {
|
---|
| 145 | //this site_name could consist of comma separated more than one site name.
|
---|
| 146 | String mr_name = (String) config.getInitParameter("messagerouter_class");
|
---|
[28985] | 147 | OAIMessageRouter message_router = null;
|
---|
[25717] | 148 | if (mr_name == null)
|
---|
| 149 | { // just use the normal MR *********
|
---|
[28985] | 150 | message_router = new OAIMessageRouter();
|
---|
[25717] | 151 | }
|
---|
| 152 | else
|
---|
| 153 | { // try the specified one
|
---|
| 154 | try
|
---|
| 155 | {
|
---|
[28985] | 156 | message_router = (OAIMessageRouter) Class.forName("org.greenstone.gsdl3.core." + mr_name).newInstance();
|
---|
[25717] | 157 | }
|
---|
| 158 | catch (Exception e)
|
---|
| 159 | { // cant use this new one, so use normal one
|
---|
[28985] | 160 | logger.error("OAIServlet configure exception when trying to use a new OAIMessageRouter " + mr_name, e);
|
---|
| 161 | message_router = new OAIMessageRouter();
|
---|
[25717] | 162 | }
|
---|
| 163 | }
|
---|
| 164 |
|
---|
| 165 | message_router.setSiteName(site_name);
|
---|
[28985] | 166 | // lots of work is done in this step; see OAIMessageRouter.java
|
---|
[28852] | 167 | if (!message_router.configure()) {
|
---|
[28985] | 168 | throw new UnavailableException("OAIServer: Couldn't configure OAIMessageRouter");
|
---|
[28852] | 169 | }
|
---|
[25717] | 170 | this.recept.setSiteName(site_name);
|
---|
| 171 | this.recept.setMessageRouter(message_router);
|
---|
| 172 |
|
---|
| 173 | }
|
---|
| 174 | else
|
---|
| 175 | {
|
---|
| 176 | // talking to a remote site, create a communicator
|
---|
| 177 | Communicator communicator = null;
|
---|
| 178 | // we need to create the XML to configure the communicator
|
---|
[28966] | 179 | Document site_doc = XMLConverter.newDOM();
|
---|
[28852] | 180 | Element site_elem = site_doc.createElement(GSXML.SITE_ELEM);
|
---|
[25717] | 181 | site_elem.setAttribute(GSXML.TYPE_ATT, remote_site_type);
|
---|
| 182 | site_elem.setAttribute(GSXML.NAME_ATT, remote_site_name);
|
---|
| 183 | site_elem.setAttribute(GSXML.ADDRESS_ATT, remote_site_address);
|
---|
| 184 |
|
---|
| 185 | if (remote_site_type.equals(GSXML.COMM_TYPE_SOAP_JAVA))
|
---|
| 186 | {
|
---|
| 187 | communicator = new SOAPCommunicator();
|
---|
| 188 | }
|
---|
| 189 | else
|
---|
| 190 | {
|
---|
[28852] | 191 | logger.error("OAIServlet.init Error: invalid Communicator type: " + remote_site_type);
|
---|
| 192 | throw new UnavailableException("OAIServer: invalid communicator type");
|
---|
[25717] | 193 | }
|
---|
| 194 |
|
---|
| 195 | if (!communicator.configure(site_elem))
|
---|
| 196 | {
|
---|
[28852] | 197 | logger.error("OAIServlet.init Error: Couldn't configure communicator");
|
---|
| 198 | throw new UnavailableException("OAIServer: Couldn't configure communicator");
|
---|
[25717] | 199 | }
|
---|
| 200 | this.recept.setSiteName(remote_site_name);
|
---|
| 201 | this.recept.setMessageRouter(communicator);
|
---|
| 202 | }
|
---|
| 203 |
|
---|
| 204 | // Read in OAIConfig.xml (residing web/WEB-INF/classes/) and
|
---|
[28852] | 205 | //use it to configure the receptionist.
|
---|
[25717] | 206 | Element oai_config = OAIXML.getOAIConfigXML();
|
---|
| 207 | if (oai_config == null)
|
---|
| 208 | {
|
---|
| 209 | logger.error("Fail to parse oai config file OAIConfig.xml.");
|
---|
[28852] | 210 | throw new UnavailableException("OAIServer: Couldn't parse OAIConfig.xml");
|
---|
[25717] | 211 | }
|
---|
| 212 | // pass it to the receptionist
|
---|
[28852] | 213 | if (!this.recept.configure(oai_config)) {
|
---|
| 214 | logger.error("Couldn't configure receptionist");
|
---|
| 215 | throw new UnavailableException("OAIServer: Couldn't configure receptionist");
|
---|
| 216 | }
|
---|
[25717] | 217 | // also, we have something we want to get from here - useOAIStylesheet
|
---|
| 218 | this.configure(oai_config);
|
---|
[28852] | 219 |
|
---|
[25717] | 220 | }//end of init()
|
---|
| 221 |
|
---|
| 222 | private void configure(Element oai_config)
|
---|
| 223 | {
|
---|
| 224 | Element use_stylesheet_elem = (Element) GSXML.getChildByTagName(oai_config, OAIXML.USE_STYLESHEET);
|
---|
| 225 | if (use_stylesheet_elem != null)
|
---|
| 226 | {
|
---|
| 227 | String value = GSXML.getNodeText(use_stylesheet_elem);
|
---|
| 228 | if (value.equals("no"))
|
---|
| 229 | {
|
---|
| 230 | this.use_oai_stylesheet = false;
|
---|
| 231 | }
|
---|
| 232 | }
|
---|
| 233 | if (this.use_oai_stylesheet)
|
---|
| 234 | {
|
---|
| 235 | // now see if there is a custom stylesheet specified
|
---|
| 236 | Element stylesheet_elem = (Element) GSXML.getChildByTagName(oai_config, OAIXML.STYLESHEET);
|
---|
| 237 | if (stylesheet_elem != null)
|
---|
| 238 | {
|
---|
| 239 | String value = GSXML.getNodeText(stylesheet_elem);
|
---|
| 240 | if (!value.equals(""))
|
---|
| 241 | {
|
---|
| 242 | oai_stylesheet = value;
|
---|
| 243 | }
|
---|
| 244 | }
|
---|
| 245 |
|
---|
| 246 | }
|
---|
[21779] | 247 | }
|
---|
[23913] | 248 |
|
---|
[25717] | 249 | private void initVerbs()
|
---|
| 250 | {
|
---|
| 251 | verb_set = new HashSet<String>();
|
---|
| 252 | verb_set.add(OAIXML.GET_RECORD);
|
---|
| 253 | verb_set.add(OAIXML.LIST_RECORDS);
|
---|
| 254 | verb_set.add(OAIXML.LIST_IDENTIFIERS);
|
---|
| 255 | verb_set.add(OAIXML.LIST_SETS);
|
---|
| 256 | verb_set.add(OAIXML.LIST_METADATA_FORMATS);
|
---|
| 257 | verb_set.add(OAIXML.IDENTIFY);
|
---|
| 258 | }
|
---|
[23913] | 259 |
|
---|
[25717] | 260 | private void initParams()
|
---|
| 261 | {
|
---|
| 262 | param_set = new HashSet<String>();
|
---|
| 263 | param_set.add(OAIXML.METADATA_PREFIX);
|
---|
| 264 | param_set.add(OAIXML.FROM);
|
---|
| 265 | param_set.add(OAIXML.UNTIL);
|
---|
| 266 | param_set.add(OAIXML.SET);
|
---|
| 267 | param_set.add(OAIXML.RESUMPTION_TOKEN);
|
---|
| 268 | param_set.add(OAIXML.IDENTIFIER);
|
---|
| 269 | }
|
---|
[14207] | 270 |
|
---|
[25717] | 271 | private void logUsageInfo(HttpServletRequest request)
|
---|
| 272 | {
|
---|
| 273 | String usageInfo = "";
|
---|
[23922] | 274 |
|
---|
[25717] | 275 | String query = (queryString == null) ? request.getQueryString() : queryString;
|
---|
[21779] | 276 |
|
---|
[25717] | 277 | //logged info = general-info + session-info
|
---|
| 278 | usageInfo = request.getContextPath() + " " + //session id
|
---|
| 279 | request.getServletPath() + " " + //serlvet
|
---|
| 280 | "[" + query + "]" + " " + //the query string
|
---|
| 281 | "[" + usageInfo.trim() + "]" + " " + // params stored in a session
|
---|
| 282 | request.getRemoteAddr() + " " + //remote address
|
---|
| 283 | request.getHeader("user-agent") + " "; //the remote brower info
|
---|
[23913] | 284 |
|
---|
[25717] | 285 | logger.info(usageInfo);
|
---|
| 286 | }
|
---|
[23913] | 287 |
|
---|
[25717] | 288 | /**
|
---|
| 289 | * return true if the url is in the form of baseURL?verb=...,
|
---|
| 290 | */
|
---|
| 291 | private boolean validate(String query, String verb)
|
---|
| 292 | {
|
---|
| 293 | //Here in OAIServer, only the verbs are validated. All the validation for individual verb
|
---|
| 294 | // is taken in their doXXX() methods.
|
---|
| 295 | if (query == null || !query.startsWith(OAIXML.VERB + "="))
|
---|
| 296 | {
|
---|
| 297 | return false;
|
---|
| 298 | }
|
---|
| 299 | if (!verb_set.contains(verb))
|
---|
| 300 | {
|
---|
| 301 | return false;
|
---|
| 302 | }
|
---|
| 303 | return true;
|
---|
| 304 | }
|
---|
[14207] | 305 |
|
---|
[25717] | 306 | private String getVerb(String query)
|
---|
| 307 | {
|
---|
| 308 | if (query == null)
|
---|
| 309 | return "";
|
---|
| 310 | int verb_start_index = query.indexOf("=") + 1;// first occurence of '='
|
---|
| 311 | int verb_end_index = query.indexOf("&");
|
---|
| 312 | if (verb_end_index == -1)
|
---|
| 313 | {
|
---|
| 314 | return query.substring(verb_start_index);
|
---|
| 315 | }
|
---|
| 316 | return query.substring(verb_start_index, verb_end_index);
|
---|
| 317 | }
|
---|
[14207] | 318 |
|
---|
[25717] | 319 | public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
|
---|
| 320 | {
|
---|
| 321 | logUsageInfo(request);
|
---|
[14207] | 322 |
|
---|
[25717] | 323 | // oai always requires the content type be text/xml
|
---|
| 324 | request.setCharacterEncoding("UTF-8");
|
---|
| 325 | response.setContentType("text/xml;charset=UTF-8");
|
---|
| 326 | PrintWriter out = response.getWriter();
|
---|
[14207] | 327 |
|
---|
[25717] | 328 | //
|
---|
| 329 | String lang = request.getParameter(GSParams.LANGUAGE);
|
---|
| 330 | if (lang == null || lang.equals(""))
|
---|
| 331 | {
|
---|
| 332 | // use the default
|
---|
| 333 | lang = this.default_lang;
|
---|
| 334 | }
|
---|
| 335 | //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.
|
---|
| 336 | //For example, puka.cs.waikato.ac.nz vs www.greenstone.org
|
---|
| 337 | //String base_url = request.getRequestURL().toString();
|
---|
[28884] | 338 |
|
---|
[25717] | 339 | // if called by doPost (if this was originally a POST request), var queryString would have been set
|
---|
| 340 | String query = (queryString == null) ? request.getQueryString() : queryString;
|
---|
| 341 | queryString = null; // reset member variable, else no doGet will work as long as the server remains running
|
---|
[14207] | 342 |
|
---|
[28884] | 343 | if (query!=null && query.equals("reset")) {
|
---|
| 344 | logger.error("reset was called*******************");
|
---|
| 345 | out.println("<?xml version='1.0' encoding='UTF-8' ?>");
|
---|
| 346 | out.println(this.recept.process("<message><request reset='true'/></message>"));
|
---|
| 347 | return;
|
---|
| 348 | }
|
---|
[25717] | 349 | String[] pairs = (query == null) ? null : query.split("&");//split into key/value pairs
|
---|
[28884] | 350 |
|
---|
[25717] | 351 | String verb = getVerb(query);
|
---|
[28966] | 352 | Document response_doc = XMLConverter.newDOM();
|
---|
[28852] | 353 | Element xml_response = OAIXML.createBasicResponse(response_doc, verb, pairs);
|
---|
[25717] | 354 | Element verb_elem = null;
|
---|
[14207] | 355 |
|
---|
[25717] | 356 | if (validate(query, verb) == false)
|
---|
| 357 | {
|
---|
| 358 | if (verb_set.contains(verb) == false)
|
---|
| 359 | {
|
---|
| 360 | logger.error(OAIXML.BAD_VERB + ": " + query);
|
---|
[28852] | 361 | verb_elem = OAIXML.createErrorElement(response_doc, OAIXML.BAD_VERB, OAIXML.ILLEGAL_OAI_VERB);
|
---|
[25717] | 362 | }
|
---|
| 363 | else
|
---|
| 364 | {
|
---|
| 365 | //must be something else other than bad verbs caused an error, so bad argument
|
---|
| 366 | logger.error(OAIXML.BAD_ARGUMENT + ": " + query);
|
---|
[28852] | 367 | verb_elem = OAIXML.createErrorElement(response_doc, OAIXML.BAD_ARGUMENT, "");
|
---|
[25717] | 368 | }
|
---|
| 369 | xml_response.appendChild(verb_elem);
|
---|
[14207] | 370 |
|
---|
[25717] | 371 | out.println("<?xml version='1.0' encoding='UTF-8' ?>");
|
---|
| 372 | if (this.use_oai_stylesheet)
|
---|
| 373 | {
|
---|
| 374 | out.println("<?xml-stylesheet type='text/xsl' href='" + this.oai_stylesheet + "' ?>\n");
|
---|
| 375 | }
|
---|
[28981] | 376 | out.println(XMLConverter.getPrettyString(xml_response));
|
---|
[25717] | 377 | return;
|
---|
| 378 | }//end of if(validate
|
---|
[14207] | 379 |
|
---|
[25717] | 380 | // The query is valid, we can now
|
---|
| 381 | // compose the request message to the receptionist
|
---|
[28966] | 382 | Document request_doc = XMLConverter.newDOM();
|
---|
[28852] | 383 | Element xml_message = request_doc.createElement(GSXML.MESSAGE_ELEM);
|
---|
| 384 | Element xml_request = request_doc.createElement(GSXML.REQUEST_ELEM);
|
---|
[25717] | 385 | // The type attribute is set to be 'oaiService' from OAIServer to OAIReceptionist.
|
---|
| 386 | //xml_request.setAttribute(GSXML.TYPE_ATT, OAIXML.OAI_SERVICE);
|
---|
| 387 | xml_request.setAttribute(GSXML.LANG_ATT, lang);
|
---|
| 388 | xml_request.setAttribute(GSXML.TO_ATT, verb);
|
---|
| 389 | addParams(xml_request, pairs);
|
---|
[14207] | 390 |
|
---|
[25717] | 391 | //xml_request.setAttribute(GSXML.OUTPUT_ATT, output);????
|
---|
| 392 | xml_message.appendChild(xml_request);
|
---|
[14207] | 393 |
|
---|
[25717] | 394 | Node xml_result = this.recept.process(xml_message);
|
---|
| 395 | if (xml_result == null)
|
---|
| 396 | {
|
---|
| 397 | logger.info("xml_result is null");
|
---|
[28852] | 398 | verb_elem = OAIXML.createErrorElement(response_doc, "Internal error", "");
|
---|
[25717] | 399 | xml_response.appendChild(verb_elem);
|
---|
| 400 | }
|
---|
| 401 | else
|
---|
| 402 | {
|
---|
[14207] | 403 |
|
---|
[25717] | 404 | /**
|
---|
| 405 | * All response elements are in the form (with a corresponding verb
|
---|
| 406 | * name): <message> <response> <verb> ... <resumptionToken> .. this
|
---|
| 407 | * is optional! </resumptionToken> </verb> </response> </message>
|
---|
| 408 | */
|
---|
[28852] | 409 | Node res = GSXML.getChildByTagName(xml_result, GSXML.RESPONSE_ELEM);
|
---|
[25717] | 410 | if (res == null)
|
---|
| 411 | {
|
---|
| 412 | logger.info("response element in xml_result is null");
|
---|
[28852] | 413 | verb_elem = OAIXML.createErrorElement(response_doc, "Internal error", "");
|
---|
[25717] | 414 | }
|
---|
| 415 | else
|
---|
| 416 | {
|
---|
| 417 | verb_elem = GSXML.getFirstElementChild(res);
|
---|
| 418 | }
|
---|
[14207] | 419 |
|
---|
[28852] | 420 | if ( verb_elem.getTagName().equals(OAIXML.ERROR))
|
---|
[25717] | 421 | {
|
---|
[28852] | 422 | xml_response.appendChild(response_doc.importNode(verb_elem, true));
|
---|
[25717] | 423 | }
|
---|
[28852] | 424 | else if (OAIXML.oai_version.equals(OAIXML.OAI_VERSION2)) {
|
---|
| 425 | xml_response.appendChild(response_doc.importNode(verb_elem, true));
|
---|
| 426 | }
|
---|
[25717] | 427 | else
|
---|
| 428 | {
|
---|
| 429 | GSXML.copyAllChildren(xml_response, verb_elem);
|
---|
| 430 | }
|
---|
| 431 | }
|
---|
| 432 | out.println("<?xml version='1.0' encoding='UTF-8' ?>");
|
---|
| 433 | if (this.use_oai_stylesheet)
|
---|
| 434 | {
|
---|
| 435 | out.println("<?xml-stylesheet type='text/xsl' href='" + this.oai_stylesheet + "' ?>\n");
|
---|
| 436 | }
|
---|
[28981] | 437 | out.println(XMLConverter.getPrettyString(xml_response));
|
---|
[25717] | 438 | return;
|
---|
| 439 | }
|
---|
[14207] | 440 |
|
---|
[25717] | 441 | /** append parameter elements to the request sent to the receptionist */
|
---|
| 442 | public void addParams(Element request, String[] pairs)
|
---|
| 443 | {
|
---|
[28852] | 444 | Document doc = request.getOwnerDocument();
|
---|
[25717] | 445 | // no params apart from the verb
|
---|
| 446 | if (pairs == null || pairs.length < 2)
|
---|
| 447 | return;
|
---|
[14207] | 448 |
|
---|
[25717] | 449 | /**
|
---|
| 450 | * the request xml is composed in the form: <request> <param name=.../>
|
---|
| 451 | * <param name=.../> </request> (No paramList element in between).
|
---|
| 452 | */
|
---|
| 453 | for (int i = 1; i < pairs.length; i++)
|
---|
| 454 | {
|
---|
| 455 | //the first pair in pairs is the verb=xxx
|
---|
| 456 | int index = pairs[i].indexOf("=");
|
---|
| 457 | if (index != -1)
|
---|
| 458 | { //just a double check
|
---|
[28852] | 459 | Element param = GSXML.createParameter(doc, pairs[i].substring(0, index), OAIXML.oaiDecode(pairs[i].substring(index + 1)));
|
---|
| 460 | request.appendChild(param);
|
---|
[25717] | 461 | }
|
---|
| 462 | }
|
---|
| 463 | }
|
---|
[14207] | 464 |
|
---|
[25717] | 465 | // For OAI version 2.0, validation tests indicated that POST needs to be supported. Some
|
---|
| 466 | // modification was required in order to ensure that the request is passed intact to doGet()
|
---|
| 467 | public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
|
---|
| 468 | {
|
---|
| 469 |
|
---|
| 470 | // the post method returns a wrapper of type RequestFacade by apache and there
|
---|
| 471 | // is no getQueryString() method defined for it. Therefore, need to work this out
|
---|
| 472 | // manually before calling doGet(request, response) so that doGet can work as before.
|
---|
| 473 |
|
---|
| 474 | queryString = "";
|
---|
| 475 | Iterator parameter_entries = request.getParameterMap().entrySet().iterator();
|
---|
| 476 | while (parameter_entries.hasNext())
|
---|
| 477 | {
|
---|
| 478 | Map.Entry param_entry = (Map.Entry) parameter_entries.next();
|
---|
| 479 | String[] paramVals = (String[]) param_entry.getValue();
|
---|
| 480 | if (paramVals != null)
|
---|
| 481 | {
|
---|
| 482 | if (paramVals.length > 0)
|
---|
| 483 | {
|
---|
| 484 | logger.error("POST request received: " + param_entry.getKey() + " - " + paramVals[0]);
|
---|
| 485 | queryString = queryString + "&" + param_entry.getKey() + "=" + paramVals[0];
|
---|
| 486 | }
|
---|
| 487 | }
|
---|
| 488 | }
|
---|
| 489 | if (queryString.length() > 0)
|
---|
| 490 | {
|
---|
| 491 | queryString = queryString.substring(1);
|
---|
| 492 | //queryString = OAIXML.oaiEncode(queryString);
|
---|
| 493 | }
|
---|
| 494 | if (queryString.equals(""))
|
---|
| 495 | {
|
---|
| 496 | queryString = null;
|
---|
| 497 | }
|
---|
| 498 | doGet(request, response);
|
---|
| 499 | }
|
---|
[28873] | 500 |
|
---|
| 501 |
|
---|
| 502 | public void destroy()
|
---|
| 503 | {
|
---|
| 504 | recept.cleanUp();
|
---|
| 505 | }
|
---|
| 506 |
|
---|
[25717] | 507 | }
|
---|