Search in sources :

Example 26 with Handshake

use of okhttp3.Handshake in project edx-app-android by edx.

the class CustomCacheQueryInterceptor method intercept.

@Override
public Response intercept(@NonNull final Chain chain) throws IOException {
    final Request request = chain.request();
    Response response = chain.proceed(request);
    final String urlString = request.url().toString();
    // If the OkHttp client
    if (response.cacheResponse() != null) {
        cacheManager.remove(urlString);
    } else {
        final String cachedBody = cacheManager.get(urlString);
        if (cachedBody != null) {
            /* Since we don't cache the metadata, the cached entries should always be assumed to
                 * have a 200 response code. The body is not provided in this response, because it
                 * would only be needed if we don't have a network response.
                 */
            Response cacheResponse = response.newBuilder().code(HttpStatus.OK).message("OK").handshake(request.isHttps() ? DUMMY_HANDSHAKE : null).priorResponse(null).networkResponse(null).cacheResponse(null).body(null).build();
            final CacheStrategy cacheStrategy = new CacheStrategy.Factory(System.currentTimeMillis(), request, cacheResponse).get();
            cacheResponse = cacheStrategy.cacheResponse;
            if (cacheResponse != null) {
                /* Either querying the server is forbidden by the Cache-Control headers (if
                     * there is no network response), or they require a conditional query
                     * (otherwise). In the latter case, either the server has validated the cached
                     * response or not. Only in the last case would the network response be
                     * delivered; in the first two cases the cached response would be delivered
                     * instead.
                     */
                final Response networkResponse = response.networkResponse();
                if (networkResponse == null || shouldUseCachedResponse(cacheResponse, networkResponse)) {
                    response = response.newBuilder().code(HttpStatus.OK).cacheResponse(cacheResponse).body(ResponseBody.create(MediaType.parse("application/json"), cachedBody)).build();
                } else {
                    response = response.newBuilder().cacheResponse(cacheResponse).build();
                    if (HttpEngine.hasBody(response) && HttpMethod.invalidatesCache(request.method())) {
                        cacheManager.remove(urlString);
                    }
                }
            }
        }
    }
    return response;
}
Also used : Response(okhttp3.Response) Request(okhttp3.Request) CacheStrategy(okhttp3.internal.http.CacheStrategy)

Aggregations

Handshake (okhttp3.Handshake)13 Request (okhttp3.Request)12 Response (okhttp3.Response)11 Test (org.junit.jupiter.api.Test)9 Certificate (java.security.cert.Certificate)7 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)7 RecordingHostnameVerifier (okhttp3.RecordingHostnameVerifier)7 HandshakeCertificates (okhttp3.tls.HandshakeCertificates)7 CacheResponse (java.net.CacheResponse)6 SecureCacheResponse (java.net.SecureCacheResponse)6 SSLPeerUnverifiedException (javax.net.ssl.SSLPeerUnverifiedException)5 MockResponse (mockwebserver3.MockResponse)5 HeldCertificate (okhttp3.tls.HeldCertificate)5 Test (org.junit.Test)5 BufferedReader (java.io.BufferedReader)4 IOException (java.io.IOException)4 InputStreamReader (java.io.InputStreamReader)4 Headers (okhttp3.Headers)4 HttpUrl (okhttp3.HttpUrl)4 ResponseBody (okhttp3.ResponseBody)4