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