001    package net.sf.logdistiller.publishers;
002    
003    /*
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     *     http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    
017    import java.io.*;
018    import java.util.Properties;
019    import javax.activation.DataHandler;
020    import javax.activation.DataSource;
021    import javax.activation.FileDataSource;
022    import javax.mail.*;
023    import javax.mail.internet.*;
024    
025    import net.sf.logdistiller.*;
026    
027    /**
028     * Send mail report of a LogDistillation. When the report is a group report, a file containing logevents (if saved) is
029     * added as attachment. Global output parameters are used as JavaMail session properties: the most important property is
030     * <code>mail.smtp.host</code>, but see JavaMail documentation to find all available properties.
031     * <p>
032     * Parameters:
033     * <ul>
034     * <li><code>to</code>, <code>cc</code> (at least one): recipients of the mail, comma separated values if multiple
035     * recipients
036     * <li><code>from</code> (default: <code>smtp.from</code> global output parameter, or
037     * <code>logdistiller@sourceforge.net</code>
038     * <li><code>attachLogs</code> (default: <code>true</code>): add corresponding logevents as attachment
039     * </ul>
040     */
041    public class MailPublisher
042        extends Publisher
043    {
044        public String getId()
045        {
046            return "mail";
047        }
048    
049        public void publish( LogDistillation logdistillation, LogDistiller.Report report )
050            throws IOException, PublishException
051        {
052            String content = logdistillation.getContent();
053            String subject =
054                "[logdistiller:" + logdistillation.getDefinition().getOutput().getId() + "]"
055                    + ( ( content == null ) ? "" : ( " " + content ) ) + " global report: "
056                    + logdistillation.getEventCount() + " logevents";
057            ReportFormat format = ReportFormats.getReportFormat( report.getFormat() );
058            StringWriter sw = new StringWriter();
059            format.report( logdistillation, sw );
060            String body = sw.toString();
061            try
062            {
063                sendMail( logdistillation, report, subject, body, format.getContentType(), null );
064            }
065            catch ( MessagingException me )
066            {
067                throw new PublishException( "error while sending global report by mail: " + me.getMessage(), me );
068            }
069        }
070    
071        public void publish( LogDistillation.Group group, LogDistiller.Report report )
072            throws IOException, PublishException
073        {
074            LogDistillation logdistillation = group.getLogdistillation();
075            String content = logdistillation.getContent();
076            String subject =
077                "[logdistiller:" + logdistillation.getDefinition().getOutput().getId() + "]"
078                    + ( ( content == null ) ? "" : ( " " + content ) ) + " report for group "
079                    + group.getDefinition().getId() + ": " + group.getEventCount() + " logevents";
080            ReportFormat format = ReportFormats.getReportFormat( report.getFormat() );
081            StringWriter sw = new StringWriter();
082            format.report( group, sw );
083            String body = sw.toString();
084            try
085            {
086                sendMail( logdistillation, report, subject, body, format.getContentType(), group.getLogFile() );
087            }
088            catch ( MessagingException me )
089            {
090                throw new PublishException( "error while sending group report by mail: " + me.getMessage(), me );
091            }
092        }
093    
094        protected void sendMail( LogDistillation logdistillation, LogDistiller.Report report, String subject,
095                                 String content, String mimetype, File attach )
096            throws MessagingException
097        {
098            LogDistiller ld = logdistillation.getDefinition();
099            String to = report.getParam( "to" );
100            String cc = report.getParam( "cc" );
101            if ( ( to == null ) && ( cc == null ) )
102            {
103                // nobody to send to: TODO add warning
104                return;
105            }
106            Properties props = new Properties();
107            props.putAll( ld.getOutput().getParams() );
108            Session session = Session.getDefaultInstance( props, null );
109            MimeMessage msg = new MimeMessage( session );
110    
111            msg.setSubject( subject );
112            Multipart mp = new MimeMultipart();
113            BodyPart body = new MimeBodyPart();
114            body.setContent( content, mimetype );
115            mp.addBodyPart( body );
116            msg.setContent( mp );
117            if ( to != null )
118            {
119                msg.setRecipients( Message.RecipientType.TO, InternetAddress.parse( to ) );
120            }
121            if ( cc != null )
122            {
123                msg.setRecipients( Message.RecipientType.CC, InternetAddress.parse( cc ) );
124            }
125            String from = report.getParam( "from", "logdistiller@sourceforge.net" );
126            msg.setFrom( new InternetAddress( from ) );
127    
128            String attachLogs = report.getParam( "attachLogs", "true" );
129            if ( ( attach != null ) && ( attach.exists() ) && ( "true".equals( attachLogs ) ) )
130            {
131                BodyPart attachment = new MimeBodyPart();
132                DataSource ds = new FileDataSource( attach );
133                attachment.setDataHandler( new DataHandler( ds ) );
134                attachment.setFileName( attach.getName() );
135                mp.addBodyPart( attachment );
136            }
137    
138            sendMail( msg );
139        }
140    
141        protected void sendMail( Message msg )
142            throws MessagingException
143        {
144            Transport.send( msg );
145        }
146    }