Changeset 19984


Ignore:
Timestamp:
2009-07-13T10:25:21+12:00 (15 years ago)
Author:
oranfry
Message:

initial implementation of support for site-level metadata

Location:
greenstone3/trunk/src/java/org/greenstone/gsdl3
Files:
11 edited

Legend:

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

    r19771 r19984  
    580580    }
    581581
    582     // get all the <a> elements
    583     NodeList hrefs = data.getElementsByTagName("a");
    584     for (int i=0; i<hrefs.getLength(); i++) {
    585       Element a = (Element)hrefs.item(i);
    586       // ugly hack to get rid of : in the args - interferes with session handling
    587       String href = a.getAttribute("href");
    588       if (!href.equals("")) {
    589         if (href.indexOf("?")!=-1) {
    590           String[] parts = href.split("\\?", -1);
    591           parts[1]=parts[1].replaceAll(":", "%3A");
    592           href = parts[0]+"?"+parts[1];
    593         }
    594         a.setAttribute("href", response.encodeURL(href));
    595       }
    596     }
    597    
    598     // now find any submit bits - get all the <form> elements
    599     NodeList forms = data.getElementsByTagName("form");
    600     for (int i=0; i<forms.getLength(); i++) {
    601       Element form = (Element)forms.item(i);
    602       form.setAttribute("action", response.encodeURL(form.getAttribute("action")));
    603     }
    604     // are these the only cases where URLs occur??
    605     // we should only do this for greenstone urls?
     582    if ( data != null ) {
     583
     584        // get all the <a> elements
     585        NodeList hrefs = data.getElementsByTagName("a");
     586        for (int i=0; hrefs!=null && i<hrefs.getLength(); i++) {
     587          Element a = (Element)hrefs.item(i);
     588          // ugly hack to get rid of : in the args - interferes with session handling
     589          String href = a.getAttribute("href");
     590          if (!href.equals("")) {
     591            if (href.indexOf("?")!=-1) {
     592              String[] parts = href.split("\\?", -1);
     593              parts[1]=parts[1].replaceAll(":", "%3A");
     594              href = parts[0]+"?"+parts[1];
     595            }
     596            a.setAttribute("href", response.encodeURL(href));
     597          }
     598        }
     599       
     600        // now find any submit bits - get all the <form> elements
     601        NodeList forms = data.getElementsByTagName("form");
     602        for (int i=0; forms!=null && i<forms.getLength(); i++) {
     603          Element form = (Element)forms.item(i);
     604          form.setAttribute("action", response.encodeURL(form.getAttribute("action")));
     605        }
     606        // are these the only cases where URLs occur??
     607        // we should only do this for greenstone urls?
     608    }
    606609   
    607610  }
  • greenstone3/trunk/src/java/org/greenstone/gsdl3/action/Action.java

    r16688 r19984  
    3737    this.converter = new XMLConverter();
    3838    this.doc = this.converter.newDOM();
     39    System.err.println("action doc: " + doc );
    3940    }
    4041    /** the config variables must be set before configure is called */
     
    134135    return true;
    135136    }
     137
     138    void addSiteMetadata( Element element, String lang, String uid ) {
     139        //ADD SITE METADATA
     140        Element metadata_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_DESCRIBE, "", lang, uid);
     141        //create a hashmap of params
     142        HashMap subset_params = new HashMap(1);
     143        subset_params.put(GSXML.SUBSET_PARAM, GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER);
     144        //create the element to put the params in
     145        Element param_list = this.doc.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
     146        //put them in
     147        GSXML.addParametersToList( this.doc, param_list, subset_params );
     148        metadata_request.appendChild(param_list);
     149        //create the message
     150        Element metadata_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
     151        metadata_message.appendChild(metadata_request);
     152
     153        //get response
     154        Element metadata_response_message = (Element)this.mr.process(metadata_message);
     155
     156        //drill down to response
     157        Element metadata_response = (Element)GSXML.getChildByTagName(metadata_response_message, GSXML.RESPONSE_ELEM);
     158
     159        GSXML.mergeMetadataLists(element,metadata_response);
     160
     161    }
    136162}
    137163
  • greenstone3/trunk/src/java/org/greenstone/gsdl3/action/BrowseAction.java

    r16688 r19984  
    9191    page_response.appendChild(this.doc.importNode(service_description, true));
    9292   
     93    //append site metadata
     94    addSiteMetadata(page_response, lang, uid);
     95
    9396    // if rt=d, then we are just displaying the service
    9497    String request_type = (String)params.get(GSParams.REQUEST_TYPE);
  • greenstone3/trunk/src/java/org/greenstone/gsdl3/action/DocumentAction.java

    r19639 r19984  
    8585    // just in case there are some that need to get passed to the services
    8686    HashMap service_params = (HashMap)params.get("s0");
     87
    8788   
    8889    String has_rl = null;
     
    131132        }
    132133    }
     134
     135    //append site metadata
     136    addSiteMetadata( page_response, lang, uid);
     137
    133138    // get the additional data needed for the page
    134139    getBackgroundData(page_response, collection, lang, uid);
  • greenstone3/trunk/src/java/org/greenstone/gsdl3/action/GS2BrowseAction.java

    r16688 r19984  
    8282    page_response.appendChild(this.doc.importNode(service_description, true));
    8383   
     84    //append site metadata
     85    addSiteMetadata( page_response, lang, uid);
     86
    8487    // if rt=d, then we are just displaying the service
    8588    String request_type = (String)params.get(GSParams.REQUEST_TYPE);
  • greenstone3/trunk/src/java/org/greenstone/gsdl3/action/GeneralAction.java

    r19851 r19984  
    2323    Element request = (Element)GSXML.getChildByTagName(message, GSXML.REQUEST_ELEM);
    2424    logger.debug(" request="+this.converter.getString(request));
     25
     26    String lang = request.getAttribute(GSXML.LANG_ATT);
     27    String uid = request.getAttribute(GSXML.USER_ID_ATT);
     28
    2529    //A shortcut action serving the flax web page requests
    2630    if(request.getAttribute(GSXML.SUBACTION_ATT).equals(GSXML.FLAX_PAGE_GENERATION)
     
    3337            page_response.appendChild((Element)this.doc.importNode(flaxPageNode, true));
    3438        }
    35         return result;     
     39        addSiteMetadata(result, lang, uid);
     40        System.err.println("a");
     41        return result;
    3642    }
    3743   
     
    4753    }
    4854    String request_type = (String) params.get(GSParams.REQUEST_TYPE);
    49     String lang = request.getAttribute(GSXML.LANG_ATT);
    50     String uid = request.getAttribute(GSXML.USER_ID_ATT);
    5155    // what is carried out depends on the request_type
    5256    // if rt=d, then a describe request is done,
     
    8993        if (response_only) {
    9094        // just send the reponse as is
     95        addSiteMetadata(result_response, lang, uid);
     96        System.err.println("b");
    9197        return result_response;
    9298        }
     
    116122    }
    117123
     124    addSiteMetadata(page_response, lang, uid);
     125    System.err.println("c");
    118126    return result;
    119127    }
  • greenstone3/trunk/src/java/org/greenstone/gsdl3/action/PageAction.java

    r19641 r19984  
    55//XML classes
    66import org.w3c.dom.Node;
    7 import org.w3c.dom.NodeList; 
    8 import org.w3c.dom.Document; 
     7import org.w3c.dom.NodeList;
     8import org.w3c.dom.Document;
    99import org.w3c.dom.Element;
    1010
     
    115115        String uid = request.getAttribute(GSXML.USER_ID_ATT);
    116116        // extract the params from the cgi-request,
    117         Element cgi_paramList = (Element)GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);       
     117        Element cgi_paramList = (Element)GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
    118118        HashMap params = GSXML.extractParams(cgi_paramList, false);
    119119       
     
    122122            logger.error("about page requested with no collection or cluster specified!");
    123123            // return an empty response
    124             return this.doc.createElement(GSXML.RESPONSE_ELEM);
     124            Element response = this.doc.createElement(GSXML.RESPONSE_ELEM);
     125            addSiteMetadata(response, lang, uid);
     126            return response;
    125127        }
    126128
     
    128130        Element coll_about_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
    129131
    130         Element coll_about_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_DESCRIBE,  coll_name, lang, uid);
     132        Element coll_about_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_DESCRIBE, coll_name, lang, uid);
    131133        coll_about_message.appendChild(coll_about_request);
    132 
    133134        Element coll_about_response = (Element)this.mr.process(coll_about_message);
    134        
     135
    135136        // add collection type attribute to paramList
    136137        String col_type = "";
     
    159160            return null;
    160161        }
    161        
     162
     163
    162164        // second, get the info for each service - we only want display items
    163165        // but for now, we'll just get it all
     
    168170
    169171        Element response = (Element) GSXML.getChildByTagName(coll_about_response, GSXML.RESPONSE_ELEM);
     172        //add the site metadata
     173        addSiteMetadata(response, lang, uid);
    170174        return response;
    171175    }
     
    190194        if (coll_name == null || coll_name.equals("")) {
    191195            // just return an empty response
    192             return this.doc.createElement(GSXML.RESPONSE_ELEM);
     196            Element response = this.doc.createElement(GSXML.RESPONSE_ELEM);
     197            addSiteMetadata(response, lang, uid);
     198            return response;
    193199        }
    194200
     
    205211
    206212        Element response = (Element) GSXML.getChildByTagName(coll_about_response, GSXML.RESPONSE_ELEM);
     213
     214        //add the site metadata
     215        addSiteMetadata(response, lang, uid);
     216
    207217        return response;
    208218
     
    275285        return page_response;
    276286    }
     287
     288
    277289}
  • greenstone3/trunk/src/java/org/greenstone/gsdl3/action/QueryAction.java

    r19640 r19984  
    4646     */
    4747    protected Element basicQuery(Element request) {
    48    
     48
    4949    // the result
    5050    Element page_response = this.doc.createElement(GSXML.RESPONSE_ELEM);
    51    
     51
    5252    // extract the params from the cgi-request, and check that we have a coll specified
    5353    Element cgi_param_list = (Element)GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
     
    8484        page_response.appendChild(service_description);
    8585    }
    86    
     86
    8787    if (request_type.indexOf("r") == -1) {
    8888        // just a display request, no actual processing to do
    89         return page_response;
    90     }
    91    
     89        System.err.println("just a display request, no actual processing to do");
     90        //append site metadata
     91        addSiteMetadata( page_response, lang, uid);
     92        return page_response;
     93    }
     94
    9295    // check that we have some service params
    9396    HashMap service_params = (HashMap)params.get("s1");
    9497    if (service_params == null) { // no query
     98        //append site metadata
     99        addSiteMetadata( page_response, lang, uid);
    95100        return page_response;
    96101    }
     
    116121    // check for errors
    117122    if (processErrorElements(mr_query_response, page_response)) {
     123        //append site metadata
     124        addSiteMetadata( page_response, lang, uid);
    118125        return page_response;
    119126    }
     
    143150        // add in a dummy doc node list - used by the display. need to think about this
    144151        page_response.appendChild(this.doc.createElement(GSXML.DOC_NODE_ELEM + GSXML.LIST_MODIFIER));
     152        //append site metadata
     153        addSiteMetadata( page_response, lang, uid);
    145154        return page_response;
    146155    }
     
    152161        // append the doc list to the result
    153162        page_response.appendChild(this.doc.importNode(document_list, true));
     163        //append site metadata
     164        addSiteMetadata( page_response, lang, uid);
    154165        return page_response;
    155166    }
     
    203214   
    204215    logger.debug("Query page:\n" + this.converter.getPrettyString(page_response));
     216    //append site metadata
     217    addSiteMetadata( page_response, lang, uid);
    205218    return page_response;
    206219    }
  • greenstone3/trunk/src/java/org/greenstone/gsdl3/core/MessageRouter.java

    r19893 r19984  
    107107  /** list of sites that can be reached */
    108108  protected Element site_list = null;
     109  /** list of metadata for the site */
     110  protected Element metadata_list = null;
    109111 
    110112 
     
    120122    this.converter = new XMLConverter();
    121123    this.doc = this.converter.newDOM();
     124    System.err.println("mr doc: " + doc );
    122125  }
    123126 
     
    420423    Element site_list_elem = (Element)GSXML.getChildByTagName(config_info, GSXML.SITE_ELEM+GSXML.LIST_MODIFIER);
    421424    configureExternalSites(site_list_elem);
     425
     426    // load up the site metadata
     427    this.metadata_list = this.doc.createElement(GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER);
     428    Element metadata_list_elem = (Element)GSXML.getChildByTagName(config_info, GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER);
     429    loadMetadata(metadata_list_elem);
     430
    422431       
    423432    return true;
     
    724733    return true;
    725734  }
     735
     736  /** Goes through the metadataList and loads each metadatum found */
     737  protected boolean loadMetadata(Element config_metadata_list) {
     738   
     739    // load up the sites
     740    logger.info("loading site metadata...");
     741    if (config_metadata_list ==null ) {
     742      logger.info("...none found");
     743      return true;
     744    }
     745   
     746    NodeList metadata = config_metadata_list.getElementsByTagName(GSXML.METADATA_ELEM);
     747    if (metadata.getLength()==0) {
     748      logger.info("...none found");
     749      return true;
     750    }
     751   
     752
     753    for (int i=0; i<metadata.getLength(); i++) {
     754      Element s = (Element)metadata.item(i);
     755      this.metadata_list.appendChild(this.doc.importNode(s, true));
     756    }
     757    return true;
     758  }
     759
    726760 
    727761  /** get site info from external site
     
    882916        response.appendChild(this.site_list);
    883917        response.appendChild(this.service_list);
     918        response.appendChild(this.metadata_list);
    884919        return response;
    885920      }
     921
    886922      NodeList params = param_list.getElementsByTagName(GSXML.PARAM_ELEM);
    887923     
     
    895931          if (info.equals(GSXML.COLLECTION_ELEM+GSXML.LIST_MODIFIER)) {
    896932            response.appendChild(this.collection_list);
    897            
    898933          } else if (info.equals(GSXML.CLUSTER_ELEM+GSXML.LIST_MODIFIER)) {
    899934            response.appendChild(this.cluster_list);
    900            
    901935          } else if (info.equals(GSXML.SERVICE_ELEM+GSXML.LIST_MODIFIER)) {
    902936            response.appendChild(this.service_list);
    903937          } else if (info.equals(GSXML.SITE_ELEM+GSXML.LIST_MODIFIER)) {
    904938            response.appendChild(this.site_list);
     939          } else if (info.equals(GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER)) {
     940            response.appendChild(this.metadata_list);
    905941          }
    906942        }
  • greenstone3/trunk/src/java/org/greenstone/gsdl3/service/BerryBasket.java

    r13270 r19984  
    3232import org.greenstone.gsdl3.util.GSXML;
    3333import org.greenstone.gsdl3.util.GSPath;
     34import org.greenstone.gsdl3.util.GlobalProperties;
    3435
    3536import java.net.InetAddress;
     
    483484
    484485
    485     public Element processSendMail(Element request){
    486     // Create a new (empty) result message
    487     Element result = this.doc.createElement(GSXML.RESPONSE_ELEM);
    488    
    489     // Get the parameters of the request
    490     Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
    491        
    492         GSXML.printXMLNode(param_list);
    493      
    494     if (param_list == null) {
    495         logger.error("BerryBasket Error: SendMail request had no paramList.");
    496         return result;  // Return the empty result
    497     }
    498 
    499     HashMap params = GSXML.extractParams(param_list, false);
    500      
    501     String to = (String)params.get("address");
    502         String subject = (String)params.get("subject");
    503     String mailhost = (String)params.get("host");
    504         String content =  (String)params.get("content");
    505         String cc = (String)params.get("cc");
    506         String bcc = (String)params.get("bcc");
    507         username = (String)params.get("user");
    508         password = (String)params.get("password");
    509     String mailer = "msgsend";       
    510        
    511     try {
    512        
    513         Properties props = System.getProperties();
    514         // XXX - could use Session.getTransport() and Transport.connect()
    515         // XXX - assume we're using SMTP
    516         // TODO: need to get Mail server info from the configuration file
    517         if (mailhost != null && mailhost.trim().equals(""))
    518         props.put("mail.smtp.host", mailhost);
    519         else{
    520         props.put("mail.smtp.host", "webmail.cs.waikato.ac.nz");
    521         }
    522 
    523         // TODO: need to get account info from the configuration file
    524             Authenticator auth = new Authenticator(){
    525          protected  PasswordAuthentication  getPasswordAuthentication(){
    526              if (username == null || username.equals("")){
    527              username = "xxxxx";
    528                          password = "xxxxx";
    529              }
    530               return new PasswordAuthentication(username,password);
    531           }
    532             };
    533    
    534            Session session = Session.getInstance(props, auth);
    535 
    536        Message msg = new MimeMessage(session);
    537        msg.setFrom();
    538 
    539        msg.setRecipients(Message.RecipientType.TO,
    540                  InternetAddress.parse(to, false));
    541        if (cc != null)
    542            msg.setRecipients(Message.RecipientType.CC,
    543                  InternetAddress.parse(cc, false));
    544         if (bcc != null)
    545         msg.setRecipients(Message.RecipientType.BCC,
    546                   InternetAddress.parse(bcc, false));
    547 
    548         msg.setSubject(subject);
    549        
    550             msg.setText(content.replaceAll("-------","&"));
    551         msg.setHeader("X-Mailer", mailer);
    552         msg.setSentDate(new Date());
    553 
    554         // send the thing off
    555         Transport.send(msg);
    556        
    557         logger.info("\nMail was sent successfully.");
    558             result.appendChild(this.doc.createTextNode("Mail was sent successfully."));
    559     } catch (Exception e) {
    560         e.printStackTrace();
    561             result.appendChild(this.doc.createTextNode(e.getMessage()));
    562     }
    563    
    564     return result;
    565     }
    566 
    567     protected class Item {
    568 
    569     public String collection;
    570     public String docid;
    571         public String title="";
    572         public String query="";
    573         public String date="";
    574         public String rootTitle="";
    575 
    576     public Item(String coll, String id) {
    577         this.collection = coll;
    578         this.docid = id;
    579         }
     486    public Element processSendMail(Element request){
     487        // Create a new (empty) result message
     488        Element result = this.doc.createElement(GSXML.RESPONSE_ELEM);
     489       
     490        // Get the parameters of the request
     491        Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
     492       
     493        GSXML.printXMLNode(param_list);
     494       
     495        if (param_list == null) {
     496            logger.error("BerryBasket Error: SendMail request had no paramList.");
     497            return result;  // Return the empty result
     498        }
     499       
     500        HashMap params = GSXML.extractParams(param_list, false);
     501       
     502        String to = (String)params.get("address");
     503        String subject = (String)params.get("subject");
     504        String content = (String)params.get("content");
     505        String cc = (String)params.get("cc");
     506        String bcc = (String)params.get("bcc");
     507       
     508        String mailhost = GlobalProperties.getProperty("mail.smtp.host");
     509        username = GlobalProperties.getProperty("mail.smtp.username");
     510        password = GlobalProperties.getProperty("mail.smtp.password");
     511        String from = GlobalProperties.getProperty("mail.from");
     512       
     513        String mailer = "msgsend";
     514       
     515        try {
     516           
     517            Properties props = System.getProperties();
     518           
     519            //Setup smtp host and from address
     520            // XXX - could use Session.getTransport() and Transport.connect()
     521            // XXX - assume we're using SMTP
     522            if ( mailhost != null && !mailhost.trim().equals("") ) {
     523                props.put("mail.smtp.host", mailhost);
     524            } else {
     525                props.put("mail.smtp.host", "localhost");
     526            }
     527            if ( from != null && !from.trim().equals("") ) {
     528                props.put("mail.from", from);
     529            }
     530           
     531            //setup username and password to the smtp server
     532            if ( username == null || username.trim().equals("") ) username = "";
     533            if ( password == null || password.trim().equals("") ) password = "";
     534            Authenticator auth = new Authenticator() {
     535                protected PasswordAuthentication getPasswordAuthentication() {
     536                    return new PasswordAuthentication( username, password );
     537                }
     538            };
     539           
     540            Session session = Session.getInstance(props, auth);
     541           
     542            Message msg = new MimeMessage(session);
     543            msg.setFrom();
     544            msg.setRecipients( Message.RecipientType.TO, InternetAddress.parse(to, false) );
     545            if ( cc != null ) {
     546                msg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(cc, false) );
     547            }
     548            if ( bcc != null ) {
     549                msg.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(bcc, false) );
     550            }
     551            msg.setSubject(subject);
     552            msg.setText(content.replaceAll("-------","&"));
     553            msg.setHeader("X-Mailer", mailer);
     554            msg.setSentDate(new Date());
     555           
     556            // send the thing off
     557            Transport.send(msg);
     558           
     559            logger.info("\nMail was sent successfully.");
     560            result.appendChild(this.doc.createTextNode("Mail was sent successfully."));
     561        } catch (Exception e) {
     562            e.printStackTrace();
     563            result.appendChild(this.doc.createTextNode(e.getMessage()));
     564        }
     565       
     566        return result;
     567    }
     568
     569
     570    protected class Item {
     571        public String collection;
     572        public String docid;
     573        public String title="";
     574        public String query="";
     575        public String date="";
     576        public String rootTitle="";
     577       
     578        public Item(String coll, String id) {
     579            this.collection = coll;
     580            this.docid = id;
     581        }
    580582
    581583        public boolean equals(Object o){
  • greenstone3/trunk/src/java/org/greenstone/gsdl3/util/GSXML.java

    r16882 r19984  
    2525import org.w3c.dom.Document;
    2626import org.w3c.dom.Text;
     27
     28
     29import javax.xml.transform.TransformerFactory;
     30import javax.xml.transform.Transformer;
     31import java.io.StringWriter;
     32import javax.xml.transform.stream.StreamResult;
     33import javax.xml.transform.dom.DOMSource;
    2734
    2835import java.util.Map;
     
    970977   
    971978  }
     979
     980  public static void elementToLogAsString(Element e) {
     981    try {
     982      TransformerFactory tf = TransformerFactory.newInstance();
     983      Transformer trans = tf.newTransformer();
     984      StringWriter sw = new StringWriter();
     985      trans.transform(new DOMSource(e), new StreamResult(sw));
     986      System.err.println( sw.toString() );
     987    } catch( Exception ex ) {
     988      System.err.println( "couldn't write " + e + " to log" );
     989    }
     990
     991  }
    972992}
Note: See TracChangeset for help on using the changeset viewer.