Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/java/net/CookieHandler.java
38829 views
/*1* Copyright (c) 2003, 2013, 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 that to used by the HTTP protocol39* handler can be registered by doing a40* 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* The system-wide cookie handler that will apply cookies to the54* request headers and manage cookies from the response headers.55*56* @see setDefault(CookieHandler)57* @see getDefault()58*/59private static CookieHandler cookieHandler;6061/**62* Gets the system-wide cookie handler.63*64* @return the system-wide cookie handler; A null return means65* there is no system-wide cookie handler currently set.66* @throws SecurityException67* If a security manager has been installed and it denies68* {@link NetPermission}{@code ("getCookieHandler")}69* @see #setDefault(CookieHandler)70*/71public synchronized static CookieHandler getDefault() {72SecurityManager sm = System.getSecurityManager();73if (sm != null) {74sm.checkPermission(SecurityConstants.GET_COOKIEHANDLER_PERMISSION);75}76return cookieHandler;77}7879/**80* Sets (or unsets) the system-wide cookie handler.81*82* Note: non-standard http protocol handlers may ignore this setting.83*84* @param cHandler The HTTP cookie handler, or85* {@code null} to unset.86* @throws SecurityException87* If a security manager has been installed and it denies88* {@link NetPermission}{@code ("setCookieHandler")}89* @see #getDefault()90*/91public synchronized static void setDefault(CookieHandler cHandler) {92SecurityManager sm = System.getSecurityManager();93if (sm != null) {94sm.checkPermission(SecurityConstants.SET_COOKIEHANDLER_PERMISSION);95}96cookieHandler = cHandler;97}9899/**100* Gets all the applicable cookies from a cookie cache for the101* specified uri in the request header.102*103* <P>The {@code URI} passed as an argument specifies the intended use for104* the cookies. In particular the scheme should reflect whether the cookies105* will be sent over http, https or used in another context like javascript.106* The host part should reflect either the destination of the cookies or107* their origin in the case of javascript.</P>108* <P>It is up to the implementation to take into account the {@code URI} and109* the cookies attributes and security settings to determine which ones110* should be returned.</P>111*112* <P>HTTP protocol implementers should make sure that this method is113* called after all request headers related to choosing cookies114* are added, and before the request is sent.</P>115*116* @param uri a {@code URI} representing the intended use for the117* cookies118* @param requestHeaders - a Map from request header119* field names to lists of field values representing120* the current request headers121* @return an immutable map from state management headers, with122* field names "Cookie" or "Cookie2" to a list of123* cookies containing state information124*125* @throws IOException if an I/O error occurs126* @throws IllegalArgumentException if either argument is null127* @see #put(URI, Map)128*/129public abstract Map<String, List<String>>130get(URI uri, Map<String, List<String>> requestHeaders)131throws IOException;132133/**134* Sets all the applicable cookies, examples are response header135* fields that are named Set-Cookie2, present in the response136* headers into a cookie cache.137*138* @param uri a {@code URI} where the cookies come from139* @param responseHeaders an immutable map from field names to140* lists of field values representing the response141* header fields returned142* @throws IOException if an I/O error occurs143* @throws IllegalArgumentException if either argument is null144* @see #get(URI, Map)145*/146public abstract void147put(URI uri, Map<String, List<String>> responseHeaders)148throws IOException;149}150151152