Home | History | Annotate | Download | only in huc
      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.internal.huc;
     17 
     18 import com.squareup.okhttp.Request;
     19 import com.squareup.okhttp.Response;
     20 import com.squareup.okhttp.internal.InternalCache;
     21 import com.squareup.okhttp.internal.http.CacheRequest;
     22 import com.squareup.okhttp.internal.http.CacheStrategy;
     23 import java.io.IOException;
     24 import java.io.OutputStream;
     25 import java.net.CacheResponse;
     26 import java.net.HttpURLConnection;
     27 import java.net.ResponseCache;
     28 import java.net.URI;
     29 import java.util.List;
     30 import java.util.Map;
     31 import okio.Okio;
     32 import okio.Sink;
     33 
     34 /** Adapts {@link ResponseCache} to {@link InternalCache}. */
     35 public final class CacheAdapter implements InternalCache {
     36   private final ResponseCache delegate;
     37 
     38   public CacheAdapter(ResponseCache delegate) {
     39     this.delegate = delegate;
     40   }
     41 
     42   public ResponseCache getDelegate() {
     43     return delegate;
     44   }
     45 
     46   @Override public Response get(Request request) throws IOException {
     47     CacheResponse javaResponse = getJavaCachedResponse(request);
     48     if (javaResponse == null) {
     49       return null;
     50     }
     51     return JavaApiConverter.createOkResponseForCacheGet(request, javaResponse);
     52   }
     53 
     54   @Override public CacheRequest put(Response response) throws IOException {
     55     URI uri = response.request().uri();
     56     HttpURLConnection connection = JavaApiConverter.createJavaUrlConnectionForCachePut(response);
     57     final java.net.CacheRequest request = delegate.put(uri, connection);
     58     if (request == null) {
     59       return null;
     60     }
     61     return new CacheRequest() {
     62       @Override public Sink body() throws IOException {
     63         OutputStream body = request.getBody();
     64         return body != null ? Okio.sink(body) : null;
     65       }
     66 
     67       @Override public void abort() {
     68         request.abort();
     69       }
     70     };
     71   }
     72 
     73   @Override public void remove(Request request) throws IOException {
     74     // This method is treated as optional and there is no obvious way of implementing it with
     75     // ResponseCache. Removing items from the cache due to modifications made from this client is
     76     // not essential given that modifications could be made from any other client. We have to assume
     77     // that it's ok to keep using the cached data. Otherwise the server shouldn't declare it as
     78     // cacheable or the client should be careful about caching it.
     79   }
     80 
     81   @Override public void update(Response cached, Response network) throws IOException {
     82     // This method is treated as optional and there is no obvious way of implementing it with
     83     // ResponseCache. Updating headers is useful if the server changes the metadata for a resource
     84     // (e.g. max age) to extend or truncate the life of that resource in the cache. If the metadata
     85     // is not updated the caching behavior may not be optimal, but will obey the metadata sent
     86     // with the original cached response.
     87   }
     88 
     89   @Override public void trackConditionalCacheHit() {
     90     // This method is optional.
     91   }
     92 
     93   @Override public void trackResponse(CacheStrategy cacheStrategy) {
     94     // This method is optional.
     95   }
     96 
     97   /**
     98    * Returns the {@link CacheResponse} from the delegate by converting the
     99    * OkHttp {@link Request} into the arguments required by the {@link ResponseCache}.
    100    */
    101   private CacheResponse getJavaCachedResponse(Request request) throws IOException {
    102     Map<String, List<String>> headers = JavaApiConverter.extractJavaHeaders(request);
    103     return delegate.get(request.uri(), request.method(), headers);
    104   }
    105 }
    106