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