List of usage examples for org.apache.http.client.cache HttpCacheContext getCacheResponseStatus
public CacheResponseStatus getCacheResponseStatus()
From source file:it.tidalwave.bluemarine2.downloader.impl.DefaultDownloader.java
/******************************************************************************************************************* * * * ******************************************************************************************************************/ /* VisibleForTesting */ void onDownloadRequest(final @ListensTo @Nonnull DownloadRequest request) throws URISyntaxException { try {/*from ww w .java 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)); } }