Home | History | Annotate | Download | only in okhttp
      1 /*
      2  * Copyright (C) 2015 Square, Inc.
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 package com.squareup.okhttp;
     17 
     18 import com.squareup.okhttp.internal.huc.CacheAdapter;
     19 
     20 import java.net.ResponseCache;
     21 
     22 /**
     23  * Back doors to enable the use of OkHttp within the Android platform libraries. OkHttp is used to
     24  * provide the default {@link java.net.HttpURLConnection} / {@link javax.net.ssl.HttpsURLConnection}
     25  * implementation including support for a custom {@link ResponseCache}.
     26  */
     27 public class AndroidInternal {
     28 
     29   private AndroidInternal() {
     30   }
     31 
     32   /** Sets the response cache to be used to read and write cached responses. */
     33   public static void setResponseCache(OkUrlFactory okUrlFactory, ResponseCache responseCache) {
     34     OkHttpClient client = okUrlFactory.client();
     35     if (responseCache instanceof OkCacheContainer) {
     36       // Avoid adding layers of wrappers. Rather than wrap the ResponseCache in yet another layer to
     37       // make the ResponseCache look like an InternalCache, we can unwrap the Cache instead.
     38       // This means that Cache stats will be correctly updated.
     39       OkCacheContainer okCacheContainer = (OkCacheContainer) responseCache;
     40       client.setCache(okCacheContainer.getCache());
     41     } else {
     42       client.setInternalCache(responseCache != null ? new CacheAdapter(responseCache) : null);
     43     }
     44   }
     45 }
     46