aboutsummaryrefslogtreecommitdiff
path: root/android/src/main/java/com/android/okhttp/internalandroidapi/AndroidResponseCacheAdapter.java
blob: a7a5eba4188b7dc9719514823296ace6da56814a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
 * Copyright (C) 2015 Square, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.okhttp.internalandroidapi;

import com.android.okhttp.internalandroidapi.HasCacheHolder.CacheHolder;
import com.squareup.okhttp.Cache;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import com.squareup.okhttp.internal.huc.JavaApiConverter;

import java.io.IOException;
import java.net.CacheRequest;
import java.net.CacheResponse;
import java.net.URI;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;

/**
 * A modified copy of {@link com.squareup.okhttp.AndroidShimResponseCache} that exposes a
 * {@link CacheHolder} instead of a {@link Cache}. We want to keep the framework code that interacts
 * with OkHttp at arms length. By delegating to this class the Android HttpResponseCache class has
 * no knowledge of OkHttp internal classes at class resolution time and there are no internal
 * classes appearing on method signatures.
 * @hide
 */
@libcore.api.CorePlatformApi
public final class AndroidResponseCacheAdapter {

    private final CacheHolder cacheHolder;
    private final Cache okHttpCache;

    @libcore.api.CorePlatformApi
    public AndroidResponseCacheAdapter(CacheHolder cacheHolder) {
        this.cacheHolder = cacheHolder;
        // Avoid one level of dereferencing by storing the reference to the OkHttp cache for later.
        this.okHttpCache = cacheHolder.getCache();
    }

    /**
     * Returns the {@link CacheHolder} associated with this instance and can be used by OkHttp
     * internal code to obtain the underlying OkHttp Cache object.
     */
    @libcore.api.CorePlatformApi
    public CacheHolder getCacheHolder() {
        return cacheHolder;
    }

    /**
     * Used to implement {@link java.net.ResponseCache#get(URI, String, Map)}. See that method for
     * details.
     */
    @libcore.api.CorePlatformApi
    public CacheResponse get(URI uri, String requestMethod,
            Map<String, List<String>> requestHeaders) throws IOException {
        Request okRequest = JavaApiConverter.createOkRequest(uri, requestMethod, requestHeaders);
        Response okResponse = okHttpCache.internalCache.get(okRequest);
        if (okResponse == null) {
            return null;
        }
        return JavaApiConverter.createJavaCacheResponse(okResponse);
    }

    /**
     * Used to implement {@link java.net.ResponseCache#put(URI, URLConnection)}. See that method for
     * details.
     */
    @libcore.api.CorePlatformApi
    public CacheRequest put(URI uri, URLConnection urlConnection) throws IOException {
        Response okResponse = JavaApiConverter.createOkResponseForCachePut(uri, urlConnection);
        if (okResponse == null) {
            // The URLConnection is not cacheable or could not be converted. Stop.
            return null;
        }
        com.squareup.okhttp.internal.http.CacheRequest okCacheRequest =
                okHttpCache.internalCache.put(okResponse);
        if (okCacheRequest == null) {
            return null;
        }
        return JavaApiConverter.createJavaCacheRequest(okCacheRequest);
    }

    /**
     * Returns the number of bytes currently being used to store the values in
     * this cache. This may be greater than the {@link #getMaxSize()} if a background
     * deletion is pending. IOException is thrown if the size cannot be determined.
     */
    @libcore.api.CorePlatformApi
    public long getSize() throws IOException {
        return okHttpCache.getSize();
    }

    /**
     * Returns the maximum number of bytes that this cache should use to store
     * its data.
     */
    @libcore.api.CorePlatformApi
    public long getMaxSize() {
        return okHttpCache.getMaxSize();
    }

    /**
     * Force buffered operations to the filesystem. This ensures that responses
     * written to the cache will be available the next time the cache is opened,
     * even if this process is killed. IOException is thrown if the flush fails.
     */
    @libcore.api.CorePlatformApi
    public void flush() throws IOException {
        okHttpCache.flush();
    }

    /**
     * Returns the number of HTTP requests that required the network to either
     * supply a response or validate a locally cached response.
     */
    @libcore.api.CorePlatformApi
    public int getNetworkCount() {
        return okHttpCache.getNetworkCount();
    }

    /**
     * Returns the number of HTTP requests whose response was provided by the
     * cache. This may include conditional {@code GET} requests that were
     * validated over the network.
     */
    @libcore.api.CorePlatformApi
    public int getHitCount() {
        return okHttpCache.getHitCount();
    }

    /**
     * Returns the total number of HTTP requests that were made. This includes
     * both client requests and requests that were made on the client's behalf
     * to handle a redirects and retries.
     */
    @libcore.api.CorePlatformApi
    public int getRequestCount() {
        return okHttpCache.getRequestCount();
    }

    /** Closes this cache. Stored values will remain on the filesystem. */
    @libcore.api.CorePlatformApi
    public void close() throws IOException {
        okHttpCache.close();
    }

    /**
     * Closes the cache and deletes all of its stored values. This will delete
     * all files in the cache directory including files that weren't created by
     * the cache.
     */
    @libcore.api.CorePlatformApi
    public void delete() throws IOException {
        okHttpCache.delete();
    }
}