This project has retired. For details please refer to its Attic page.
CmisCookieStoreImpl xref

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  /*
20   * This class has been taken from Apache Harmony (http://harmony.apache.org/) 
21   * and has been modified to work with OpenCMIS.
22   */
23  package org.apache.chemistry.opencmis.client.bindings.spi.cookies;
24  
25  import java.io.Serializable;
26  import java.net.URI;
27  import java.util.ArrayList;
28  import java.util.Collection;
29  import java.util.Collections;
30  import java.util.LinkedHashMap;
31  import java.util.List;
32  import java.util.Map;
33  
34  /**
35   * Provides an in-memory cookie store.
36   */
37  class CmisCookieStoreImpl implements Serializable {
38      private static final long serialVersionUID = 1L;
39  
40      private final Map<URI, ArrayList<CmisHttpCookie>> storeMap;
41  
42      public CmisCookieStoreImpl() {
43          this(1000);
44      }
45  
46      public CmisCookieStoreImpl(final int maxUrls) {
47          storeMap = new LinkedHashMap<URI, ArrayList<CmisHttpCookie>>(maxUrls + 1, 0.70f, true) {
48              private static final long serialVersionUID = 1L;
49  
50              @Override
51              public boolean removeEldestEntry(Map.Entry<URI, ArrayList<CmisHttpCookie>> eldest) {
52                  return size() > maxUrls;
53              }
54          };
55      }
56  
57      public void add(URI uri, CmisHttpCookie cookie) {
58          if (uri == null || cookie == null) {
59              throw new NullPointerException();
60          }
61  
62          ArrayList<CmisHttpCookie> cookies = null;
63          if (storeMap.containsKey(uri)) {
64              cookies = storeMap.get(uri);
65              cookies.remove(cookie);
66              cookies.add(cookie);
67  
68              // eliminate expired cookies
69              if (cookies.size() > 1) {
70                  cleanCookieList(cookies);
71              }
72          } else {
73              cookies = new ArrayList<CmisHttpCookie>();
74              cookies.add(cookie);
75              storeMap.put(uri, cookies);
76          }
77      }
78  
79      public List<CmisHttpCookie> get(URI uri) {
80          if (uri == null) {
81              throw new NullPointerException("URI is null!");
82          }
83  
84          // get cookies associated with given URI. If none, returns an empty list
85          List<CmisHttpCookie> cookies = storeMap.get(uri);
86          if (cookies == null) {
87              cookies = new ArrayList<CmisHttpCookie>();
88          } else {
89              // eliminate expired cookies
90              cleanCookieList(cookies);
91          }
92  
93          // get cookies whose domain matches the given URI
94          List<URI> uris = new ArrayList<URI>(storeMap.keySet());
95          for (URI u : uris) {
96              // exclude the given URI
97              if (!u.equals(uri)) {
98                  List<CmisHttpCookie> listCookie = storeMap.get(u);
99                  for (CmisHttpCookie cookie : listCookie) {
100                     if (CmisHttpCookie.domainMatches(cookie.getDomain(), uri.getHost())) {
101                         if (cookie.hasExpired()) {
102                             listCookie.remove(cookie);
103                             if (listCookie.isEmpty()) {
104                                 storeMap.remove(u);
105                             }
106                         } else if (!(cookie.hasExpired() || cookies.contains(cookie))) {
107                             cookies.add(cookie);
108                         }
109                     }
110                 }
111             }
112         }
113 
114         return cookies;
115     }
116 
117     private void cleanCookieList(List<CmisHttpCookie> cookies) {
118         for (CmisHttpCookie cookie : cookies) {
119             if (cookie.hasExpired()) {
120                 cookies.remove(cookie);
121             }
122         }
123     }
124 
125     public List<CmisHttpCookie> getCookies() {
126         List<CmisHttpCookie> cookies = new ArrayList<CmisHttpCookie>();
127         Collection<ArrayList<CmisHttpCookie>> values = storeMap.values();
128         for (ArrayList<CmisHttpCookie> list : values) {
129             for (CmisHttpCookie cookie : list) {
130                 if (cookie.hasExpired()) {
131                     list.remove(cookie); // eliminate expired cookies
132                 } else if (!cookies.contains(cookie)) {
133                     cookies.add(cookie);
134                 }
135             }
136         }
137 
138         return Collections.unmodifiableList(cookies);
139     }
140 
141     public List<URI> getURIs() {
142         return new ArrayList<URI>(storeMap.keySet());
143     }
144 
145     public boolean remove(URI uri, CmisHttpCookie cookie) {
146         if (cookie == null) {
147             throw new NullPointerException("Cookie is null!");
148         }
149 
150         boolean success = false;
151         Collection<ArrayList<CmisHttpCookie>> values = storeMap.values();
152         for (ArrayList<CmisHttpCookie> list : values) {
153             if (list.remove(cookie)) {
154                 success = true;
155             }
156         }
157 
158         return success;
159     }
160 
161     public boolean removeAll() {
162         if (!storeMap.isEmpty()) {
163             storeMap.clear();
164         }
165 
166         return true;
167     }
168 }