|
| 1 | +package nl.melp.linkchecker; |
| 2 | + |
| 3 | +import nl.melp.linkchecker.URIResolver.InvalidURIException; |
| 4 | +import org.apache.http.HttpEntity; |
| 5 | +import org.apache.http.client.config.RequestConfig; |
| 6 | +import org.apache.http.client.methods.CloseableHttpResponse; |
| 7 | +import org.apache.http.client.methods.HttpGet; |
| 8 | +import org.apache.http.impl.client.CloseableHttpClient; |
| 9 | +import org.slf4j.Logger; |
| 10 | + |
| 11 | +import java.io.IOException; |
| 12 | +import java.net.URI; |
| 13 | +import java.util.LinkedHashSet; |
| 14 | +import java.util.Set; |
| 15 | + |
| 16 | +public class Fetcher { |
| 17 | + private static final int timeout = 30; |
| 18 | + |
| 19 | + private static final RequestConfig requestConfig = RequestConfig.custom() |
| 20 | + .setConnectTimeout(timeout * 1000) |
| 21 | + .setConnectionRequestTimeout(timeout * 1000) |
| 22 | + .setSocketTimeout(timeout * 1000) |
| 23 | + .build(); |
| 24 | + |
| 25 | + public static class Result { |
| 26 | + private final URI uri; |
| 27 | + private final int statusCode; |
| 28 | + private final Set<URI> referredLinks; |
| 29 | + private final Set<String> invalidLinks; |
| 30 | + |
| 31 | + public Result(URI uri, int statusCode, Set<URI> referredLinks, Set<String> invalidLinks) { |
| 32 | + this.uri = uri; |
| 33 | + this.statusCode = statusCode; |
| 34 | + this.referredLinks = referredLinks; |
| 35 | + this.invalidLinks = invalidLinks; |
| 36 | + } |
| 37 | + |
| 38 | + public URI getUri() { |
| 39 | + return uri; |
| 40 | + } |
| 41 | + |
| 42 | + public int getStatusCode() { |
| 43 | + return statusCode; |
| 44 | + } |
| 45 | + |
| 46 | + public Set<URI> getReferredLinks() { |
| 47 | + return referredLinks; |
| 48 | + } |
| 49 | + |
| 50 | + public Set<String> getInvalidLinks() { |
| 51 | + return invalidLinks; |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + private final Logger logger; |
| 56 | + private final RunConfig config; |
| 57 | + private final Extractor extractor; |
| 58 | + private final URIResolver resolver; |
| 59 | + |
| 60 | + public Fetcher(Logger logger, RunConfig config, Extractor extractor, URIResolver resolver) { |
| 61 | + this.logger = logger; |
| 62 | + this.config = config; |
| 63 | + this.extractor = extractor; |
| 64 | + this.resolver = resolver; |
| 65 | + } |
| 66 | + |
| 67 | + public Result fetch(CloseableHttpClient httpClient, URI url) { |
| 68 | + var request = new HttpGet(url); |
| 69 | + request.setConfig(requestConfig); |
| 70 | + try (CloseableHttpResponse response = httpClient.execute(request)) { |
| 71 | + int statusCode = response.getStatusLine().getStatusCode(); |
| 72 | + |
| 73 | + logger.trace("Got status " + statusCode + " at " + url); |
| 74 | + |
| 75 | + HttpEntity responseEntity = response.getEntity(); |
| 76 | + Set<URI> links = new LinkedHashSet<>(); |
| 77 | + Set<String> invalidLinks = new LinkedHashSet<>(); |
| 78 | + |
| 79 | + if (config.shouldExtractLinks(url)) { |
| 80 | + for (String link : extractor.extract(url, statusCode, response, responseEntity)) { |
| 81 | + try { |
| 82 | + final URI target = resolver.resolveUri(url, link); |
| 83 | + if (target != null) { |
| 84 | + links.add(target); |
| 85 | + } |
| 86 | + } catch (InvalidURIException e) { |
| 87 | + invalidLinks.add(link); |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + return new Result(url, statusCode, links, invalidLinks); |
| 92 | + } catch (IOException e) { |
| 93 | + return new Result(url, 0, null, null); |
| 94 | + } |
| 95 | + } |
| 96 | +} |
0 commit comments