jdk/src/share/classes/java/net/InMemoryCookieStore.java
author never
Mon, 12 Jul 2010 22:27:18 -0700
changeset 5926 a36f90d986b6
parent 5506 202f599c92aa
child 10698 b2da906f8080
permissions -rw-r--r--
6968385: malformed xml in sweeper logging Reviewed-by: kvn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3858
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3858
diff changeset
     2
 * Copyright (c) 2005, 2008, Oracle and/or its affiliates. All rights reserved.
3858
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
     4
 *
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3858
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
3858
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3858
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
3858
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
    10
 *
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
    15
 * accompanied this code).
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
    16
 *
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3858
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3858
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3858
diff changeset
    23
 * questions.
3858
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
    24
 */
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
    25
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
    26
package java.net;
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
    27
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
    28
import java.net.URI;
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
    29
import java.net.CookieStore;
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
    30
import java.net.HttpCookie;
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
    31
import java.net.URISyntaxException;
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
    32
import java.util.List;
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
    33
import java.util.Map;
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
    34
import java.util.ArrayList;
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
    35
import java.util.HashMap;
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
    36
import java.util.Collections;
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
    37
import java.util.Iterator;
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
    38
import java.util.concurrent.locks.ReentrantLock;
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
    39
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
    40
/**
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
    41
 * A simple in-memory java.net.CookieStore implementation
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
    42
 *
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
    43
 * @author Edward Wang
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
    44
 * @since 1.6
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
    45
 */
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
    46
class InMemoryCookieStore implements CookieStore {
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
    47
    // the in-memory representation of cookies
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
    48
    private List<HttpCookie> cookieJar = null;
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
    49
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
    50
    // the cookies are indexed by its domain and associated uri (if present)
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
    51
    // CAUTION: when a cookie removed from main data structure (i.e. cookieJar),
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
    52
    //          it won't be cleared in domainIndex & uriIndex. Double-check the
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
    53
    //          presence of cookie when retrieve one form index store.
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
    54
    private Map<String, List<HttpCookie>> domainIndex = null;
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
    55
    private Map<URI, List<HttpCookie>> uriIndex = null;
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
    56
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
    57
    // use ReentrantLock instead of syncronized for scalability
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
    58
    private ReentrantLock lock = null;
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
    59
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
    60
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
    61
    /**
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
    62
     * The default ctor
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
    63
     */
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
    64
    public InMemoryCookieStore() {
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
    65
        cookieJar = new ArrayList<HttpCookie>();
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
    66
        domainIndex = new HashMap<String, List<HttpCookie>>();
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
    67
        uriIndex = new HashMap<URI, List<HttpCookie>>();
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
    68
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
    69
        lock = new ReentrantLock(false);
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
    70
    }
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
    71
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
    72
    /**
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
    73
     * Add one cookie into cookie store.
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
    74
     */
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
    75
    public void add(URI uri, HttpCookie cookie) {
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
    76
        // pre-condition : argument can't be null
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
    77
        if (cookie == null) {
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
    78
            throw new NullPointerException("cookie is null");
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
    79
        }
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
    80
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
    81
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
    82
        lock.lock();
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
    83
        try {
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
    84
            // remove the ole cookie if there has had one
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
    85
            cookieJar.remove(cookie);
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
    86
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
    87
            // add new cookie if it has a non-zero max-age
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
    88
            if (cookie.getMaxAge() != 0) {
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
    89
                cookieJar.add(cookie);
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
    90
                // and add it to domain index
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
    91
                if (cookie.getDomain() != null) {
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
    92
                    addIndex(domainIndex, cookie.getDomain(), cookie);
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
    93
                }
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
    94
                // add it to uri index, too
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
    95
                addIndex(uriIndex, getEffectiveURI(uri), cookie);
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
    96
            }
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
    97
        } finally {
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
    98
            lock.unlock();
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
    99
        }
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   100
    }
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   101
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   102
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   103
    /**
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   104
     * Get all cookies, which:
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   105
     *  1) given uri domain-matches with, or, associated with
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   106
     *     given uri when added to the cookie store.
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   107
     *  3) not expired.
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   108
     * See RFC 2965 sec. 3.3.4 for more detail.
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   109
     */
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   110
    public List<HttpCookie> get(URI uri) {
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   111
        // argument can't be null
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   112
        if (uri == null) {
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   113
            throw new NullPointerException("uri is null");
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   114
        }
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   115
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   116
        List<HttpCookie> cookies = new ArrayList<HttpCookie>();
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   117
        boolean secureLink = "https".equalsIgnoreCase(uri.getScheme());
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   118
        lock.lock();
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   119
        try {
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   120
            // check domainIndex first
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   121
            getInternal1(cookies, domainIndex, uri.getHost(), secureLink);
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   122
            // check uriIndex then
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   123
            getInternal2(cookies, uriIndex, getEffectiveURI(uri), secureLink);
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   124
        } finally {
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   125
            lock.unlock();
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   126
        }
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   127
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   128
        return cookies;
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   129
    }
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   130
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   131
    /**
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   132
     * Get all cookies in cookie store, except those have expired
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   133
     */
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   134
    public List<HttpCookie> getCookies() {
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   135
        List<HttpCookie> rt;
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   136
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   137
        lock.lock();
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   138
        try {
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   139
            Iterator<HttpCookie> it = cookieJar.iterator();
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   140
            while (it.hasNext()) {
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   141
                if (it.next().hasExpired()) {
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   142
                    it.remove();
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   143
                }
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   144
            }
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   145
        } finally {
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   146
            rt = Collections.unmodifiableList(cookieJar);
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   147
            lock.unlock();
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   148
        }
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   149
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   150
        return rt;
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   151
    }
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   152
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   153
    /**
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   154
     * Get all URIs, which are associated with at least one cookie
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   155
     * of this cookie store.
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   156
     */
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   157
    public List<URI> getURIs() {
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   158
        List<URI> uris = new ArrayList<URI>();
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   159
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   160
        lock.lock();
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   161
        try {
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   162
            Iterator<URI> it = uriIndex.keySet().iterator();
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   163
            while (it.hasNext()) {
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   164
                URI uri = it.next();
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   165
                List<HttpCookie> cookies = uriIndex.get(uri);
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   166
                if (cookies == null || cookies.size() == 0) {
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   167
                    // no cookies list or an empty list associated with
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   168
                    // this uri entry, delete it
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   169
                    it.remove();
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   170
                }
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   171
            }
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   172
        } finally {
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   173
            uris.addAll(uriIndex.keySet());
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   174
            lock.unlock();
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   175
        }
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   176
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   177
        return uris;
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   178
    }
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   179
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   180
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   181
    /**
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   182
     * Remove a cookie from store
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   183
     */
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   184
    public boolean remove(URI uri, HttpCookie ck) {
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   185
        // argument can't be null
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   186
        if (ck == null) {
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   187
            throw new NullPointerException("cookie is null");
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   188
        }
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   189
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   190
        boolean modified = false;
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   191
        lock.lock();
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   192
        try {
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   193
            modified = cookieJar.remove(ck);
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   194
        } finally {
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   195
            lock.unlock();
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   196
        }
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   197
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   198
        return modified;
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   199
    }
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   200
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   201
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   202
    /**
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   203
     * Remove all cookies in this cookie store.
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   204
     */
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   205
    public boolean removeAll() {
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   206
        lock.lock();
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   207
        try {
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   208
            cookieJar.clear();
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   209
            domainIndex.clear();
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   210
            uriIndex.clear();
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   211
        } finally {
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   212
            lock.unlock();
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   213
        }
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   214
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   215
        return true;
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   216
    }
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   217
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   218
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   219
    /* ---------------- Private operations -------------- */
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   220
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   221
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   222
    /*
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   223
     * This is almost the same as HttpCookie.domainMatches except for
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   224
     * one difference: It won't reject cookies when the 'H' part of the
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   225
     * domain contains a dot ('.').
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   226
     * I.E.: RFC 2965 section 3.3.2 says that if host is x.y.domain.com
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   227
     * and the cookie domain is .domain.com, then it should be rejected.
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   228
     * However that's not how the real world works. Browsers don't reject and
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   229
     * some sites, like yahoo.com do actually expect these cookies to be
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   230
     * passed along.
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   231
     * And should be used for 'old' style cookies (aka Netscape type of cookies)
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   232
     */
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   233
    private boolean netscapeDomainMatches(String domain, String host)
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   234
    {
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   235
        if (domain == null || host == null) {
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   236
            return false;
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   237
        }
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   238
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   239
        // if there's no embedded dot in domain and domain is not .local
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   240
        boolean isLocalDomain = ".local".equalsIgnoreCase(domain);
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   241
        int embeddedDotInDomain = domain.indexOf('.');
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   242
        if (embeddedDotInDomain == 0) {
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   243
            embeddedDotInDomain = domain.indexOf('.', 1);
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   244
        }
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   245
        if (!isLocalDomain && (embeddedDotInDomain == -1 || embeddedDotInDomain == domain.length() - 1)) {
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   246
            return false;
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   247
        }
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   248
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   249
        // if the host name contains no dot and the domain name is .local
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   250
        int firstDotInHost = host.indexOf('.');
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   251
        if (firstDotInHost == -1 && isLocalDomain) {
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   252
            return true;
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   253
        }
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   254
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   255
        int domainLength = domain.length();
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   256
        int lengthDiff = host.length() - domainLength;
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   257
        if (lengthDiff == 0) {
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   258
            // if the host name and the domain name are just string-compare euqal
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   259
            return host.equalsIgnoreCase(domain);
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   260
        } else if (lengthDiff > 0) {
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   261
            // need to check H & D component
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   262
            String H = host.substring(0, lengthDiff);
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   263
            String D = host.substring(lengthDiff);
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   264
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   265
            return (D.equalsIgnoreCase(domain));
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   266
        } else if (lengthDiff == -1) {
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   267
            // if domain is actually .host
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   268
            return (domain.charAt(0) == '.' &&
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   269
                    host.equalsIgnoreCase(domain.substring(1)));
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   270
        }
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   271
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   272
        return false;
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   273
    }
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   274
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   275
    private void getInternal1(List<HttpCookie> cookies, Map<String, List<HttpCookie>> cookieIndex,
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   276
            String host, boolean secureLink) {
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   277
        // Use a separate list to handle cookies that need to be removed so
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   278
        // that there is no conflict with iterators.
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   279
        ArrayList<HttpCookie> toRemove = new ArrayList<HttpCookie>();
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   280
        for (Map.Entry<String, List<HttpCookie>> entry : cookieIndex.entrySet()) {
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   281
            String domain = entry.getKey();
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   282
            List<HttpCookie> lst = entry.getValue();
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   283
            for (HttpCookie c : lst) {
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   284
                if ((c.getVersion() == 0 && netscapeDomainMatches(domain, host)) ||
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   285
                        (c.getVersion() == 1 && HttpCookie.domainMatches(domain, host))) {
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   286
                    if ((cookieJar.indexOf(c) != -1)) {
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   287
                        // the cookie still in main cookie store
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   288
                        if (!c.hasExpired()) {
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   289
                            // don't add twice and make sure it's the proper
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   290
                            // security level
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   291
                            if ((secureLink || !c.getSecure()) &&
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   292
                                    !cookies.contains(c)) {
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   293
                                cookies.add(c);
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   294
                            }
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   295
                        } else {
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   296
                            toRemove.add(c);
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   297
                        }
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   298
                    } else {
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   299
                        // the cookie has beed removed from main store,
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   300
                        // so also remove it from domain indexed store
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   301
                        toRemove.add(c);
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   302
                    }
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   303
                }
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   304
            }
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   305
            // Clear up the cookies that need to be removed
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   306
            for (HttpCookie c : toRemove) {
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   307
                lst.remove(c);
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   308
                cookieJar.remove(c);
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   309
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   310
            }
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   311
            toRemove.clear();
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   312
        }
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   313
    }
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   314
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   315
    // @param cookies           [OUT] contains the found cookies
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   316
    // @param cookieIndex       the index
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   317
    // @param comparator        the prediction to decide whether or not
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   318
    //                          a cookie in index should be returned
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   319
    private <T> void getInternal2(List<HttpCookie> cookies,
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   320
                                Map<T, List<HttpCookie>> cookieIndex,
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   321
                                Comparable<T> comparator, boolean secureLink)
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   322
    {
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   323
        for (T index : cookieIndex.keySet()) {
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   324
            if (comparator.compareTo(index) == 0) {
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   325
                List<HttpCookie> indexedCookies = cookieIndex.get(index);
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   326
                // check the list of cookies associated with this domain
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   327
                if (indexedCookies != null) {
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   328
                    Iterator<HttpCookie> it = indexedCookies.iterator();
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   329
                    while (it.hasNext()) {
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   330
                        HttpCookie ck = it.next();
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   331
                        if (cookieJar.indexOf(ck) != -1) {
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   332
                            // the cookie still in main cookie store
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   333
                            if (!ck.hasExpired()) {
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   334
                                // don't add twice
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   335
                                if ((secureLink || !ck.getSecure()) &&
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   336
                                        !cookies.contains(ck))
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   337
                                    cookies.add(ck);
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   338
                            } else {
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   339
                                it.remove();
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   340
                                cookieJar.remove(ck);
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   341
                            }
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   342
                        } else {
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   343
                            // the cookie has beed removed from main store,
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   344
                            // so also remove it from domain indexed store
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   345
                            it.remove();
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   346
                        }
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   347
                    }
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   348
                } // end of indexedCookies != null
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   349
            } // end of comparator.compareTo(index) == 0
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   350
        } // end of cookieIndex iteration
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   351
    }
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   352
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   353
    // add 'cookie' indexed by 'index' into 'indexStore'
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   354
    private <T> void addIndex(Map<T, List<HttpCookie>> indexStore,
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   355
                              T index,
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   356
                              HttpCookie cookie)
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   357
    {
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   358
        if (index != null) {
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   359
            List<HttpCookie> cookies = indexStore.get(index);
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   360
            if (cookies != null) {
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   361
                // there may already have the same cookie, so remove it first
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   362
                cookies.remove(cookie);
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   363
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   364
                cookies.add(cookie);
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   365
            } else {
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   366
                cookies = new ArrayList<HttpCookie>();
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   367
                cookies.add(cookie);
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   368
                indexStore.put(index, cookies);
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   369
            }
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   370
        }
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   371
    }
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   372
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   373
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   374
    //
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   375
    // for cookie purpose, the effective uri should only be http://host
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   376
    // the path will be taken into account when path-match algorithm applied
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   377
    //
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   378
    private URI getEffectiveURI(URI uri) {
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   379
        URI effectiveURI = null;
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   380
        try {
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   381
            effectiveURI = new URI("http",
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   382
                                   uri.getHost(),
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   383
                                   null,  // path component
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   384
                                   null,  // query component
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   385
                                   null   // fragment component
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   386
                                  );
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   387
        } catch (URISyntaxException ignored) {
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   388
            effectiveURI = uri;
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   389
        }
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   390
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   391
        return effectiveURI;
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   392
    }
ea9c34fc8590 6882609: Move default InMemoryCookieStore to java.net
chegar
parents:
diff changeset
   393
}