2 * Copyright (c) 2005 UPS_SCS NL
5 package org.wamblee.crawler.kiss;
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 {
35 private String _subject;
36 private String _htmlXslt;
37 private String _textXslt;
38 private MailServer _server;
41 * Constructs the notifier.
43 * @param aFrom Sender mail address to use.
44 * @param aTo Recipient mail address to use.
45 * @param aSubject Subject to use in the email.
46 * @param aHtmlXslt XSLT file to transform the report into HTML.
47 * @param aTextXslt XSLT file to transform the report into text.
48 * @param aServer Mail server to use.
50 public MailNotifier( String aFrom, String aTo, String aSubject,
51 String aHtmlXslt, String aTextXslt, MailServer aServer) {
55 _htmlXslt = aHtmlXslt;
56 _textXslt = aTextXslt;
62 * @see org.wamblee.crawler.kiss.Notifier#send(org.dom4j.Element)
64 public void send( Element aReport ) throws NotificationException {
65 HtmlEmail mail = new HtmlEmail();
68 mail.setTo(Arrays.asList(new InternetAddress[] { new InternetAddress(_to) }));
69 mail.setSentDate(new Date());
70 mail.setSubject(_subject);
72 String htmlText = transformReport( aReport, new FileResource(new File(_htmlXslt)) );
73 String plainText = transformReport( aReport, new FileResource(new File(_textXslt)) );
77 mail.setHtmlMsg(htmlText);
78 mail.setTextMsg(plainText);
82 catch (EmailException e) {
83 throw new NotificationException(e.getMessage(), e);
84 } catch (TransformerException e) {
85 throw new NotificationException(e.getMessage(), e);
86 } catch (IOException e) {
87 throw new NotificationException(e.getMessage(), e);
88 } catch (AddressException e) {
89 throw new NotificationException(e.getMessage(), e);
94 * Transforms a report into a destination format.
95 * @param aReport Report to transform
96 * @param aXslt XSLT to use.
97 * @return Transformed result.
98 * @throws IOException In case of IO problems.
99 * @throws TransformerException In case of problems transforming.
101 private String transformReport( Element aReport, InputResource aXslt ) throws IOException, TransformerException {
102 String reportXmlText = aReport.asXML();
103 return new XSLT().textTransform(reportXmlText.getBytes(), aXslt);