2 * Copyright (c) 2005 UPS_SCS NL
5 package org.wamblee.crawler.kiss.notification;
8 import java.io.IOException;
9 import java.util.Arrays;
10 import java.util.Date;
12 import javax.mail.internet.AddressException;
13 import javax.mail.internet.InternetAddress;
14 import javax.xml.transform.TransformerException;
16 import org.apache.commons.mail.EmailException;
17 import org.apache.commons.mail.HtmlEmail;
18 import org.dom4j.Element;
19 import org.wamblee.io.FileResource;
20 import org.wamblee.io.InputResource;
21 import org.wamblee.xml.XSLT;
24 * A notifier that uses SMTP to notify users by mail.
27 public class MailNotifier implements Notifier {
33 private String _subject;
35 private String _htmlXslt;
37 private String _textXslt;
39 private MailServer _server;
42 * Constructs the notifier.
45 * Sender mail address to use.
47 * Recipient mail address to use.
49 * Subject to use in the email.
51 * XSLT file to transform the report into HTML.
53 * XSLT file to transform the report into text.
57 public MailNotifier(String aFrom, String aTo, String aSubject,
58 String aHtmlXslt, String aTextXslt, MailServer aServer) {
62 _htmlXslt = aHtmlXslt;
63 _textXslt = aTextXslt;
70 * @see org.wamblee.crawler.kiss.Notifier#send(org.dom4j.Element)
72 public void send(Element aReport) throws NotificationException {
73 HtmlEmail mail = new HtmlEmail();
78 .asList(new InternetAddress[] { new InternetAddress(
80 mail.setSentDate(new Date());
81 mail.setSubject(_subject);
83 String htmlText = transformReport(aReport, new FileResource(
84 new File(_htmlXslt)));
85 String plainText = transformReport(aReport, new FileResource(
86 new File(_textXslt)));
88 mail.setHtmlMsg(htmlText);
89 mail.setTextMsg(plainText);
92 } catch (EmailException e) {
93 throw new NotificationException(e.getMessage(), e);
94 } catch (TransformerException e) {
95 throw new NotificationException(e.getMessage(), e);
96 } catch (IOException e) {
97 throw new NotificationException(e.getMessage(), e);
98 } catch (AddressException e) {
99 throw new NotificationException(e.getMessage(), e);
104 * Transforms a report into a destination format.
107 * Report to transform
110 * @return Transformed result.
111 * @throws IOException
112 * In case of IO problems.
113 * @throws TransformerException
114 * In case of problems transforming.
116 private String transformReport(Element aReport, InputResource aXslt)
117 throws IOException, TransformerException {
118 String reportXmlText = aReport.asXML();
119 return new XSLT().textTransform(reportXmlText.getBytes(), aXslt);