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;
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;
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;
_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 {
}
/**
- * @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();
serializer.serialize(document);
return transformed.toString();
}
-
- /**
- * @param args
- */
- public static void main( String[] args ) {
- // TODO Auto-generated method stub
-
- }
-
}
import org.apache.commons.mail.Email;
import org.apache.commons.mail.EmailException;
+/**
+ * Mail server.
+ */
public class MailServer {
private String _host;
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");
*/
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);
}
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;
*/
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";
}
_filters = filters;
- Element notifier = root.element("notification");
+ Element notifier = root.element(ELEM_NOTIFICATION);
_notifier = parseNotifier(notifier);
} catch (DocumentException e) {
* @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;
package org.wamblee.crawler.kiss;
-import org.wamblee.crawler.kiss.Program.RecordingResult;
/**
* Represents an action to record a program.
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 ) {
}
return new PasswordAuthentication(_username, _password);
}
-
-
}