diff --git a/ion/src/com/koushikdutta/ion/Ion.java b/ion/src/com/koushikdutta/ion/Ion.java index acfedf3c..7eebaa23 100644 --- a/ion/src/com/koushikdutta/ion/Ion.java +++ b/ion/src/com/koushikdutta/ion/Ion.java @@ -185,7 +185,7 @@ private Ion(Context context, String name) { // TODO: Support pre GB? if (Build.VERSION.SDK_INT >= 9) - addCookieMiddleware(); + setCookieMiddleware(new CookieMiddleware(this)); httpClient.getSocketMiddleware().setConnectAllAddresses(true); httpClient.getSSLSocketMiddleware().setConnectAllAddresses(true); @@ -413,8 +413,11 @@ static class FutureSet extends WeakHashMap { // maintain a list of futures that are in being processed, allow for bulk cancellation WeakHashMap inFlight = new WeakHashMap(); - private void addCookieMiddleware() { - httpClient.insertMiddleware(cookieMiddleware = new CookieMiddleware(this)); + public void setCookieMiddleware(CookieMiddleware middleware) { + if (cookieMiddleware != null) { + httpClient.getMiddleware().remove(cookieMiddleware); + } + httpClient.insertMiddleware(cookieMiddleware = middleware); } /** diff --git a/ion/src/com/koushikdutta/ion/cookie/CookieMiddleware.java b/ion/src/com/koushikdutta/ion/cookie/CookieMiddleware.java index efd39ecd..339212fb 100644 --- a/ion/src/com/koushikdutta/ion/cookie/CookieMiddleware.java +++ b/ion/src/com/koushikdutta/ion/cookie/CookieMiddleware.java @@ -38,13 +38,17 @@ public CookieManager getCookieManager() { return manager; } + protected CookieManager initializeCookieManager(Ion ion) { + return new CookieManager(null, null); + } + Ion ion; public CookieMiddleware(Ion ion) { this.ion = ion; } public void reinit() { - manager = new CookieManager(null, null); + manager = initializeCookieManager(ion); preferences = ion.getContext().getSharedPreferences(ion.getName() + "-cookies", Context.MODE_PRIVATE); Map allPrefs = preferences.getAll(); @@ -89,8 +93,8 @@ public void onRequest(OnRequestData data) { maybeInit(); try { Map> cookies = manager.get( - URI.create( - data.request.getUri().toString()), + URI.create( + data.request.getUri().toString()), data.request.getHeaders().getMultiMap()); addCookies(cookies, data.request.getHeaders()); }