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