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