jdk/src/java.base/share/classes/java/net/ResponseCache.java
author martin
Tue, 15 Sep 2015 21:56:04 -0700
changeset 32649 2ee9017c7597
parent 25859 3317bb8137f4
permissions -rw-r--r--
8136583: Core libraries should use blessed modifier order Summary: Run blessed-modifier-order script (see bug) Reviewed-by: psandoz, chegar, alanb, plevart
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 5506
diff changeset
     2
 * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.net;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import sun.security.util.SecurityConstants;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * Represents implementations of URLConnection caches. An instance of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * such a class can be registered with the system by doing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * ResponseCache.setDefault(ResponseCache), and the system will call
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * this object in order to:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *    <ul><li>store resource data which has been retrieved from an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *            external source into the cache</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *         <li>try to fetch a requested resource that may have been
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *            stored in the cache</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *    </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * The ResponseCache implementation decides which resources
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * should be cached, and for how long they should be cached. If a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * request resource cannot be retrieved from the cache, then the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * protocol handlers will fetch the resource from its original
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * The settings for URLConnection#useCaches controls whether the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * protocol is allowed to use a cached response.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * For more information on HTTP caching, see <a
708
a780486c413c 6630348: Invalid html tags (extra double quote)
chegar
parents: 2
diff changeset
    55
 * href="http://www.ietf.org/rfc/rfc2616.txt"><i>RFC&nbsp;2616: Hypertext
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * Transfer Protocol -- HTTP/1.1</i></a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * @author Yingxian Wang
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
public abstract class ResponseCache {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * The system wide cache that provides access to a url
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * caching mechanism.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * @see #setDefault(ResponseCache)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * @see #getDefault()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    private static ResponseCache theResponseCache;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * Gets the system-wide response cache.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * @throws  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     *          If a security manager has been installed and it denies
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 5506
diff changeset
    77
     * {@link NetPermission}{@code ("getResponseCache")}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * @see #setDefault(ResponseCache)
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 5506
diff changeset
    80
     * @return the system-wide {@code ResponseCache}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 25859
diff changeset
    83
    public static synchronized ResponseCache getDefault() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            sm.checkPermission(SecurityConstants.GET_RESPONSECACHE_PERMISSION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        return theResponseCache;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * Sets (or unsets) the system-wide cache.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * Note: non-standard procotol handlers may ignore this setting.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * @param responseCache The response cache, or
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 5506
diff changeset
    97
     *          {@code null} to unset the cache.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * @throws  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     *          If a security manager has been installed and it denies
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 5506
diff changeset
   101
     * {@link NetPermission}{@code ("setResponseCache")}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * @see #getDefault()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 25859
diff changeset
   106
    public static synchronized void setDefault(ResponseCache responseCache) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            sm.checkPermission(SecurityConstants.SET_RESPONSECACHE_PERMISSION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        theResponseCache = responseCache;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * Retrieve the cached response based on the requesting uri,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * request method and request headers. Typically this method is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * called by the protocol handler before it sends out the request
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * to get the network resource. If a cached response is returned,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * that resource is used instead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 5506
diff changeset
   121
     * @param uri a {@code URI} used to reference the requested
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     *            network resource
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 5506
diff changeset
   123
     * @param rqstMethod a {@code String} representing the request
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     *            method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * @param rqstHeaders - a Map from request header
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     *            field names to lists of field values representing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     *            the current request headers
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 5506
diff changeset
   128
     * @return a {@code CacheResponse} instance if available
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     *          from cache, or null otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * @throws  IOException if an I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * @throws  IllegalArgumentException if any one of the arguments is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * @see     java.net.URLConnection#setUseCaches(boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * @see     java.net.URLConnection#getUseCaches()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * @see     java.net.URLConnection#setDefaultUseCaches(boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * @see     java.net.URLConnection#getDefaultUseCaches()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    public abstract CacheResponse
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        get(URI uri, String rqstMethod, Map<String, List<String>> rqstHeaders)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * The protocol handler calls this method after a resource has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * been retrieved, and the ResponseCache must decide whether or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * not to store the resource in its cache. If the resource is to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * be cached, then put() must return a CacheRequest object which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * contains an OutputStream that the protocol handler will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * use to write the resource into the cache. If the resource is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * not to be cached, then put must return null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 5506
diff changeset
   151
     * @param uri a {@code URI} used to reference the requested
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     *            network resource
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * @param conn - a URLConnection instance that is used to fetch
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     *            the response to be cached
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 5506
diff changeset
   155
     * @return a {@code CacheRequest} for recording the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     *            response to be cached. Null return indicates that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     *            the caller does not intend to cache the response.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * @throws IOException if an I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * @throws IllegalArgumentException if any one of the arguments is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     *            null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    public abstract CacheRequest put(URI uri, URLConnection conn)  throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
}