@Override public void close() throws IOException { if (ResponseCache.getDefault() == this.delegate) { ResponseCache.setDefault(null); } delegate.close(); }
/** * Returns a shallow copy of this OkHttpClient that uses the system-wide default for each field * that hasn't been explicitly configured. */ OkHttpClient copyWithDefaults() { OkHttpClient result = clone(); if (result.proxySelector == null) { result.proxySelector = ProxySelector.getDefault(); } if (result.cookieHandler == null) { result.cookieHandler = CookieHandler.getDefault(); } if (result.cache == null && result.cacheAdapter == null) { // TODO: drop support for the default response cache. ResponseCache defaultCache = ResponseCache.getDefault(); result.cacheAdapter = defaultCache != null ? new CacheAdapter(defaultCache) : null; } if (result.socketFactory == null) { result.socketFactory = SocketFactory.getDefault(); } if (result.sslSocketFactory == null) { result.sslSocketFactory = getDefaultSSLSocketFactory(); } if (result.hostnameVerifier == null) { result.hostnameVerifier = OkHostnameVerifier.INSTANCE; } if (result.authenticator == null) { result.authenticator = AuthenticatorAdapter.INSTANCE; } if (result.connectionPool == null) { result.connectionPool = ConnectionPool.getDefault(); } if (result.protocols == null) { result.protocols = Util.immutableList(Protocol.HTTP_2, Protocol.SPDY_3, Protocol.HTTP_1_1); } return result; }
// if user sets useCache to true, tries to put response to cache if cache // exists private void putToCache() throws IOException { if (useCaches && null != responseCache) { cacheRequest = responseCache.put(uri, this); if (null != cacheRequest) { cacheOut = cacheRequest.getBody(); } } }
public static HttpResponseCache install(Context context) throws IOException { ResponseCache installed = ResponseCache.getDefault(); if (installed instanceof Closeable) { ((Closeable) installed).close(); } File directory = SystemUtils.getCacheDir(context, "http"); long maxSize = SystemUtils.calculateDiskCacheSize(directory, MIN_DISK_CACHE_SIZE, MAX_DISK_CACHE_SIZE); return new HttpResponseCache(android.net.http.HttpResponseCache.install(directory, maxSize)); }
@Override public void onCreate() { super.onCreate(); // Setting up caching ResponseCache.setDefault(new CEResponseCache()); this.prefs = PreferenceManager.getDefaultSharedPreferences(this); this.challengeData = new ChallengeData(this); this.activityData = new ActivityData(this); // Prepare Intent for UpdaterService updaterServiceIntent = new Intent(this, UpdateService.class); updaterPendingIntent = PendingIntent.getService(this, -1, updaterServiceIntent, PendingIntent.FLAG_UPDATE_CURRENT); }
@Override public CacheRequest put(Response response) throws IOException { URI uri = response.request().url().uri(); HttpURLConnection connection = JavaApiConverter.createJavaUrlConnectionForCachePut(response); final java.net.CacheRequest request = delegate.put(uri, connection); if (request == null) { return null; } return new CacheRequest() { @Override public Sink body() throws IOException { OutputStream body = request.getBody(); return body != null ? Okio.sink(body) : null; } @Override public void abort() { request.abort(); } }; }
// Tries to get head and body from cache, return true if has got this time // or // already got before private boolean getFromCache() throws IOException { if (useCaches && null != responseCache && !hasTriedCache) { hasTriedCache = true; if (null == resHeader) { resHeader = new Header(); } cacheResponse = responseCache.get(uri, method, resHeader.getFieldMap()); if (null != cacheResponse) { Map<String, List<String>> headMap = cacheResponse.getHeaders(); if (null != headMap) { resHeader = new Header(headMap); } is = cacheResponse.getBody(); if (null != is) { return true; } } } if (hasTriedCache && null != is) { return true; } return false; }
@Override public CacheRequest put(URI uri, URLConnection urlConnection) throws IOException { return responseCache.put(uri, urlConnection); }
@Override public CacheResponse get(URI uri, String requestMethod, Map<String, List<String>> requestHeaders) throws IOException { return responseCache.get(uri, requestMethod, requestHeaders); }
/** * Returns the {@link CacheResponse} from the delegate by converting the OkHttp {@link Request} * into the arguments required by the {@link ResponseCache}. */ private CacheResponse getJavaCachedResponse(Request request) throws IOException { Map<String, List<String>> headers = JavaApiConverter.extractJavaHeaders(request); return delegate.get(request.url().uri(), request.method(), headers); }
public BitmapProcessor(final Context context) { ResponseCache.setDefault(new ImageResponseCache(context.getCacheDir())); }
public static void installResponseCache() { if (!IS_CACHE_DISABLED) { ResponseCache.setDefault(new LocalResponseCache()); } }