Example usage for org.apache.http.impl.client DefaultRedirectStrategy REDIRECT_LOCATIONS

List of usage examples for org.apache.http.impl.client DefaultRedirectStrategy REDIRECT_LOCATIONS

Introduction

In this page you can find the example usage for org.apache.http.impl.client DefaultRedirectStrategy REDIRECT_LOCATIONS.

Prototype

String REDIRECT_LOCATIONS

To view the source code for org.apache.http.impl.client DefaultRedirectStrategy REDIRECT_LOCATIONS.

Click Source Link

Usage

From source file:org.gradle.internal.resource.transport.http.HttpClientHelper.java

public HttpResponse performHttpRequest(HttpRequestBase request) throws IOException {
    // Without this, HTTP Client prohibits multiple redirects to the same location within the same context
    httpContext.removeAttribute(DefaultRedirectStrategy.REDIRECT_LOCATIONS);

    LOGGER.debug("Performing HTTP {}: {}", request.getMethod(), request.getURI());
    return client.execute(request, httpContext);
}

From source file:org.tallison.cc.CCGetter.java

private void fetch(CCIndexRecord r, Path rootDir, BufferedWriter writer) throws IOException {
    Path targFile = rootDir.resolve(r.getDigest().substring(0, 2) + "/" + r.getDigest());

    if (Files.isRegularFile(targFile)) {
        writeStatus(r, FETCH_STATUS.ALREADY_IN_REPOSITORY, writer);
        logger.info("already retrieved:" + targFile.toAbsolutePath());
        return;//from www. j a va  2  s  .com
    }

    String url = AWS_BASE + r.getFilename();
    URI uri = null;
    try {
        uri = new URI(url);
    } catch (URISyntaxException e) {
        logger.warn("Bad url: " + url);
        writeStatus(r, FETCH_STATUS.BAD_URL, writer);
        return;
    }
    CloseableHttpClient httpClient = HttpClients.createDefault();
    HttpHost target = new HttpHost(uri.getHost());
    String urlPath = uri.getRawPath();
    if (uri.getRawQuery() != null) {
        urlPath += "?" + uri.getRawQuery();
    }
    HttpGet httpGet = null;
    try {
        httpGet = new HttpGet(urlPath);
    } catch (Exception e) {
        logger.warn("bad path " + uri.toString(), e);
        writeStatus(r, FETCH_STATUS.BAD_URL, writer);
        return;
    }
    if (proxyHost != null && proxyPort > -1) {
        HttpHost proxy = new HttpHost(proxyHost, proxyPort, "http");
        RequestConfig requestConfig = RequestConfig.custom().setProxy(proxy).build();
        httpGet.setConfig(requestConfig);
    }
    httpGet.addHeader("Range", r.getOffsetHeader());
    HttpCoreContext coreContext = new HttpCoreContext();
    CloseableHttpResponse httpResponse = null;
    URI lastURI = null;
    try {
        httpResponse = httpClient.execute(target, httpGet, coreContext);
        RedirectLocations redirectLocations = (RedirectLocations) coreContext
                .getAttribute(DefaultRedirectStrategy.REDIRECT_LOCATIONS);
        if (redirectLocations != null) {
            for (URI redirectURI : redirectLocations.getAll()) {
                lastURI = redirectURI;
            }
        } else {
            lastURI = httpGet.getURI();
        }
    } catch (IOException e) {
        logger.warn("IOException for " + uri.toString(), e);
        writeStatus(r, FETCH_STATUS.FETCHED_IO_EXCEPTION, writer);
        return;
    }
    lastURI = uri.resolve(lastURI);

    if (httpResponse.getStatusLine().getStatusCode() != 200
            && httpResponse.getStatusLine().getStatusCode() != 206) {
        logger.warn("Bad status for " + uri.toString() + " : " + httpResponse.getStatusLine().getStatusCode());
        writeStatus(r, FETCH_STATUS.FETCHED_NOT_200, writer);
        return;
    }
    Path tmp = null;
    Header[] headers = null;
    boolean isTruncated = false;
    try {
        //this among other parts is plagiarized from centic9's CommonCrawlDocumentDownload
        //probably saved me hours.  Thank you, Dominik!
        tmp = Files.createTempFile("cc-getter", "");
        try (InputStream is = new GZIPInputStream(httpResponse.getEntity().getContent())) {
            WARCRecord warcRecord = new WARCRecord(new FastBufferedInputStream(is), "", 0);
            ArchiveRecordHeader archiveRecordHeader = warcRecord.getHeader();
            if (archiveRecordHeader.getHeaderFields().containsKey(WARCConstants.HEADER_KEY_TRUNCATED)) {
                isTruncated = true;
            }
            headers = LaxHttpParser.parseHeaders(warcRecord, "UTF-8");

            Files.copy(warcRecord, tmp, StandardCopyOption.REPLACE_EXISTING);
        }
    } catch (IOException e) {
        writeStatus(r, null, headers, 0L, isTruncated, FETCH_STATUS.FETCHED_IO_EXCEPTION_READING_ENTITY,
                writer);
        deleteTmp(tmp);
        return;
    }

    String digest = null;
    long tmpLength = 0l;
    try (InputStream is = Files.newInputStream(tmp)) {
        digest = base32.encodeAsString(DigestUtils.sha1(is));
        tmpLength = Files.size(tmp);
    } catch (IOException e) {
        writeStatus(r, null, headers, tmpLength, isTruncated, FETCH_STATUS.FETCHED_IO_EXCEPTION_SHA1, writer);
        logger.warn("IOException during digesting: " + tmp.toAbsolutePath());
        deleteTmp(tmp);
        return;
    }

    if (Files.exists(targFile)) {
        writeStatus(r, digest, headers, tmpLength, isTruncated, FETCH_STATUS.ALREADY_IN_REPOSITORY, writer);
        deleteTmp(tmp);
        return;
    }
    try {
        Files.createDirectories(targFile.getParent());
        Files.copy(tmp, targFile);
    } catch (IOException e) {
        writeStatus(r, digest, headers, tmpLength, isTruncated,
                FETCH_STATUS.FETCHED_EXCEPTION_COPYING_TO_REPOSITORY, writer);
        deleteTmp(tmp);

    }
    writeStatus(r, digest, headers, tmpLength, isTruncated, FETCH_STATUS.ADDED_TO_REPOSITORY, writer);
    deleteTmp(tmp);
}