From 0c4954928b038e33adeb73ab1938cac3f70c363d Mon Sep 17 00:00:00 2001 From: masensio Date: Fri, 13 Jun 2014 11:49:11 +0200 Subject: [PATCH] Add cookie restoring to SingleSessionManager#getClientFor(Uri ... ) after calling OwnCloudClientFactory#create --- .../lib/common/OwnCloudClientFactory.java | 36 +--------- .../lib/common/OwnCloudClientManager.java | 2 +- .../lib/common/SingleSessionManager.java | 16 ++++- .../lib/common/accounts/AccountUtils.java | 71 ++++++++++++++++++- .../common/operations/RemoteOperation.java | 9 ++- 5 files changed, 92 insertions(+), 42 deletions(-) diff --git a/src/com/owncloud/android/lib/common/OwnCloudClientFactory.java b/src/com/owncloud/android/lib/common/OwnCloudClientFactory.java index 6784796c..6c1944a5 100644 --- a/src/com/owncloud/android/lib/common/OwnCloudClientFactory.java +++ b/src/com/owncloud/android/lib/common/OwnCloudClientFactory.java @@ -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); - } - } - } - - } } diff --git a/src/com/owncloud/android/lib/common/OwnCloudClientManager.java b/src/com/owncloud/android/lib/common/OwnCloudClientManager.java index b0b26c96..0dc87e4a 100644 --- a/src/com/owncloud/android/lib/common/OwnCloudClientManager.java +++ b/src/com/owncloud/android/lib/common/OwnCloudClientManager.java @@ -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; diff --git a/src/com/owncloud/android/lib/common/SingleSessionManager.java b/src/com/owncloud/android/lib/common/SingleSessionManager.java index e7ba5e57..4a9551b0 100644 --- a/src/com/owncloud/android/lib/common/SingleSessionManager.java +++ b/src/com/owncloud/android/lib/common/SingleSessionManager.java @@ -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; diff --git a/src/com/owncloud/android/lib/common/accounts/AccountUtils.java b/src/com/owncloud/android/lib/common/accounts/AccountUtils.java index f9b86e6c..b70b24e1 100644 --- a/src/com/owncloud/android/lib/common/accounts/AccountUtils.java +++ b/src/com/owncloud/android/lib/common/accounts/AccountUtils.java @@ -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 { diff --git a/src/com/owncloud/android/lib/common/operations/RemoteOperation.java b/src/com/owncloud/android/lib/common/operations/RemoteOperation.java index 1045d6fd..5476f166 100644 --- a/src/com/owncloud/android/lib/common/operations/RemoteOperation.java +++ b/src/com/owncloud/android/lib/common/operations/RemoteOperation.java @@ -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() {