1/* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements.  See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License.  You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package java.net;
18
19import java.io.IOException;
20import java.util.List;
21import java.util.Map;
22
23/**
24 * This class provides a way to manage cookies with a HTTP protocol handler.
25 */
26public abstract class CookieHandler {
27
28    private static CookieHandler systemWideCookieHandler;
29
30    private final static NetPermission getCookieHandlerPermission = new NetPermission(
31            "getCookieHandler"); //$NON-NLS-1$
32
33    private final static NetPermission setCookieHandlerPermission = new NetPermission(
34            "setCookieHandler"); //$NON-NLS-1$
35
36    /**
37     * Returns the system-wide cookie handler or {@code null} if not set.
38     *
39     * @return the system-wide cookie handler.
40     */
41    public static CookieHandler getDefault() {
42        SecurityManager sm = System.getSecurityManager();
43        if (null != sm) {
44            sm.checkPermission(getCookieHandlerPermission);
45        }
46        return systemWideCookieHandler;
47    }
48
49    /**
50     * Sets the system-wide cookie handler.
51     *
52     * @param cHandler
53     *            a cookie handler to set as the system-wide default handler.
54     */
55    public static void setDefault(CookieHandler cHandler) {
56        SecurityManager sm = System.getSecurityManager();
57        if (null != sm) {
58            sm.checkPermission(setCookieHandlerPermission);
59        }
60        systemWideCookieHandler = cHandler;
61    }
62
63    /**
64     * Gets all cookies for a specific URI from the cookie cache.
65     *
66     * @param uri
67     *            a URI to search for applicable cookies.
68     * @param requestHeaders
69     *            a list of request headers.
70     * @return an unchangeable map of all appropriate cookies.
71     * @throws IOException
72     *             if an error occurs during the I/O operation.
73     */
74    public abstract Map<String, List<String>> get(URI uri,
75            Map<String, List<String>> requestHeaders) throws IOException;
76
77    /**
78     * Sets all cookies of a specific URI in the {@code responseHeaders} into
79     * the cookie cache.
80     *
81     * @param uri
82     *            the origin URI of the cookies.
83     * @param responseHeaders
84     *            a list of request headers.
85     * @throws IOException
86     *             if an error occurs during the I/O operation.
87     */
88    public abstract void put(URI uri, Map<String, List<String>> responseHeaders)
89            throws IOException;
90}
91