OSDN Git Service

original
[gb-231r1-is01/Gingerbread_2.3.3_r1_IS01.git] / libcore / luni / src / main / java / java / net / ResponseCache.java
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package java.net;
17
18 import java.io.IOException;
19 import java.util.List;
20 import java.util.Map;
21
22 /**
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,
29  * and for how long.
30  */
31 public abstract class ResponseCache {
32     private static ResponseCache defaultResponseCache = null;
33
34     /**
35      * Returns the system's default response cache, or null.
36      */
37     public static ResponseCache getDefault() {
38         SecurityManager sm = System.getSecurityManager();
39         if (sm != null) {
40             sm.checkPermission(new NetPermission("getResponseCache"));
41         }
42         return defaultResponseCache;
43     }
44
45     /**
46      * Sets the system's default response cache. Use null to remove the response cache.
47      */
48     public static void setDefault(ResponseCache responseCache) {
49         SecurityManager sm = System.getSecurityManager();
50         if (sm != null) {
51             sm.checkPermission(new NetPermission("setResponseCache"));
52         }
53         defaultResponseCache = responseCache;
54     }
55
56     /**
57      * Returns the cached response corresponding to the given request.
58      *
59      * @param uri
60      *            the request URI.
61      * @param requestMethod
62      *            the request method.
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.
67      * @throws IOException
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}.
71      */
72     public abstract CacheResponse get(URI uri, String requestMethod,
73             Map<String, List<String>> requestHeaders) throws IOException;
74
75     /**
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}.
80      *
81      * @param uri
82      *            the reference to the requested resource.
83      * @param conn
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.
87      * @throws IOException
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}.
91      */
92     public abstract CacheRequest put(URI uri, URLConnection conn) throws IOException;
93 }