View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with 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,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
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.Contract;
37  import org.apache.http.annotation.ThreadingBehavior;
38  import org.apache.http.client.CookieStore;
39  import org.apache.http.cookie.Cookie;
40  import org.apache.http.cookie.CookieIdentityComparator;
41  
42  /**
43   * Default implementation of {@link CookieStore}
44   *
45   *
46   * @since 4.0
47   */
48  @Contract(threading = ThreadingBehavior.SAFE)
49  public class BasicCookieStore implements CookieStore, Serializable {
50  
51      private static final long serialVersionUID = -7581093305228232025L;
52  
53      private final TreeSet<Cookie> cookies;
54  
55      public BasicCookieStore() {
56          super();
57          this.cookies = new TreeSet<Cookie>(new CookieIdentityComparator());
58      }
59  
60      /**
61       * Adds an {@link Cookie HTTP cookie}, replacing any existing equivalent cookies.
62       * If the given cookie has already expired it will not be added, but existing
63       * values will still be removed.
64       *
65       * @param cookie the {@link Cookie cookie} to be added
66       *
67       * @see #addCookies(Cookie[])
68       *
69       */
70      @Override
71      public synchronized void addCookie(final Cookie cookie) {
72          if (cookie != null) {
73              // first remove any old cookie that is equivalent
74              cookies.remove(cookie);
75              if (!cookie.isExpired(new Date())) {
76                  cookies.add(cookie);
77              }
78          }
79      }
80  
81      /**
82       * Adds an array of {@link Cookie HTTP cookies}. Cookies are added individually and
83       * in the given array order. If any of the given cookies has already expired it will
84       * not be added, but existing values will still be removed.
85       *
86       * @param cookies the {@link Cookie cookies} to be added
87       *
88       * @see #addCookie(Cookie)
89       *
90       */
91      public synchronized void addCookies(final Cookie[] cookies) {
92          if (cookies != null) {
93              for (final Cookie cooky : cookies) {
94                  this.addCookie(cooky);
95              }
96          }
97      }
98  
99      /**
100      * Returns an immutable array of {@link Cookie cookies} that this HTTP
101      * state currently contains.
102      *
103      * @return an array of {@link Cookie cookies}.
104      */
105     @Override
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     @Override
120     public synchronized boolean clearExpired(final Date date) {
121         if (date == null) {
122             return false;
123         }
124         boolean removed = false;
125         for (final Iterator<Cookie> it = cookies.iterator(); it.hasNext();) {
126             if (it.next().isExpired(date)) {
127                 it.remove();
128                 removed = true;
129             }
130         }
131         return removed;
132     }
133 
134     /**
135      * Clears all cookies.
136      */
137     @Override
138     public synchronized void clear() {
139         cookies.clear();
140     }
141 
142     @Override
143     public synchronized String toString() {
144         return cookies.toString();
145     }
146 
147 }