Path: blob/master/src/java.base/share/classes/java/net/CookieHandler.java
41152 views
/*1* Copyright (c) 2003, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.net;2627import java.util.Map;28import java.util.List;29import java.io.IOException;30import sun.security.util.SecurityConstants;3132/**33* A CookieHandler object provides a callback mechanism to hook up a34* HTTP state management policy implementation into the HTTP protocol35* handler. The HTTP state management mechanism specifies a way to36* create a stateful session with HTTP requests and responses.37*38* <p> A system-wide CookieHandler to be used by the {@linkplain39* HttpURLConnection HTTP URL stream protocol handler} can be registered by40* doing a CookieHandler.setDefault(CookieHandler). The currently registered41* CookieHandler can be retrieved by calling42* CookieHandler.getDefault().43*44* For more information on HTTP state management, see <a45* href="http://www.ietf.org/rfc/rfc2965.txt"><i>RFC 2965: HTTP46* State Management Mechanism</i></a>47*48* @author Yingxian Wang49* @since 1.550*/51public abstract class CookieHandler {52/**53* Constructor for subclasses to call.54*/55public CookieHandler() {}5657/**58* The system-wide cookie handler that will apply cookies to the59* request headers and manage cookies from the response headers.60*61* @see setDefault(CookieHandler)62* @see getDefault()63*/64private static CookieHandler cookieHandler;6566/**67* Gets the system-wide cookie handler.68*69* @return the system-wide cookie handler; A null return means70* there is no system-wide cookie handler currently set.71* @throws SecurityException72* If a security manager has been installed and it denies73* {@link NetPermission}{@code ("getCookieHandler")}74* @see #setDefault(CookieHandler)75*/76public static synchronized CookieHandler getDefault() {77@SuppressWarnings("removal")78SecurityManager sm = System.getSecurityManager();79if (sm != null) {80sm.checkPermission(SecurityConstants.GET_COOKIEHANDLER_PERMISSION);81}82return cookieHandler;83}8485/**86* Sets (or unsets) the system-wide cookie handler.87*88* Note: non-standard http protocol handlers may ignore this setting.89*90* @param cHandler The HTTP cookie handler, or91* {@code null} to unset.92* @throws SecurityException93* If a security manager has been installed and it denies94* {@link NetPermission}{@code ("setCookieHandler")}95* @see #getDefault()96*/97public static synchronized void setDefault(CookieHandler cHandler) {98@SuppressWarnings("removal")99SecurityManager sm = System.getSecurityManager();100if (sm != null) {101sm.checkPermission(SecurityConstants.SET_COOKIEHANDLER_PERMISSION);102}103cookieHandler = cHandler;104}105106/**107* Gets all the applicable cookies from a cookie cache for the108* specified uri in the request header.109*110* <P>The {@code URI} passed as an argument specifies the intended use for111* the cookies. In particular the scheme should reflect whether the cookies112* will be sent over http, https or used in another context like javascript.113* The host part should reflect either the destination of the cookies or114* their origin in the case of javascript.</P>115* <P>It is up to the implementation to take into account the {@code URI} and116* the cookies attributes and security settings to determine which ones117* should be returned.</P>118*119* <P>HTTP protocol implementers should make sure that this method is120* called after all request headers related to choosing cookies121* are added, and before the request is sent.</P>122*123* @param uri a {@code URI} representing the intended use for the124* cookies125* @param requestHeaders - a Map from request header126* field names to lists of field values representing127* the current request headers128* @return an immutable map from state management headers, with129* field names "Cookie" or "Cookie2" to a list of130* cookies containing state information131*132* @throws IOException if an I/O error occurs133* @throws IllegalArgumentException if either argument is null134* @see #put(URI, Map)135*/136public abstract Map<String, List<String>>137get(URI uri, Map<String, List<String>> requestHeaders)138throws IOException;139140/**141* Sets all the applicable cookies, examples are response header142* fields that are named Set-Cookie2, present in the response143* headers into a cookie cache.144*145* @param uri a {@code URI} where the cookies come from146* @param responseHeaders an immutable map from field names to147* lists of field values representing the response148* header fields returned149* @throws IOException if an I/O error occurs150* @throws IllegalArgumentException if either argument is null151* @see #get(URI, Map)152*/153public abstract void154put(URI uri, Map<String, List<String>> responseHeaders)155throws IOException;156}157158159