ab0c5469b5bdbf67af0bcbc9dc57b8b8ca8ca298
[utils] / crawler / kiss / src / org / wamblee / crawler / kiss / MailNotifier.java
1 /**
2  * Copyright (c) 2005 UPS_SCS NL
3  *  
4  */
5 package org.wamblee.crawler.kiss;
6
7 import java.io.ByteArrayOutputStream;
8 import java.io.File;
9 import java.io.IOException;
10 import java.util.Arrays;
11 import java.util.Date;
12
13 import javax.mail.internet.AddressException;
14 import javax.mail.internet.InternetAddress;
15 import javax.xml.transform.TransformerException;
16
17 import org.apache.commons.mail.EmailException;
18 import org.apache.commons.mail.HtmlEmail;
19 import org.apache.xml.serialize.OutputFormat;
20 import org.apache.xml.serialize.XMLSerializer;
21 import org.dom4j.Element;
22 import org.w3c.dom.Document;
23 import org.wamblee.io.FileResource;
24 import org.wamblee.io.InputResource;
25 import org.wamblee.xml.XSLT;
26
27 /**
28  * A notifier that uses SMTP to notify users by mail.
29  * 
30  */
31 public class MailNotifier implements Notifier {
32
33     private String _from;
34
35     private String _to;
36
37     private String _subject;
38
39     private String _htmlXslt;
40
41     private String _textXslt;
42
43     private MailServer _server;
44
45     /**
46      * Constructs the notifier.
47      * 
48      * @param aFrom
49      *            Sender mail address to use.
50      * @param aTo
51      *            Recipient mail address to use.
52      * @param aSubject
53      *            Subject to use in the email.
54      * @param aHtmlXslt
55      *            XSLT file to transform the report into HTML.
56      * @param aTextXslt
57      *            XSLT file to transform the report into text.
58      * @param aServer
59      *            Mail server to use.
60      */
61     public MailNotifier(String aFrom, String aTo, String aSubject,
62             String aHtmlXslt, String aTextXslt, MailServer aServer) {
63         _from = aFrom;
64         _to = aTo;
65         _subject = aSubject;
66         _htmlXslt = aHtmlXslt;
67         _textXslt = aTextXslt;
68         _server = aServer;
69     }
70
71     /*
72      * (non-Javadoc)
73      * 
74      * @see org.wamblee.crawler.kiss.Notifier#send(org.dom4j.Element)
75      */
76     public void send(Element aReport) throws NotificationException {
77         HtmlEmail mail = new HtmlEmail();
78         try {
79             mail.setFrom(_from);
80             mail
81                     .setTo(Arrays
82                             .asList(new InternetAddress[] { new InternetAddress(
83                                     _to) }));
84             mail.setSentDate(new Date());
85             mail.setSubject(_subject);
86
87             String htmlText = transformReport(aReport, new FileResource(
88                     new File(_htmlXslt)));
89             String plainText = transformReport(aReport, new FileResource(
90                     new File(_textXslt)));
91
92             mail.setHtmlMsg(htmlText);
93             mail.setTextMsg(plainText);
94
95             _server.send(mail);
96         } catch (EmailException e) {
97             throw new NotificationException(e.getMessage(), e);
98         } catch (TransformerException e) {
99             throw new NotificationException(e.getMessage(), e);
100         } catch (IOException e) {
101             throw new NotificationException(e.getMessage(), e);
102         } catch (AddressException e) {
103             throw new NotificationException(e.getMessage(), e);
104         }
105     }
106
107     /**
108      * Transforms a report into a destination format.
109      * 
110      * @param aReport
111      *            Report to transform
112      * @param aXslt
113      *            XSLT to use.
114      * @return Transformed result.
115      * @throws IOException
116      *             In case of IO problems.
117      * @throws TransformerException
118      *             In case of problems transforming.
119      */
120     private String transformReport(Element aReport, InputResource aXslt)
121             throws IOException, TransformerException {
122         String reportXmlText = aReport.asXML();
123         return new XSLT().textTransform(reportXmlText.getBytes(), aXslt);
124     }
125 }