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;
}
Aggregations