blob: 1c5c6e618e71acc743a9e85af1321fdffc86e4c6 [file] [log] [blame]
Aurimas Liutikasdc3f8852024-07-11 10:07:48 -07001/* GENERATED SOURCE. DO NOT MODIFY. */
2/*
3 * Copyright (C) 2015 Square, Inc.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18package com.android.okhttp.internalandroidapi;
19
20import static android.annotation.SystemApi.Client.MODULE_LIBRARIES;
21
22import android.annotation.SystemApi;
23
24import com.android.okhttp.internalandroidapi.HasCacheHolder.CacheHolder;
25
26import libcore.util.NonNull;
27import libcore.util.Nullable;
28
29import com.android.okhttp.Cache;
30import com.android.okhttp.Request;
31import com.android.okhttp.Response;
32import com.android.okhttp.internal.huc.JavaApiConverter;
33
34import java.io.IOException;
35import java.net.CacheRequest;
36import java.net.CacheResponse;
37import java.net.URI;
38import java.net.URLConnection;
39import java.util.List;
40import java.util.Map;
41
42/**
43 * A modified copy of {@link com.android.okhttp.AndroidShimResponseCache} that exposes a
44 * {@link CacheHolder} instead of a {@link Cache}. We want to keep the framework code that interacts
45 * with OkHttp at arms length. By delegating to this class the Android HttpResponseCache class has
46 * no knowledge of OkHttp internal classes at class resolution time and there are no internal
47 * classes appearing on method signatures.
48 * @hide
49 * @hide This class is not part of the Android public SDK API
50 */
51@SystemApi(client = MODULE_LIBRARIES)
52@libcore.api.CorePlatformApi(status = libcore.api.CorePlatformApi.Status.STABLE)
53public final class AndroidResponseCacheAdapter {
54
55 private final CacheHolder cacheHolder;
56 private final Cache okHttpCache;
57
58 /**
59 * Creates an instance from {@link CacheHolder}
60 *
61 * @hide
62 */
63 @SystemApi(client = MODULE_LIBRARIES)
64 @libcore.api.CorePlatformApi(status = libcore.api.CorePlatformApi.Status.STABLE)
65 public AndroidResponseCacheAdapter(@NonNull CacheHolder cacheHolder) {
66 this.cacheHolder = cacheHolder;
67 // Avoid one level of dereferencing by storing the reference to the OkHttp cache for later.
68 this.okHttpCache = cacheHolder.getCache();
69 }
70
71 /**
72 * Returns the {@link CacheHolder} associated with this instance and can be used by OkHttp
73 * internal code to obtain the underlying OkHttp Cache object.
74 *
75 * @hide
76 */
77 @SystemApi(client = MODULE_LIBRARIES)
78 @libcore.api.CorePlatformApi(status = libcore.api.CorePlatformApi.Status.STABLE)
79 public @NonNull CacheHolder getCacheHolder() {
80 return cacheHolder;
81 }
82
83 /**
84 * Used to implement {@link java.net.ResponseCache#get(URI, String, Map)}. See that method for
85 * details.
86 *
87 * @hide
88 */
89 @SystemApi(client = MODULE_LIBRARIES)
90 @libcore.api.CorePlatformApi(status = libcore.api.CorePlatformApi.Status.STABLE)
91 public @Nullable CacheResponse get(@NonNull URI uri, @NonNull String requestMethod,
92 @Nullable Map<String, List<String>> requestHeaders) throws IOException {
93 Request okRequest = JavaApiConverter.createOkRequest(uri, requestMethod, requestHeaders);
94 Response okResponse = okHttpCache.internalCache.get(okRequest);
95 if (okResponse == null) {
96 return null;
97 }
98 return JavaApiConverter.createJavaCacheResponse(okResponse);
99 }
100
101 /**
102 * Used to implement {@link java.net.ResponseCache#put(URI, URLConnection)}. See that method for
103 * details.
104 *
105 * @hide
106 */
107 @SystemApi(client = MODULE_LIBRARIES)
108 @libcore.api.CorePlatformApi(status = libcore.api.CorePlatformApi.Status.STABLE)
109 public @Nullable CacheRequest put(@NonNull URI uri, @NonNull URLConnection urlConnection)
110 throws IOException {
111 Response okResponse = JavaApiConverter.createOkResponseForCachePut(uri, urlConnection);
112 if (okResponse == null) {
113 // The URLConnection is not cacheable or could not be converted. Stop.
114 return null;
115 }
116 com.android.okhttp.internal.http.CacheRequest okCacheRequest =
117 okHttpCache.internalCache.put(okResponse);
118 if (okCacheRequest == null) {
119 return null;
120 }
121 return JavaApiConverter.createJavaCacheRequest(okCacheRequest);
122 }
123
124 /**
125 * Returns the number of bytes currently being used to store the values in
126 * this cache. This may be greater than the {@link #getMaxSize()} if a background
127 * deletion is pending. IOException is thrown if the size cannot be determined.
128 *
129 * @hide
130 */
131 @SystemApi(client = MODULE_LIBRARIES)
132 @libcore.api.CorePlatformApi(status = libcore.api.CorePlatformApi.Status.STABLE)
133 public long getSize() throws IOException {
134 return okHttpCache.getSize();
135 }
136
137 /**
138 * Returns the maximum number of bytes that this cache should use to store
139 * its data.
140 *
141 * @hide
142 */
143 @SystemApi(client = MODULE_LIBRARIES)
144 @libcore.api.CorePlatformApi(status = libcore.api.CorePlatformApi.Status.STABLE)
145 public long getMaxSize() {
146 return okHttpCache.getMaxSize();
147 }
148
149 /**
150 * Force buffered operations to the filesystem. This ensures that responses
151 * written to the cache will be available the next time the cache is opened,
152 * even if this process is killed. IOException is thrown if the flush fails.
153 *
154 * @hide
155 */
156 @SystemApi(client = MODULE_LIBRARIES)
157 @libcore.api.CorePlatformApi(status = libcore.api.CorePlatformApi.Status.STABLE)
158 public void flush() throws IOException {
159 okHttpCache.flush();
160 }
161
162 /**
163 * Returns the number of HTTP requests that required the network to either
164 * supply a response or validate a locally cached response.
165 *
166 * @hide
167 */
168 @SystemApi(client = MODULE_LIBRARIES)
169 @libcore.api.CorePlatformApi(status = libcore.api.CorePlatformApi.Status.STABLE)
170 public int getNetworkCount() {
171 return okHttpCache.getNetworkCount();
172 }
173
174 /**
175 * Returns the number of HTTP requests whose response was provided by the
176 * cache. This may include conditional {@code GET} requests that were
177 * validated over the network.
178 *
179 * @hide
180 */
181 @SystemApi(client = MODULE_LIBRARIES)
182 @libcore.api.CorePlatformApi(status = libcore.api.CorePlatformApi.Status.STABLE)
183 public int getHitCount() {
184 return okHttpCache.getHitCount();
185 }
186
187 /**
188 * Returns the total number of HTTP requests that were made. This includes
189 * both client requests and requests that were made on the client's behalf
190 * to handle a redirects and retries.
191 *
192 * @hide
193 */
194 @SystemApi(client = MODULE_LIBRARIES)
195 @libcore.api.CorePlatformApi(status = libcore.api.CorePlatformApi.Status.STABLE)
196 public int getRequestCount() {
197 return okHttpCache.getRequestCount();
198 }
199
200 /**
201 * Closes this cache. Stored values will remain on the filesystem.
202 *
203 * @hide
204 */
205 @SystemApi(client = MODULE_LIBRARIES)
206 @libcore.api.CorePlatformApi(status = libcore.api.CorePlatformApi.Status.STABLE)
207 public void close() throws IOException {
208 okHttpCache.close();
209 }
210
211 /**
212 * Closes the cache and deletes all of its stored values. This will delete
213 * all files in the cache directory including files that weren't created by
214 * the cache.
215 *
216 * @hide
217 */
218 @SystemApi(client = MODULE_LIBRARIES)
219 @libcore.api.CorePlatformApi(status = libcore.api.CorePlatformApi.Status.STABLE)
220 public void delete() throws IOException {
221 okHttpCache.delete();
222 }
223}