(no commit message)
authorerik <erik@77661180-640e-0410-b3a8-9f9b13e6d0e0>
Tue, 21 Mar 2006 15:43:05 +0000 (15:43 +0000)
committererik <erik@77661180-640e-0410-b3a8-9f9b13e6d0e0>
Tue, 21 Mar 2006 15:43:05 +0000 (15:43 +0000)
crawler/kiss/src/org/wamblee/crawler/kiss/KissCrawler.java
crawler/kiss/src/org/wamblee/crawler/kiss/MailNotifier.java [new file with mode: 0644]
crawler/kiss/src/org/wamblee/crawler/kiss/MailServer.java [new file with mode: 0644]
crawler/kiss/src/org/wamblee/crawler/kiss/NotificationException.java [new file with mode: 0644]
crawler/kiss/src/org/wamblee/crawler/kiss/Notifier.java [new file with mode: 0644]
crawler/kiss/src/org/wamblee/crawler/kiss/ProgramConfigurationParser.java
crawler/kiss/src/org/wamblee/crawler/kiss/UsernamePasswordAuthenticator.java [new file with mode: 0644]

index 0549e48f3a8a99910512c8ca125f574a021d027c..3ea7249902fd65d59c61a960c19b1e6009cac5bb 100644 (file)
@@ -135,19 +135,20 @@ public class KissCrawler {
 
         try {
             HttpClient client = new HttpClient();
-            // client.getHostConfiguration().setProxy("127.0.0.1", 3128);
+            //client.getHostConfiguration().setProxy("127.0.0.1", 3128);
 
             Crawler crawler = createCrawler(aCrawlerConfig, os, client);
             InputStream programConfigFile = new FileInputStream(new File(
                     aProgramConfig));
-            List<ProgramFilter> programFilters = new ProgramConfigurationParser()
-                    .parse(programConfigFile);
-
+            ProgramConfigurationParser parser = new ProgramConfigurationParser();
+            parser.parse(programConfigFile);
+            List<ProgramFilter> programFilters = parser.getFilters(); 
+            
             Page page = getStartPage(aStartUrl, crawler);
             TVGuide guide = createGuide(page);
             PrintVisitor printer = new PrintVisitor(System.out);
             guide.accept(printer);
-            processResults(programFilters, guide);
+            processResults(programFilters, guide, parser.getNotifier());
         } finally {
             os.flush();
             os.close();
@@ -166,7 +167,7 @@ public class KissCrawler {
      *             In case of problems sending a summary mail.
      */
     private void processResults(List<ProgramFilter> aProgramCondition,
-            TVGuide aGuide) throws MessagingException {
+            TVGuide aGuide, Notifier aNotifier) throws MessagingException {
         ProgramActionExecutor executor = new ProgramActionExecutor();
         for (ProgramFilter filter : aProgramCondition) {
             List<Program> programs = filter.apply(aGuide);
@@ -176,6 +177,11 @@ public class KissCrawler {
             }
         }
         executor.commit();
+        try {
+            aNotifier.send(executor.getXmlReport());
+        } catch (NotificationException e) { 
+            throw new RuntimeException(e);
+        }
         sendMail(executor);
     }
 
diff --git a/crawler/kiss/src/org/wamblee/crawler/kiss/MailNotifier.java b/crawler/kiss/src/org/wamblee/crawler/kiss/MailNotifier.java
new file mode 100644 (file)
index 0000000..c602f63
--- /dev/null
@@ -0,0 +1,96 @@
+/**
+ * Copyright (c) 2005 UPS_SCS NL
+ *  
+ */
+package org.wamblee.crawler.kiss;
+
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.Date;
+
+import javax.mail.internet.AddressException;
+import javax.mail.internet.InternetAddress;
+import javax.xml.transform.TransformerException;
+
+import org.apache.commons.mail.EmailException;
+import org.apache.commons.mail.HtmlEmail;
+import org.apache.xml.serialize.OutputFormat;
+import org.apache.xml.serialize.XMLSerializer;
+import org.dom4j.Element;
+import org.w3c.dom.Document;
+import org.wamblee.io.FileResource;
+import org.wamblee.io.InputResource;
+import org.wamblee.xml.XSLT;
+
+public class MailNotifier implements Notifier {
+    
+    private String _from;
+    private String _to;
+    private String _subject;
+    private String _htmlXslt;
+    private String _textXslt;
+    private MailServer _server;
+    
+    public MailNotifier( String aFrom, String aTo, String aSubject,
+            String aHtmlXslt, String aTextXslt, MailServer aServer) {
+        _from = aFrom;
+        _to = aTo;
+        _subject = aSubject;
+        _htmlXslt = aHtmlXslt;
+        _textXslt = aTextXslt; 
+        _server = aServer;
+    }
+
+    public void send( Element aReport ) throws NotificationException {
+        HtmlEmail mail = new HtmlEmail();
+        try {
+            mail.setFrom(_from);
+            mail.setTo(Arrays.asList(new InternetAddress[] { new InternetAddress(_to) }));
+            mail.setSentDate(new Date());
+            mail.setSubject(_subject);
+            
+            String htmlText = transformReport( aReport, new FileResource(new File(_htmlXslt)) );
+            String plainText = transformReport( aReport, new FileResource(new File(_textXslt)) );
+            
+            mail.setHtmlMsg(htmlText);
+            mail.setTextMsg(plainText);
+            
+            _server.send(mail);
+        }
+        catch (EmailException e) {
+            throw new NotificationException(e.getMessage(), e);
+        } catch (TransformerException e) { 
+            throw new NotificationException(e.getMessage(), e);
+        } catch (IOException e) { 
+            throw new NotificationException(e.getMessage(), e);
+        } catch (AddressException e) { 
+            throw new NotificationException(e.getMessage(), e);
+        }
+    }
+
+    /**
+     * @param aReport
+     * @return
+     * @throws IOException
+     * @throws TransformerException
+     */
+    private String transformReport( Element aReport, InputResource aXslt ) throws IOException, TransformerException {
+        String reportXmlText = aReport.asXML();
+        Document document = new XSLT().transform(reportXmlText.getBytes(), aXslt);
+        ByteArrayOutputStream transformed = new ByteArrayOutputStream();
+        XMLSerializer serializer = new XMLSerializer(transformed, new OutputFormat());
+        serializer.serialize(document);
+        return transformed.toString();
+    }
+
+    /**
+     * @param args
+     */
+    public static void main( String[] args ) {
+        // TODO Auto-generated method stub
+
+    }
+
+}
diff --git a/crawler/kiss/src/org/wamblee/crawler/kiss/MailServer.java b/crawler/kiss/src/org/wamblee/crawler/kiss/MailServer.java
new file mode 100644 (file)
index 0000000..b814e36
--- /dev/null
@@ -0,0 +1,39 @@
+/**
+ * Copyright (c) 2005 UPS_SCS NL
+ *  
+ */
+package org.wamblee.crawler.kiss;
+
+import java.util.Properties;
+
+import javax.mail.Session;
+
+import org.apache.commons.mail.Email;
+import org.apache.commons.mail.EmailException;
+
+public class MailServer {
+    
+    private String _host; 
+    private int _port;
+    private String _username;
+    private String _password;
+
+    public MailServer( 
+            String aHost, int aPort, String aUsername, String aPassword) {
+        _host = aHost;
+        _port = aPort;
+        _username = aUsername;
+        _password = aPassword;
+    }
+    
+    public void send(Email aMail) throws EmailException {
+        Properties props = new Properties();
+        props.put("mail.transport.protocol", "smtp");
+        props.put("mail.smtp.host", _host);
+        props.put("mail.smtp.port", "" + _port);
+
+        Session mailSession = Session.getInstance(props, new UsernamePasswordAuthenticator(_username, _password));
+        aMail.setMailSession(mailSession);
+        aMail.send();
+    }
+}
diff --git a/crawler/kiss/src/org/wamblee/crawler/kiss/NotificationException.java b/crawler/kiss/src/org/wamblee/crawler/kiss/NotificationException.java
new file mode 100644 (file)
index 0000000..1845c24
--- /dev/null
@@ -0,0 +1,16 @@
+/**
+ * Copyright (c) 2005 UPS_SCS NL
+ *  
+ */
+package org.wamblee.crawler.kiss;
+
+public class NotificationException extends Exception {
+
+    public NotificationException(String aMsg) { 
+        super(aMsg);
+    }
+    
+    public NotificationException(String aMsg, Throwable aCause) { 
+        super(aMsg, aCause);
+    }
+}
diff --git a/crawler/kiss/src/org/wamblee/crawler/kiss/Notifier.java b/crawler/kiss/src/org/wamblee/crawler/kiss/Notifier.java
new file mode 100644 (file)
index 0000000..96dc09e
--- /dev/null
@@ -0,0 +1,20 @@
+/**
+ * Copyright (c) 2005 UPS_SCS NL
+ *  
+ */
+package org.wamblee.crawler.kiss;
+
+import org.dom4j.Element;
+
+/**
+ * Object used to send notifications about the actions of the crawler.
+ *
+ */
+public interface Notifier {
+
+    /**
+     * Sends a notification. 
+     * @param aReport Report to send. 
+     */
+    void send(Element aReport) throws NotificationException;
+}
index e9ba272e3ec33fe25497c6637d87974af91075f8..b75d3e058b765f4a470ed39675b9d8b416adeddc 100644 (file)
@@ -18,9 +18,14 @@ package org.wamblee.crawler.kiss;
 
 import java.io.InputStream;
 import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Date;
 import java.util.Iterator;
 import java.util.List;
 
+import javax.mail.internet.InternetAddress;
+
+import org.apache.commons.mail.SimpleEmail;
 import org.dom4j.Attribute;
 import org.dom4j.Document;
 import org.dom4j.DocumentException;
@@ -44,6 +49,15 @@ class ProgramConfigurationParser {
     private static final String ELEM_CATEGORY = "category";
 
     private static final String ACTION_NOTIFY = "notify";
+    
+    private List<ProgramFilter> _filters; 
+    
+    private Notifier _notifier; 
+    
+    ProgramConfigurationParser() { 
+        _filters = null; 
+        _notifier = null; 
+    }
 
     /**
      * Parses the condition used to match the desired programs.
@@ -52,7 +66,7 @@ class ProgramConfigurationParser {
      *            Input stream to parse from.
      * @return Condition.
      */
-    List<ProgramFilter> parse(InputStream aStream) {
+    void parse(InputStream aStream) {
         List<ProgramFilter> filters = new ArrayList<ProgramFilter>();
         try {
             SAXReader reader = new SAXReader();
@@ -93,9 +107,66 @@ class ProgramConfigurationParser {
                 Condition<Program> condition = new AndCondition<Program>(regexConditions);
                 filters.add(new ProgramFilter(condition, action));
             }
-            return filters;
+            _filters = filters;
+            
+            Element notifier = root.element("notification");
+            _notifier = parseNotifier(notifier);
+            
         } catch (DocumentException e) {
             throw new RuntimeException("Error parsing program configuraiton", e);
         }
     }
+    
+    /**
+     * Parses the notifier
+     * @return Notifier
+     */
+    private Notifier parseNotifier(Element aNotifier) { 
+        String from = aNotifier.elementTextTrim("from");
+        String to = aNotifier.elementTextTrim("to");
+        String subject = aNotifier.elementTextTrim("subject");
+        
+        Element smtp = aNotifier.element("smtp");
+        MailServer server = parseMailServer( smtp );
+        
+        Element format = aNotifier.element("format");
+        String htmlXslt = format.elementTextTrim("html");
+        String textXslt = format.elementTextTrim("text");
+        
+        return new MailNotifier(from, to, subject, htmlXslt, textXslt, server);
+    }
+
+    /**
+     * @param smtp
+     * @return
+     */
+    private MailServer parseMailServer( Element smtp ) {
+        String host = smtp.elementTextTrim("host");
+        Element portElem = smtp.element("port");
+        int port = 25; 
+        if ( portElem != null ) {
+            port = Integer.valueOf(portElem.getTextTrim());
+        }
+        String username = smtp.elementTextTrim("username");
+        String password = smtp.elementTextTrim("password");
+        
+        MailServer server = new MailServer(host, port, username, password);
+        return server;
+    }
+    
+    /**
+     * Returns the list of program filters.
+     * @return Filter list.
+     */
+    public List<ProgramFilter> getFilters() { 
+        return _filters;
+    }
+    
+    /**
+     * Returns the notifier to use. 
+     * @return Notifier.
+     */
+    public Notifier getNotifier() { 
+        return _notifier; 
+    }
 }
diff --git a/crawler/kiss/src/org/wamblee/crawler/kiss/UsernamePasswordAuthenticator.java b/crawler/kiss/src/org/wamblee/crawler/kiss/UsernamePasswordAuthenticator.java
new file mode 100644 (file)
index 0000000..d7ba6e0
--- /dev/null
@@ -0,0 +1,29 @@
+/**
+ * Copyright (c) 2005 UPS_SCS NL
+ *  
+ */
+package org.wamblee.crawler.kiss;
+
+import javax.mail.Authenticator;
+import javax.mail.PasswordAuthentication;
+
+public class UsernamePasswordAuthenticator extends Authenticator {
+    
+    private String _username; 
+    private String _password; 
+
+    public UsernamePasswordAuthenticator(String aUsername, String aPassword) { 
+        _username = aUsername; 
+        _password = aPassword; 
+    }
+    
+    @Override
+    protected PasswordAuthentication getPasswordAuthentication( ) {
+        if ( _username == null ) { 
+            return null; 
+        }
+        return new PasswordAuthentication(_username, _password);
+    }
+  
+
+}