1 /* 2 * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/client/BasicCookieStore.java $ 3 * $Revision: 653041 $ 4 * $Date: 2008-05-03 03:39:28 -0700 (Sat, 03 May 2008) $ 5 * 6 * ==================================================================== 7 * 8 * Licensed to the Apache Software Foundation (ASF) under one or more 9 * contributor license agreements. See the NOTICE file distributed with 10 * this work for additional information regarding copyright ownership. 11 * The ASF licenses this file to You under the Apache License, Version 2.0 12 * (the "License"); you may not use this file except in compliance with 13 * the License. You may obtain a copy of the License at 14 * 15 * http://www.apache.org/licenses/LICENSE-2.0 16 * 17 * Unless required by applicable law or agreed to in writing, software 18 * distributed under the License is distributed on an "AS IS" BASIS, 19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 20 * See the License for the specific language governing permissions and 21 * limitations under the License. 22 * ==================================================================== 23 * 24 * This software consists of voluntary contributions made by many 25 * individuals on behalf of the Apache Software Foundation. For more 26 * information on the Apache Software Foundation, please see 27 * <http://www.apache.org/>. 28 * 29 */ 30 31 package org.apache.http.impl.client; 32 33 import java.util.ArrayList; 34 import java.util.Collections; 35 import java.util.Comparator; 36 import java.util.Date; 37 import java.util.Iterator; 38 import java.util.List; 39 40 import org.apache.http.client.CookieStore; 41 import org.apache.http.cookie.Cookie; 42 import org.apache.http.cookie.CookieIdentityComparator; 43 44 /** 45 * Default implementation of {@link CookieStore} 46 * 47 * @author <a href="mailto:remm@apache.org">Remy Maucherat</a> 48 * @author Rodney Waldhoff 49 * @author <a href="mailto:jsdever@apache.org">Jeff Dever</a> 50 * @author Sean C. Sullivan 51 * @author <a href="mailto:becke@u.washington.edu">Michael Becke</a> 52 * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a> 53 * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a> 54 * @author <a href="mailto:adrian@intencha.com">Adrian Sutton</a> 55 * 56 * @since 4.0 57 * 58 * @deprecated Please use {@link java.net.URL#openConnection} instead. 59 * Please visit <a href="http://android-developers.blogspot.com/2011/09/androids-http-clients.html">this webpage</a> 60 * for further details. 61 */ 62 @Deprecated 63 public class BasicCookieStore implements CookieStore { 64 65 private final ArrayList<Cookie> cookies; 66 67 private final Comparator<Cookie> cookieComparator; 68 69 // -------------------------------------------------------- Class Variables 70 71 /** 72 * Default constructor. 73 */ BasicCookieStore()74 public BasicCookieStore() { 75 super(); 76 this.cookies = new ArrayList<Cookie>(); 77 this.cookieComparator = new CookieIdentityComparator(); 78 } 79 80 /** 81 * Adds an {@link Cookie HTTP cookie}, replacing any existing equivalent cookies. 82 * If the given cookie has already expired it will not be added, but existing 83 * values will still be removed. 84 * 85 * @param cookie the {@link Cookie cookie} to be added 86 * 87 * @see #addCookies(Cookie[]) 88 * 89 */ addCookie(Cookie cookie)90 public synchronized void addCookie(Cookie cookie) { 91 if (cookie != null) { 92 // first remove any old cookie that is equivalent 93 for (Iterator<Cookie> it = cookies.iterator(); it.hasNext();) { 94 if (cookieComparator.compare(cookie, it.next()) == 0) { 95 it.remove(); 96 break; 97 } 98 } 99 if (!cookie.isExpired(new Date())) { 100 cookies.add(cookie); 101 } 102 } 103 } 104 105 /** 106 * Adds an array of {@link Cookie HTTP cookies}. Cookies are added individually and 107 * in the given array order. If any of the given cookies has already expired it will 108 * not be added, but existing values will still be removed. 109 * 110 * @param cookies the {@link Cookie cookies} to be added 111 * 112 * @see #addCookie(Cookie) 113 * 114 */ addCookies(Cookie[] cookies)115 public synchronized void addCookies(Cookie[] cookies) { 116 if (cookies != null) { 117 for (Cookie cooky : cookies) { 118 this.addCookie(cooky); 119 } 120 } 121 } 122 123 /** 124 * Returns an immutable array of {@link Cookie cookies} that this HTTP 125 * state currently contains. 126 * 127 * @return an array of {@link Cookie cookies}. 128 */ getCookies()129 public synchronized List<Cookie> getCookies() { 130 return Collections.unmodifiableList(this.cookies); 131 } 132 133 /** 134 * Removes all of {@link Cookie cookies} in this HTTP state 135 * that have expired by the specified {@link java.util.Date date}. 136 * 137 * @return true if any cookies were purged. 138 * 139 * @see Cookie#isExpired(Date) 140 */ clearExpired(final Date date)141 public synchronized boolean clearExpired(final Date date) { 142 if (date == null) { 143 return false; 144 } 145 boolean removed = false; 146 for (Iterator<Cookie> it = cookies.iterator(); it.hasNext();) { 147 if (it.next().isExpired(date)) { 148 it.remove(); 149 removed = true; 150 } 151 } 152 return removed; 153 } 154 155 @Override toString()156 public String toString() { 157 return cookies.toString(); 158 } 159 160 /** 161 * Clears all cookies. 162 */ clear()163 public synchronized void clear() { 164 cookies.clear(); 165 } 166 167 } 168