chemistry-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From f...@apache.org
Subject svn commit: r1214286 - /chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-bindings/src/main/java/org/apache/chemistry/opencmis/client/bindings/spi/cookies/CmisCookieStoreImpl.java
Date Wed, 14 Dec 2011 15:04:41 GMT
Author: fmui
Date: Wed Dec 14 15:04:41 2011
New Revision: 1214286

URL: http://svn.apache.org/viewvc?rev=1214286&view=rev
Log:
Limit the number of cookies per session

Modified:
    chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-bindings/src/main/java/org/apache/chemistry/opencmis/client/bindings/spi/cookies/CmisCookieStoreImpl.java

Modified: chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-bindings/src/main/java/org/apache/chemistry/opencmis/client/bindings/spi/cookies/CmisCookieStoreImpl.java
URL: http://svn.apache.org/viewvc/chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-bindings/src/main/java/org/apache/chemistry/opencmis/client/bindings/spi/cookies/CmisCookieStoreImpl.java?rev=1214286&r1=1214285&r2=1214286&view=diff
==============================================================================
--- chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-bindings/src/main/java/org/apache/chemistry/opencmis/client/bindings/spi/cookies/CmisCookieStoreImpl.java
(original)
+++ chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-bindings/src/main/java/org/apache/chemistry/opencmis/client/bindings/spi/cookies/CmisCookieStoreImpl.java
Wed Dec 14 15:04:41 2011
@@ -27,7 +27,7 @@ import java.net.URI;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
-import java.util.HashMap;
+import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
@@ -38,18 +38,38 @@ import java.util.Set;
 class CmisCookieStoreImpl implements Serializable {
     private static final long serialVersionUID = 1L;
 
-    private Map<URI, ArrayList<CmisHttpCookie>> storeMap = new HashMap<URI,
ArrayList<CmisHttpCookie>>();
+    private final Map<URI, ArrayList<CmisHttpCookie>> storeMap;
+
+    public CmisCookieStoreImpl() {
+        this(1000);
+    }
+
+    public CmisCookieStoreImpl(final int maxUrls) {
+        storeMap = new LinkedHashMap<URI, ArrayList<CmisHttpCookie>>(maxUrls
+ 1, 0.70f, true) {
+            private static final long serialVersionUID = 1L;
+
+            @Override
+            public boolean removeEldestEntry(Map.Entry<URI, ArrayList<CmisHttpCookie>>
eldest) {
+                return size() > maxUrls;
+            }
+        };
+    }
 
     public void add(URI uri, CmisHttpCookie cookie) {
         if (uri == null || cookie == null) {
             throw new NullPointerException();
         }
 
-        ArrayList<CmisHttpCookie> cookies;
+        ArrayList<CmisHttpCookie> cookies = null;
         if (storeMap.containsKey(uri)) {
             cookies = storeMap.get(uri);
             cookies.remove(cookie);
             cookies.add(cookie);
+
+            // eliminate expired cookies
+            if (cookies.size() > 1) {
+                cleanCookieList(cookies);
+            }
         } else {
             cookies = new ArrayList<CmisHttpCookie>();
             cookies.add(cookie);
@@ -68,11 +88,7 @@ class CmisCookieStoreImpl implements Ser
             cookies = new ArrayList<CmisHttpCookie>();
         } else {
             // eliminate expired cookies
-            for (CmisHttpCookie cookie : cookies) {
-                if (cookie.hasExpired()) {
-                    cookies.remove(cookie);
-                }
-            }
+            cleanCookieList(cookies);
         }
 
         // get cookies whose domain matches the given URI
@@ -96,6 +112,14 @@ class CmisCookieStoreImpl implements Ser
         return cookies;
     }
 
+    private void cleanCookieList(List<CmisHttpCookie> cookies) {
+        for (CmisHttpCookie cookie : cookies) {
+            if (cookie.hasExpired()) {
+                cookies.remove(cookie);
+            }
+        }
+    }
+
     public List<CmisHttpCookie> getCookies() {
         List<CmisHttpCookie> cookies = new ArrayList<CmisHttpCookie>();
         Collection<ArrayList<CmisHttpCookie>> values = storeMap.values();



Mime
View raw message