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.ClasspathUriResolver;
19 import org.wamblee.xml.XslTransformer;
22 * A notifier that uses SMTP to notify users by mail.
25 public class MailNotifier implements Notifier {
31 private String _subject;
33 private String _htmlXslt;
35 private String _textXslt;
37 private MailServer _server;
40 * Constructs the notifier.
43 * Sender mail address to use.
45 * Recipient mail address to use.
47 * Subject to use in the email.
49 * XSLT file to transform the report into HTML.
51 * XSLT file to transform the report into text.
55 public MailNotifier(String aFrom, String aTo, String aSubject,
56 String aHtmlXslt, String aTextXslt, MailServer aServer) {
60 _htmlXslt = aHtmlXslt;
61 _textXslt = aTextXslt;
68 * @see org.wamblee.crawler.kiss.Notifier#send(org.dom4j.Element)
70 public void send(Element aReport) throws NotificationException {
71 HtmlEmail mail = new HtmlEmail();
76 .asList(new InternetAddress[] { new InternetAddress(
78 mail.setSentDate(new Date());
79 mail.setSubject(_subject);
81 String htmlText = transformReport(aReport, _htmlXslt);
82 String plainText = transformReport(aReport, _textXslt);
84 mail.setHtmlMsg(htmlText);
85 mail.setTextMsg(plainText);
88 } catch (EmailException e) {
89 throw new NotificationException(e.getMessage(), e);
90 } catch (TransformerException e) {
91 throw new NotificationException(e.getMessage(), e);
92 } catch (IOException e) {
93 throw new NotificationException(e.getMessage(), e);
94 } catch (AddressException e) {
95 throw new NotificationException(e.getMessage(), e);
100 * Transforms a report into a destination format.
103 * Report to transform
106 * @return Transformed result.
107 * @throws IOException
108 * In case of IO problems.
109 * @throws TransformerException
110 * In case of problems transforming.
112 private String transformReport(Element aReport, String aXslt)
113 throws IOException, TransformerException {
114 String reportXmlText = aReport.asXML();
115 XslTransformer xsltProcessor = new XslTransformer(new ClasspathUriResolver());
116 return xsltProcessor.textTransform(reportXmlText.getBytes(), xsltProcessor.resolve(aXslt));