1 /*
2 * ====================================================================
3 *
4 * Licensed to the Apache Software Foundation (ASF) under one or more
5 * contributor license agreements. See the NOTICE file distributed with
6 * this work for additional information regarding copyright ownership.
7 * The ASF licenses this file to You under the Apache License, Version 2.0
8 * (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ====================================================================
19 *
20 * This software consists of voluntary contributions made by many
21 * individuals on behalf of the Apache Software Foundation. For more
22 * information on the Apache Software Foundation, please see
23 * <http://www.apache.org/>.
24 *
25 */
26
27 package org.apache.http.impl.client;
28
29 import java.io.Serializable;
30 import java.util.ArrayList;
31 import java.util.Date;
32 import java.util.Iterator;
33 import java.util.List;
34 import java.util.TreeSet;
35
36 import org.apache.http.annotation.GuardedBy;
37 import org.apache.http.annotation.ThreadSafe;
38
39 import org.apache.http.client.CookieStore;
40 import org.apache.http.cookie.Cookie;
41 import org.apache.http.cookie.CookieIdentityComparator;
42
43 /**
44 * Default implementation of {@link CookieStore}
45 *
46 *
47 * @since 4.0
48 */
49 @ThreadSafe
50 public class BasicCookieStore implements CookieStore, Serializable {
51
52 private static final long serialVersionUID = -7581093305228232025L;
53
54 @GuardedBy("this")
55 private final TreeSet<Cookie> cookies;
56
57 public BasicCookieStore() {
58 super();
59 this.cookies = new TreeSet<Cookie>(new CookieIdentityComparator());
60 }
61
62 /**
63 * Adds an {@link Cookie HTTP cookie}, replacing any existing equivalent cookies.
64 * If the given cookie has already expired it will not be added, but existing
65 * values will still be removed.
66 *
67 * @param cookie the {@link Cookie cookie} to be added
68 *
69 * @see #addCookies(Cookie[])
70 *
71 */
72 public synchronized void addCookie(Cookie cookie) {
73 if (cookie != null) {
74 // first remove any old cookie that is equivalent
75 cookies.remove(cookie);
76 if (!cookie.isExpired(new Date())) {
77 cookies.add(cookie);
78 }
79 }
80 }
81
82 /**
83 * Adds an array of {@link Cookie HTTP cookies}. Cookies are added individually and
84 * in the given array order. If any of the given cookies has already expired it will
85 * not be added, but existing values will still be removed.
86 *
87 * @param cookies the {@link Cookie cookies} to be added
88 *
89 * @see #addCookie(Cookie)
90 *
91 */
92 public synchronized void addCookies(Cookie[] cookies) {
93 if (cookies != null) {
94 for (Cookie cooky : cookies) {
95 this.addCookie(cooky);
96 }
97 }
98 }
99
100 /**
101 * Returns an immutable array of {@link Cookie cookies} that this HTTP
102 * state currently contains.
103 *
104 * @return an array of {@link Cookie cookies}.
105 */
106 public synchronized List<Cookie> getCookies() {
107 //create defensive copy so it won't be concurrently modified
108 return new ArrayList<Cookie>(cookies);
109 }
110
111 /**
112 * Removes all of {@link Cookie cookies} in this HTTP state
113 * that have expired by the specified {@link java.util.Date date}.
114 *
115 * @return true if any cookies were purged.
116 *
117 * @see Cookie#isExpired(Date)
118 */
119 public synchronized boolean clearExpired(final Date date) {
120 if (date == null) {
121 return false;
122 }
123 boolean removed = false;
124 for (Iterator<Cookie> it = cookies.iterator(); it.hasNext();) {
125 if (it.next().isExpired(date)) {
126 it.remove();
127 removed = true;
128 }
129 }
130 return removed;
131 }
132
133 /**
134 * Clears all cookies.
135 */
136 public synchronized void clear() {
137 cookies.clear();
138 }
139
140 @Override
141 public synchronized String toString() {
142 return cookies.toString();
143 }
144
145 }