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