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