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

Add cookie restoring to SingleSessionManager#getClientFor(Uri ... ) after calling OwnCloudClientFactory#create

This commit is contained in:
masensio 2014-06-13 11:49:11 +02:00
parent b28701ca30
commit 0c4954928b
5 changed files with 92 additions and 42 deletions

View File

@ -27,12 +27,9 @@ 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;
@ -113,7 +110,7 @@ public class OwnCloudClientFactory {
}
// Restore cookies
restoreCookies(am, account, client);
AccountUtils.restoreCookies(account, client, appContext);
return client;
}
@ -179,7 +176,7 @@ public class OwnCloudClientFactory {
}
// Restore cookies
restoreCookies(am, account, client);
AccountUtils.restoreCookies(account, client, appContext);
return client;
}
@ -208,34 +205,5 @@ 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

@ -50,7 +50,7 @@ public interface OwnCloudClientManager {
public OwnCloudClient getClientFor(
Uri serverBaseUri, OwnCloudCredentials credentials, Context context);
public void saveAllClients(Context context, String accountType)
throws AccountNotFoundException, AuthenticatorException,
IOException, OperationCanceledException;

View File

@ -107,6 +107,10 @@ public class SingleSessionManager implements OwnCloudClientManager {
account.getBaseUri(),
context.getApplicationContext(),
true);
// Restore Cookies ??
AccountUtils.restoreCookies(accountName, client, context);
client.setCredentials(account.getCredentials());
if (accountName != null) {
mClientsWithKnownUsername.put(accountName, client);
@ -135,7 +139,12 @@ public class SingleSessionManager implements OwnCloudClientManager {
OwnCloudCredentials credentials =
AccountUtils.getCredentialsForAccount(context, savedAccount);
return getClientFor(serverBaseUri, credentials, context);
OwnCloudClient client = getClientFor(serverBaseUri, credentials, context);
// Restore Cookies ??
AccountUtils.restoreCookies(savedAccount, client, context);
return client;
}
@ -166,8 +175,13 @@ public class SingleSessionManager implements OwnCloudClientManager {
context.getApplicationContext(),
true);
// Restore Cookies
String accountName = AccountUtils.buildAccountName(serverBaseUri, credentials.getUsername());
AccountUtils.restoreCookies(accountName, client, context);
client.setCredentials(credentials);
clientsPerAccount.put(credentials, client);
}
return client;

View File

@ -26,6 +26,9 @@
package com.owncloud.android.lib.common.accounts;
import java.io.IOException;
import org.apache.commons.httpclient.Cookie;
import com.owncloud.android.lib.common.OwnCloudClient;
import com.owncloud.android.lib.common.OwnCloudCredentials;
import com.owncloud.android.lib.common.OwnCloudCredentialsFactory;
@ -37,11 +40,13 @@ import android.accounts.AccountsException;
import android.accounts.AuthenticatorException;
import android.accounts.OperationCanceledException;
import android.content.Context;
import android.util.Log;
import android.net.Uri;
import android.util.Log;
public class AccountUtils {
private static final String TAG = AccountUtils.class.getSimpleName();
public static final String WEBDAV_PATH_1_2 = "/webdav/owncloud.php";
public static final String WEBDAV_PATH_2_0 = "/files/webdav.php";
public static final String WEBDAV_PATH_4_0 = "/remote.php/webdav";
@ -212,13 +217,73 @@ public class AccountUtils {
String cookiesString = client.getCookiesString();
if (cookiesString != "") {
ac.setUserData(savedAccount, Constants.KEY_COOKIES, cookiesString);
Log.d("AccountUtils", "Saving Cookies: "+ cookiesString );
Log.d(TAG, "Saving Cookies: "+ cookiesString );
}
}
}
/**
* Restore the client cookies
* @param account
* @param client
* @param context
*/
public static void restoreCookies(Account account, OwnCloudClient client, Context context) {
Log.d(TAG, "Restoring cookies for " + account.name);
// Account Manager
AccountManager am = AccountManager.get(context.getApplicationContext());
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);
}
}
}
}
/**
* Restore the client cookies from accountName
* @param accountName
* @param client
* @param context
*/
public static void restoreCookies(String accountName, OwnCloudClient client, Context context) {
Log.d(TAG, "Restoring cookies for " + accountName);
// Account Manager
AccountManager am = AccountManager.get(context.getApplicationContext());
// Get account
Account account = null;
Account accounts[] = am.getAccounts();
for (Account a : accounts) {
if (a.name.equals(accountName)) {
account = a;
break;
}
}
// Restoring cookies
if (account != null) {
restoreCookies(account, client, context);
}
}
public static class AccountNotFoundException extends AccountsException {

View File

@ -30,7 +30,6 @@ import com.owncloud.android.lib.common.OwnCloudClient;
import com.owncloud.android.lib.common.OwnCloudClientFactory;
import com.owncloud.android.lib.common.OwnCloudClientManagerFactory;
import com.owncloud.android.lib.common.OwnCloudCredentials;
import com.owncloud.android.lib.common.SingleSessionManager;
import com.owncloud.android.lib.common.accounts.AccountUtils;
import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode;
@ -257,8 +256,7 @@ public abstract class RemoteOperation implements Runnable {
mClient = OwnCloudClientManagerFactory.getDefaultSingleton().
getClientFor(mAccount, mContext);
}
// Save Client Cookies
AccountUtils.saveClient(mClient, mAccount, mContext);
} else {
throw new IllegalStateException("Trying to run a remote operation asynchronously with no client instance or account");
}
@ -303,6 +301,11 @@ public abstract class RemoteOperation implements Runnable {
/** EOF DEPRECATED BLOCK **/
} while (repeat);
if (mAccount != null && mContext != null) {
// Save Client Cookies
AccountUtils.saveClient(mClient, mAccount, mContext);
}
final RemoteOperationResult resultToSend = result;
if (mListenerHandler != null && mListener != null) {
mListenerHandler.post(new Runnable() {