Changeset 28852

Show
Ignore:
Timestamp:
27.02.2014 11:37:20 (6 years ago)
Author:
kjdon
Message:

Got rid of this.document. DOM not thread safe, so now we must create a new Document each time we are generating a message. A little bit of tidying up too.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • main/trunk/greenstone3/src/java/org/greenstone/gsdl3/OAIServer.java

    r25717 r28852  
    2727import javax.servlet.ServletConfig; 
    2828import javax.servlet.ServletException; 
     29import javax.servlet.UnavailableException; 
    2930import javax.servlet.http.HttpServletRequest; 
    3031import javax.servlet.http.HttpServletResponse; 
     
    7576 
    7677    /** 
    77      * a converter class to parse XML and create Docs This is only used for 
    78      * generating internal requests passed to MessageRouter. The response 
    79      * message is generated by parsing an existing xml skeleton file 
    80      * (web/WEB-INF/oaixml/oaiversion2.xml, for example). 
     78     * a converter class to parse XML and create Documents for  
     79     * XML generation 
    8180     */ 
    8281    protected XMLConverter converter = null; 
    83     /** 
    84      * container Document to create XML Nodes (but only request to the oai 
    85      * receptionist, not response (which is created in OAIXML.java) created by 
    86      * converter class 
    87      */ 
    88     protected Document doc = null; 
    8982 
    9083    /** A HashSet which contains all the legal verbs. */ 
     
    10194    protected String site = ""; 
    10295 
     96  // can be overriddden in OAIConfig.xml 
    10397    // do we output the stylesheet processing instruction? 
    10498    protected boolean use_oai_stylesheet = true; 
     
    136130            if (remote_site_name == null || remote_site_type == null || remote_site_address == null) 
    137131            { 
    138                 System.err.println("initialisation paramters not all set!"); 
    139                 System.err.println("if site_name is not set, then you must have remote_site_name, remote_site_type and remote_site_address set"); 
    140                 System.exit(1); 
     132                logger.error("initialisation paramters not all set!"); 
     133                logger.error("if site_name is not set, then you must have remote_site_name, remote_site_type and remote_site_address set"); 
     134                throw new UnavailableException("OAIServer: incorrect servlet parameters"); 
    141135            } 
    142136        } 
     
    150144        // the receptionist -the servlet will talk to this 
    151145        this.recept = new OAIReceptionist(); 
    152  
     146        // the converter - used to get new Documents to generate XML messages 
     147        this.converter = new XMLConverter(); 
    153148        // the receptionist uses a MessageRouter or Communicator to send its requests to. We either create a MessageRouter 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. 
    154149        if (site_name != null) 
     
    169164                catch (Exception e) 
    170165                { // cant use this new one, so use normal one 
    171                     System.err.println("OAIServlet configure exception when trying to use a new MessageRouter " + mr_name + ": " + e.getMessage()); 
    172                     e.printStackTrace(); 
    173                     message_router = new MessageRouter(); 
     166                  logger.error("OAIServlet configure exception when trying to use a new MessageRouter " + mr_name, e); 
     167                  message_router = new MessageRouter(); 
    174168                } 
    175169            } 
     
    177171            message_router.setSiteName(site_name); 
    178172            // lots of work is done in this step; see MessageRouter.java 
    179             message_router.configure(); 
     173            if (!message_router.configure()) { 
     174              throw new UnavailableException("OAIServer: Couldn't configure MessageRouter"); 
     175            } 
    180176            this.recept.setSiteName(site_name); 
    181177            this.recept.setMessageRouter(message_router); 
     
    187183            Communicator communicator = null; 
    188184            // we need to create the XML to configure the communicator 
    189             Element site_elem = this.doc.createElement(GSXML.SITE_ELEM); 
     185            Document site_doc = this.converter.newDOM(); 
     186            Element site_elem = site_doc.createElement(GSXML.SITE_ELEM); 
    190187            site_elem.setAttribute(GSXML.TYPE_ATT, remote_site_type); 
    191188            site_elem.setAttribute(GSXML.NAME_ATT, remote_site_name); 
     
    198195            else 
    199196            { 
    200                 System.err.println("OAIServlet.init Error: invalid Communicator type: " + remote_site_type); 
    201                 System.exit(1); 
     197                logger.error("OAIServlet.init Error: invalid Communicator type: " + remote_site_type); 
     198                throw new UnavailableException("OAIServer: invalid communicator type"); 
    202199            } 
    203200 
    204201            if (!communicator.configure(site_elem)) 
    205202            { 
    206                 System.err.println("OAIServlet.init Error: Couldn't configure communicator"); 
    207                 System.exit(1); 
     203                logger.error("OAIServlet.init Error: Couldn't configure communicator"); 
     204                throw new UnavailableException("OAIServer: Couldn't configure communicator"); 
    208205            } 
    209206            this.recept.setSiteName(remote_site_name); 
    210207            this.recept.setMessageRouter(communicator); 
    211208        } 
    212         // used for composing internal xml requests, but not xml responses. 
    213         // the converter may be used to get pretty xml, though.     
    214         this.converter = new XMLConverter(); 
    215         this.doc = this.converter.newDOM(); 
    216209 
    217210        // Read in OAIConfig.xml (residing web/WEB-INF/classes/) and  
    218         //use it to configure the receptionist. The init() is also called in which 
    219         //the resumption token file is read in and all expired tokens cleared. 
     211        //use it to configure the receptionist.  
    220212        Element oai_config = OAIXML.getOAIConfigXML(); 
    221213        if (oai_config == null) 
    222214        { 
    223215            logger.error("Fail to parse oai config file OAIConfig.xml."); 
    224             return; 
     216            throw new UnavailableException("OAIServer: Couldn't parse OAIConfig.xml"); 
    225217        } 
    226218        // pass it to the receptionist 
    227         this.recept.configure(oai_config); 
    228  
     219        if (!this.recept.configure(oai_config)) { 
     220          logger.error("Couldn't configure receptionist"); 
     221          throw new UnavailableException("OAIServer: Couldn't configure receptionist");  
     222        } 
    229223        // also, we have something we want to get from here - useOAIStylesheet 
    230224        this.configure(oai_config); 
     225        // Initialise the resumption tokens 
     226        OAIResumptionToken.init(); 
     227 
    231228    }//end of init() 
    232229 
     
    331328    { 
    332329        logUsageInfo(request); 
    333         //out.println("url="+request.getRequestURL());// /oaiserver     
    334         //out.println("query="+request.getQueryString());// is /greenstone3 
    335330 
    336331        // oai always requires the content type be text/xml 
     
    355350        String[] pairs = (query == null) ? null : query.split("&");//split into key/value pairs 
    356351        String verb = getVerb(query); 
    357         Element xml_response = OAIXML.createBasicResponse(verb, pairs); 
     352        Document response_doc = this.converter.newDOM(); 
     353        Element xml_response = OAIXML.createBasicResponse(response_doc, verb, pairs); 
    358354        Element verb_elem = null; 
    359355 
     
    363359            { 
    364360                logger.error(OAIXML.BAD_VERB + ": " + query); 
    365                 verb_elem = OAIXML.createErrorElement(OAIXML.BAD_VERB, OAIXML.ILLEGAL_OAI_VERB); 
     361                verb_elem = OAIXML.createErrorElement(response_doc, OAIXML.BAD_VERB, OAIXML.ILLEGAL_OAI_VERB); 
    366362            } 
    367363            else 
     
    369365                //must be something else other than bad verbs caused an error, so bad argument 
    370366                logger.error(OAIXML.BAD_ARGUMENT + ": " + query); 
    371                 verb_elem = OAIXML.createErrorElement(OAIXML.BAD_ARGUMENT, ""); 
     367                verb_elem = OAIXML.createErrorElement(response_doc, OAIXML.BAD_ARGUMENT, ""); 
    372368            } 
    373369            xml_response.appendChild(verb_elem); 
     
    384380        // The query is valid, we can now 
    385381        // compose the request message to the receptionist 
    386         Element xml_message = this.doc.createElement(GSXML.MESSAGE_ELEM); 
    387         Element xml_request = this.doc.createElement(GSXML.REQUEST_ELEM); 
     382        Document request_doc = this.converter.newDOM(); 
     383        Element xml_message = request_doc.createElement(GSXML.MESSAGE_ELEM); 
     384        Element xml_request = request_doc.createElement(GSXML.REQUEST_ELEM); 
    388385        // The type attribute is set to be 'oaiService' from OAIServer to OAIReceptionist. 
    389386        //xml_request.setAttribute(GSXML.TYPE_ATT, OAIXML.OAI_SERVICE); 
     
    399396        { 
    400397            logger.info("xml_result is null"); 
    401             verb_elem = OAIXML.createErrorElement("Internal error", ""); 
     398            verb_elem = OAIXML.createErrorElement(response_doc, "Internal error", ""); 
    402399            xml_response.appendChild(verb_elem); 
    403400        } 
     
    410407             * is optional! </resumptionToken> </verb> </response> </message> 
    411408             */ 
    412             Node res = GSXML.getChildByTagName(xml_result, OAIXML.RESPONSE); 
     409            Node res = GSXML.getChildByTagName(xml_result, GSXML.RESPONSE_ELEM); 
    413410            if (res == null) 
    414411            { 
    415412                logger.info("response element in xml_result is null"); 
    416                 verb_elem = OAIXML.createErrorElement("Internal error", ""); 
     413                verb_elem = OAIXML.createErrorElement(response_doc, "Internal error", ""); 
    417414            } 
    418415            else 
     
    421418            } 
    422419 
    423             if (OAIXML.oai_version.equals(OAIXML.OAI_VERSION2) || verb_elem.getTagName().equals(OAIXML.ERROR)) 
    424             { 
    425                 xml_response.appendChild(xml_response.getOwnerDocument().importNode(verb_elem, true)); 
     420            if ( verb_elem.getTagName().equals(OAIXML.ERROR)) 
     421            { 
     422              xml_response.appendChild(response_doc.importNode(verb_elem, true)); 
     423            } 
     424            else if (OAIXML.oai_version.equals(OAIXML.OAI_VERSION2)) { 
     425              xml_response.appendChild(response_doc.importNode(verb_elem, true)); 
    426426            } 
    427427            else 
     
    442442    public void addParams(Element request, String[] pairs) 
    443443    { 
     444      Document doc = request.getOwnerDocument(); 
    444445        // no params apart from the verb 
    445446        if (pairs == null || pairs.length < 2) 
     
    456457            if (index != -1) 
    457458            { //just a double check 
    458                 Element param = this.doc.createElement(OAIXML.PARAM); 
    459                 param.setAttribute(OAIXML.NAME, pairs[i].substring(0, index)); 
    460                 param.setAttribute(OAIXML.VALUE, OAIXML.oaiDecode(pairs[i].substring(index + 1))); 
    461                 request.appendChild(param); 
     459              Element param = GSXML.createParameter(doc, pairs[i].substring(0, index), OAIXML.oaiDecode(pairs[i].substring(index + 1))); 
     460              request.appendChild(param); 
    462461            } 
    463462        }