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 * @author Erik Brakkee
37 public class MailNotifier implements Notifier {
43 private String _subject;
45 private String _htmlXslt;
47 private String _textXslt;
49 private MailServer _server;
51 private XslTransformer _transformer;
54 * Constructs the notifier.
57 * Sender mail address to use.
59 * Recipient mail address to use.
61 * Subject to use in the email.
63 * XSLT file to transform the report into HTML.
65 * XSLT file to transform the report into text.
68 * @param aTransformer Transformer to use.
70 public MailNotifier(String aFrom, String aTo, String aSubject,
71 String aHtmlXslt, String aTextXslt, MailServer aServer, XslTransformer aTransformer) {
75 _htmlXslt = aHtmlXslt;
76 _textXslt = aTextXslt;
78 _transformer = aTransformer;
84 * @see org.wamblee.crawler.kiss.Notifier#send(org.dom4j.Element)
86 public void send(Element aReport) throws NotificationException {
87 HtmlEmail mail = new HtmlEmail();
92 .asList(new InternetAddress[] { new InternetAddress(
94 mail.setSentDate(new Date());
95 mail.setSubject(_subject);
97 String htmlText = transformReport(aReport, _htmlXslt);
98 String plainText = transformReport(aReport, _textXslt);
100 mail.setHtmlMsg(htmlText);
101 mail.setTextMsg(plainText);
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);
116 * Transforms a report into a destination format.
119 * Report to transform
122 * @return Transformed result.
123 * @throws IOException
124 * In case of IO problems.
125 * @throws TransformerException
126 * In case of problems transforming.
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));
135 * @see org.wamblee.crawler.kiss.notification.Notifier#asHtml(org.dom4j.Element)
137 public String asHtml(Element aReport) throws IOException, TransformerException {
138 return transformReport(aReport, _htmlXslt);
142 * @see org.wamblee.crawler.kiss.notification.Notifier#asText(org.dom4j.Element)
144 public String asText(Element aReport) throws IOException, TransformerException {
145 return transformReport(aReport, _textXslt);