0N/A/*
2362N/A * Copyright (c) 2003, 2008, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage java.net;
0N/A
0N/Aimport java.util.Map;
0N/Aimport java.util.List;
0N/Aimport java.io.IOException;
0N/Aimport sun.security.util.SecurityConstants;
0N/A
0N/A/**
0N/A * A CookieHandler object provides a callback mechanism to hook up a
0N/A * HTTP state management policy implementation into the HTTP protocol
0N/A * handler. The HTTP state management mechanism specifies a way to
0N/A * create a stateful session with HTTP requests and responses.
0N/A *
0N/A * <p>A system-wide CookieHandler that to used by the HTTP protocol
0N/A * handler can be registered by doing a
0N/A * CookieHandler.setDefault(CookieHandler). The currently registered
0N/A * CookieHandler can be retrieved by calling
0N/A * CookieHandler.getDefault().
0N/A *
0N/A * For more information on HTTP state management, see <a
374N/A * href="http://www.ietf.org/rfc/rfc2965.txt"><i>RFC&nbsp;2965: HTTP
0N/A * State Management Mechanism</i></a>
0N/A *
0N/A * @author Yingxian Wang
0N/A * @since 1.5
0N/A */
0N/Apublic abstract class CookieHandler {
0N/A /**
0N/A * The system-wide cookie handler that will apply cookies to the
0N/A * request headers and manage cookies from the response headers.
0N/A *
0N/A * @see setDefault(CookieHandler)
0N/A * @see getDefault()
0N/A */
0N/A private static CookieHandler cookieHandler;
0N/A
0N/A /**
0N/A * Gets the system-wide cookie handler.
0N/A *
0N/A * @return the system-wide cookie handler; A null return means
0N/A * there is no system-wide cookie handler currently set.
0N/A * @throws SecurityException
0N/A * If a security manager has been installed and it denies
0N/A * {@link NetPermission}<tt>("getCookieHandler")</tt>
0N/A * @see #setDefault(CookieHandler)
0N/A */
0N/A public synchronized static CookieHandler getDefault() {
0N/A SecurityManager sm = System.getSecurityManager();
0N/A if (sm != null) {
0N/A sm.checkPermission(SecurityConstants.GET_COOKIEHANDLER_PERMISSION);
0N/A }
0N/A return cookieHandler;
0N/A }
0N/A
0N/A /**
0N/A * Sets (or unsets) the system-wide cookie handler.
0N/A *
0N/A * Note: non-standard http protocol handlers may ignore this setting.
0N/A *
0N/A * @param cHandler The HTTP cookie handler, or
0N/A * <code>null</code> to unset.
0N/A * @throws SecurityException
0N/A * If a security manager has been installed and it denies
0N/A * {@link NetPermission}<tt>("setCookieHandler")</tt>
0N/A * @see #getDefault()
0N/A */
0N/A public synchronized static void setDefault(CookieHandler cHandler) {
0N/A SecurityManager sm = System.getSecurityManager();
0N/A if (sm != null) {
0N/A sm.checkPermission(SecurityConstants.SET_COOKIEHANDLER_PERMISSION);
0N/A }
0N/A cookieHandler = cHandler;
0N/A }
0N/A
0N/A /**
0N/A * Gets all the applicable cookies from a cookie cache for the
0N/A * specified uri in the request header.
0N/A *
1788N/A * <P>The {@code URI} passed as an argument specifies the intended use for
1788N/A * the cookies. In particular the scheme should reflect whether the cookies
1788N/A * will be sent over http, https or used in another context like javascript.
1788N/A * The host part should reflect either the destination of the cookies or
1788N/A * their origin in the case of javascript.</P>
1788N/A * <P>It is up to the implementation to take into account the {@code URI} and
1788N/A * the cookies attributes and security settings to determine which ones
1788N/A * should be returned.</P>
1788N/A *
1788N/A * <P>HTTP protocol implementers should make sure that this method is
0N/A * called after all request headers related to choosing cookies
1788N/A * are added, and before the request is sent.</P>
0N/A *
1788N/A * @param uri a <code>URI</code> representing the intended use for the
1788N/A * cookies
0N/A * @param requestHeaders - a Map from request header
0N/A * field names to lists of field values representing
0N/A * the current request headers
0N/A * @return an immutable map from state management headers, with
0N/A * field names "Cookie" or "Cookie2" to a list of
0N/A * cookies containing state information
0N/A *
0N/A * @throws IOException if an I/O error occurs
0N/A * @throws IllegalArgumentException if either argument is null
0N/A * @see #put(URI, Map)
0N/A */
0N/A public abstract Map<String, List<String>>
0N/A get(URI uri, Map<String, List<String>> requestHeaders)
0N/A throws IOException;
0N/A
0N/A /**
0N/A * Sets all the applicable cookies, examples are response header
0N/A * fields that are named Set-Cookie2, present in the response
0N/A * headers into a cookie cache.
0N/A *
0N/A * @param uri a <code>URI</code> where the cookies come from
0N/A * @param responseHeaders an immutable map from field names to
0N/A * lists of field values representing the response
0N/A * header fields returned
0N/A * @throws IOException if an I/O error occurs
0N/A * @throws IllegalArgumentException if either argument is null
0N/A * @see #get(URI, Map)
0N/A */
0N/A public abstract void
0N/A put(URI uri, Map<String, List<String>> responseHeaders)
0N/A throws IOException;
0N/A}