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();
* 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);
}
}
executor.commit();
+ try {
+ aNotifier.send(executor.getXmlReport());
+ } catch (NotificationException e) {
+ throw new RuntimeException(e);
+ }
sendMail(executor);
}
--- /dev/null
+/**
+ * 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
+
+ }
+
+}
--- /dev/null
+/**
+ * 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();
+ }
+}
--- /dev/null
+/**
+ * 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);
+ }
+}
--- /dev/null
+/**
+ * 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;
+}
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;
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.
* 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();
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;
+ }
}
--- /dev/null
+/**
+ * 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);
+ }
+
+
+}