Example usage for org.apache.http.impl.client RedirectLocations get

List of usage examples for org.apache.http.impl.client RedirectLocations get

Introduction

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

Prototype

@Override
public URI get(final int index) 

Source Link

Document

Returns the URI at the specified position in this list.

Usage

From source file:com.frochr123.helper.CachedFileDownloader.java

public static SimpleEntry<String, ByteArrayOutputStream> getResultFromURL(String url) throws IOException {
    SimpleEntry<String, ByteArrayOutputStream> result = null;
    String finalUrl = null;//from  w ww. j a  v  a2s  . c  o  m
    ByteArrayOutputStream outputStream = null;

    if (url == null || url.isEmpty()) {
        return null;
    }

    // Create HTTP client and cusomized config for timeouts
    CloseableHttpClient httpClient = HttpClients.createDefault();
    RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(CACHE_DOWNLOADER_DEFAULT_TIMEOUT)
            .setConnectTimeout(CACHE_DOWNLOADER_DEFAULT_TIMEOUT)
            .setConnectionRequestTimeout(CACHE_DOWNLOADER_DEFAULT_TIMEOUT).build();

    // Create HTTP Get request
    HttpGet httpGet = new HttpGet(url);
    httpGet.setConfig(requestConfig);

    // Create context
    HttpContext context = new BasicHttpContext();

    // Check for temporary FabQR download of own configured private FabQR instance
    // In that case Authorization needs to be added
    if (FabQRFunctions.getFabqrPrivateURL() != null && !FabQRFunctions.getFabqrPrivateURL().isEmpty()
            && url.startsWith(FabQRFunctions.getFabqrPrivateURL())
            && url.contains("/" + FabQRFunctions.FABQR_TEMPORARY_MARKER + "/")) {
        // Set authentication information
        String encodedCredentials = Helper.getEncodedCredentials(FabQRFunctions.getFabqrPrivateUser(),
                FabQRFunctions.getFabqrPrivatePassword());
        if (!encodedCredentials.isEmpty()) {
            httpGet.addHeader("Authorization", "Basic " + encodedCredentials);
        }
    }

    // Send request
    CloseableHttpResponse response = httpClient.execute(httpGet, context);

    // Get all redirected locations from context, if there are any
    RedirectLocations redirectLocations = (RedirectLocations) (context
            .getAttribute(HttpClientContext.REDIRECT_LOCATIONS));
    if (redirectLocations != null) {
        finalUrl = redirectLocations.getAll().get(redirectLocations.getAll().size() - 1).toString();
    } else {
        finalUrl = url;
    }

    // Check response valid and max file size
    if (response.getEntity() == null
            || response.getEntity().getContentLength() > CACHE_DOWNLOADER_MAX_FILESIZE_BYTES) {
        return null;
    }

    // Get data
    outputStream = new ByteArrayOutputStream();
    response.getEntity().writeTo(outputStream);

    // Return result
    result = new SimpleEntry<String, ByteArrayOutputStream>(finalUrl, outputStream);
    return result;
}

From source file:com.serphacker.serposcope.scraper.http.ScrapClient.java

public int request(HttpRequestBase request) {
    synchronized (connManager) {
        try {/* ww  w  .ja v a2  s  .c o  m*/
            clearPreviousRequest();
            executionTimeMS = System.currentTimeMillis();

            HttpClientContext context = HttpClientContext.create();
            initializeRequest(request, context);

            response = client.execute(request, context);
            statusCode = response.getStatusLine().getStatusCode();
            RedirectLocations redirects = context.getAttribute(HttpClientContext.REDIRECT_LOCATIONS,
                    RedirectLocations.class);
            if (redirects != null && !redirects.isEmpty()) {
                lastRedirect = redirects.get(redirects.size() - 1).toString();
            }

            HttpEntity entity = response.getEntity();
            long contentLength = entity.getContentLength();

            if (contentLength > maxResponseLength) {
                throw new ResponseTooBigException("content length (" + contentLength + ") "
                        + "is greater than max response leength (" + maxResponseLength + ")");
            }

            InputStream stream = entity.getContent();
            int totalRead = 0;
            int read = 0;

            while (totalRead < maxResponseLength
                    && (read = stream.read(buffer, totalRead, maxResponseLength - totalRead)) != -1) {
                totalRead += read;
            }

            if (totalRead == maxResponseLength && read != 0) {
                throw new ResponseTooBigException("already read " + totalRead + " bytes");
            }
            content = Arrays.copyOfRange(buffer, 0, totalRead);

        } catch (Exception ex) {
            content = null;
            statusCode = -1;
            exception = ex;
        } finally {
            proxyChangedSinceLastRequest = false;
            closeResponse();
            executionTimeMS = System.currentTimeMillis() - executionTimeMS;
        }

        return statusCode;
    }
}