Home | History | Annotate | Download | only in recipes
      1 /*
      2  * Copyright (C) 2014 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.recipes;
     17 
     18 import com.squareup.okhttp.Cache;
     19 import com.squareup.okhttp.Interceptor;
     20 import com.squareup.okhttp.OkHttpClient;
     21 import com.squareup.okhttp.Request;
     22 import com.squareup.okhttp.Response;
     23 import java.io.File;
     24 import java.io.IOException;
     25 
     26 public final class RewriteResponseCacheControl {
     27   /** Dangerous interceptor that rewrites the server's cache-control header. */
     28   private static final Interceptor REWRITE_CACHE_CONTROL_INTERCEPTOR = new Interceptor() {
     29     @Override public Response intercept(Chain chain) throws IOException {
     30       Response originalResponse = chain.proceed(chain.request());
     31       return originalResponse.newBuilder()
     32           .header("Cache-Control", "max-age=60")
     33           .build();
     34     }
     35   };
     36 
     37   private final OkHttpClient client;
     38 
     39   public RewriteResponseCacheControl(File cacheDirectory) throws Exception {
     40     Cache cache = new Cache(cacheDirectory, 1024 * 1024);
     41     cache.evictAll();
     42 
     43     client = new OkHttpClient();
     44     client.setCache(cache);
     45   }
     46 
     47   public void run() throws Exception {
     48     for (int i = 0; i < 5; i++) {
     49       System.out.println("    Request: " + i);
     50 
     51       Request request = new Request.Builder()
     52           .url("https://api.github.com/search/repositories?q=http")
     53           .build();
     54 
     55       if (i == 2) {
     56         // Force this request's response to be written to the cache. This way, subsequent responses
     57         // can be read from the cache.
     58         System.out.println("Force cache: true");
     59         client.networkInterceptors().add(REWRITE_CACHE_CONTROL_INTERCEPTOR);
     60       } else {
     61         System.out.println("Force cache: false");
     62         client.networkInterceptors().clear();
     63       }
     64 
     65       Response response = client.newCall(request).execute();
     66       response.body().close();
     67       if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
     68 
     69       System.out.println("    Network: " + (response.networkResponse() != null));
     70       System.out.println();
     71     }
     72   }
     73 
     74   public static void main(String... args) throws Exception {
     75     new RewriteResponseCacheControl(new File("RewriteResponseCacheControl.tmp")).run();
     76   }
     77 }
     78