Search in sources :

Example 11 with Cache

use of com.android.volley.Cache in project platform_frameworks_base by android.

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;
    }
}
Also used : NetworkResponse(com.android.volley.NetworkResponse) Cache(com.android.volley.Cache)

Example 12 with Cache

use of com.android.volley.Cache in project Douya by DreaminginCodeZH.

the class BasicNetwork method performRequest.

@Override
public NetworkResponse performRequest(Request<?> request) throws VolleyError {
    long requestStart = SystemClock.elapsedRealtime();
    while (true) {
        HttpResponse httpResponse = null;
        byte[] responseContents = null;
        Map<String, String> responseHeaders = Collections.emptyMap();
        try {
            // Gather headers.
            Map<String, String> headers = new HashMap<>();
            addCacheHeaders(headers, request.getCacheEntry());
            httpResponse = mHttpStack.performRequest(request, headers);
            StatusLine statusLine = httpResponse.getStatusLine();
            int statusCode = statusLine.getStatusCode();
            responseHeaders = convertHeaders(httpResponse.getAllHeaders());
            // Handle cache validation.
            if (statusCode == HttpStatus.SC_NOT_MODIFIED) {
                Cache.Entry entry = request.getCacheEntry();
                if (entry == null) {
                    return new NetworkResponse(HttpStatus.SC_NOT_MODIFIED, null, responseHeaders, true, SystemClock.elapsedRealtime() - requestStart);
                }
                // A HTTP 304 response does not have all header fields. We
                // have to use the header fields from the cache entry plus
                // the new ones from the response.
                // http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.5
                entry.responseHeaders.putAll(responseHeaders);
                return new NetworkResponse(HttpStatus.SC_NOT_MODIFIED, entry.data, entry.responseHeaders, true, SystemClock.elapsedRealtime() - requestStart);
            }
            // Some responses such as 204s do not have content.  We must check.
            if (httpResponse.getEntity() != null) {
                responseContents = entityToBytes(httpResponse.getEntity());
            } else {
                // Add 0 byte response as a way of honestly representing a
                // no-content request.
                responseContents = new byte[0];
            }
            // if the request is slow, log it.
            long requestLifetime = SystemClock.elapsedRealtime() - requestStart;
            logSlowRequests(requestLifetime, request, responseContents, statusLine);
            if (statusCode < 200 || statusCode > 299) {
                throw new IOException();
            }
            return new NetworkResponse(statusCode, responseContents, responseHeaders, false, SystemClock.elapsedRealtime() - requestStart);
        } catch (MalformedURLException e) {
            throw new RuntimeException("Bad URL " + request.getUrl(), e);
        } catch (SocketTimeoutException e) {
            attemptRetryOnException("socket-timeout", request, new TimeoutError());
        } catch (ConnectTimeoutException e) {
            attemptRetryOnException("connection-timeout", request, new TimeoutError());
        } catch (IOException e) {
            int statusCode;
            if (httpResponse != null) {
                statusCode = httpResponse.getStatusLine().getStatusCode();
            } else {
                // PATCH: Let RetryPolicy decide whether the request should be aborted.
                attemptRetryOnException("no-connection", request, new NoConnectionError(e));
                continue;
            }
            VolleyLog.e("Unexpected response code %d for %s", statusCode, request.getUrl());
            if (responseContents != null) {
                NetworkResponse networkResponse = new NetworkResponse(statusCode, responseContents, responseHeaders, false, SystemClock.elapsedRealtime() - requestStart);
                if (statusCode == HttpStatus.SC_UNAUTHORIZED || statusCode == HttpStatus.SC_FORBIDDEN) {
                    attemptRetryOnException("auth", request, new AuthFailureError(networkResponse));
                } else {
                    // PATCH: Let RetryPolicy decide whether the request should be aborted.
                    // TODO: Only throw ServerError for 5xx status codes.
                    attemptRetryOnException("server", request, new ServerError(networkResponse));
                }
            } else {
                // PATCH: Let RetryPolicy decide whether the request should be aborted.
                attemptRetryOnException("network", request, new NetworkError());
            }
        }
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) HashMap(java.util.HashMap) ServerError(com.android.volley.ServerError) HttpResponse(org.apache.http.HttpResponse) AuthFailureError(com.android.volley.AuthFailureError) NetworkError(com.android.volley.NetworkError) NoConnectionError(com.android.volley.NoConnectionError) IOException(java.io.IOException) TimeoutError(com.android.volley.TimeoutError) StatusLine(org.apache.http.StatusLine) SocketTimeoutException(java.net.SocketTimeoutException) NetworkResponse(com.android.volley.NetworkResponse) Cache(com.android.volley.Cache) ConnectTimeoutException(org.apache.http.conn.ConnectTimeoutException)

Example 13 with Cache

use of com.android.volley.Cache in project TaEmCasa by Dionen.

the class CacheTestUtils method makeRandomCacheEntry.

/**
     * Makes a random cache entry.
     * @param data Data to use, or null to use random data
     * @param isExpired Whether the TTLs should be set such that this entry is expired
     * @param needsRefresh Whether the TTLs should be set such that this entry needs refresh
     */
public static Cache.Entry makeRandomCacheEntry(byte[] data, boolean isExpired, boolean needsRefresh) {
    Random random = new Random();
    Cache.Entry entry = new Cache.Entry();
    if (data != null) {
        entry.data = data;
    } else {
        entry.data = new byte[random.nextInt(1024)];
    }
    entry.etag = String.valueOf(random.nextLong());
    entry.lastModified = random.nextLong();
    entry.ttl = isExpired ? 0 : Long.MAX_VALUE;
    entry.softTtl = needsRefresh ? 0 : Long.MAX_VALUE;
    return entry;
}
Also used : Random(java.util.Random) Cache(com.android.volley.Cache)

Example 14 with Cache

use of com.android.volley.Cache in project android_frameworks_base by DirtyUnicorns.

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;
    }
}
Also used : NetworkResponse(com.android.volley.NetworkResponse) Cache(com.android.volley.Cache)

Example 15 with Cache

use of com.android.volley.Cache in project android_frameworks_base by ResurrectionRemix.

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;
    }
}
Also used : NetworkResponse(com.android.volley.NetworkResponse) Cache(com.android.volley.Cache)

Aggregations

Cache (com.android.volley.Cache)15 NetworkResponse (com.android.volley.NetworkResponse)9 Random (java.util.Random)4 Header (org.apache.http.Header)3 BasicHeader (org.apache.http.message.BasicHeader)3 Test (org.junit.Test)3 AuthFailureError (com.android.volley.AuthFailureError)1 Network (com.android.volley.Network)1 NetworkError (com.android.volley.NetworkError)1 NoConnectionError (com.android.volley.NoConnectionError)1 RequestQueue (com.android.volley.RequestQueue)1 ServerError (com.android.volley.ServerError)1 TimeoutError (com.android.volley.TimeoutError)1 BasicNetwork (com.android.volley.toolbox.BasicNetwork)1 DiskBasedCache (com.android.volley.toolbox.DiskBasedCache)1 HurlStack (com.android.volley.toolbox.HurlStack)1 DataInputStream (java.io.DataInputStream)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1