View Javadoc
1   package net.sf.logdistiller.publishers;
2   
3   /*
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  import java.io.*;
18  import java.util.Properties;
19  import javax.activation.DataHandler;
20  import javax.activation.DataSource;
21  import javax.activation.FileDataSource;
22  import javax.mail.*;
23  import javax.mail.internet.*;
24  
25  import net.sf.logdistiller.*;
26  
27  /**
28   * Send mail report of a LogDistillation. When the report is a group report, a file containing logevents (if saved) is
29   * added as attachment. Global output parameters are used as JavaMail session properties: the most important property is
30   * <code>mail.smtp.host</code>, but see JavaMail documentation to find all available properties.
31   * <p>
32   * Parameters:
33   * <ul>
34   * <li><code>to</code>, <code>cc</code> (at least one): recipients of the mail, comma separated values if multiple
35   * recipients
36   * <li><code>from</code> (default: <code>smtp.from</code> global output parameter, or
37   * <code>logdistiller@sourceforge.net</code>
38   * <li><code>attachLogs</code> (default: <code>true</code>): add corresponding logevents as attachment
39   * </ul>
40   */
41  public class MailPublisher
42      extends Publisher
43  {
44      public String getId()
45      {
46          return "mail";
47      }
48  
49      public void publish( LogDistillation logdistillation, LogDistiller.Report report )
50          throws IOException, PublishException
51      {
52          String content = logdistillation.getContent();
53          String subject =
54              "[logdistiller:" + logdistillation.getDefinition().getOutput().getId() + "]"
55                  + ( ( content == null ) ? "" : ( " " + content ) ) + " global report: "
56                  + logdistillation.getEventCount() + " logevents";
57          ReportFormat format = ReportFormats.getReportFormat( report.getFormat() );
58          StringWriter sw = new StringWriter();
59          format.report( logdistillation, sw );
60          String body = sw.toString();
61          try
62          {
63              sendMail( logdistillation, report, subject, body, format.getContentType(), null );
64          }
65          catch ( MessagingException me )
66          {
67              throw new PublishException( "error while sending global report by mail: " + me.getMessage(), me );
68          }
69      }
70  
71      public void publish( LogDistillation.Group group, LogDistiller.Report report )
72          throws IOException, PublishException
73      {
74          LogDistillation logdistillation = group.getLogdistillation();
75          String content = logdistillation.getContent();
76          String subject =
77              "[logdistiller:" + logdistillation.getDefinition().getOutput().getId() + "]"
78                  + ( ( content == null ) ? "" : ( " " + content ) ) + " report for group "
79                  + group.getDefinition().getId() + ": " + group.getEventCount() + " logevents";
80          ReportFormat format = ReportFormats.getReportFormat( report.getFormat() );
81          StringWriter sw = new StringWriter();
82          format.report( group, sw );
83          String body = sw.toString();
84          try
85          {
86              sendMail( logdistillation, report, subject, body, format.getContentType(), group.getLogFile() );
87          }
88          catch ( MessagingException me )
89          {
90              throw new PublishException( "error while sending group report by mail: " + me.getMessage(), me );
91          }
92      }
93  
94      protected void sendMail( LogDistillation logdistillation, LogDistiller.Report report, String subject,
95                               String content, String mimetype, File attach )
96          throws MessagingException
97      {
98          LogDistiller ld = logdistillation.getDefinition();
99          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 }