jdk/src/share/classes/java/net/ResponseCache.java
author chegar
Fri, 18 Sep 2009 16:24:26 +0100
changeset 3858 ea9c34fc8590
parent 715 f16baef3a20e
child 5506 202f599c92aa
permissions -rw-r--r--
6882609: Move default InMemoryCookieStore to java.net Summary: remove static dependency on sun.net.www.protocol.http Reviewed-by: alanb, jccollet
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
715
f16baef3a20e 6719955: Update copyright year
xdono
parents: 708
diff changeset
     2
 * Copyright 2003-2008 Sun Microsystems, Inc.  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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * {@link NetPermission}<tt>("getResponseCache")</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * @see #setDefault(ResponseCache)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * @return the system-wide <code>ResponseCache</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    public synchronized  static ResponseCache getDefault() {
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     *          <code>null</code> to unset the cache.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * {@link NetPermission}<tt>("setResponseCache")</tt>
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
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    public synchronized static void setDefault(ResponseCache responseCache) {
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
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * @param uri a <code>URI</code> used to reference the requested
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     *            network resource
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * @param rqstMethod a <code>String</code> representing the request
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * @return a <code>CacheResponse</code> instance if available
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
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * @param uri a <code>URI</code> used to reference the requested
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * @return a <code>CacheRequest</code> for recording the
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
}