(no commit message)
[utils] / crawler / kiss / src / main / java / org / wamblee / crawler / kiss / notification / MailNotifier.java
1 /*
2  * Copyright 2006 the original author or authors.
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.wamblee.crawler.kiss.notification;
17
18 import java.io.IOException;
19 import java.util.Arrays;
20 import java.util.Date;
21
22 import javax.mail.internet.AddressException;
23 import javax.mail.internet.InternetAddress;
24 import javax.xml.transform.TransformerException;
25
26 import org.apache.commons.mail.EmailException;
27 import org.apache.commons.mail.HtmlEmail;
28 import org.dom4j.Element;
29 import org.wamblee.xml.XslTransformer;
30
31 /**
32  * A notifier that uses SMTP to notify users by mail.
33  * 
34  *
35  * @author Erik Brakkee
36  */
37 public class MailNotifier implements Notifier {
38
39     private String _from;
40
41     private String _to;
42
43     private String _subject;
44
45     private String _htmlXslt;
46
47     private String _textXslt;
48
49     private MailServer _server;
50     
51     private XslTransformer _transformer;
52
53     /**
54      * Constructs the notifier.
55      * 
56      * @param aFrom
57      *            Sender mail address to use.
58      * @param aTo
59      *            Recipient mail address to use.
60      * @param aSubject
61      *            Subject to use in the email.
62      * @param aHtmlXslt
63      *            XSLT file to transform the report into HTML.
64      * @param aTextXslt
65      *            XSLT file to transform the report into text.
66      * @param aServer
67      *            Mail server to use.
68      * @param aTransformer Transformer to use. 
69      */
70     public MailNotifier(String aFrom, String aTo, String aSubject,
71             String aHtmlXslt, String aTextXslt, MailServer aServer, XslTransformer aTransformer) {
72         _from = aFrom;
73         _to = aTo;
74         _subject = aSubject;
75         _htmlXslt = aHtmlXslt;
76         _textXslt = aTextXslt;
77         _server = aServer;
78         _transformer = aTransformer;
79     }
80
81     /*
82      * (non-Javadoc)
83      * 
84      * @see org.wamblee.crawler.kiss.Notifier#send(org.dom4j.Element)
85      */
86     public void send(Element aReport) throws NotificationException {
87         HtmlEmail mail = new HtmlEmail();
88         try {
89             mail.setFrom(_from);
90             mail
91                     .setTo(Arrays
92                             .asList(new InternetAddress[] { new InternetAddress(
93                                     _to) }));
94             mail.setSentDate(new Date());
95             mail.setSubject(_subject);
96
97             String htmlText = transformReport(aReport, _htmlXslt);
98             String plainText = transformReport(aReport, _textXslt);
99
100             mail.setHtmlMsg(htmlText);
101             mail.setTextMsg(plainText);
102
103             _server.send(mail);
104         } catch (EmailException e) {
105             throw new NotificationException(e.getMessage(), e);
106         } catch (TransformerException e) {
107             throw new NotificationException(e.getMessage(), e);
108         } catch (IOException e) {
109             throw new NotificationException(e.getMessage(), e);
110         } catch (AddressException e) {
111             throw new NotificationException(e.getMessage(), e);
112         }
113     }
114
115     /**
116      * Transforms a report into a destination format.
117      * 
118      * @param aReport
119      *            Report to transform
120      * @param aXslt
121      *            XSLT to use.
122      * @return Transformed result.
123      * @throws IOException
124      *             In case of IO problems.
125      * @throws TransformerException
126      *             In case of problems transforming.
127      */
128     private String transformReport(Element aReport, String aXslt)
129             throws IOException, TransformerException {
130         String reportXmlText = aReport.asXML();
131         return _transformer.textTransform(reportXmlText.getBytes(), _transformer.resolve(aXslt));
132     }
133     
134     /* (non-Javadoc)
135      * @see org.wamblee.crawler.kiss.notification.Notifier#asHtml(org.dom4j.Element)
136      */
137     public String asHtml(Element aReport) throws IOException, TransformerException {
138         return transformReport(aReport, _htmlXslt);
139     }
140     
141     /* (non-Javadoc)
142      * @see org.wamblee.crawler.kiss.notification.Notifier#asText(org.dom4j.Element)
143      */
144     public String asText(Element aReport) throws IOException, TransformerException {
145         return transformReport(aReport, _textXslt);
146     }
147 }