Add URL fetcher.
[arachne.git] / src / net / pterodactylus / arachne / core / URLFetcher.java
1 package net.pterodactylus.arachne.core;
2
3 import java.io.IOException;
4 import java.net.URL;
5 import java.net.URLConnection;
6 import java.util.logging.Level;
7 import java.util.logging.Logger;
8
9 import de.ina.util.validation.Validation;
10
11 /**
12  * Fetches URLs, parses the received content (if it is HTML) and adds all
13  * resulting links to the queue in the core.
14  *
15  * @author David ‘Bombe’ Roden <bombe@pterodactylus.net>
16  */
17 class URLFetcher implements Runnable {
18
19         /** The logger. */
20         private static final Logger logger = Logger.getLogger(URLFetcher.class.getName());
21
22         /** The core. */
23         private final Core core;
24
25         /** The URL to fetch. */
26         private final URL url;
27
28         /**
29          * Creates a new fetcher for the given URL.
30          *
31          * @param url
32          *            The URL to fetch
33          * @param core
34          *            TODO
35          */
36         public URLFetcher(Core core, URL url) {
37                 Validation.begin().isNotNull("core", core).isNotNull("url", url).check();
38                 this.core = core;
39                 this.url = url;
40         }
41
42         /**
43          * {@inheritdoc}
44          *
45          * @see java.lang.Runnable#run()
46          */
47         public void run() {
48                 logger.log(Level.INFO, "Starting URL Fetcher for “" + url + "”.");
49                 try {
50                         URLConnection urlConnection = url.openConnection();
51                         long contentLength = urlConnection.getContentLength();
52                         String contentType = urlConnection.getContentType();
53                         logger.log(Level.INFO, "Type is “" + contentType + "”, length is " + contentLength + ".");
54                 } catch (IOException ioe1) {
55                         logger.log(Level.WARNING, "Could not fetch “" + url + "”.", ioe1);
56                 }
57         }
58
59 }