(no commit message)
[utils] / crawler / kiss / src / 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 public class MailNotifier implements Notifier {
36
37     private String _from;
38
39     private String _to;
40
41     private String _subject;
42
43     private String _htmlXslt;
44
45     private String _textXslt;
46
47     private MailServer _server;
48     
49     private XslTransformer _transformer;
50
51     /**
52      * Constructs the notifier.
53      * 
54      * @param aFrom
55      *            Sender mail address to use.
56      * @param aTo
57      *            Recipient mail address to use.
58      * @param aSubject
59      *            Subject to use in the email.
60      * @param aHtmlXslt
61      *            XSLT file to transform the report into HTML.
62      * @param aTextXslt
63      *            XSLT file to transform the report into text.
64      * @param aServer
65      *            Mail server to use.
66      * @param aTransformer Transformer to use. 
67      */
68     public MailNotifier(String aFrom, String aTo, String aSubject,
69             String aHtmlXslt, String aTextXslt, MailServer aServer, XslTransformer aTransformer) {
70         _from = aFrom;
71         _to = aTo;
72         _subject = aSubject;
73         _htmlXslt = aHtmlXslt;
74         _textXslt = aTextXslt;
75         _server = aServer;
76         _transformer = aTransformer;
77     }
78
79     /*
80      * (non-Javadoc)
81      * 
82      * @see org.wamblee.crawler.kiss.Notifier#send(org.dom4j.Element)
83      */
84     public void send(Element aReport) throws NotificationException {
85         HtmlEmail mail = new HtmlEmail();
86         try {
87             mail.setFrom(_from);
88             mail
89                     .setTo(Arrays
90                             .asList(new InternetAddress[] { new InternetAddress(
91                                     _to) }));
92             mail.setSentDate(new Date());
93             mail.setSubject(_subject);
94
95             String htmlText = transformReport(aReport, _htmlXslt);
96             String plainText = transformReport(aReport, _textXslt);
97
98             mail.setHtmlMsg(htmlText);
99             mail.setTextMsg(plainText);
100
101             _server.send(mail);
102         } catch (EmailException e) {
103             throw new NotificationException(e.getMessage(), e);
104         } catch (TransformerException e) {
105             throw new NotificationException(e.getMessage(), e);
106         } catch (IOException e) {
107             throw new NotificationException(e.getMessage(), e);
108         } catch (AddressException e) {
109             throw new NotificationException(e.getMessage(), e);
110         }
111     }
112
113     /**
114      * Transforms a report into a destination format.
115      * 
116      * @param aReport
117      *            Report to transform
118      * @param aXslt
119      *            XSLT to use.
120      * @return Transformed result.
121      * @throws IOException
122      *             In case of IO problems.
123      * @throws TransformerException
124      *             In case of problems transforming.
125      */
126     private String transformReport(Element aReport, String aXslt)
127             throws IOException, TransformerException {
128         String reportXmlText = aReport.asXML();
129         return _transformer.textTransform(reportXmlText.getBytes(), _transformer.resolve(aXslt));
130     }
131 }