(no commit message)
[utils] / crawler / kiss / src / org / wamblee / crawler / kiss / MailNotifier.java
1 /**
2  * Copyright (c) 2005 UPS_SCS NL
3  *  
4  */
5 package org.wamblee.crawler.kiss;
6
7 import java.io.ByteArrayOutputStream;
8 import java.io.File;
9 import java.io.IOException;
10 import java.util.Arrays;
11 import java.util.Date;
12
13 import javax.mail.internet.AddressException;
14 import javax.mail.internet.InternetAddress;
15 import javax.xml.transform.TransformerException;
16
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;
26
27 /**
28  * A notifier that uses SMTP to notify users by mail. 
29  *
30  */
31 public class MailNotifier implements Notifier {
32     
33     private String _from;
34     private String _to;
35     private String _subject;
36     private String _htmlXslt;
37     private String _textXslt;
38     private MailServer _server;
39     
40     /**
41      * Constructs the notifier. 
42      * 
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. 
49      */
50     public MailNotifier( String aFrom, String aTo, String aSubject,
51             String aHtmlXslt, String aTextXslt, MailServer aServer) {
52         _from = aFrom;
53         _to = aTo;
54         _subject = aSubject;
55         _htmlXslt = aHtmlXslt;
56         _textXslt = aTextXslt; 
57         _server = aServer;
58     }
59
60     /*
61      *  (non-Javadoc)
62      * @see org.wamblee.crawler.kiss.Notifier#send(org.dom4j.Element)
63      */
64     public void send( Element aReport ) throws NotificationException {
65         HtmlEmail mail = new HtmlEmail();
66         try {
67             mail.setFrom(_from);
68             mail.setTo(Arrays.asList(new InternetAddress[] { new InternetAddress(_to) }));
69             mail.setSentDate(new Date());
70             mail.setSubject(_subject);
71             
72             String htmlText = transformReport( aReport, new FileResource(new File(_htmlXslt)) );
73             String plainText = transformReport( aReport, new FileResource(new File(_textXslt)) );
74             
75             
76             
77             mail.setHtmlMsg(htmlText);
78             mail.setTextMsg(plainText);
79             
80             _server.send(mail);
81         }
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);
90         }
91     }
92
93     /**
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. 
100      */
101     private String transformReport( Element aReport, InputResource aXslt ) throws IOException, TransformerException {
102         String reportXmlText = aReport.asXML();
103         return new XSLT().textTransform(reportXmlText.getBytes(), aXslt);
104     }
105 }