Changeset 27672

Show
Ignore:
Timestamp:
20.06.2013 14:02:39 (6 years ago)
Author:
kjdon
Message:

adding new functionality to identify request

Files:
1 modified

Legend:

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

    r25635 r27672  
    2626  /** Instead of a config_params object, only a site_name is needed by oai receptionist. */ 
    2727  protected String site_name = null; 
     28  /** The unique  repository identifier */ 
     29  protected String repository_id = null; 
     30   
    2831  /** container Document to create XML Nodes for requests sent to message router 
    2932   *  Not used for response  
     
    4346  protected ModuleInterface mr = null; 
    4447   
     48 
     49  // Some of the data/responses will not change while the servlet is running, so 
     50  // we can cache them 
     51   
     52  /** A list of all the collections available to this OAI server */ 
     53  protected NodeList collection_list = null; 
     54 
     55  /** The identify response */ 
     56  protected Element identify_response = null; 
     57   
    4558  public OAIReceptionist() { 
    4659    this.converter = new XMLConverter(); 
     
    7487    resume_after = getResumeAfter(); 
    7588     
     89    repository_id = getRepositoryId();  
     90    collection_list = getOAICollectionList(); 
     91 
    7692    //clear out expired resumption tokens stored in OAIResumptionToken.xml 
    7793    OAIXML.init(); 
     
    190206    return -1; 
    191207  } 
     208  private String getRepositoryId() { 
     209    Element ri = (Element)GSXML.getChildByTagName(oai_config, OAIXML.REPOSITORY_ID); 
     210    if (ri != null) {  
     211      return GSXML.getNodeText(ri); 
     212    } 
     213    return ""; 
     214  } 
    192215  /** method to compose a set element 
    193216   */ 
     
    198221    Element list_sets_elem = OAIXML.createElement(OAIXML.LIST_SETS); 
    199222 
    200     //ask the message router for a list of oai collections 
    201     NodeList oai_coll = getOAICollectionList(); 
    202     int oai_coll_size = oai_coll.getLength(); 
     223    int oai_coll_size = collection_list.getLength();  
    203224    if (oai_coll_size == 0) { 
    204225      return getMessage(list_sets_elem); 
     
    229250        //no resumeptionToken stored in OAIConfig.xml. 
    230251        //As long as the verb is 'ListSets', we ignore the rest of the parameters 
    231         getSets(list_sets_elem, oai_coll, 0, oai_coll_size); 
     252        getSets(list_sets_elem, collection_list, 0, oai_coll_size); 
    232253        return getMessage(list_sets_elem); 
    233254      } 
    234255       
    235256      //append required sets to list_sets_elem (may be a complete or incomplete list) 
    236       getSets(list_sets_elem, oai_coll, 0, smaller); 
     257      getSets(list_sets_elem, collection_list, 0, smaller); 
    237258       
    238259      if(oai_coll_size > resume_after) { 
     
    272293      //Yes, we are. 
    273294      //append required sets to list_sets_elem (list is complete) 
    274       getSets(list_sets_elem, oai_coll, cursor, oai_coll_size); 
     295      getSets(list_sets_elem, collection_list, cursor, oai_coll_size); 
    275296      //An incomplete list is sent; append a resumptionToken element 
    276297      token_elem = createResumptionTokenElement(oai_coll_size, cursor, -1, false); 
     
    279300      //No, we are not. 
    280301      //append required sets to list_sets_elem (list is incomplete) 
    281       getSets(list_sets_elem, oai_coll, cursor, cursor + resume_after); 
     302      getSets(list_sets_elem, collection_list, cursor, cursor + resume_after); 
    282303      token_elem = createResumptionTokenElement(oai_coll_size, cursor, cursor + resume_after, true); 
    283304      //store this token 
     
    377398 
    378399    //ask the message router for a list of oai collections 
    379     NodeList oai_coll = getOAICollectionList(); 
    380     int oai_coll_size = oai_coll.getLength(); 
     400    //NodeList oai_coll = collection_list; //getOAICollectionList(); 
     401    int oai_coll_size = collection_list.getLength(); 
    381402    if (oai_coll_size == 0) { 
    382403      logger.info("returned oai collection list is empty"); 
     
    396417 
    397418      for(int i=0; i<oai_coll_size; i++) { 
    398         if(set_spec_str.equals(((Element)oai_coll.item(i)).getAttribute(OAIXML.NAME))) { 
     419        if(set_spec_str.equals(((Element)collection_list.item(i)).getAttribute(OAIXML.NAME))) { 
    399420          set_supported = true; 
    400421        } 
     
    452473          } 
    453474        } 
    454         String full_name = ((Element)oai_coll.item(i)).getAttribute(OAIXML.NAME); 
     475        String full_name = ((Element)collection_list.item(i)).getAttribute(OAIXML.NAME); 
    455476        coll_name = full_name.substring(full_name.indexOf(":") + 1); 
    456477        req.setAttribute(OAIXML.TO, coll_name + "/" + verb); 
     
    589610     
    590611    //ask the message router for a list of oai collections 
    591     NodeList oai_coll = getOAICollectionList(); 
    592     int oai_coll_size = oai_coll.getLength(); 
     612    //NodeList oai_coll = getOAICollectionList(); 
     613    int oai_coll_size = collection_list.getLength(); 
    593614    if (oai_coll_size == 0) { 
    594615      logger.info("returned oai collection list is empty"); 
     
    610631 
    611632      for(int i=0; i<oai_coll_size; i++) { 
    612         if(set_spec_str.equals(((Element)oai_coll.item(i)).getAttribute(OAIXML.NAME))) { 
     633        if(set_spec_str.equals(((Element)collection_list.item(i)).getAttribute(OAIXML.NAME))) { 
    613634          set_supported = true; 
    614635        } 
     
    701722          } 
    702723        } 
    703         String full_name = ((Element)oai_coll.item(i)).getAttribute(OAIXML.NAME); 
     724        String full_name = ((Element)collection_list.item(i)).getAttribute(OAIXML.NAME); 
    704725        coll_name = full_name.substring(full_name.indexOf(":") + 1); 
    705726        req.setAttribute(OAIXML.TO, coll_name + "/" + verb); 
     
    832853      //this is requesting metadata formats for the whole repository 
    833854      //read the oaiConfig.xml file, return the metadata formats specified there. 
    834       Element oai_config = OAIXML.getOAIConfigXML(); 
    835       if (oai_config == null) { 
    836         return getMessage(OAIXML.createErrorElement(OAIXML.ERROR, OAIXML.SERVICE_UNAVAILABLE)); 
    837       } else { 
    838         Element format_list = (Element)GSXML.getChildByTagName(oai_config, OAIXML.LIST_METADATA_FORMATS); 
    839         if(format_list == null) { 
    840           logger.error("OAIConfig.xml must contain the supported metadata formats"); 
    841           return getMessage(list_metadata_formats); 
     855      Element format_list = (Element)GSXML.getChildByTagName(oai_config, OAIXML.LIST_METADATA_FORMATS); 
     856      if(format_list == null) { 
     857    logger.error("OAIConfig.xml must contain the supported metadata formats"); 
     858    return getMessage(list_metadata_formats); 
     859      } 
     860      NodeList formats = format_list.getElementsByTagName(OAIXML.METADATA_FORMAT); 
     861      for(int i=0; i<formats.getLength(); i++) { 
     862    Element meta_fmt = OAIXML.createElement(OAIXML.METADATA_FORMAT); 
     863    Element first_meta_format = (Element)formats.item(i); 
     864    //the element also contains mappings, but we don't want them 
     865    meta_fmt.appendChild(meta_fmt.getOwnerDocument().importNode(GSXML.getChildByTagName(first_meta_format, OAIXML.METADATA_PREFIX), true)); 
     866    meta_fmt.appendChild(meta_fmt.getOwnerDocument().importNode(GSXML.getChildByTagName(first_meta_format, OAIXML.SCHEMA), true)); 
     867    meta_fmt.appendChild(meta_fmt.getOwnerDocument().importNode(GSXML.getChildByTagName(first_meta_format, OAIXML.METADATA_NAMESPACE), true)); 
     868    list_metadata_formats.appendChild(meta_fmt); 
    842869        } 
    843         NodeList formats = format_list.getElementsByTagName(OAIXML.METADATA_FORMAT); 
    844         for(int i=0; i<formats.getLength(); i++) { 
    845           Element meta_fmt = OAIXML.createElement(OAIXML.METADATA_FORMAT); 
    846           Element first_meta_format = (Element)formats.item(i); 
    847           //the element also contains mappings, but we don't want them 
    848           meta_fmt.appendChild(meta_fmt.getOwnerDocument().importNode(GSXML.getChildByTagName(first_meta_format, OAIXML.METADATA_PREFIX), true)); 
    849           meta_fmt.appendChild(meta_fmt.getOwnerDocument().importNode(GSXML.getChildByTagName(first_meta_format, OAIXML.SCHEMA), true)); 
    850           meta_fmt.appendChild(meta_fmt.getOwnerDocument().importNode(GSXML.getChildByTagName(first_meta_format, OAIXML.METADATA_NAMESPACE), true)); 
    851           list_metadata_formats.appendChild(meta_fmt); 
    852         } 
    853         return getMessage(list_metadata_formats); 
    854       } 
     870      return getMessage(list_metadata_formats); 
    855871       
    856     } else if (params.getLength() > 1) { 
     872       
     873    }  
     874 
     875    if (params.getLength() > 1) { 
    857876      //Bad argument. Can't be more than one parameters for ListMetadataFormats verb 
    858877      return getMessage(OAIXML.createErrorElement(OAIXML.BAD_ARGUMENT, "")); 
    859     } else { 
    860       // This is a request for the metadata of a particular item with an identifier 
     878    }  
     879     
     880    // This is a request for the metadata of a particular item with an identifier 
    861881      /**the request xml is in the form: <request> 
    862882       *                                   <param name=.../> 
     
    904924    return converter.nodeToElement(result_node); 
    905925      } 
    906     } 
    907      
    908   } 
     926   
     927     
     928  } 
     929 
     930 
    909931  private void appendParam(Element req, String name, String value) {     
    910932        Element param = req.getOwnerDocument().createElement(OAIXML.PARAM); 
     
    913935        req.appendChild(param); 
    914936  } 
    915   private void copyElement(Element identify, String tag_name) { 
    916     Element from_repository_name = (Element)GSXML.getChildByTagName(oai_config, tag_name); 
    917     if(from_repository_name != null) { 
    918       Element this_repository_name = OAIXML.createElement(tag_name); 
    919       GSXML.setNodeText(this_repository_name, GSXML.getNodeText(from_repository_name)); 
    920       identify.appendChild(this_repository_name); 
    921     } 
    922   } 
     937  private void copyNamedElementfromConfig(Element to_elem, String element_name) { 
     938    Element original_element = (Element)GSXML.getChildByTagName(oai_config, element_name); 
     939    if(original_element != null) { 
     940      copyNode(to_elem, original_element); 
     941    } 
     942  } 
     943 
     944  private void copyNode(Element to_elem, Node original_element) { 
     945    to_elem.appendChild(to_elem.getOwnerDocument().importNode(original_element, true)); 
     946 
     947  } 
     948 
    923949  private Element doIdentify() { 
    924950    //The validation for this verb has been done in OAIServer.validate(). So no bother here. 
    925951    logger.info(""); 
     952    if (this.identify_response != null) { 
     953      // we have already created it 
     954      return getMessage(this.identify_response); 
     955    } 
    926956     
    927957    Element identify = OAIXML.createElement(OAIXML.IDENTIFY); 
    928958    //do the repository name 
    929     copyElement(identify, OAIXML.REPOSITORY_NAME); 
     959    copyNamedElementfromConfig(identify, OAIXML.REPOSITORY_NAME); 
    930960    //do the baseurl 
    931     copyElement(identify, OAIXML.BASE_URL); 
     961    copyNamedElementfromConfig(identify, OAIXML.BASE_URL); 
    932962    //do the protocol version 
    933     copyElement(identify, OAIXML.PROTOCOL_VERSION); 
     963    copyNamedElementfromConfig(identify, OAIXML.PROTOCOL_VERSION); 
    934964         
    935965    //There can be more than one admin email according to the OAI specification 
     
    941971    } 
    942972    for (int i=0; i<num_admin; i++) { 
    943       copyElement(identify, OAIXML.ADMIN_EMAIL); 
     973      copyNode(identify, admin_emails.item(i)); 
    944974    } 
    945975 
     
    947977    //send request to mr to search through the earliest datestamp amongst all oai collections in the repository. 
    948978    //ask the message router for a list of oai collections 
    949     NodeList oai_coll = getOAICollectionList(); 
    950     long earliestDatestamp = getEarliestDateStamp(oai_coll); 
     979    //NodeList oai_coll = getOAICollectionList(); 
     980    long earliestDatestamp = getEarliestDateStamp(collection_list); 
    951981    String earliestDatestamp_str = OAIXML.getTime(earliestDatestamp); 
    952982    Element earliestDatestamp_elem = OAIXML.createElement(OAIXML.EARLIEST_DATESTAMP); 
     
    955985 
    956986    //do the deletedRecord 
    957     copyElement(identify, OAIXML.DELETED_RECORD); 
     987    copyNamedElementfromConfig(identify, OAIXML.DELETED_RECORD); 
    958988    //do the granularity 
    959     copyElement(identify, OAIXML.GRANULARITY); 
    960          
     989    copyNamedElementfromConfig(identify, OAIXML.GRANULARITY); 
     990      
     991    // output the oai identifier 
     992    Element description = OAIXML.createElement(OAIXML.DESCRIPTION); 
     993    identify.appendChild(description); 
     994    Element oaiIdentifier = OAIXML.createOAIIdentifierXML(repository_id, "lucene-jdbm-demo", "ec159e"); 
     995    description.appendChild(oaiIdentifier); 
     996 
     997    // if there are any oaiInfo metadata, add them in too. 
     998    Element info = (Element)GSXML.getChildByTagName(oai_config, OAIXML.OAI_INFO); 
     999    if (info != null) { 
     1000      NodeList meta = GSXML.getChildrenByTagName(info, OAIXML.INFO_METADATA); 
     1001      if (meta != null && meta.getLength() > 0) { 
     1002    Element gsdl = OAIXML.createGSDLElement(); 
     1003    description.appendChild(gsdl); 
     1004    for (int m = 0; m<meta.getLength(); m++) { 
     1005      copyNode(gsdl, meta.item(m)); 
     1006    } 
     1007     
     1008      } 
     1009    } 
     1010    this.identify_response = identify; 
    9611011    return getMessage(identify); 
    9621012  }