Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/java/net/ResponseCache.java
38829 views
/*1* Copyright (c) 2003, 2013, 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* The system wide cache that provides access to a url64* caching mechanism.65*66* @see #setDefault(ResponseCache)67* @see #getDefault()68*/69private static ResponseCache theResponseCache;7071/**72* Gets the system-wide response cache.73*74* @throws SecurityException75* If a security manager has been installed and it denies76* {@link NetPermission}{@code ("getResponseCache")}77*78* @see #setDefault(ResponseCache)79* @return the system-wide {@code ResponseCache}80* @since 1.581*/82public synchronized static ResponseCache getDefault() {83SecurityManager sm = System.getSecurityManager();84if (sm != null) {85sm.checkPermission(SecurityConstants.GET_RESPONSECACHE_PERMISSION);86}87return theResponseCache;88}8990/**91* Sets (or unsets) the system-wide cache.92*93* Note: non-standard procotol handlers may ignore this setting.94*95* @param responseCache The response cache, or96* {@code null} to unset the cache.97*98* @throws SecurityException99* If a security manager has been installed and it denies100* {@link NetPermission}{@code ("setResponseCache")}101*102* @see #getDefault()103* @since 1.5104*/105public synchronized static void setDefault(ResponseCache responseCache) {106SecurityManager sm = System.getSecurityManager();107if (sm != null) {108sm.checkPermission(SecurityConstants.SET_RESPONSECACHE_PERMISSION);109}110theResponseCache = responseCache;111}112113/**114* Retrieve the cached response based on the requesting uri,115* request method and request headers. Typically this method is116* called by the protocol handler before it sends out the request117* to get the network resource. If a cached response is returned,118* that resource is used instead.119*120* @param uri a {@code URI} used to reference the requested121* network resource122* @param rqstMethod a {@code String} representing the request123* method124* @param rqstHeaders - a Map from request header125* field names to lists of field values representing126* the current request headers127* @return a {@code CacheResponse} instance if available128* from cache, or null otherwise129* @throws IOException if an I/O error occurs130* @throws IllegalArgumentException if any one of the arguments is null131*132* @see java.net.URLConnection#setUseCaches(boolean)133* @see java.net.URLConnection#getUseCaches()134* @see java.net.URLConnection#setDefaultUseCaches(boolean)135* @see java.net.URLConnection#getDefaultUseCaches()136*/137public abstract CacheResponse138get(URI uri, String rqstMethod, Map<String, List<String>> rqstHeaders)139throws IOException;140141/**142* The protocol handler calls this method after a resource has143* been retrieved, and the ResponseCache must decide whether or144* not to store the resource in its cache. If the resource is to145* be cached, then put() must return a CacheRequest object which146* contains an OutputStream that the protocol handler will147* use to write the resource into the cache. If the resource is148* not to be cached, then put must return null.149*150* @param uri a {@code URI} used to reference the requested151* network resource152* @param conn - a URLConnection instance that is used to fetch153* the response to be cached154* @return a {@code CacheRequest} for recording the155* response to be cached. Null return indicates that156* the caller does not intend to cache the response.157* @throws IOException if an I/O error occurs158* @throws IllegalArgumentException if any one of the arguments is159* null160*/161public abstract CacheRequest put(URI uri, URLConnection conn) throws IOException;162}163164165