List of usage examples for org.apache.http.client.cache HttpCacheEntry getResource
public Resource getResource()
From source file:gr.wavesoft.webng.io.cache.DiskCacheStorage.java
public void putEntry(String string, HttpCacheEntry hce) throws IOException { String key = WebNGSystem.SHA1Sum(string); Long size = hce.getResource().length(); // Firstly, can we fit the item? if (size > WebNGCache.config.MAX_OBJECT_SIZE) { systemLogger.error("Oversized item requested for key " + key); return;//w ww . ja v a2 s . c o m } // If yes, cleanup enough space to fit this item (if needed) if (!cleanupToFit(size)) { systemLogger.error("Unable to fit item " + key + " in cache"); return; } // Dump file to disk InputStream is = hce.getResource().getInputStream(); FileOutputStream fos = new FileOutputStream(baseDir + "/" + itemPrefix + key + ".cache"); byte[] b = new byte[IO_BUFFER_SIZE]; int read; while ((read = is.read(b)) != -1) { fos.write(b, 0, read); } fos.close(); is.close(); /* // Build headers String headers = ""; for (Header h: hce.getAllHeaders()) { headers += h.getName()+": "+h.getValue()+"\n"; } // Prepare some vars ProtocolVersion pv = hce.getStatusLine().getProtocolVersion(); */ // Update entry Database.assignArgStatements(stmInsert, new Object[] { key, System.currentTimeMillis(), "", size }); // Save to DB try { stmInsert.setBytes(3, serialize(hce)); stmInsert.execute(); } catch (SQLException ex) { systemLogger.except(ex); return; } }
From source file:it.tidalwave.bluemarine2.downloader.impl.SimpleHttpCacheStorage.java
/******************************************************************************************************************* * * {@inheritDoc}//from www.j a va2 s . co m * ******************************************************************************************************************/ @Override public void putEntry(final @Nonnull String key, final @Nonnull HttpCacheEntry entry) throws IOException { try { log.debug("putEntry({}, {})", key, entry); final Path cachePath = getCacheItemPath(new URL(key)); createDirectories(cachePath); final Path cacheHeadersPath = cachePath.resolve(PATH_HEADERS); final Path cacheContentPath = cachePath.resolve(PATH_CONTENT); @Cleanup final OutputStream os = newOutputStream(cacheHeadersPath, CREATE); final SessionOutputBufferImpl sob = sessionOutputBufferFrom(os); final DefaultHttpResponseWriter writer = new DefaultHttpResponseWriter(sob); writer.write(responseFrom(entry)); sob.flush(); if (entry.getResource().length() > 0) { copy(entry.getResource().getInputStream(), cacheContentPath, REPLACE_EXISTING); } } catch (HttpException e) { throw new IOException(e); } }
From source file:org.apache.http.impl.client.cache.BasicHttpCache.java
HttpCacheEntry doGetUpdatedParentEntry(final String requestId, final HttpCacheEntry existing, final HttpCacheEntry entry, final String variantKey, final String variantCacheKey) throws IOException { HttpCacheEntry src = existing; if (src == null) { src = entry;/*from w w w .j a va 2s . c om*/ } Resource resource = resourceFactory.copy(requestId, src.getResource()); Map<String, String> variantMap = new HashMap<String, String>(src.getVariantMap()); variantMap.put(variantKey, variantCacheKey); return new HttpCacheEntry(src.getRequestDate(), src.getResponseDate(), src.getStatusLine(), src.getAllHeaders(), resource, variantMap); }
From source file:org.apache.http.impl.client.cache.TestHttpCacheEntrySerializers.java
private boolean areEqual(HttpCacheEntry one, HttpCacheEntry two) throws IOException { // dates are only stored with second precision, so scrub milliseconds if (!((one.getRequestDate().getTime() / 1000) == (two.getRequestDate().getTime() / 1000))) return false; if (!((one.getResponseDate().getTime() / 1000) == (two.getResponseDate().getTime() / 1000))) return false; if (!one.getProtocolVersion().equals(two.getProtocolVersion())) return false; byte[] onesByteArray = resourceToBytes(one.getResource()); byte[] twosByteArray = resourceToBytes(two.getResource()); if (!Arrays.equals(onesByteArray, twosByteArray)) return false; Header[] oneHeaders = one.getAllHeaders(); Header[] twoHeaders = two.getAllHeaders(); if (!(oneHeaders.length == twoHeaders.length)) return false; for (int i = 0; i < oneHeaders.length; i++) { if (!oneHeaders[i].getName().equals(twoHeaders[i].getName())) return false; if (!oneHeaders[i].getValue().equals(twoHeaders[i].getValue())) return false; }/*w ww . j av a 2s.co m*/ return true; }