From 3e7fabe4d28276a0c1c87069e6df43f1790b4fb8 Mon Sep 17 00:00:00 2001 From: erik Date: Tue, 21 Mar 2006 16:09:01 +0000 Subject: [PATCH] --- .../org/wamblee/crawler/kiss/KissCrawler.java | 3 - .../wamblee/crawler/kiss/MailNotifier.java | 37 +++++++---- .../org/wamblee/crawler/kiss/MailServer.java | 20 +++++- .../crawler/kiss/NotificationException.java | 14 ++++ .../kiss/ProgramConfigurationParser.java | 66 +++++++++++++------ .../crawler/kiss/RecordProgramAction.java | 1 - .../kiss/UsernamePasswordAuthenticator.java | 16 ++++- 7 files changed, 116 insertions(+), 41 deletions(-) diff --git a/crawler/kiss/src/org/wamblee/crawler/kiss/KissCrawler.java b/crawler/kiss/src/org/wamblee/crawler/kiss/KissCrawler.java index 3ea72499..d96ef3e5 100644 --- a/crawler/kiss/src/org/wamblee/crawler/kiss/KissCrawler.java +++ b/crawler/kiss/src/org/wamblee/crawler/kiss/KissCrawler.java @@ -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; diff --git a/crawler/kiss/src/org/wamblee/crawler/kiss/MailNotifier.java b/crawler/kiss/src/org/wamblee/crawler/kiss/MailNotifier.java index c602f63f..f8566d51 100644 --- a/crawler/kiss/src/org/wamblee/crawler/kiss/MailNotifier.java +++ b/crawler/kiss/src/org/wamblee/crawler/kiss/MailNotifier.java @@ -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 - - } - } diff --git a/crawler/kiss/src/org/wamblee/crawler/kiss/MailServer.java b/crawler/kiss/src/org/wamblee/crawler/kiss/MailServer.java index b814e36e..768d6e7a 100644 --- a/crawler/kiss/src/org/wamblee/crawler/kiss/MailServer.java +++ b/crawler/kiss/src/org/wamblee/crawler/kiss/MailServer.java @@ -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"); diff --git a/crawler/kiss/src/org/wamblee/crawler/kiss/NotificationException.java b/crawler/kiss/src/org/wamblee/crawler/kiss/NotificationException.java index 1845c24a..cce6b0f2 100644 --- a/crawler/kiss/src/org/wamblee/crawler/kiss/NotificationException.java +++ b/crawler/kiss/src/org/wamblee/crawler/kiss/NotificationException.java @@ -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); } diff --git a/crawler/kiss/src/org/wamblee/crawler/kiss/ProgramConfigurationParser.java b/crawler/kiss/src/org/wamblee/crawler/kiss/ProgramConfigurationParser.java index b75d3e05..3237ca79 100644 --- a/crawler/kiss/src/org/wamblee/crawler/kiss/ProgramConfigurationParser.java +++ b/crawler/kiss/src/org/wamblee/crawler/kiss/ProgramConfigurationParser.java @@ -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; diff --git a/crawler/kiss/src/org/wamblee/crawler/kiss/RecordProgramAction.java b/crawler/kiss/src/org/wamblee/crawler/kiss/RecordProgramAction.java index ac784f67..1a0d6cc6 100644 --- a/crawler/kiss/src/org/wamblee/crawler/kiss/RecordProgramAction.java +++ b/crawler/kiss/src/org/wamblee/crawler/kiss/RecordProgramAction.java @@ -16,7 +16,6 @@ package org.wamblee.crawler.kiss; -import org.wamblee.crawler.kiss.Program.RecordingResult; /** * Represents an action to record a program. diff --git a/crawler/kiss/src/org/wamblee/crawler/kiss/UsernamePasswordAuthenticator.java b/crawler/kiss/src/org/wamblee/crawler/kiss/UsernamePasswordAuthenticator.java index d7ba6e0a..b3d29546 100644 --- a/crawler/kiss/src/org/wamblee/crawler/kiss/UsernamePasswordAuthenticator.java +++ b/crawler/kiss/src/org/wamblee/crawler/kiss/UsernamePasswordAuthenticator.java @@ -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); } - - } -- 2.31.1