Path: blob/master/src/java.base/share/classes/java/net/ResponseCache.java
41152 views
/*1* Copyright (c) 2003, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.net;2627import java.io.IOException;28import java.util.Map;29import java.util.List;30import sun.security.util.SecurityConstants;3132/**33* Represents implementations of URLConnection caches. An instance of34* such a class can be registered with the system by doing35* ResponseCache.setDefault(ResponseCache), and the system will call36* this object in order to:37*38* <ul><li>store resource data which has been retrieved from an39* external source into the cache</li>40* <li>try to fetch a requested resource that may have been41* stored in the cache</li>42* </ul>43*44* The ResponseCache implementation decides which resources45* should be cached, and for how long they should be cached. If a46* request resource cannot be retrieved from the cache, then the47* protocol handlers will fetch the resource from its original48* location.49*50* The settings for URLConnection#useCaches controls whether the51* protocol is allowed to use a cached response.52*53* For more information on HTTP caching, see <a54* href="http://www.ietf.org/rfc/rfc2616.txt"><i>RFC 2616: Hypertext55* Transfer Protocol -- HTTP/1.1</i></a>56*57* @author Yingxian Wang58* @since 1.559*/60public abstract class ResponseCache {6162/**63* Constructor for subclasses to call.64*/65public ResponseCache() {}6667/**68* The system wide cache that provides access to a url69* caching mechanism.70*71* @see #setDefault(ResponseCache)72* @see #getDefault()73*/74private static ResponseCache theResponseCache;7576/**77* Gets the system-wide response cache.78*79* @throws SecurityException80* If a security manager has been installed and it denies81* {@link NetPermission}{@code ("getResponseCache")}82*83* @see #setDefault(ResponseCache)84* @return the system-wide {@code ResponseCache}85* @since 1.586*/87public static synchronized ResponseCache getDefault() {88@SuppressWarnings("removal")89SecurityManager sm = System.getSecurityManager();90if (sm != null) {91sm.checkPermission(SecurityConstants.GET_RESPONSECACHE_PERMISSION);92}93return theResponseCache;94}9596/**97* Sets (or unsets) the system-wide cache.98*99* Note: non-standard protocol handlers may ignore this setting.100*101* @param responseCache The response cache, or102* {@code null} to unset the cache.103*104* @throws SecurityException105* If a security manager has been installed and it denies106* {@link NetPermission}{@code ("setResponseCache")}107*108* @see #getDefault()109* @since 1.5110*/111public static synchronized void setDefault(ResponseCache responseCache) {112@SuppressWarnings("removal")113SecurityManager sm = System.getSecurityManager();114if (sm != null) {115sm.checkPermission(SecurityConstants.SET_RESPONSECACHE_PERMISSION);116}117theResponseCache = responseCache;118}119120/**121* Retrieve the cached response based on the requesting uri,122* request method and request headers. Typically this method is123* called by the protocol handler before it sends out the request124* to get the network resource. If a cached response is returned,125* that resource is used instead.126*127* @param uri a {@code URI} used to reference the requested128* network resource129* @param rqstMethod a {@code String} representing the request130* method131* @param rqstHeaders - a Map from request header132* field names to lists of field values representing133* the current request headers134* @return a {@code CacheResponse} instance if available135* from cache, or null otherwise136* @throws IOException if an I/O error occurs137* @throws IllegalArgumentException if any one of the arguments is null138*139* @see java.net.URLConnection#setUseCaches(boolean)140* @see java.net.URLConnection#getUseCaches()141* @see java.net.URLConnection#setDefaultUseCaches(boolean)142* @see java.net.URLConnection#getDefaultUseCaches()143*/144public abstract CacheResponse145get(URI uri, String rqstMethod, Map<String, List<String>> rqstHeaders)146throws IOException;147148/**149* The protocol handler calls this method after a resource has150* been retrieved, and the ResponseCache must decide whether or151* not to store the resource in its cache. If the resource is to152* be cached, then put() must return a CacheRequest object which153* contains an OutputStream that the protocol handler will154* use to write the resource into the cache. If the resource is155* not to be cached, then put must return null.156*157* @param uri a {@code URI} used to reference the requested158* network resource159* @param conn - a URLConnection instance that is used to fetch160* the response to be cached161* @return a {@code CacheRequest} for recording the162* response to be cached. Null return indicates that163* the caller does not intend to cache the response.164* @throws IOException if an I/O error occurs165* @throws IllegalArgumentException if any one of the arguments is166* null167*/168public abstract CacheRequest put(URI uri, URLConnection conn) throws IOException;169}170171172