Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.base/share/classes/java/net/CookieHandler.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.util.Map;
29
import java.util.List;
30
import java.io.IOException;
31
import sun.security.util.SecurityConstants;
32
33
/**
34
* A CookieHandler object provides a callback mechanism to hook up a
35
* HTTP state management policy implementation into the HTTP protocol
36
* handler. The HTTP state management mechanism specifies a way to
37
* create a stateful session with HTTP requests and responses.
38
*
39
* <p> A system-wide CookieHandler to be used by the {@linkplain
40
* HttpURLConnection HTTP URL stream protocol handler} can be registered by
41
* doing a CookieHandler.setDefault(CookieHandler). The currently registered
42
* CookieHandler can be retrieved by calling
43
* CookieHandler.getDefault().
44
*
45
* For more information on HTTP state management, see <a
46
* href="http://www.ietf.org/rfc/rfc2965.txt"><i>RFC&nbsp;2965: HTTP
47
* State Management Mechanism</i></a>
48
*
49
* @author Yingxian Wang
50
* @since 1.5
51
*/
52
public abstract class CookieHandler {
53
/**
54
* Constructor for subclasses to call.
55
*/
56
public CookieHandler() {}
57
58
/**
59
* The system-wide cookie handler that will apply cookies to the
60
* request headers and manage cookies from the response headers.
61
*
62
* @see setDefault(CookieHandler)
63
* @see getDefault()
64
*/
65
private static CookieHandler cookieHandler;
66
67
/**
68
* Gets the system-wide cookie handler.
69
*
70
* @return the system-wide cookie handler; A null return means
71
* there is no system-wide cookie handler currently set.
72
* @throws SecurityException
73
* If a security manager has been installed and it denies
74
* {@link NetPermission}{@code ("getCookieHandler")}
75
* @see #setDefault(CookieHandler)
76
*/
77
public static synchronized CookieHandler getDefault() {
78
@SuppressWarnings("removal")
79
SecurityManager sm = System.getSecurityManager();
80
if (sm != null) {
81
sm.checkPermission(SecurityConstants.GET_COOKIEHANDLER_PERMISSION);
82
}
83
return cookieHandler;
84
}
85
86
/**
87
* Sets (or unsets) the system-wide cookie handler.
88
*
89
* Note: non-standard http protocol handlers may ignore this setting.
90
*
91
* @param cHandler The HTTP cookie handler, or
92
* {@code null} to unset.
93
* @throws SecurityException
94
* If a security manager has been installed and it denies
95
* {@link NetPermission}{@code ("setCookieHandler")}
96
* @see #getDefault()
97
*/
98
public static synchronized void setDefault(CookieHandler cHandler) {
99
@SuppressWarnings("removal")
100
SecurityManager sm = System.getSecurityManager();
101
if (sm != null) {
102
sm.checkPermission(SecurityConstants.SET_COOKIEHANDLER_PERMISSION);
103
}
104
cookieHandler = cHandler;
105
}
106
107
/**
108
* Gets all the applicable cookies from a cookie cache for the
109
* specified uri in the request header.
110
*
111
* <P>The {@code URI} passed as an argument specifies the intended use for
112
* the cookies. In particular the scheme should reflect whether the cookies
113
* will be sent over http, https or used in another context like javascript.
114
* The host part should reflect either the destination of the cookies or
115
* their origin in the case of javascript.</P>
116
* <P>It is up to the implementation to take into account the {@code URI} and
117
* the cookies attributes and security settings to determine which ones
118
* should be returned.</P>
119
*
120
* <P>HTTP protocol implementers should make sure that this method is
121
* called after all request headers related to choosing cookies
122
* are added, and before the request is sent.</P>
123
*
124
* @param uri a {@code URI} representing the intended use for the
125
* cookies
126
* @param requestHeaders - a Map from request header
127
* field names to lists of field values representing
128
* the current request headers
129
* @return an immutable map from state management headers, with
130
* field names "Cookie" or "Cookie2" to a list of
131
* cookies containing state information
132
*
133
* @throws IOException if an I/O error occurs
134
* @throws IllegalArgumentException if either argument is null
135
* @see #put(URI, Map)
136
*/
137
public abstract Map<String, List<String>>
138
get(URI uri, Map<String, List<String>> requestHeaders)
139
throws IOException;
140
141
/**
142
* Sets all the applicable cookies, examples are response header
143
* fields that are named Set-Cookie2, present in the response
144
* headers into a cookie cache.
145
*
146
* @param uri a {@code URI} where the cookies come from
147
* @param responseHeaders an immutable map from field names to
148
* lists of field values representing the response
149
* header fields returned
150
* @throws IOException if an I/O error occurs
151
* @throws IllegalArgumentException if either argument is null
152
* @see #get(URI, Map)
153
*/
154
public abstract void
155
put(URI uri, Map<String, List<String>> responseHeaders)
156
throws IOException;
157
}
158
159