X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fnet%2Fpterodactylus%2Farachne%2Fcore%2FURLFetcher.java;h=ea109d0c82c49f3f6ebabe4ee5d95b0a5bd40dea;hb=bc2a6cc23e93037cae7f52815f20bdf24504ce5d;hp=df8180f038e064a986143bd44b7acbedde07ea92;hpb=0669aab9c004816d56a5e16c17b0492b53ddf9a8;p=arachne.git diff --git a/src/net/pterodactylus/arachne/core/URLFetcher.java b/src/net/pterodactylus/arachne/core/URLFetcher.java index df8180f..ea109d0 100644 --- a/src/net/pterodactylus/arachne/core/URLFetcher.java +++ b/src/net/pterodactylus/arachne/core/URLFetcher.java @@ -2,13 +2,20 @@ package net.pterodactylus.arachne.core; import java.io.IOException; 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.Parser; +import net.pterodactylus.arachne.parser.ParserFactory; import net.pterodactylus.arachne.parser.ParserListener; +import de.ina.util.io.MessageDigestInputStream; import de.ina.util.validation.Validation; /** @@ -22,40 +29,104 @@ class URLFetcher implements Runnable, ParserListener { /** The logger. */ private static final Logger logger = Logger.getLogger(URLFetcher.class.getName()); - /** The core. */ - private final Core core; + /** The parser factory. */ + private final ParserFactory parserFactory; /** 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(); + + /** The title of the URL. */ + private String title; + /** * Creates a new fetcher for the given URL. * + * @param parserFactory + * The parser factory that is used to create content-type + * specific parsers * @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(ParserFactory parserFactory, URL url) throws NoSuchAlgorithmException { + Validation.begin().isNotNull("parserFactory", parserFactory).isNotNull("url", url).check(); + this.parserFactory = parserFactory; this.url = url; + messageDigest = MessageDigest.getInstance("SHA-256"); + } + + // + // ACCESSORS + // + + /** + * Returns the title of the fetched URL. + * + * @return The fetched URL’s title + */ + public String getTitle() { + return title; + } + + /** + * 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} + * {@inheritDoc} * * @see java.lang.Runnable#run() */ 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 + "."); - HtmlEditorKitParser htmlEditorKitParser = new HtmlEditorKitParser(); - htmlEditorKitParser.parse(this, urlConnection.getInputStream(), "UTF-8"); + urlInputStream = urlConnection.getInputStream(); + hashInputStream = new MessageDigestInputStream(urlInputStream, messageDigest); + Parser parser = parserFactory.getParser(contentType); + if (parser == null) { + logger.log(Level.INFO, "No parser found for “" + contentType + "”."); + return; + } + parser.parse(this, hashInputStream, "UTF-8"); + hash = messageDigest.digest(); } catch (IOException ioe1) { logger.log(Level.WARNING, "Could not fetch “" + url + "”.", ioe1); } @@ -69,14 +140,27 @@ class URLFetcher implements Runnable, ParserListener { * {@inheritDoc} */ public void parsedLink(InputStream inputStream, String linkTarget, String linkTitle, String linkText) { - System.out.println("Found link to “" + linkTarget + "” named “" + linkText + "” or “" + linkTitle + "”."); + URL newLink = null; + try { + newLink = new URL(url, linkTarget); + try { + Page newPage = Page.fromURL(newLink); + if (newPage != null) { + collectedPages.add(newPage); + } + } catch (IllegalArgumentException iae1) { + /* ignore. */ + } + } catch (MalformedURLException mue1) { + logger.log(Level.WARNING, "Could not create URL from “" + url + "” and “" + linkTarget + "”.", mue1); + } } /** * {@inheritDoc} */ public void parsedTitle(InputStream inputStream, String title) { - System.out.println("Found title “" + title + "”."); + this.title = title; } }