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

initial implementation of support for site-level metadata

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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){
Note: See TracChangeset for help on using the changeset viewer.