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

Merge pull request #52 from owncloud/update_OC_client_manager

Update oc client manager
This commit is contained in:
masensio 2015-02-24 14:05:47 +01:00
commit 942971a202
7 changed files with 142 additions and 61 deletions

View File

@ -42,7 +42,52 @@ public class OwnCloudAccount {
private String mSavedAccountName; private String mSavedAccountName;
private Account mSavedAccount;
/**
* Constructor for already saved OC accounts.
*
* Do not use for anonymous credentials.
*/
public OwnCloudAccount(Account savedAccount, Context context) throws AccountNotFoundException {
if (savedAccount == null) {
throw new IllegalArgumentException("Parameter 'savedAccount' cannot be null");
}
if (context == null) {
throw new IllegalArgumentException("Parameter 'context' cannot be null");
}
mSavedAccount = savedAccount;
mSavedAccountName = savedAccount.name;
mBaseUri = Uri.parse(AccountUtils.getBaseUrlForAccount(context, mSavedAccount));
mCredentials = null;
}
/**
* Method for deferred load of account attributes from AccountManager
*
* @param context
* @throws AccountNotFoundException
* @throws AuthenticatorException
* @throws IOException
* @throws OperationCanceledException
*/
public void loadCredentials(Context context)
throws AccountNotFoundException, AuthenticatorException,
IOException, OperationCanceledException {
if (context == null) {
throw new IllegalArgumentException("Parameter 'context' cannot be null");
}
if (mSavedAccount != null) {
mCredentials = AccountUtils.getCredentialsForAccount(context, mSavedAccount);
}
}
/*
public OwnCloudAccount(Account savedAccount, Context context) public OwnCloudAccount(Account savedAccount, Context context)
throws AccountNotFoundException, AuthenticatorException, throws AccountNotFoundException, AuthenticatorException,
IOException, OperationCanceledException { IOException, OperationCanceledException {
@ -61,12 +106,19 @@ public class OwnCloudAccount {
mCredentials = OwnCloudCredentialsFactory.getAnonymousCredentials(); mCredentials = OwnCloudCredentialsFactory.getAnonymousCredentials();
} }
} }
*/
/**
* Constructor for non yet saved OC accounts.
*
* @param baseUri URI to the OC server to get access to.
* @param credentials Credentials to authenticate in the server. NULL is valid for anonymous credentials.
*/
public OwnCloudAccount(Uri baseUri, OwnCloudCredentials credentials) { public OwnCloudAccount(Uri baseUri, OwnCloudCredentials credentials) {
if (baseUri == null) { if (baseUri == null) {
throw new IllegalArgumentException("Parameter 'baseUri' cannot be null"); throw new IllegalArgumentException("Parameter 'baseUri' cannot be null");
} }
mSavedAccount = null;
mSavedAccountName = null; mSavedAccountName = null;
mBaseUri = baseUri; mBaseUri = baseUri;
mCredentials = credentials != null ? mCredentials = credentials != null ?
@ -80,7 +132,7 @@ public class OwnCloudAccount {
public boolean isAnonymous() { public boolean isAnonymous() {
return (mCredentials == null); return (mCredentials == null);
} } // TODO no more
public Uri getBaseUri() { public Uri getBaseUri() {
return mBaseUri; return mBaseUri;

View File

@ -42,7 +42,8 @@ import com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundExce
public interface OwnCloudClientManager { public interface OwnCloudClientManager {
public OwnCloudClient getClientFor(OwnCloudAccount account, Context context); public OwnCloudClient getClientFor(OwnCloudAccount account, Context context)
throws AccountNotFoundException, OperationCanceledException, AuthenticatorException, IOException;
public OwnCloudClient removeClientFor(OwnCloudAccount account); public OwnCloudClient removeClientFor(OwnCloudAccount account);

View File

@ -25,31 +25,41 @@
package com.owncloud.android.lib.common; package com.owncloud.android.lib.common;
import android.accounts.AuthenticatorException;
import android.accounts.OperationCanceledException;
import android.content.Context; import android.content.Context;
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.utils.Log_OC; import com.owncloud.android.lib.common.utils.Log_OC;
import java.io.IOException;
public class SimpleFactoryManager implements OwnCloudClientManager { public class SimpleFactoryManager implements OwnCloudClientManager {
private static final String TAG = SimpleFactoryManager.class.getSimpleName(); private static final String TAG = SimpleFactoryManager.class.getSimpleName();
@Override @Override
public OwnCloudClient getClientFor(OwnCloudAccount account, Context context) { public OwnCloudClient getClientFor(OwnCloudAccount account, Context context)
throws AccountNotFoundException, OperationCanceledException, AuthenticatorException, IOException {
Log_OC.d(TAG, "getClientFor(OwnCloudAccount ... : "); Log_OC.d(TAG, "getClientFor(OwnCloudAccount ... : ");
OwnCloudClient client = OwnCloudClientFactory.createOwnCloudClient( OwnCloudClient client = OwnCloudClientFactory.createOwnCloudClient(
account.getBaseUri(), account.getBaseUri(),
context.getApplicationContext(), context.getApplicationContext(),
true); true);
Log_OC.d(TAG, " new client {" + Log_OC.v(TAG, " new client {" +
(account.getName() != null ? (account.getName() != null ?
account.getName() : account.getName() :
AccountUtils.buildAccountName( AccountUtils.buildAccountName(account.getBaseUri(), "")
account.getBaseUri(),
account.getCredentials().getAuthToken())) +
", " + client.hashCode() + "}");
) + ", " + client.hashCode() + "}");
if (account.getCredentials() == null) {
account.loadCredentials(context);
}
client.setCredentials(account.getCredentials()); client.setCredentials(account.getCredentials());
return client; return client;
} }

View File

@ -62,7 +62,9 @@ public class SingleSessionManager implements OwnCloudClientManager {
@Override @Override
public synchronized OwnCloudClient getClientFor(OwnCloudAccount account, Context context) { public synchronized OwnCloudClient getClientFor(OwnCloudAccount account, Context context)
throws AccountNotFoundException, OperationCanceledException, AuthenticatorException, IOException {
Log_OC.d(TAG, "getClientFor(OwnCloudAccount ... : "); Log_OC.d(TAG, "getClientFor(OwnCloudAccount ... : ");
if (account == null) { if (account == null) {
throw new IllegalArgumentException("Cannot get an OwnCloudClient for a null account"); throw new IllegalArgumentException("Cannot get an OwnCloudClient for a null account");
@ -70,9 +72,12 @@ public class SingleSessionManager implements OwnCloudClientManager {
OwnCloudClient client = null; OwnCloudClient client = null;
String accountName = account.getName(); String accountName = account.getName();
String sessionName = AccountUtils.buildAccountName( String sessionName = account.getCredentials() == null ? "" :
AccountUtils.buildAccountName (
account.getBaseUri(), account.getBaseUri(),
account.getCredentials().getAuthToken()); account.getCredentials().getAuthToken()
)
;
if (accountName != null) { if (accountName != null) {
client = mClientsWithKnownUsername.get(accountName); client = mClientsWithKnownUsername.get(accountName);
@ -82,6 +87,7 @@ public class SingleSessionManager implements OwnCloudClientManager {
if (accountName != null) { if (accountName != null) {
client = mClientsWithUnknownUsername.remove(sessionName); client = mClientsWithUnknownUsername.remove(sessionName);
if (client != null) { if (client != null) {
// TODO REMOVE THIS LOG
Log_OC.d(TAG, " reusing client {" + sessionName + ", " + Log_OC.d(TAG, " reusing client {" + sessionName + ", " +
client.hashCode() + "}"); client.hashCode() + "}");
mClientsWithKnownUsername.put(accountName, client); mClientsWithKnownUsername.put(accountName, client);
@ -105,10 +111,9 @@ public class SingleSessionManager implements OwnCloudClientManager {
client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY); client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
// enable cookie tracking // enable cookie tracking
// Restore Cookies ??
AccountUtils.restoreCookies(accountName, client, context); AccountUtils.restoreCookies(accountName, client, context);
account.loadCredentials(context);
client.setCredentials(account.getCredentials()); client.setCredentials(account.getCredentials());
if (accountName != null) { if (accountName != null) {
mClientsWithKnownUsername.put(accountName, client); mClientsWithKnownUsername.put(accountName, client);
@ -116,10 +121,12 @@ public class SingleSessionManager implements OwnCloudClientManager {
} else { } else {
mClientsWithUnknownUsername.put(sessionName, client); mClientsWithUnknownUsername.put(sessionName, client);
// TODO REMOVE THIS LOG
Log_OC.d(TAG, " new client {" + sessionName + ", " + client.hashCode() + "}"); Log_OC.d(TAG, " new client {" + sessionName + ", " + client.hashCode() + "}");
} }
} else { } else {
if (!reusingKnown) { if (!reusingKnown) {
// TODO REMOVE THIS LOG
Log_OC.d(TAG, " reusing client {" + sessionName + ", " + client.hashCode() + "}"); Log_OC.d(TAG, " reusing client {" + sessionName + ", " + client.hashCode() + "}");
} }
keepCredentialsUpdated(account, client); keepCredentialsUpdated(account, client);
@ -149,17 +156,8 @@ public class SingleSessionManager implements OwnCloudClientManager {
} }
} }
String sessionName = AccountUtils.buildAccountName( mClientsWithUnknownUsername.clear();
account.getBaseUri(),
account.getCredentials().getAuthToken());
client = mClientsWithUnknownUsername.remove(sessionName);
if (client != null) {
Log_OC.d(TAG, "Removed client {" + sessionName + ", " + client.hashCode() + "}");
return client;
}
Log_OC.d(TAG, "No client tracked for {" + sessionName + "}");
Log_OC.d(TAG, "No client removed");
return null; return null;
} }
@ -186,7 +184,7 @@ public class SingleSessionManager implements OwnCloudClientManager {
private void keepCredentialsUpdated(OwnCloudAccount account, OwnCloudClient reusedClient) { private void keepCredentialsUpdated(OwnCloudAccount account, OwnCloudClient reusedClient) {
OwnCloudCredentials recentCredentials = account.getCredentials(); OwnCloudCredentials recentCredentials = account.getCredentials();
if (!recentCredentials.getAuthToken().equals( if (recentCredentials != null && !recentCredentials.getAuthToken().equals(
reusedClient.getCredentials().getAuthToken())) { reusedClient.getCredentials().getAuthToken())) {
reusedClient.setCredentials(recentCredentials); reusedClient.setCredentials(recentCredentials);
} }

View File

@ -92,6 +92,7 @@ public class AdvancedSslSocketFactory implements SecureProtocolSocketFactory {
/** /**
* @see ProtocolSocketFactory#createSocket(java.lang.String,int,java.net.InetAddress,int) * @see ProtocolSocketFactory#createSocket(java.lang.String,int,java.net.InetAddress,int)
*/ */
@Override
public Socket createSocket(String host, int port, InetAddress clientHost, int clientPort) public Socket createSocket(String host, int port, InetAddress clientHost, int clientPort)
throws IOException, UnknownHostException { throws IOException, UnknownHostException {
@ -157,6 +158,7 @@ public class AdvancedSslSocketFactory implements SecureProtocolSocketFactory {
* @throws UnknownHostException if the IP address of the host cannot be * @throws UnknownHostException if the IP address of the host cannot be
* determined * determined
*/ */
@Override
public Socket createSocket(final String host, final int port, public Socket createSocket(final String host, final int port,
final InetAddress localAddress, final int localPort, final InetAddress localAddress, final int localPort,
final HttpConnectionParams params) throws IOException, final HttpConnectionParams params) throws IOException,
@ -187,6 +189,7 @@ public class AdvancedSslSocketFactory implements SecureProtocolSocketFactory {
/** /**
* @see ProtocolSocketFactory#createSocket(java.lang.String,int) * @see ProtocolSocketFactory#createSocket(java.lang.String,int)
*/ */
@Override
public Socket createSocket(String host, int port) throws IOException, public Socket createSocket(String host, int port) throws IOException,
UnknownHostException { UnknownHostException {
Log_OC.d(TAG, "Creating SSL Socket with remote " + host + ":" + port); Log_OC.d(TAG, "Creating SSL Socket with remote " + host + ":" + port);

View File

@ -94,6 +94,7 @@ public class SimpleFactoryManagerTest extends AndroidTestCase {
} }
public void testGetClientFor() { public void testGetClientFor() {
try {
OwnCloudClient client = mSFMgr.getClientFor(mValidAccount, getContext()); OwnCloudClient client = mSFMgr.getClientFor(mValidAccount, getContext());
assertNotSame("Got same client instances for same account", assertNotSame("Got same client instances for same account",
@ -102,15 +103,22 @@ public class SimpleFactoryManagerTest extends AndroidTestCase {
assertNotSame("Got same client instances for different accounts", assertNotSame("Got same client instances for different accounts",
client, mSFMgr.getClientFor(mAnonymousAccount, getContext())); client, mSFMgr.getClientFor(mAnonymousAccount, getContext()));
} catch (Exception e) {
throw new AssertionFailedError("Exception getting client for account: " + e.getMessage());
}
// TODO harder tests // TODO harder tests
} }
public void testRemoveClientFor() { public void testRemoveClientFor() {
try {
OwnCloudClient client = mSFMgr.getClientFor(mValidAccount, getContext()); OwnCloudClient client = mSFMgr.getClientFor(mValidAccount, getContext());
mSFMgr.removeClientFor(mValidAccount); mSFMgr.removeClientFor(mValidAccount);
assertNotSame("Got same client instance after removing it from manager", assertNotSame("Got same client instance after removing it from manager",
client, mSFMgr.getClientFor(mValidAccount, getContext())); client, mSFMgr.getClientFor(mValidAccount, getContext()));
} catch (Exception e) {
throw new AssertionFailedError("Exception getting client for account: " + e.getMessage());
}
// TODO harder tests // TODO harder tests
} }

View File

@ -93,6 +93,7 @@ public class SingleSessionManagerTest extends AndroidTestCase {
} }
public void testGetClientFor() { public void testGetClientFor() {
try {
OwnCloudClient client1 = mSSMgr.getClientFor(mValidAccount, getContext()); OwnCloudClient client1 = mSSMgr.getClientFor(mValidAccount, getContext());
OwnCloudClient client2 = mSSMgr.getClientFor(mAnonymousAccount, getContext()); OwnCloudClient client2 = mSSMgr.getClientFor(mAnonymousAccount, getContext());
@ -101,14 +102,22 @@ public class SingleSessionManagerTest extends AndroidTestCase {
assertSame("Got different client instances for same account", assertSame("Got different client instances for same account",
client1, mSSMgr.getClientFor(mValidAccount, getContext())); client1, mSSMgr.getClientFor(mValidAccount, getContext()));
} catch (Exception e) {
throw new AssertionFailedError("Exception getting client for account: " + e.getMessage());
}
// TODO harder tests // TODO harder tests
} }
public void testRemoveClientFor() { public void testRemoveClientFor() {
try {
OwnCloudClient client1 = mSSMgr.getClientFor(mValidAccount, getContext()); OwnCloudClient client1 = mSSMgr.getClientFor(mValidAccount, getContext());
mSSMgr.removeClientFor(mValidAccount); mSSMgr.removeClientFor(mValidAccount);
assertNotSame("Got same client instance after removing it from manager", assertNotSame("Got same client instance after removing it from manager",
client1, mSSMgr.getClientFor(mValidAccount, getContext())); client1, mSSMgr.getClientFor(mValidAccount, getContext()));
} catch (Exception e) {
throw new AssertionFailedError("Exception getting client for account: " + e.getMessage());
}
// TODO harder tests // TODO harder tests
} }