Example usage for org.apache.http.client.cache HttpCacheContext create

List of usage examples for org.apache.http.client.cache HttpCacheContext create

Introduction

In this page you can find the example usage for org.apache.http.client.cache HttpCacheContext create.

Prototype

public static HttpCacheContext create() 

Source Link

Usage

From source file:piecework.content.concrete.RemoteResource.java

protected synchronized void ensureInitialized() {
    if (!this.initialized) {
        this.initialized = true;
        HttpCacheContext context = HttpCacheContext.create();
        HttpHead head = new HttpHead(uri);

        final ConnectionCloser connectionCloser = new ConnectionCloser();
        try {//ww  w .  ja  v a 2s . c o m
            LOG.info("Retrieving resource from " + uri.toString());
            CloseableHttpResponse response = client.execute(head, context);
            connectionCloser.setResponse(response);
            addDetails(response);
        } catch (Exception e) {
            LOG.error("Unable to retrieve details about remote resource", e);
        } finally {
            connectionCloser.closeEverythingQuietly();
        }
    }
}

From source file:piecework.content.concrete.RemoteResource.java

@Override
public InputStream getInputStream() throws IOException {
    HttpCacheContext context = HttpCacheContext.create();
    HttpGet get = new HttpGet(uri);

    addHeaders(get);/* w  w w.  ja va 2 s  .  c o  m*/
    final ConnectionCloser connectionCloser = new ConnectionCloser();
    try {
        LOG.info("Retrieving resource from " + uri.toString());
        CloseableHttpResponse response = client.execute(get, context);
        connectionCloser.setResponse(response);
        addDetails(response);
        HttpEntity entity = response.getEntity();
        connectionCloser.setEntity(entity);

        InputStream inputStream = entity.getContent();

        if (inputStream != null) {
            inputStream = new AutoCloseInputStream(inputStream) {
                @Override
                public void close() throws IOException {
                    super.close();
                    connectionCloser.closeEverythingQuietly();
                }
            };
        }

        return inputStream;

    } catch (Exception e) {
        connectionCloser.closeEverythingQuietly();
        LOG.error("Unable to retrieve remote resource", e);
        throw new IOException("Unable to retrieve remote resource content", e);
    }
}

From source file:it.tidalwave.bluemarine2.downloader.impl.DefaultDownloader.java

/*******************************************************************************************************************
 *
 *
 *
 ******************************************************************************************************************/
/* VisibleForTesting */ void onDownloadRequest(final @ListensTo @Nonnull DownloadRequest request)
        throws URISyntaxException {
    try {//from w ww  .j  a va 2  s.c o  m
        log.info("onDownloadRequest({})", request);

        URL url = request.getUrl();

        for (;;) {
            final HttpCacheContext context = HttpCacheContext.create();
            @Cleanup
            final CloseableHttpResponse response = httpClient.execute(new HttpGet(url.toURI()), context);
            final byte[] bytes = bytesFrom(response);
            final CacheResponseStatus cacheResponseStatus = context.getCacheResponseStatus();
            log.debug(">>>> cacheResponseStatus: {}", cacheResponseStatus);

            final Origin origin = cacheResponseStatus.equals(CacheResponseStatus.CACHE_HIT) ? Origin.CACHE
                    : Origin.NETWORK;

            // FIXME: shouldn't do this by myself
            // FIXME: upon configuration, everything should be cached (needed for supporting integration tests)
            if (!origin.equals(Origin.CACHE)
                    && Arrays.asList(200, 303).contains(response.getStatusLine().getStatusCode())) {
                final Date date = new Date();
                final Resource resource = new HeapResource(bytes);
                cacheStorage.putEntry(url.toExternalForm(), new HttpCacheEntry(date, date,
                        response.getStatusLine(), response.getAllHeaders(), resource));
            }

            // FIXME: if the redirect were enabled, we could drop this check
            if (request.isOptionPresent(DownloadRequest.Option.FOLLOW_REDIRECT)
                    && response.getStatusLine().getStatusCode() == 303) // SEE_OTHER FIXME
            {
                url = new URL(response.getFirstHeader("Location").getValue());
                log.info(">>>> following 'see also' to {} ...", url);
            } else {
                messageBus.publish(new DownloadComplete(request.getUrl(),
                        response.getStatusLine().getStatusCode(), bytes, origin));
                return;
            }
        }
    } catch (IOException e) {
        log.error("{}: {}", request.getUrl(), e.toString());
        messageBus.publish(new DownloadComplete(request.getUrl(), -1, new byte[0], Origin.NETWORK));
    }
}