jdk/src/share/classes/java/net/CookieHandler.java
author mchung
Mon, 14 Sep 2009 13:37:26 -0700
changeset 3849 34469964aa98
parent 715 f16baef3a20e
child 4154 afd948aaf965
permissions -rw-r--r--
6878481: Add performance counters in the JDK Summary: Added new performance counters in the JDK to track performance metrics Reviewed-by: alanb, dholmes, iris, forax, andrew
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
715
f16baef3a20e 6719955: Update copyright year
xdono
parents: 708
diff changeset
     2
 * Copyright 2003-2008 Sun Microsystems, Inc.  All Rights Reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.net;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import sun.security.util.SecurityConstants;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * A CookieHandler object provides a callback mechanism to hook up a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * HTTP state management policy implementation into the HTTP protocol
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * handler. The HTTP state management mechanism specifies a way to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * create a stateful session with HTTP requests and responses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * <p>A system-wide CookieHandler that to used by the HTTP protocol
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * handler can be registered by doing a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * CookieHandler.setDefault(CookieHandler). The currently registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * CookieHandler can be retrieved by calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * CookieHandler.getDefault().
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * For more information on HTTP state management, see <a
708
a780486c413c 6630348: Invalid html tags (extra double quote)
chegar
parents: 2
diff changeset
    46
 * href="http://www.ietf.org/rfc/rfc2965.txt"><i>RFC&nbsp;2965: HTTP
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * State Management Mechanism</i></a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * @author Yingxian Wang
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
public abstract class CookieHandler {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * The system-wide cookie handler that will apply cookies to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * request headers and manage cookies from the response headers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * @see setDefault(CookieHandler)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * @see getDefault()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    private static CookieHandler cookieHandler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * Gets the system-wide cookie handler.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * @return the system-wide cookie handler; A null return means
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     *        there is no system-wide cookie handler currently set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * @throws SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     *       If a security manager has been installed and it denies
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * {@link NetPermission}<tt>("getCookieHandler")</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * @see #setDefault(CookieHandler)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    public synchronized static CookieHandler getDefault() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            sm.checkPermission(SecurityConstants.GET_COOKIEHANDLER_PERMISSION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        return cookieHandler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * Sets (or unsets) the system-wide cookie handler.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * Note: non-standard http protocol handlers may ignore this setting.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * @param cHandler The HTTP cookie handler, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     *       <code>null</code> to unset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * @throws SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     *       If a security manager has been installed and it denies
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * {@link NetPermission}<tt>("setCookieHandler")</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * @see #getDefault()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    public synchronized static void setDefault(CookieHandler cHandler) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            sm.checkPermission(SecurityConstants.SET_COOKIEHANDLER_PERMISSION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        cookieHandler = cHandler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * Gets all the applicable cookies from a cookie cache for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * specified uri in the request header.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * HTTP protocol implementers should make sure that this method is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * called after all request headers related to choosing cookies
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * are added, and before the request is sent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * @param uri a <code>URI</code> to send cookies to in a request
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * @param requestHeaders - a Map from request header
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     *            field names to lists of field values representing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     *            the current request headers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * @return an immutable map from state management headers, with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     *            field names "Cookie" or "Cookie2" to a list of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     *            cookies containing state information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * @throws IOException if an I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * @throws IllegalArgumentException if either argument is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * @see #put(URI, Map)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    public abstract Map<String, List<String>>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        get(URI uri, Map<String, List<String>> requestHeaders)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * Sets all the applicable cookies, examples are response header
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * fields that are named Set-Cookie2, present in the response
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * headers into a cookie cache.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * @param uri a <code>URI</code> where the cookies come from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * @param responseHeaders an immutable map from field names to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     *            lists of field values representing the response
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     *            header fields returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * @throws  IOException if an I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * @throws  IllegalArgumentException if either argument is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * @see #get(URI, Map)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    public abstract void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        put(URI uri, Map<String, List<String>> responseHeaders)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
}