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      @Override
72      public synchronized void addCookie(final 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(final Cookie[] cookies) {
93          if (cookies != null) {
94              for (final 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     @Override
107     public synchronized List<Cookie> getCookies() {
108         //create defensive copy so it won't be concurrently modified
109         return new ArrayList<Cookie>(cookies);
110     }
111 
112     /**
113      * Removes all of {@link Cookie cookies} in this HTTP state
114      * that have expired by the specified {@link java.util.Date date}.
115      *
116      * @return true if any cookies were purged.
117      *
118      * @see Cookie#isExpired(Date)
119      */
120     @Override
121     public synchronized boolean clearExpired(final Date date) {
122         if (date == null) {
123             return false;
124         }
125         boolean removed = false;
126         for (final Iterator<Cookie> it = cookies.iterator(); it.hasNext();) {
127             if (it.next().isExpired(date)) {
128                 it.remove();
129                 removed = true;
130             }
131         }
132         return removed;
133     }
134 
135     /**
136      * Clears all cookies.
137      */
138     @Override
139     public synchronized void clear() {
140         cookies.clear();
141     }
142 
143     @Override
144     public synchronized String toString() {
145         return cookies.toString();
146     }
147 
148 }