2 * Copyright (c) 2005 UPS_SCS NL
5 package org.wamblee.crawler.kiss.notification;
7 import java.io.ByteArrayOutputStream;
9 import java.io.IOException;
10 import java.util.Arrays;
11 import java.util.Date;
13 import javax.mail.internet.AddressException;
14 import javax.mail.internet.InternetAddress;
15 import javax.xml.transform.TransformerException;
17 import org.apache.commons.mail.EmailException;
18 import org.apache.commons.mail.HtmlEmail;
19 import org.apache.xml.serialize.OutputFormat;
20 import org.apache.xml.serialize.XMLSerializer;
21 import org.dom4j.Element;
22 import org.w3c.dom.Document;
23 import org.wamblee.io.FileResource;
24 import org.wamblee.io.InputResource;
25 import org.wamblee.xml.XSLT;
28 * A notifier that uses SMTP to notify users by mail.
31 public class MailNotifier implements Notifier {
37 private String _subject;
39 private String _htmlXslt;
41 private String _textXslt;
43 private MailServer _server;
46 * Constructs the notifier.
49 * Sender mail address to use.
51 * Recipient mail address to use.
53 * Subject to use in the email.
55 * XSLT file to transform the report into HTML.
57 * XSLT file to transform the report into text.
61 public MailNotifier(String aFrom, String aTo, String aSubject,
62 String aHtmlXslt, String aTextXslt, MailServer aServer) {
66 _htmlXslt = aHtmlXslt;
67 _textXslt = aTextXslt;
74 * @see org.wamblee.crawler.kiss.Notifier#send(org.dom4j.Element)
76 public void send(Element aReport) throws NotificationException {
77 HtmlEmail mail = new HtmlEmail();
82 .asList(new InternetAddress[] { new InternetAddress(
84 mail.setSentDate(new Date());
85 mail.setSubject(_subject);
87 String htmlText = transformReport(aReport, new FileResource(
88 new File(_htmlXslt)));
89 String plainText = transformReport(aReport, new FileResource(
90 new File(_textXslt)));
92 mail.setHtmlMsg(htmlText);
93 mail.setTextMsg(plainText);
96 } catch (EmailException e) {
97 throw new NotificationException(e.getMessage(), e);
98 } catch (TransformerException e) {
99 throw new NotificationException(e.getMessage(), e);
100 } catch (IOException e) {
101 throw new NotificationException(e.getMessage(), e);
102 } catch (AddressException e) {
103 throw new NotificationException(e.getMessage(), e);
108 * Transforms a report into a destination format.
111 * Report to transform
114 * @return Transformed result.
115 * @throws IOException
116 * In case of IO problems.
117 * @throws TransformerException
118 * In case of problems transforming.
120 private String transformReport(Element aReport, InputResource aXslt)
121 throws IOException, TransformerException {
122 String reportXmlText = aReport.asXML();
123 return new XSLT().textTransform(reportXmlText.getBytes(), aXslt);