X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fnet%2Fpterodactylus%2Farachne%2Fcore%2FURLFetcher.java;h=ddfbddd815ac68a385fb0f9a3a8ae3cf1a160cce;hb=e6d67f8ff35ca69feb69811b4e09c52d3118fa9c;hp=0b4cb47abbf9ed95f996f68f6bbba88b39200950;hpb=6c8cff3cb5a52300d182a30286a53b4ec12059db;p=arachne.git diff --git a/src/net/pterodactylus/arachne/core/URLFetcher.java b/src/net/pterodactylus/arachne/core/URLFetcher.java index 0b4cb47..ddfbddd 100644 --- a/src/net/pterodactylus/arachne/core/URLFetcher.java +++ b/src/net/pterodactylus/arachne/core/URLFetcher.java @@ -5,11 +5,16 @@ import java.io.InputStream; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.util.ArrayList; +import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; import net.pterodactylus.arachne.parser.HtmlEditorKitParser; import net.pterodactylus.arachne.parser.ParserListener; +import de.ina.util.io.MessageDigestInputStream; import de.ina.util.validation.Validation; /** @@ -23,26 +28,62 @@ class URLFetcher implements Runnable, ParserListener { /** The logger. */ private static final Logger logger = Logger.getLogger(URLFetcher.class.getName()); - /** The core. */ - private final Core core; - /** The URL to fetch. */ private final URL url; + /** The message digest. */ + private final MessageDigest messageDigest; + + /** The hash of the fetched URL. */ + private byte[] hash; + + /** The collected URLs. */ + private final List collectedPages = new ArrayList(); + /** * Creates a new fetcher for the given URL. * * @param url * The URL to fetch - * @param core - * TODO + * @throws NoSuchAlgorithmException + * if no {@link MessageDigest} instance with an + * SHA-256 algorithm can be created */ - public URLFetcher(Core core, URL url) { - Validation.begin().isNotNull("core", core).isNotNull("url", url).check(); - this.core = core; + public URLFetcher(URL url) throws NoSuchAlgorithmException { + Validation.begin().isNotNull("url", url).check(); this.url = url; + messageDigest = MessageDigest.getInstance("SHA-256"); + } + + // + // ACCESSORS + // + + /** + * Returns the pages collected while parsing this URL. + * + * @return The collected pages + */ + public List getCollectedPages() { + return collectedPages; + } + + /** + * Returns the hash of the content of the fetched URL. The returned value is + * only valid after {@link #run()} has been called. + * + * @return The hash of the fetched content + */ + public byte[] getHash() { + byte[] hashCopy = new byte[hash.length]; + System.arraycopy(hash, 0, hashCopy, 0, hash.length); + return hashCopy; } + // + // INTERFACE Runnable + // + /** * {@inheritdoc} * @@ -50,13 +91,18 @@ class URLFetcher implements Runnable, ParserListener { */ public void run() { logger.log(Level.INFO, "Starting URL Fetcher for “" + url + "”."); + InputStream urlInputStream = null; + MessageDigestInputStream hashInputStream = null; try { URLConnection urlConnection = url.openConnection(); long contentLength = urlConnection.getContentLength(); String contentType = urlConnection.getContentType(); logger.log(Level.INFO, "Type is “" + contentType + "”, length is " + contentLength + "."); + urlInputStream = urlConnection.getInputStream(); + hashInputStream = new MessageDigestInputStream(urlInputStream, messageDigest); HtmlEditorKitParser htmlEditorKitParser = new HtmlEditorKitParser(); - htmlEditorKitParser.parse(this, urlConnection.getInputStream(), "UTF-8"); + htmlEditorKitParser.parse(this, hashInputStream, "UTF-8"); + hash = messageDigest.digest(); } catch (IOException ioe1) { logger.log(Level.WARNING, "Could not fetch “" + url + "”.", ioe1); } @@ -73,11 +119,12 @@ class URLFetcher implements Runnable, ParserListener { URL newLink = null; try { newLink = new URL(url, linkTarget); - core.addPage(newLink); + Page newPage = Page.fromURL(newLink); + if (newPage != null) { + collectedPages.add(newPage); + } } catch (MalformedURLException mue1) { logger.log(Level.WARNING, "Could not create URL from “" + url + "” and “" + linkTarget + "”.", mue1); - } catch (IllegalArgumentException iae1) { - logger.log(Level.WARNING, "Could not add “" + newLink + "” to core queue.", iae1); } }