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.GuardedBy;
37  import org.apache.http.annotation.ThreadSafe;
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  @ThreadSafe
49  public class BasicCookieStore implements CookieStore, Serializable {
50  
51      private static final long serialVersionUID = -7581093305228232025L;
52  
53      @GuardedBy("this")
54      private final TreeSet<Cookie> cookies;
55  
56      public BasicCookieStore() {
57          super();
58          this.cookies = new TreeSet<Cookie>(new CookieIdentityComparator());
59      }
60  
61      /**
62       * Adds an {@link Cookie HTTP cookie}, replacing any existing equivalent cookies.
63       * If the given cookie has already expired it will not be added, but existing
64       * values will still be removed.
65       *
66       * @param cookie the {@link Cookie cookie} to be added
67       *
68       * @see #addCookies(Cookie[])
69       *
70       */
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     public synchronized List<Cookie> getCookies() {
106         //create defensive copy so it won't be concurrently modified
107         return new ArrayList<Cookie>(cookies);
108     }
109 
110     /**
111      * Removes all of {@link Cookie cookies} in this HTTP state
112      * that have expired by the specified {@link java.util.Date date}.
113      *
114      * @return true if any cookies were purged.
115      *
116      * @see Cookie#isExpired(Date)
117      */
118     public synchronized boolean clearExpired(final Date date) {
119         if (date == null) {
120             return false;
121         }
122         boolean removed = false;
123         for (final Iterator<Cookie> it = cookies.iterator(); it.hasNext();) {
124             if (it.next().isExpired(date)) {
125                 it.remove();
126                 removed = true;
127             }
128         }
129         return removed;
130     }
131 
132     /**
133      * Clears all cookies.
134      */
135     public synchronized void clear() {
136         cookies.clear();
137     }
138 
139     @Override
140     public synchronized String toString() {
141         return cookies.toString();
142     }
143 
144 }