Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added possibility to set a custom CookieMiddleware and CookieManager #847

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions ion/src/com/koushikdutta/ion/Ion.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -413,8 +413,11 @@ static class FutureSet extends WeakHashMap<Future, Boolean> {
// maintain a list of futures that are in being processed, allow for bulk cancellation
WeakHashMap<Object, FutureSet> inFlight = new WeakHashMap<Object, FutureSet>();

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);
}

/**
Expand Down
10 changes: 7 additions & 3 deletions ion/src/com/koushikdutta/ion/cookie/CookieMiddleware.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, ?> allPrefs = preferences.getAll();
Expand Down Expand Up @@ -89,8 +93,8 @@ public void onRequest(OnRequestData data) {
maybeInit();
try {
Map<String, List<String>> cookies = manager.get(
URI.create(
data.request.getUri().toString()),
URI.create(
data.request.getUri().toString()),
data.request.getHeaders().getMultiMap());
addCookies(cookies, data.request.getHeaders());
}
Expand Down