13c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller/* 23c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * Copyright (C) 2014 Square, Inc. 33c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * 43c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * Licensed under the Apache License, Version 2.0 (the "License"); 53c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * you may not use this file except in compliance with the License. 63c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * You may obtain a copy of the License at 73c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * 83c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * http://www.apache.org/licenses/LICENSE-2.0 93c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * 103c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * Unless required by applicable law or agreed to in writing, software 113c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * distributed under the License is distributed on an "AS IS" BASIS, 123c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 133c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * See the License for the specific language governing permissions and 143c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * limitations under the License. 153c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller */ 163c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fullerpackage com.squareup.okhttp.internal.http; 173c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller 183c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fullerimport com.squareup.okhttp.OkResponseCache; 193c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fullerimport com.squareup.okhttp.Request; 203c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fullerimport com.squareup.okhttp.Response; 213c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fullerimport com.squareup.okhttp.ResponseSource; 223c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fullerimport java.io.IOException; 233c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fullerimport java.net.CacheRequest; 243c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fullerimport java.net.CacheResponse; 253c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fullerimport java.net.HttpURLConnection; 263c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fullerimport java.net.ResponseCache; 273c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fullerimport java.net.URI; 283c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fullerimport java.util.List; 293c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fullerimport java.util.Map; 303c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller 313c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller/** 323c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * An adapter from {@link ResponseCache} to {@link com.squareup.okhttp.OkResponseCache}. This class 333c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * enables OkHttp to continue supporting Java standard response cache implementations. 343c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller */ 353c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fullerpublic class ResponseCacheAdapter implements OkResponseCache { 363c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller 373c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller private final ResponseCache delegate; 383c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller 393c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller public ResponseCacheAdapter(ResponseCache delegate) { 403c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller this.delegate = delegate; 413c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller } 423c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller 433c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller public ResponseCache getDelegate() { 443c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller return delegate; 453c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller } 463c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller 473c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller @Override 483c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller public Response get(Request request) throws IOException { 493c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller CacheResponse javaResponse = getJavaCachedResponse(request); 503c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller if (javaResponse == null) { 513c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller return null; 523c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller } 534944713f5c5b141966ac82973d6a31a634e8e01eNeil Fuller return JavaApiConverter.createOkResponse(request, javaResponse); 543c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller } 553c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller 563c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller @Override 573c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller public CacheRequest put(Response response) throws IOException { 583c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller URI uri = response.request().uri(); 594944713f5c5b141966ac82973d6a31a634e8e01eNeil Fuller HttpURLConnection connection = JavaApiConverter.createJavaUrlConnection(response); 603c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller return delegate.put(uri, connection); 613c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller } 623c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller 633c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller @Override 643c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller public boolean maybeRemove(Request request) throws IOException { 653c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller // This method is treated as optional and there is no obvious way of implementing it with 663c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller // ResponseCache. Removing items from the cache due to modifications made from this client is 673c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller // not essential given that modifications could be made from any other client. We have to assume 683c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller // that it's ok to keep using the cached data. Otherwise the server shouldn't declare it as 693c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller // cacheable or the client should be careful about caching it. 703c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller return false; 713c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller } 723c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller 733c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller @Override 743c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller public void update(Response cached, Response network) throws IOException { 753c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller // This method is treated as optional and there is no obvious way of implementing it with 763c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller // ResponseCache. Updating headers is useful if the server changes the metadata for a resource 773c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller // (e.g. max age) to extend or truncate the life of that resource in the cache. If the metadata 783c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller // is not updated the caching behavior may not be optimal, but will obey the metadata sent 793c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller // with the original cached response. 803c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller } 813c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller 823c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller @Override 833c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller public void trackConditionalCacheHit() { 843c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller // This method is treated as optional. 853c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller } 863c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller 873c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller @Override 883c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller public void trackResponse(ResponseSource source) { 893c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller // This method is treated as optional. 903c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller } 913c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller 923c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller /** 933c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * Returns the {@link CacheResponse} from the delegate by converting the 943c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * OkHttp {@link Request} into the arguments required by the {@link ResponseCache}. 953c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller */ 963c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller private CacheResponse getJavaCachedResponse(Request request) throws IOException { 974944713f5c5b141966ac82973d6a31a634e8e01eNeil Fuller Map<String, List<String>> headers = JavaApiConverter.extractJavaHeaders(request); 983c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller return delegate.get(request.uri(), request.method(), headers); 993c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller } 1003c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller 1013c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller} 102