001    //$HeadURL: svn+ssh://jwilden@svn.wald.intevation.org/deegree/base/branches/2.5_testing/src/org/deegree/portal/owswatch/EmailSender.java $
002    /*----------------------------------------------------------------------------
003     This file is part of deegree, http://deegree.org/
004     Copyright (C) 2001-2009 by:
005       Department of Geography, University of Bonn
006     and
007       lat/lon GmbH
008    
009     This library is free software; you can redistribute it and/or modify it under
010     the terms of the GNU Lesser General Public License as published by the Free
011     Software Foundation; either version 2.1 of the License, or (at your option)
012     any later version.
013     This library is distributed in the hope that it will be useful, but WITHOUT
014     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
015     FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
016     details.
017     You should have received a copy of the GNU Lesser General Public License
018     along with this library; if not, write to the Free Software Foundation, Inc.,
019     59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020    
021     Contact information:
022    
023     lat/lon GmbH
024     Aennchenstr. 19, 53177 Bonn
025     Germany
026     http://lat-lon.de/
027    
028     Department of Geography, University of Bonn
029     Prof. Dr. Klaus Greve
030     Postfach 1147, 53001 Bonn
031     Germany
032     http://www.geographie.uni-bonn.de/deegree/
033    
034     e-mail: info@deegree.org
035    ----------------------------------------------------------------------------*/
036    
037    package org.deegree.portal.owswatch;
038    
039    import java.io.Serializable;
040    import java.util.Iterator;
041    import java.util.List;
042    
043    import org.deegree.framework.log.ILogger;
044    import org.deegree.framework.log.LoggerFactory;
045    import org.deegree.framework.mail.EMailMessage;
046    import org.deegree.framework.mail.MailHelper;
047    import org.deegree.framework.mail.MailMessage;
048    import org.deegree.framework.mail.UnknownMimeTypeException;
049    
050    /**
051     * This class responsible for sending emails to the registered users in case of faulty tests to the services
052     *
053     * @author <a href="mailto:elmasry@lat-lon.de">Moataz Elmasry</a>
054     * @author last edited by: $Author: jmays $
055     *
056     * @version $Revision: 20271 $, $Date: 2009-10-21 13:07:15 +0200 (Mi, 21 Okt 2009) $
057     */
058    public class EmailSender implements Serializable {
059    
060        /**
061         *
062         */
063        private static final long serialVersionUID = -6179048752410881409L;
064    
065        private static final ILogger LOG = LoggerFactory.getLogger( EmailSender.class );
066    
067        private String mailFrom = null;
068    
069        private String mailServer = null;
070    
071        private List<String> receivers = null;
072    
073        /**
074         * @param mailFrom
075         * @param mailServer
076         * @param receivers
077         */
078        public EmailSender( String mailFrom, String mailServer, List<String> receivers ) {
079    
080            this.mailFrom = mailFrom;
081            this.mailServer = mailServer;
082            this.receivers = receivers;
083        }
084    
085        protected String createEmailBody( ServiceConfiguration serviceConfiguration, ValidatorResponse response,
086                                          String protocolHttpaddress ) {
087    
088            // create body (allways the same):
089            StringBuffer body = new StringBuffer( 500 );
090            String protHref = Messages.getMessage( "SendMail.protocolAddress", protocolHttpaddress, protocolHttpaddress );
091            body.append( Messages.getString( "SendMail.header" ) );
092            body.append( Messages.getString( "SendMail.addressee" ) );
093            body.append( Messages.getMessage( "SendMail.messageBody", serviceConfiguration.getServiceName() ) );
094            body.append( Messages.getMessage( "SendMail.errorLog", serviceConfiguration.getOnlineResource(),
095                                              serviceConfiguration.getServiceVersion(),
096                                              serviceConfiguration.getRequestType(), response.getLastTest().toString(),
097                                              response.getMessage(), protHref ) );
098            body.append( Messages.getMessage( "SendMail.closingWords" ) );
099            body.append( Messages.getString( "SendMail.signer" ) );
100            body.append( Messages.getString( "SendMail.footer" ) );
101            return body.toString();
102        }
103    
104        /**
105         * this method creates a mail from text fragments in the messages_en.properties and sends it to the owsWatch admin
106         * as defined in the WEB-INF/conf/owswatch/config.xml file.
107         *
108         * If sending the mail fails, an error messages is returned, else a success message is returned.
109         *
110         * @param serviceConfiguration
111         * @param response
112         * @param protocolHttpaddress
113         *
114         */
115        public void createAndSendMail( ServiceConfiguration serviceConfiguration, ValidatorResponse response,
116                                       String protocolHttpaddress ) {
117    
118            String body = createEmailBody( serviceConfiguration, response, protocolHttpaddress );
119            String error = "";
120            String subject = Messages.getString( "SendMail.subject" );
121    
122            Iterator<String> it = receivers.iterator();
123            while ( it.hasNext() ) {
124                // send message to the user
125                EMailMessage mm = new EMailMessage( mailFrom, it.next(), subject, body.toString() );
126                try {
127                    mm.setMimeType( MailMessage.PLAIN_TEXT );
128                } catch ( UnknownMimeTypeException e ) {
129                    String errorMsg = "ERROR_UNDEFINED_TYPE";
130                    LOG.logError( errorMsg );
131                    continue;
132                }
133    
134                try {
135                    MailHelper.createAndSendMail( mm, mailServer );
136                } catch ( Exception e ) {
137                    error = Messages.arrayToString( new String[] {
138                                                                  e.getLocalizedMessage(),
139                                                                  Messages.getMessage( "ERROR_SEND_MAIL", receivers,
140                                                                                       mailFrom, mailServer ),
141                                                                  response.getMessage() }, "\n" );
142                    LOG.logError( error );
143                }
144            }
145        }
146    }