2 * Copyright 2006 the original author or authors.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.wamblee.crawler.kiss.notification;
18 import java.io.IOException;
19 import java.util.Arrays;
20 import java.util.Date;
22 import javax.mail.internet.AddressException;
23 import javax.mail.internet.InternetAddress;
24 import javax.xml.transform.TransformerException;
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;
32 * A notifier that uses SMTP to notify users by mail.
35 public class MailNotifier implements Notifier {
41 private String _subject;
43 private String _htmlXslt;
45 private String _textXslt;
47 private MailServer _server;
49 private XslTransformer _transformer;
52 * Constructs the notifier.
55 * Sender mail address to use.
57 * Recipient mail address to use.
59 * Subject to use in the email.
61 * XSLT file to transform the report into HTML.
63 * XSLT file to transform the report into text.
66 * @param aTransformer Transformer to use.
68 public MailNotifier(String aFrom, String aTo, String aSubject,
69 String aHtmlXslt, String aTextXslt, MailServer aServer, XslTransformer aTransformer) {
73 _htmlXslt = aHtmlXslt;
74 _textXslt = aTextXslt;
76 _transformer = aTransformer;
82 * @see org.wamblee.crawler.kiss.Notifier#send(org.dom4j.Element)
84 public void send(Element aReport) throws NotificationException {
85 HtmlEmail mail = new HtmlEmail();
90 .asList(new InternetAddress[] { new InternetAddress(
92 mail.setSentDate(new Date());
93 mail.setSubject(_subject);
95 String htmlText = transformReport(aReport, _htmlXslt);
96 String plainText = transformReport(aReport, _textXslt);
98 mail.setHtmlMsg(htmlText);
99 mail.setTextMsg(plainText);
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);
114 * Transforms a report into a destination format.
117 * Report to transform
120 * @return Transformed result.
121 * @throws IOException
122 * In case of IO problems.
123 * @throws TransformerException
124 * In case of problems transforming.
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));
133 * @see org.wamblee.crawler.kiss.notification.Notifier#asHtml(org.dom4j.Element)
135 public String asHtml(Element aReport) throws IOException, TransformerException {
136 return transformReport(aReport, _htmlXslt);
140 * @see org.wamblee.crawler.kiss.notification.Notifier#asText(org.dom4j.Element)
142 public String asText(Element aReport) throws IOException, TransformerException {
143 return transformReport(aReport, _textXslt);