1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 import java.io.IOException;
19 import java.util.List;
23 * Caches {@code URLConnection} responses.
24 * <p>The system's default cache can be set using {@link #setDefault}.
25 * If {@link URLConnection#getUseCaches} returns true, {@code URLConnection} will use the
26 * default response cache, if one has been set.
27 * <p>Although {@code URLConnection} will always call {@link #put}, the specific
28 * {@code ResponseCache} implementation gets to decide what will actually be cached,
31 public abstract class ResponseCache {
32 private static ResponseCache defaultResponseCache = null;
35 * Returns the system's default response cache, or null.
37 public static ResponseCache getDefault() {
38 SecurityManager sm = System.getSecurityManager();
40 sm.checkPermission(new NetPermission("getResponseCache"));
42 return defaultResponseCache;
46 * Sets the system's default response cache. Use null to remove the response cache.
48 public static void setDefault(ResponseCache responseCache) {
49 SecurityManager sm = System.getSecurityManager();
51 sm.checkPermission(new NetPermission("setResponseCache"));
53 defaultResponseCache = responseCache;
57 * Returns the cached response corresponding to the given request.
61 * @param requestMethod
63 * @param requestHeaders
64 * a map of request headers.
65 * @return the {@code CacheResponse} object if the request is available in the cache
66 * or {@code null} otherwise.
68 * if an I/O error occurs while getting the cached data.
69 * @throws IllegalArgumentException
70 * if any one of the parameters is set to {@code null}.
72 public abstract CacheResponse get(URI uri, String requestMethod,
73 Map<String, List<String>> requestHeaders) throws IOException;
76 * Allows the protocol handler to cache data after retrieving resources. The
77 * {@code ResponseCache} decides whether the resource data should be cached
78 * or not. If so, this method returns a {@code CacheRequest} to write the
79 * resource data to. Otherwise, this method returns {@code null}.
82 * the reference to the requested resource.
84 * the connection to fetch the response.
85 * @return a CacheRequest object with a WriteableByteChannel if the resource
86 * has to be cached, {@code null} otherwise.
88 * if an I/O error occurs while adding the resource.
89 * @throws IllegalArgumentException
90 * if any one of the parameters is set to {@code null}.
92 public abstract CacheRequest put(URI uri, URLConnection conn) throws IOException;