From bd7ff4fd9e484dfe7c723c3b1e12fddc03efc9ea Mon Sep 17 00:00:00 2001 From: Erik Brakkee Date: Tue, 21 Mar 2006 15:43:05 +0000 Subject: [PATCH] --- .../org/wamblee/crawler/kiss/KissCrawler.java | 18 ++-- .../wamblee/crawler/kiss/MailNotifier.java | 96 +++++++++++++++++++ .../org/wamblee/crawler/kiss/MailServer.java | 39 ++++++++ .../crawler/kiss/NotificationException.java | 16 ++++ .../org/wamblee/crawler/kiss/Notifier.java | 20 ++++ .../kiss/ProgramConfigurationParser.java | 75 ++++++++++++++- .../kiss/UsernamePasswordAuthenticator.java | 29 ++++++ 7 files changed, 285 insertions(+), 8 deletions(-) create mode 100644 crawler/kiss/src/org/wamblee/crawler/kiss/MailNotifier.java create mode 100644 crawler/kiss/src/org/wamblee/crawler/kiss/MailServer.java create mode 100644 crawler/kiss/src/org/wamblee/crawler/kiss/NotificationException.java create mode 100644 crawler/kiss/src/org/wamblee/crawler/kiss/Notifier.java create mode 100644 crawler/kiss/src/org/wamblee/crawler/kiss/UsernamePasswordAuthenticator.java diff --git a/crawler/kiss/src/org/wamblee/crawler/kiss/KissCrawler.java b/crawler/kiss/src/org/wamblee/crawler/kiss/KissCrawler.java index 0549e48f..3ea72499 100644 --- a/crawler/kiss/src/org/wamblee/crawler/kiss/KissCrawler.java +++ b/crawler/kiss/src/org/wamblee/crawler/kiss/KissCrawler.java @@ -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 programFilters = new ProgramConfigurationParser() - .parse(programConfigFile); - + ProgramConfigurationParser parser = new ProgramConfigurationParser(); + parser.parse(programConfigFile); + List 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 aProgramCondition, - TVGuide aGuide) throws MessagingException { + TVGuide aGuide, Notifier aNotifier) throws MessagingException { ProgramActionExecutor executor = new ProgramActionExecutor(); for (ProgramFilter filter : aProgramCondition) { List 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 index 00000000..c602f63f --- /dev/null +++ b/crawler/kiss/src/org/wamblee/crawler/kiss/MailNotifier.java @@ -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 index 00000000..b814e36e --- /dev/null +++ b/crawler/kiss/src/org/wamblee/crawler/kiss/MailServer.java @@ -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 index 00000000..1845c24a --- /dev/null +++ b/crawler/kiss/src/org/wamblee/crawler/kiss/NotificationException.java @@ -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 index 00000000..96dc09e2 --- /dev/null +++ b/crawler/kiss/src/org/wamblee/crawler/kiss/Notifier.java @@ -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; +} diff --git a/crawler/kiss/src/org/wamblee/crawler/kiss/ProgramConfigurationParser.java b/crawler/kiss/src/org/wamblee/crawler/kiss/ProgramConfigurationParser.java index e9ba272e..b75d3e05 100644 --- a/crawler/kiss/src/org/wamblee/crawler/kiss/ProgramConfigurationParser.java +++ b/crawler/kiss/src/org/wamblee/crawler/kiss/ProgramConfigurationParser.java @@ -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 _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 parse(InputStream aStream) { + void parse(InputStream aStream) { List filters = new ArrayList(); try { SAXReader reader = new SAXReader(); @@ -93,9 +107,66 @@ class ProgramConfigurationParser { Condition condition = new AndCondition(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 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 index 00000000..d7ba6e0a --- /dev/null +++ b/crawler/kiss/src/org/wamblee/crawler/kiss/UsernamePasswordAuthenticator.java @@ -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); + } + + +} -- 2.31.1