2 * Copyright (c) 2005 UPS_SCS NL
5 package org.wamblee.crawler.kiss.notification;
7 import java.io.IOException;
8 import java.util.Arrays;
11 import javax.mail.internet.AddressException;
12 import javax.mail.internet.InternetAddress;
13 import javax.xml.transform.TransformerException;
15 import org.apache.commons.mail.EmailException;
16 import org.apache.commons.mail.HtmlEmail;
17 import org.dom4j.Element;
18 import org.wamblee.xml.XslTransformer;
21 * A notifier that uses SMTP to notify users by mail.
24 public class MailNotifier implements Notifier {
30 private String _subject;
32 private String _htmlXslt;
34 private String _textXslt;
36 private MailServer _server;
38 private XslTransformer _transformer;
41 * Constructs the notifier.
44 * Sender mail address to use.
46 * Recipient mail address to use.
48 * Subject to use in the email.
50 * XSLT file to transform the report into HTML.
52 * XSLT file to transform the report into text.
55 * @param aTransformer Transformer to use.
57 public MailNotifier(String aFrom, String aTo, String aSubject,
58 String aHtmlXslt, String aTextXslt, MailServer aServer, XslTransformer aTransformer) {
62 _htmlXslt = aHtmlXslt;
63 _textXslt = aTextXslt;
65 _transformer = aTransformer;
71 * @see org.wamblee.crawler.kiss.Notifier#send(org.dom4j.Element)
73 public void send(Element aReport) throws NotificationException {
74 HtmlEmail mail = new HtmlEmail();
79 .asList(new InternetAddress[] { new InternetAddress(
81 mail.setSentDate(new Date());
82 mail.setSubject(_subject);
84 String htmlText = transformReport(aReport, _htmlXslt);
85 String plainText = transformReport(aReport, _textXslt);
87 mail.setHtmlMsg(htmlText);
88 mail.setTextMsg(plainText);
91 } catch (EmailException e) {
92 throw new NotificationException(e.getMessage(), e);
93 } catch (TransformerException e) {
94 throw new NotificationException(e.getMessage(), e);
95 } catch (IOException e) {
96 throw new NotificationException(e.getMessage(), e);
97 } catch (AddressException e) {
98 throw new NotificationException(e.getMessage(), e);
103 * Transforms a report into a destination format.
106 * Report to transform
109 * @return Transformed result.
110 * @throws IOException
111 * In case of IO problems.
112 * @throws TransformerException
113 * In case of problems transforming.
115 private String transformReport(Element aReport, String aXslt)
116 throws IOException, TransformerException {
117 String reportXmlText = aReport.asXML();
118 return _transformer.textTransform(reportXmlText.getBytes(), _transformer.resolve(aXslt));