1 package net.sf.logdistiller.publishers;
2
3
4
5
6
7
8
9
10
11
12
13
14
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
29
30
31
32
33
34
35
36
37
38
39
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
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 }