use of com.android.volley.Cache in project WordPress-Android by wordpress-mobile.
the class MeFragment method injectCache.
// injects a fabricated cache entry to the request cache
private void injectCache(File file, String avatarUrl) throws IOException {
final SimpleDateFormat sdf = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss z");
final long currentTimeMs = System.currentTimeMillis();
final Date currentTime = new Date(currentTimeMs);
final long fiveMinutesLaterMs = currentTimeMs + 5 * 60 * 1000;
final Date fiveMinutesLater = new Date(fiveMinutesLaterMs);
Cache.Entry entry = new Cache.Entry();
entry.data = new byte[(int) file.length()];
DataInputStream dis = new DataInputStream(new FileInputStream(file));
dis.readFully(entry.data);
dis.close();
entry.etag = null;
entry.softTtl = fiveMinutesLaterMs;
entry.ttl = fiveMinutesLaterMs;
entry.serverDate = currentTimeMs;
entry.lastModified = currentTimeMs;
entry.responseHeaders = new TreeMap<>();
entry.responseHeaders.put("Accept-Ranges", "bytes");
entry.responseHeaders.put("Access-Control-Allow-Origin", "*");
entry.responseHeaders.put("Cache-Control", "max-age=300");
entry.responseHeaders.put("Content-Disposition", "inline; filename=\"" + mAccountStore.getAccount().getAvatarUrl() + ".jpeg\"");
entry.responseHeaders.put("Content-Length", String.valueOf(file.length()));
entry.responseHeaders.put("Content-Type", "image/jpeg");
entry.responseHeaders.put("Date", sdf.format(currentTime));
entry.responseHeaders.put("Expires", sdf.format(fiveMinutesLater));
entry.responseHeaders.put("Last-Modified", sdf.format(currentTime));
entry.responseHeaders.put("Link", "<" + avatarUrl + ">; rel=\"canonical\"");
entry.responseHeaders.put("Server", "injected cache");
entry.responseHeaders.put("Source-Age", "0");
entry.responseHeaders.put("X-Android-Received-Millis", String.valueOf(currentTimeMs));
entry.responseHeaders.put("X-Android-Response-Source", "NETWORK 200");
entry.responseHeaders.put("X-Android-Selected-Protocol", "http/1.1");
entry.responseHeaders.put("X-Android-Sent-Millis", String.valueOf(currentTimeMs));
WordPress.sRequestQueue.getCache().put(Request.Method.GET + ":" + avatarUrl, entry);
}
use of com.android.volley.Cache in project TaEmCasa by Dionen.
the class HttpHeaderParserTest method parseCaseInsensitive.
@Test
public void parseCaseInsensitive() {
long now = System.currentTimeMillis();
Header[] headersArray = new Header[5];
headersArray[0] = new BasicHeader("eTAG", "Yow!");
headersArray[1] = new BasicHeader("DATE", rfc1123Date(now));
headersArray[2] = new BasicHeader("expires", rfc1123Date(now + ONE_HOUR_MILLIS));
headersArray[3] = new BasicHeader("cache-control", "public, max-age=86400");
headersArray[4] = new BasicHeader("content-type", "text/plain");
Map<String, String> headers = BasicNetwork.convertHeaders(headersArray);
NetworkResponse response = new NetworkResponse(0, null, headers, false);
Cache.Entry entry = HttpHeaderParser.parseCacheHeaders(response);
assertNotNull(entry);
assertEquals("Yow!", entry.etag);
assertEqualsWithin(now + ONE_DAY_MILLIS, entry.ttl, ONE_MINUTE_MILLIS);
assertEquals(entry.softTtl, entry.ttl);
assertEquals("ISO-8859-1", HttpHeaderParser.parseCharset(headers));
}
use of com.android.volley.Cache in project android_frameworks_base by crdroidandroid.
the class URLFetcher method getExpirationTimeMillisFromHTTPHeader.
/**
* Parses the HTTP headers to compute the ttl.
*
* @param headers a map that map the header key to the header values. Can be null.
* @return the ttl in millisecond or null if the ttl is not specified in the header.
*/
private Long getExpirationTimeMillisFromHTTPHeader(Map<String, List<String>> headers) {
if (headers == null) {
return null;
}
Map<String, String> joinedHeaders = joinHttpHeaders(headers);
NetworkResponse response = new NetworkResponse(null, joinedHeaders);
Cache.Entry cachePolicy = HttpHeaderParser.parseCacheHeaders(response);
if (cachePolicy == null) {
// Cache is disabled, set the expire time to 0.
return DO_NOT_CACHE_RESULT;
} else if (cachePolicy.ttl == 0) {
// Cache policy is not specified, set the expire time to 0.
return DO_NOT_CACHE_RESULT;
} else {
// cachePolicy.ttl is actually the expire timestamp in millisecond.
return cachePolicy.ttl;
}
}
use of com.android.volley.Cache in project android-volley by mcxiaoke.
the class HttpHeaderParserTest method parseCaseInsensitive.
@Test
public void parseCaseInsensitive() {
long now = System.currentTimeMillis();
Header[] headersArray = new Header[5];
headersArray[0] = new BasicHeader("eTAG", "Yow!");
headersArray[1] = new BasicHeader("DATE", rfc1123Date(now));
headersArray[2] = new BasicHeader("expires", rfc1123Date(now + ONE_HOUR_MILLIS));
headersArray[3] = new BasicHeader("cache-control", "public, max-age=86400");
headersArray[4] = new BasicHeader("content-type", "text/plain");
Map<String, String> headers = BasicNetwork.convertHeaders(headersArray);
NetworkResponse response = new NetworkResponse(0, null, headers, false);
Cache.Entry entry = HttpHeaderParser.parseCacheHeaders(response);
assertNotNull(entry);
assertEquals("Yow!", entry.etag);
assertEqualsWithin(now + ONE_DAY_MILLIS, entry.ttl, ONE_MINUTE_MILLIS);
assertEquals(entry.softTtl, entry.ttl);
assertEquals("ISO-8859-1", HttpHeaderParser.parseCharset(headers));
}
use of com.android.volley.Cache in project DesignLibrary by StylingAndroid.
the class VolleySingleton method getRequestQueue.
public RequestQueue getRequestQueue() {
if (requestQueue == null) {
Cache cache = new DiskBasedCache(context.getCacheDir(), CACHE_SIZE);
Network network = new BasicNetwork(new HurlStack());
requestQueue = new RequestQueue(cache, network);
requestQueue.start();
}
return requestQueue;
}
Aggregations