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:
parent
b28701ca30
commit
0c4954928b
@ -27,12 +27,9 @@ package com.owncloud.android.lib.common;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.security.GeneralSecurityException;
|
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.AccountTypeUtils;
|
||||||
import com.owncloud.android.lib.common.accounts.AccountUtils;
|
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.AccountNotFoundException;
|
||||||
import com.owncloud.android.lib.common.accounts.AccountUtils.Constants;
|
|
||||||
import com.owncloud.android.lib.common.network.NetworkUtils;
|
import com.owncloud.android.lib.common.network.NetworkUtils;
|
||||||
|
|
||||||
import android.accounts.Account;
|
import android.accounts.Account;
|
||||||
@ -113,7 +110,7 @@ public class OwnCloudClientFactory {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Restore cookies
|
// Restore cookies
|
||||||
restoreCookies(am, account, client);
|
AccountUtils.restoreCookies(account, client, appContext);
|
||||||
|
|
||||||
return client;
|
return client;
|
||||||
}
|
}
|
||||||
@ -179,7 +176,7 @@ public class OwnCloudClientFactory {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Restore cookies
|
// Restore cookies
|
||||||
restoreCookies(am, account, client);
|
AccountUtils.restoreCookies(account, client, appContext);
|
||||||
|
|
||||||
return client;
|
return client;
|
||||||
}
|
}
|
||||||
@ -208,34 +205,5 @@ public class OwnCloudClientFactory {
|
|||||||
return client;
|
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
@ -107,6 +107,10 @@ public class SingleSessionManager implements OwnCloudClientManager {
|
|||||||
account.getBaseUri(),
|
account.getBaseUri(),
|
||||||
context.getApplicationContext(),
|
context.getApplicationContext(),
|
||||||
true);
|
true);
|
||||||
|
|
||||||
|
// Restore Cookies ??
|
||||||
|
AccountUtils.restoreCookies(accountName, client, context);
|
||||||
|
|
||||||
client.setCredentials(account.getCredentials());
|
client.setCredentials(account.getCredentials());
|
||||||
if (accountName != null) {
|
if (accountName != null) {
|
||||||
mClientsWithKnownUsername.put(accountName, client);
|
mClientsWithKnownUsername.put(accountName, client);
|
||||||
@ -135,7 +139,12 @@ public class SingleSessionManager implements OwnCloudClientManager {
|
|||||||
OwnCloudCredentials credentials =
|
OwnCloudCredentials credentials =
|
||||||
AccountUtils.getCredentialsForAccount(context, savedAccount);
|
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(),
|
context.getApplicationContext(),
|
||||||
true);
|
true);
|
||||||
|
|
||||||
|
// Restore Cookies
|
||||||
|
String accountName = AccountUtils.buildAccountName(serverBaseUri, credentials.getUsername());
|
||||||
|
AccountUtils.restoreCookies(accountName, client, context);
|
||||||
|
|
||||||
client.setCredentials(credentials);
|
client.setCredentials(credentials);
|
||||||
clientsPerAccount.put(credentials, client);
|
clientsPerAccount.put(credentials, client);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return client;
|
return client;
|
||||||
|
@ -26,6 +26,9 @@
|
|||||||
package com.owncloud.android.lib.common.accounts;
|
package com.owncloud.android.lib.common.accounts;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import org.apache.commons.httpclient.Cookie;
|
||||||
|
|
||||||
import com.owncloud.android.lib.common.OwnCloudClient;
|
import com.owncloud.android.lib.common.OwnCloudClient;
|
||||||
import com.owncloud.android.lib.common.OwnCloudCredentials;
|
import com.owncloud.android.lib.common.OwnCloudCredentials;
|
||||||
import com.owncloud.android.lib.common.OwnCloudCredentialsFactory;
|
import com.owncloud.android.lib.common.OwnCloudCredentialsFactory;
|
||||||
@ -37,11 +40,13 @@ import android.accounts.AccountsException;
|
|||||||
import android.accounts.AuthenticatorException;
|
import android.accounts.AuthenticatorException;
|
||||||
import android.accounts.OperationCanceledException;
|
import android.accounts.OperationCanceledException;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
|
||||||
import android.util.Log;
|
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
|
import android.util.Log;
|
||||||
|
|
||||||
public class AccountUtils {
|
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_1_2 = "/webdav/owncloud.php";
|
||||||
public static final String WEBDAV_PATH_2_0 = "/files/webdav.php";
|
public static final String WEBDAV_PATH_2_0 = "/files/webdav.php";
|
||||||
public static final String WEBDAV_PATH_4_0 = "/remote.php/webdav";
|
public static final String WEBDAV_PATH_4_0 = "/remote.php/webdav";
|
||||||
@ -212,13 +217,73 @@ public class AccountUtils {
|
|||||||
String cookiesString = client.getCookiesString();
|
String cookiesString = client.getCookiesString();
|
||||||
if (cookiesString != "") {
|
if (cookiesString != "") {
|
||||||
ac.setUserData(savedAccount, Constants.KEY_COOKIES, 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 {
|
public static class AccountNotFoundException extends AccountsException {
|
||||||
|
|
||||||
|
@ -30,7 +30,6 @@ import com.owncloud.android.lib.common.OwnCloudClient;
|
|||||||
import com.owncloud.android.lib.common.OwnCloudClientFactory;
|
import com.owncloud.android.lib.common.OwnCloudClientFactory;
|
||||||
import com.owncloud.android.lib.common.OwnCloudClientManagerFactory;
|
import com.owncloud.android.lib.common.OwnCloudClientManagerFactory;
|
||||||
import com.owncloud.android.lib.common.OwnCloudCredentials;
|
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.accounts.AccountUtils;
|
||||||
import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode;
|
import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode;
|
||||||
|
|
||||||
@ -257,8 +256,7 @@ public abstract class RemoteOperation implements Runnable {
|
|||||||
mClient = OwnCloudClientManagerFactory.getDefaultSingleton().
|
mClient = OwnCloudClientManagerFactory.getDefaultSingleton().
|
||||||
getClientFor(mAccount, mContext);
|
getClientFor(mAccount, mContext);
|
||||||
}
|
}
|
||||||
// Save Client Cookies
|
|
||||||
AccountUtils.saveClient(mClient, mAccount, mContext);
|
|
||||||
} else {
|
} else {
|
||||||
throw new IllegalStateException("Trying to run a remote operation asynchronously with no client instance or account");
|
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 **/
|
/** EOF DEPRECATED BLOCK **/
|
||||||
} while (repeat);
|
} while (repeat);
|
||||||
|
|
||||||
|
if (mAccount != null && mContext != null) {
|
||||||
|
// Save Client Cookies
|
||||||
|
AccountUtils.saveClient(mClient, mAccount, mContext);
|
||||||
|
}
|
||||||
|
|
||||||
final RemoteOperationResult resultToSend = result;
|
final RemoteOperationResult resultToSend = result;
|
||||||
if (mListenerHandler != null && mListener != null) {
|
if (mListenerHandler != null && mListener != null) {
|
||||||
mListenerHandler.post(new Runnable() {
|
mListenerHandler.post(new Runnable() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user