Permalink
Browse files

CookieJar fixes.

  • Loading branch information...
yanchenko committed Oct 28, 2012
1 parent 209d6a1 commit 53e68172ebf0335da9b8dbc45f0d06feb91c3b1a
Showing with 32 additions and 14 deletions.
  1. +32 −14 extra/src/org/droidparts/http/CookieJar.java
@@ -16,13 +16,18 @@
package org.droidparts.http;
import static android.content.Context.MODE_PRIVATE;
+import static java.util.Collections.singletonList;
+import static java.util.Collections.singletonMap;
+import static java.util.Collections.unmodifiableList;
+import static org.droidparts.util.Strings.join;
import java.io.IOException;
import java.net.CookieHandler;
import java.net.URI;
import java.util.ArrayList;
-import java.util.Collections;
+import java.util.Collection;
import java.util.Date;
+import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
@@ -69,13 +74,11 @@ public void setPersistent(boolean persistent) {
public Map<String, List<String>> get(URI uri,
Map<String, List<String>> requestHeaders) throws IOException {
clearExpired(new Date());
- List<String> cookies = new ArrayList<String>();
- for (Cookie cookie : getCookies()) {
- if (isSuitable(uri, cookie)) {
- cookies.add(cookie.getName() + "=" + cookie.getValue());
- }
+ ArrayList<String> cookies = new ArrayList<String>();
+ for (Cookie cookie : getCookies(uri)) {
+ cookies.add(cookie.getName() + "=" + cookie.getValue());
}
- return Collections.singletonMap(SM.COOKIE, cookies);
+ return singletonMap(SM.COOKIE, singletonList(join(cookies, SEP, null)));
}
@Override
@@ -140,7 +143,7 @@ public boolean clearExpired(Date date) {
@Override
public List<Cookie> getCookies() {
L.d("Cookie count: " + cookies.size());
- return Collections.unmodifiableList(cookies);
+ return unmodifiableList(cookies);
}
// Custom
@@ -164,6 +167,27 @@ public boolean clearExpired(Date date) {
return cookies;
}
+ private Collection<Cookie> getCookies(URI uri) {
+ HashMap<String, Cookie> map = new HashMap<String, Cookie>();
+ for (Cookie cookie : getCookies()) {
+ boolean suitable = uri.getHost().equals(cookie.getDomain())
+ && uri.getPath().startsWith(cookie.getPath());
+ if (suitable) {
+ boolean put = true;
+ if (map.containsKey(cookie.getName())) {
+ Cookie otherCookie = map.get(cookie.getName());
+ boolean betterMatchingPath = cookie.getPath().length() > otherCookie
+ .getPath().length();
+ put = betterMatchingPath;
+ }
+ if (put) {
+ map.put(cookie.getName(), cookie);
+ }
+ }
+ }
+ return map.values();
+ }
+
private void persistCookies() {
Editor editor = prefs.edit();
editor.clear();
@@ -216,10 +240,4 @@ private static boolean isEqual(Cookie first, Cookie second) {
return equal;
}
- private static boolean isSuitable(URI uri, Cookie cookie) {
- boolean suitable = uri.getHost().equals(cookie.getDomain())
- && uri.getPath().startsWith(cookie.getPath());
- return suitable;
- }
-
}

0 comments on commit 53e6817

Please sign in to comment.