/* Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package java.net; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; /** * An in-memory cookie store. */ final class CookieStoreImpl implements CookieStore { /** this map may have null keys! */ private final Map> map = new HashMap>(); public synchronized void add(URI uri, HttpCookie cookie) { if (cookie == null) { throw new NullPointerException("cookie == null"); } uri = cookiesUri(uri); List cookies = map.get(uri); if (cookies == null) { cookies = new ArrayList(); map.put(uri, cookies); } else { cookies.remove(cookie); } cookies.add(cookie); } private URI cookiesUri(URI uri) { if (uri == null) { return null; } try { return new URI("http", uri.getHost(), null, null); } catch (URISyntaxException e) { return uri; // probably a URI with no host } } public synchronized List get(URI uri) { if (uri == null) { throw new NullPointerException("uri == null"); } List result = new ArrayList(); // get cookies associated with given URI. If none, returns an empty list List cookiesForUri = map.get(uri); if (cookiesForUri != null) { for (Iterator i = cookiesForUri.iterator(); i.hasNext(); ) { HttpCookie cookie = i.next(); if (cookie.hasExpired()) { i.remove(); // remove expired cookies } else { result.add(cookie); } } } // get all cookies that domain matches the URI for (Map.Entry> entry : map.entrySet()) { if (uri.equals(entry.getKey())) { continue; // skip the given URI; we've already handled it } List entryCookies = entry.getValue(); for (Iterator i = entryCookies.iterator(); i.hasNext(); ) { HttpCookie cookie = i.next(); if (!HttpCookie.domainMatches(cookie.getDomain(), uri.getHost())) { continue; } if (cookie.hasExpired()) { i.remove(); // remove expired cookies } else if (!result.contains(cookie)) { result.add(cookie); } } } return Collections.unmodifiableList(result); } public synchronized List getCookies() { List result = new ArrayList(); for (List list : map.values()) { for (Iterator i = list.iterator(); i.hasNext(); ) { HttpCookie cookie = i.next(); if (cookie.hasExpired()) { i.remove(); // remove expired cookies } else if (!result.contains(cookie)) { result.add(cookie); } } } return Collections.unmodifiableList(result); } public synchronized List getURIs() { List result = new ArrayList(map.keySet()); result.remove(null); // sigh return Collections.unmodifiableList(result); } public synchronized boolean remove(URI uri, HttpCookie cookie) { if (cookie == null) { throw new NullPointerException("cookie == null"); } List cookies = map.get(cookiesUri(uri)); if (cookies != null) { return cookies.remove(cookie); } else { return false; } } public synchronized boolean removeAll() { boolean result = !map.isEmpty(); map.clear(); return result; } }