List of usage examples for org.apache.http.impl.client.cache HeapResource HeapResource
public HeapResource(final byte[] b)
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 www . j a v a 2 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:org.apache.http.impl.client.cache.TestHttpCacheEntrySerializers.java
private HttpCacheEntry makeCacheEntryWithVariantMap() throws UnsupportedEncodingException { Header[] headers = new Header[5]; for (int i = 0; i < headers.length; i++) { headers[i] = new BasicHeader("header" + i, "value" + i); }/*from w w w.j a v a 2s .c o m*/ String body = "Lorem ipsum dolor sit amet"; ProtocolVersion pvObj = new ProtocolVersion("HTTP", 1, 1); StatusLine slObj = new BasicStatusLine(pvObj, 200, "ok"); Map<String, String> variantMap = new HashMap<String, String>(); variantMap.put("test variant 1", "true"); variantMap.put("test variant 2", "true"); HttpCacheEntry cacheEntry = new HttpCacheEntry(new Date(), new Date(), slObj, headers, new HeapResource(Base64.decodeBase64(body.getBytes(UTF8.name()))), variantMap); return cacheEntry; }