You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@chemistry.apache.org by fm...@apache.org on 2013/11/15 21:55:15 UTC

svn commit: r1542396 - in /chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-bindings/src: main/java/org/apache/chemistry/opencmis/client/bindings/spi/cookies/ test/java/org/apache/chemistry/opencmis/client/bindings/misc/

Author: fmui
Date: Fri Nov 15 20:55:15 2013
New Revision: 1542396

URL: http://svn.apache.org/r1542396
Log:
Client: simplified and corrected cookie implementation

Added:
    chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-bindings/src/test/java/org/apache/chemistry/opencmis/client/bindings/misc/CookiesTest.java   (with props)
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=1542396&r1=1542395&r2=1542396&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 Fri Nov 15 20:55:15 2013
@@ -25,12 +25,10 @@ package org.apache.chemistry.opencmis.cl
 import java.io.Serializable;
 import java.net.URI;
 import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Collections;
 import java.util.Iterator;
-import java.util.LinkedHashMap;
+import java.util.LinkedList;
 import java.util.List;
-import java.util.Map;
+import java.util.Locale;
 import java.util.regex.Pattern;
 
 /**
@@ -42,106 +40,81 @@ public class CmisCookieStoreImpl impleme
     private static final String IP_ADDRESS_PATTERN_STR = "^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])$";
     private static final Pattern IP_ADDRESS_PATTERN = Pattern.compile(IP_ADDRESS_PATTERN_STR);
 
-    private final Map<URI, ArrayList<CmisHttpCookie>> storeMap;
+    private final int maxUrls;
+    private final LinkedList<CmisHttpCookie> storeList;
 
     public CmisCookieStoreImpl() {
-        this(1000);
+        this(300);
     }
 
     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;
-            }
-        };
+        this.maxUrls = maxUrls;
+        storeList = new LinkedList<CmisHttpCookie>();
     }
 
-    public void add(URI uri, CmisHttpCookie cookie) {
+    public void add(final URI uri, final CmisHttpCookie cookie) {
         if (uri == null || cookie == null) {
-            throw new NullPointerException();
+            throw new IllegalArgumentException("URI and cookie must be set!");
+        }
+
+        if (cookie.hasExpired()) {
+            storeList.remove(cookie);
+            return;
         }
 
-        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);
+        Iterator<CmisHttpCookie> iter = storeList.iterator();
+        while (iter.hasNext()) {
+            CmisHttpCookie storeCookie = iter.next();
+
+            if (storeCookie.equals(cookie) || storeCookie.hasExpired()) {
+                iter.remove();
             }
-        } else {
-            cookies = new ArrayList<CmisHttpCookie>();
-            cookies.add(cookie);
-            storeMap.put(uri, cookies);
+        }
+
+        storeList.addFirst(cookie);
+
+        if (storeList.size() > maxUrls) {
+            storeList.removeLast();
         }
     }
 
     public List<CmisHttpCookie> get(URI uri) {
         if (uri == null) {
-            throw new NullPointerException("URI is null!");
+            throw new IllegalArgumentException("URI is null!");
         }
 
-        // get cookies associated with given URI. If none, returns an empty list
-        List<CmisHttpCookie> cookies = storeMap.get(uri);
-        if (cookies == null) {
-            cookies = new ArrayList<CmisHttpCookie>();
-        } else {
-            // eliminate expired cookies
-            cleanCookieList(cookies);
-        }
-
-        // get cookies whose domain matches the given URI
-        List<URI> uris = new ArrayList<URI>(storeMap.keySet());
-        for (URI u : uris) {
-            // exclude the given URI
-            if (!u.equals(uri)) {
-                boolean secure = false;
-                String scheme = u.getScheme();
-                if (scheme != null) {
-                    secure = scheme.toLowerCase().startsWith("https");
-                }
+        final String uriHost = uri.getHost().toLowerCase();
 
-                String newHost = uri.getHost().toLowerCase();
+        boolean isSecure = false;
+        String scheme = uri.getScheme();
+        if (scheme != null) {
+            isSecure = scheme.toLowerCase(Locale.ENGLISH).startsWith("https");
+        }
 
-                List<CmisHttpCookie> listCookie = storeMap.get(u);
-                Iterator<CmisHttpCookie> iter = listCookie.iterator();
-                while (iter.hasNext()) {
-                    CmisHttpCookie cookie = iter.next();
-
-                    if (cookie.hasExpired()) {
-                        iter.remove();
-                        if (listCookie.isEmpty()) {
-                            storeMap.remove(u);
+        List<CmisHttpCookie> cookies = new ArrayList<CmisHttpCookie>();
+
+        Iterator<CmisHttpCookie> iter = storeList.iterator();
+        while (iter.hasNext()) {
+            CmisHttpCookie cookie = iter.next();
+
+            if (cookie.hasExpired()) {
+                iter.remove();
+            } else if ((!cookie.getSecure() || isSecure) && cookie.getDomain() != null) {
+                String cookieDomain = cookie.getDomain().toLowerCase();
+
+                if (isIPAddress(uriHost) && uriHost.equals(cookieDomain)) {
+                    cookies.add(cookie);
+                } else {
+                    if (cookie.getVersion() == 0) {
+                        // Netscape, RFC 2109, RFC 6265
+                        if (uriHost.endsWith(cookieDomain)
+                                && (uriHost.length() == cookieDomain.length() || cookieDomain.charAt(0) == '.')) {
+                            cookies.add(cookie);
                         }
-                    } else if (!cookies.contains(cookie) && (!cookie.getSecure() || secure)
-                            && cookie.getDomain() != null) {
-                        String newDomain = cookie.getDomain().toLowerCase();
-
-                        if (isIPAddress(newHost)) {
-                            if (newHost.equals(newDomain)) {
-                                cookies.add(cookie);
-                            }
-                        } else {
-                            if (cookie.getVersion() == 0) {
-                                // Netscape, RFC 2109, RFC 6265
-                                if (newHost.endsWith(newDomain)) {
-                                    if (newHost.length() == newDomain.length()) {
-                                        cookies.add(cookie);
-                                    } else if (newDomain.charAt(0) == '.') {
-                                        cookies.add(cookie);
-                                    }
-                                }
-                            } else if (cookie.getVersion() == 1) {
-                                // RFC 2965
-                                if (CmisHttpCookie.domainMatches(cookie.getDomain(), newHost)) {
-                                    cookies.add(cookie);
-                                }
-                            }
+                    } else if (cookie.getVersion() == 1) {
+                        // RFC 2965
+                        if (CmisHttpCookie.domainMatches(cookieDomain, uriHost)) {
+                            cookies.add(cookie);
                         }
                     }
                 }
@@ -164,60 +137,4 @@ public class CmisCookieStoreImpl impleme
 
         return false;
     }
-
-    private void cleanCookieList(List<CmisHttpCookie> cookies) {
-        Iterator<CmisHttpCookie> iter = cookies.iterator();
-        while (iter.hasNext()) {
-            CmisHttpCookie cookie = iter.next();
-            if (cookie.hasExpired()) {
-                iter.remove();
-            }
-        }
-    }
-
-    public List<CmisHttpCookie> getCookies() {
-        List<CmisHttpCookie> cookies = new ArrayList<CmisHttpCookie>();
-        Collection<ArrayList<CmisHttpCookie>> values = storeMap.values();
-        for (ArrayList<CmisHttpCookie> list : values) {
-            Iterator<CmisHttpCookie> iter = list.iterator();
-            while (iter.hasNext()) {
-                CmisHttpCookie cookie = iter.next();
-                if (cookie.hasExpired()) {
-                    iter.remove(); // eliminate expired cookies
-                } else if (!cookies.contains(cookie)) {
-                    cookies.add(cookie);
-                }
-            }
-        }
-
-        return Collections.unmodifiableList(cookies);
-    }
-
-    public List<URI> getURIs() {
-        return new ArrayList<URI>(storeMap.keySet());
-    }
-
-    public boolean remove(URI uri, CmisHttpCookie cookie) {
-        if (cookie == null) {
-            throw new NullPointerException("Cookie is null!");
-        }
-
-        boolean success = false;
-        Collection<ArrayList<CmisHttpCookie>> values = storeMap.values();
-        for (ArrayList<CmisHttpCookie> list : values) {
-            if (list.remove(cookie)) {
-                success = true;
-            }
-        }
-
-        return success;
-    }
-
-    public boolean removeAll() {
-        if (!storeMap.isEmpty()) {
-            storeMap.clear();
-        }
-
-        return true;
-    }
 }

Added: chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-bindings/src/test/java/org/apache/chemistry/opencmis/client/bindings/misc/CookiesTest.java
URL: http://svn.apache.org/viewvc/chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-bindings/src/test/java/org/apache/chemistry/opencmis/client/bindings/misc/CookiesTest.java?rev=1542396&view=auto
==============================================================================
--- chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-bindings/src/test/java/org/apache/chemistry/opencmis/client/bindings/misc/CookiesTest.java (added)
+++ chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-bindings/src/test/java/org/apache/chemistry/opencmis/client/bindings/misc/CookiesTest.java Fri Nov 15 20:55:15 2013
@@ -0,0 +1,79 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.chemistry.opencmis.client.bindings.misc;
+
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import junit.framework.TestCase;
+
+import org.apache.chemistry.opencmis.client.bindings.spi.cookies.CmisCookieManager;
+
+public class CookiesTest extends TestCase {
+
+    public void testCookies() {
+        CmisCookieManager manager = new CmisCookieManager();
+        String url;
+
+        url = "https://www.example.com/s/test/abc?xyz";
+        addCookie(manager, url, "cookie1", "c1-1111", "/s/");
+
+        url = "https://www.example.com/s/test/abc?xyz";
+        addCookie(manager, url, "cookie2", "c2-1111", "/s/");
+        deleteCookie(manager, url, "cookie2", "/s/");
+
+        url = "https://www.example.com/s/test/abc";
+        addCookie(manager, url, "cookie1", "c1-2222", "/s/");
+
+        url = "https://www.example.com/s/test/abc";
+        addCookie(manager, url, "cookie1", "c1-3333", "/s/t");
+
+        url = "https://www.example.com/s/test/abc?abc";
+        addCookie(manager, url, "cookie1", "c1-4444", "/s/x");
+
+        List<String> cookies = manager.get("https://www.example.com/s/test/abc/s", new HashMap<String, List<String>>())
+                .get("Cookie");
+
+        assertEquals(2, cookies.size());
+        assertEquals(cookies.get(0), "cookie1=c1-3333");
+        assertEquals(cookies.get(1), "cookie1=c1-2222");
+    }
+
+    private void addCookie(CmisCookieManager manager, String url, String name, String value, String path) {
+        Map<String, List<String>> responseHeaders = new HashMap<String, List<String>>();
+
+        responseHeaders.put(null, Collections.singletonList("HTTP/1.1 200 OK"));
+        responseHeaders
+                .put("Set-Cookie", Collections.singletonList(name + "=" + value + "; Path=" + path + "; Secure"));
+
+        manager.put(url, responseHeaders);
+    }
+
+    private void deleteCookie(CmisCookieManager manager, String url, String name, String path) {
+        Map<String, List<String>> responseHeaders = new HashMap<String, List<String>>();
+
+        responseHeaders.put(null, Collections.singletonList("HTTP/1.1 200 OK"));
+        responseHeaders.put("Set-Cookie",
+                Collections.singletonList(name + "=delete; Path=" + path + "; Secure; Max-Age=0"));
+
+        manager.put(url, responseHeaders);
+    }
+}

Propchange: chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-bindings/src/test/java/org/apache/chemistry/opencmis/client/bindings/misc/CookiesTest.java
------------------------------------------------------------------------------
    svn:eol-style = native