1
0
mirror of https://github.com/owncloud/android-library.git synced 2025-06-07 16:06:08 +00:00

Add cookie restore to methods OwnCloudClientFactory#createOwnCloudClient(Account ...)

This commit is contained in:
masensio 2014-06-10 15:22:16 +02:00
parent 1b3af22b89
commit becf4f3c13
2 changed files with 42 additions and 2 deletions

View File

@ -27,9 +27,12 @@ package com.owncloud.android.lib.common;
import java.io.IOException;
import java.security.GeneralSecurityException;
import org.apache.commons.httpclient.Cookie;
import com.owncloud.android.lib.common.accounts.AccountTypeUtils;
import com.owncloud.android.lib.common.accounts.AccountUtils;
import com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundException;
import com.owncloud.android.lib.common.accounts.AccountUtils.Constants;
import com.owncloud.android.lib.common.network.NetworkUtils;
import android.accounts.Account;
@ -93,6 +96,9 @@ public class OwnCloudClientFactory {
client.setBasicCredentials(username, password);
}
// Restore cookies
restoreCookies(am, account, client);
return client;
}
@ -130,6 +136,9 @@ public class OwnCloudClientFactory {
client.setBasicCredentials(username, password);
}
// Restore cookies
restoreCookies(am, account, client);
return client;
}
@ -159,5 +168,34 @@ public class OwnCloudClientFactory {
return client;
}
/**
* Restore the client cookies
* @param am
* @param account
* @param client
*/
private static void restoreCookies(AccountManager am, Account account, OwnCloudClient client) {
Log.d(TAG, "Restoring cookies for " + account.name);
Uri serverUri = (client.getBaseUri() != null)? client.getBaseUri() : client.getWebdavUri();
String cookiesString = am.getUserData(account, Constants.KEY_COOKIES);
if (cookiesString !=null) {
String[] cookies = cookiesString.split(";");
if (cookies.length > 0) {
for (int i=0; i< cookies.length; i++) {
Cookie cookie = new Cookie();
int equalPos = cookies[i].indexOf('=');
cookie.setName(cookies[i].substring(0, equalPos));
cookie.setValue(cookies[i].substring(equalPos + 1));
cookie.setDomain(serverUri.getHost()); // VERY IMPORTANT
cookie.setPath(serverUri.getPath()); // VERY IMPORTANT
client.getState().addCookie(cookie);
}
}
}
}
}

View File

@ -124,6 +124,7 @@ public class OwnCloudClientMap {
// Get all accounts
Account [] accounts = AccountManager.get(context.getApplicationContext())
.getAccountsByType(accountType);
// Save cookies for all accounts
for(Account account: accounts){
saveClient(account, context.getApplicationContext());
@ -142,4 +143,5 @@ public class OwnCloudClientMap {
Log.d(TAG, " comment: "+ cookie.getComment() );
Log.d(TAG, " secure: "+ cookie.getSecure() );
}
}