Example usage for org.apache.http.client.cache HttpCacheEntry HttpCacheEntry

List of usage examples for org.apache.http.client.cache HttpCacheEntry HttpCacheEntry

Introduction

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

Prototype

public HttpCacheEntry(Date requestDate, Date responseDate, StatusLine statusLine, Header[] responseHeaders,
        Resource resource) 

Source Link

Document

Create a new HttpCacheEntry .

Usage

From source file:org.jboss.tools.aerogear.hybrid.core.test.TestBundleHttpStorage.java

private HttpCacheEntry makeHttpCacheEntry() {
    final Date now = new Date();
    final StatusLine statusLine = new BasicStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK");
    final Header[] headers = { new BasicHeader("Date", DateUtils.formatDate(now)),
            new BasicHeader("Server", "MockServer/1.0") };
    final Resource resource = new HeapResource(new byte[0]);
    HttpCacheEntry entry = new HttpCacheEntry(now, now, statusLine, headers, resource);
    return entry;
}

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

/*******************************************************************************************************************
 *
 *
 *
 ******************************************************************************************************************/
/* VisibleForTesting */ void onDownloadRequest(final @ListensTo @Nonnull DownloadRequest request)
        throws URISyntaxException {
    try {//from  ww w . ja v  a2  s  .  co  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));
    }
}

From source file:gr.wavesoft.webng.io.cache.DiskCacheStorage.java

private HttpCacheEntry unserialize(byte[] bytes, String key) {
    try {/*from   w w  w  . j  a  v  a 2s.  co m*/
        InputStream is = new ByteArrayInputStream(bytes);
        ObjectInputStream ois = new ObjectInputStream(is);

        // Read data
        Date dReq = new Date(ois.readLong());
        Date dRes = new Date(ois.readLong());
        StatusLine statusLine = (StatusLine) ois.readObject();
        Header[] responseHeaders = (Header[]) ois.readObject();

        // Close streams
        ois.close();
        is.close();

        // Build response
        HttpCacheEntry hce = new HttpCacheEntry(dReq, dRes, statusLine, responseHeaders,
                new FileResource(new File(baseDir + "/" + itemPrefix + key + ".cache")));

        return hce;

    } catch (ClassNotFoundException ex) {
        systemLogger.except(ex);
        return null;
    } catch (UnsupportedEncodingException ex) {
        systemLogger.except(ex);
        return null;
    } catch (IOException ex) {
        systemLogger.except(ex);
        return null;
    }
}

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

/*******************************************************************************************************************
 *
 * //from  www . j a v  a2 s  . c  om
 *
 ******************************************************************************************************************/
@Nonnull
private HttpCacheEntry entryFrom(final @Nonnull Path cacheContentPath, final @Nonnull HttpResponse response) {
    final Date date = new Date(); // FIXME: force hit?
    //                        new Date(Files.getLastModifiedTime(cacheHeadersPath).toMillis());
    final Resource resource = exists(cacheContentPath) ? new FileResource(cacheContentPath.toFile()) : null;

    List<Header> headers = new ArrayList<>(Arrays.asList(response.getAllHeaders()));

    if (neverExpiring) {
        headers = headers.stream().filter(header -> !NEVER_EXPIRING_HEADERS.contains(header.getName()))
                .collect(Collectors.toList());
        headers.add(new BasicHeader("Expires", "Mon, 31 Dec 2099 00:00:00 GMT"));
    }

    return new HttpCacheEntry(date, date, response.getStatusLine(), headers.toArray(new Header[0]), resource);
}

From source file:org.apache.http.impl.client.cache.BasicHttpCache.java

public HttpResponse cacheAndReturnResponse(HttpHost host, HttpRequest request, HttpResponse originResponse,
        Date requestSent, Date responseReceived) throws IOException {

    SizeLimitedResponseReader responseReader = getResponseReader(request, originResponse);
    responseReader.readResponse();//from   w  w  w  . ja  v a 2 s. c  o  m

    if (responseReader.isLimitReached()) {
        return responseReader.getReconstructedResponse();
    }

    Resource resource = responseReader.getResource();
    if (isIncompleteResponse(originResponse, resource)) {
        return generateIncompleteResponseError(originResponse, resource);
    }

    HttpCacheEntry entry = new HttpCacheEntry(requestSent, responseReceived, originResponse.getStatusLine(),
            originResponse.getAllHeaders(), resource);
    storeInCache(host, request, entry);
    return responseGenerator.generateResponse(entry);
}