(no commit message)
authorErik Brakkee <erik@brakkee.org>
Tue, 21 Mar 2006 16:09:01 +0000 (16:09 +0000)
committerErik Brakkee <erik@brakkee.org>
Tue, 21 Mar 2006 16:09:01 +0000 (16:09 +0000)
crawler/kiss/src/org/wamblee/crawler/kiss/KissCrawler.java
crawler/kiss/src/org/wamblee/crawler/kiss/MailNotifier.java
crawler/kiss/src/org/wamblee/crawler/kiss/MailServer.java
crawler/kiss/src/org/wamblee/crawler/kiss/NotificationException.java
crawler/kiss/src/org/wamblee/crawler/kiss/ProgramConfigurationParser.java
crawler/kiss/src/org/wamblee/crawler/kiss/RecordProgramAction.java
crawler/kiss/src/org/wamblee/crawler/kiss/UsernamePasswordAuthenticator.java

index 3ea7249902fd65d59c61a960c19b1e6009cac5bb..d96ef3e581605eec5d99e6656a9e96d2c10ad5ff 100644 (file)
@@ -32,12 +32,9 @@ import java.util.Properties;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
-import javax.mail.Message;
 import javax.mail.MessagingException;
 import javax.mail.Session;
-import javax.mail.Transport;
 import javax.mail.internet.InternetAddress;
-import javax.mail.internet.MimeMessage;
 import javax.xml.transform.TransformerException;
 
 import org.apache.commons.httpclient.HttpClient;
index c602f63f91ed1867ad9495d5d70071617400c3da..f8566d51f9031f6f563b95a56f594e869ba6f7a0 100644 (file)
@@ -24,6 +24,10 @@ import org.wamblee.io.FileResource;
 import org.wamblee.io.InputResource;
 import org.wamblee.xml.XSLT;
 
+/**
+ * A notifier that uses SMTP to notify users by mail. 
+ *
+ */
 public class MailNotifier implements Notifier {
     
     private String _from;
@@ -33,6 +37,16 @@ public class MailNotifier implements Notifier {
     private String _textXslt;
     private MailServer _server;
     
+    /**
+     * Constructs the notifier. 
+     * 
+     * @param aFrom Sender mail address to use. 
+     * @param aTo Recipient mail address to use. 
+     * @param aSubject Subject to use in the email. 
+     * @param aHtmlXslt XSLT file to transform the report into HTML.
+     * @param aTextXslt XSLT file to transform the report into text. 
+     * @param aServer Mail server to use. 
+     */
     public MailNotifier( String aFrom, String aTo, String aSubject,
             String aHtmlXslt, String aTextXslt, MailServer aServer) {
         _from = aFrom;
@@ -43,6 +57,10 @@ public class MailNotifier implements Notifier {
         _server = aServer;
     }
 
+    /*
+     *  (non-Javadoc)
+     * @see org.wamblee.crawler.kiss.Notifier#send(org.dom4j.Element)
+     */
     public void send( Element aReport ) throws NotificationException {
         HtmlEmail mail = new HtmlEmail();
         try {
@@ -71,10 +89,12 @@ public class MailNotifier implements Notifier {
     }
 
     /**
-     * @param aReport
-     * @return
-     * @throws IOException
-     * @throws TransformerException
+     * Transforms a report into a destination format.
+     * @param aReport Report to transform
+     * @param aXslt XSLT to use. 
+     * @return Transformed result. 
+     * @throws IOException In case of IO problems.
+     * @throws TransformerException In case of problems transforming. 
      */
     private String transformReport( Element aReport, InputResource aXslt ) throws IOException, TransformerException {
         String reportXmlText = aReport.asXML();
@@ -84,13 +104,4 @@ public class MailNotifier implements Notifier {
         serializer.serialize(document);
         return transformed.toString();
     }
-
-    /**
-     * @param args
-     */
-    public static void main( String[] args ) {
-        // TODO Auto-generated method stub
-
-    }
-
 }
index b814e36ebe3cd131cd7115424719739b3e7d6f8c..768d6e7a8e98d4f45085664183a55bf93840383f 100644 (file)
@@ -11,6 +11,9 @@ import javax.mail.Session;
 import org.apache.commons.mail.Email;
 import org.apache.commons.mail.EmailException;
 
+/**
+ * Mail server.
+ */
 public class MailServer {
     
     private String _host; 
@@ -18,14 +21,27 @@ public class MailServer {
     private String _username;
     private String _password;
 
-    public MailServer( 
-            String aHost, int aPort, String aUsername, String aPassword) {
+    /**
+     * Constructs the mail server interface. 
+     * @param aHost Host name of the SMTP server.
+     * @param aPort Port name of the SMTP server.
+     * @param aUsername Username to use for authentication or null if no authentication is 
+     *   required. 
+     * @param aPassword Password to use for authentication or null if no authenticatio is 
+     *   required. 
+     */
+    public MailServer(String aHost, int aPort, String aUsername, String aPassword) {
         _host = aHost;
         _port = aPort;
         _username = aUsername;
         _password = aPassword;
     }
     
+    /**
+     * Sends an e-mail. 
+     * @param aMail Mail to send. 
+     * @throws EmailException In case of problems sending the mail. 
+     */
     public void send(Email aMail) throws EmailException {
         Properties props = new Properties();
         props.put("mail.transport.protocol", "smtp");
index 1845c24a6691b9ba97693b3f6ac8e5aee542138f..cce6b0f24aeb06b1be35aefeab36cc04fe36055b 100644 (file)
@@ -4,12 +4,26 @@
  */
 package org.wamblee.crawler.kiss;
 
+/**
+ * Notification exception thrown in case of problems sending 
+ * a notification to a user. 
+ *
+ */
 public class NotificationException extends Exception {
 
+    /**
+     * Constructs the notification. 
+     * @param aMsg Message. 
+     */
     public NotificationException(String aMsg) { 
         super(aMsg);
     }
     
+    /**
+     * Constructs the notification. 
+     * @param aMsg Message. 
+     * @param aCause Cause. 
+     */
     public NotificationException(String aMsg, Throwable aCause) { 
         super(aMsg, aCause);
     }
index b75d3e058b765f4a470ed39675b9d8b416adeddc..3237ca79a449d140001b072d95fa266781fa644b 100644 (file)
@@ -18,14 +18,9 @@ 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;
@@ -40,6 +35,36 @@ import org.wamblee.conditions.PropertyRegexCondition;
  */
 class ProgramConfigurationParser {
 
+    private static final String ELEM_PASSWORD = "password";
+
+    private static final String ELEM_USERNAME = "username";
+
+    private static final String ELEM_PORT = "port";
+
+    private static final String ELEM_HOST = "host";
+
+    // Formatting configuration.
+    private static final String ELEM_FORMAT = "format";
+    
+    private static final String ELEM_TEXT = "text";
+
+    private static final String ELEM_HTML = "html";
+
+    // Mail server configuration.
+
+    private static final String ELEM_NOTIFICATION = "notification";
+    
+    private static final String ELEM_SMTP = "smtp";
+
+    private static final String ELEM_SUBJECT = "subject";
+
+    private static final String ELEM_TO = "to";
+
+    private static final String ELEM_FROM = "from";
+
+    // Configuration of interesting programs.
+
     private static final String ELEM_PROGRAM = "program";
 
     private static final String ELEM_PATTERN = "match";
@@ -109,7 +134,7 @@ class ProgramConfigurationParser {
             }
             _filters = filters;
             
-            Element notifier = root.element("notification");
+            Element notifier = root.element(ELEM_NOTIFICATION);
             _notifier = parseNotifier(notifier);
             
         } catch (DocumentException e) {
@@ -122,33 +147,34 @@ class ProgramConfigurationParser {
      * @return Notifier
      */
     private Notifier parseNotifier(Element aNotifier) { 
-        String from = aNotifier.elementTextTrim("from");
-        String to = aNotifier.elementTextTrim("to");
-        String subject = aNotifier.elementTextTrim("subject");
+        String from = aNotifier.elementTextTrim(ELEM_FROM);
+        String to = aNotifier.elementTextTrim(ELEM_TO);
+        String subject = aNotifier.elementTextTrim(ELEM_SUBJECT);
         
-        Element smtp = aNotifier.element("smtp");
+        Element smtp = aNotifier.element(ELEM_SMTP);
         MailServer server = parseMailServer( smtp );
         
-        Element format = aNotifier.element("format");
-        String htmlXslt = format.elementTextTrim("html");
-        String textXslt = format.elementTextTrim("text");
+        Element format = aNotifier.element(ELEM_FORMAT);
+        String htmlXslt = format.elementTextTrim(ELEM_HTML);
+        String textXslt = format.elementTextTrim(ELEM_TEXT);
         
         return new MailNotifier(from, to, subject, htmlXslt, textXslt, server);
     }
 
     /**
-     * @param smtp
-     * @return
+     * Parses the mail server from the XML.
+     * @param aSmtp Mail server configuration.
+     * @return Mail server.
      */
-    private MailServer parseMailServer( Element smtp ) {
-        String host = smtp.elementTextTrim("host");
-        Element portElem = smtp.element("port");
+    private MailServer parseMailServer( Element aSmtp ) {
+        String host = aSmtp.elementTextTrim(ELEM_HOST);
+        Element portElem = aSmtp.element(ELEM_PORT);
         int port = 25; 
         if ( portElem != null ) {
             port = Integer.valueOf(portElem.getTextTrim());
         }
-        String username = smtp.elementTextTrim("username");
-        String password = smtp.elementTextTrim("password");
+        String username = aSmtp.elementTextTrim(ELEM_USERNAME);
+        String password = aSmtp.elementTextTrim(ELEM_PASSWORD);
         
         MailServer server = new MailServer(host, port, username, password);
         return server;
index ac784f67ba80715b8e626151065a85681f82bc8f..1a0d6cc6d559c34152b2724ff0b330ec9e48ba16 100644 (file)
@@ -16,7 +16,6 @@
 
 package org.wamblee.crawler.kiss;
 
-import org.wamblee.crawler.kiss.Program.RecordingResult;
 
 /**
  * Represents an action to record a program. 
index d7ba6e0a7fe9c5f024bb9237979e90dbc347ce01..b3d295463c6fd5dd3c817d42a83463d74a522296 100644 (file)
@@ -7,16 +7,30 @@ package org.wamblee.crawler.kiss;
 import javax.mail.Authenticator;
 import javax.mail.PasswordAuthentication;
 
+/**
+ * Authenticator to supply username and password to the mail server
+ * (if needed).
+ *
+ */
 public class UsernamePasswordAuthenticator extends Authenticator {
     
     private String _username; 
     private String _password; 
 
+    /**
+     * Constructs the authenticator.
+     * @param aUsername User name. 
+     * @param aPassword Password.
+     */
     public UsernamePasswordAuthenticator(String aUsername, String aPassword) { 
         _username = aUsername; 
         _password = aPassword; 
     }
     
+    /*
+     *  (non-Javadoc)
+     * @see javax.mail.Authenticator#getPasswordAuthentication()
+     */
     @Override
     protected PasswordAuthentication getPasswordAuthentication( ) {
         if ( _username == null ) { 
@@ -24,6 +38,4 @@ public class UsernamePasswordAuthenticator extends Authenticator {
         }
         return new PasswordAuthentication(_username, _password);
     }
-  
-
 }