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

Added map/pool component (in progress) for OwnCloudClient instances and removed internal dependencies on OwnCloudClientFactory#createOwnCloudClient(ACCOUNT, ...) methods

This commit is contained in:
David A. Velasco 2014-06-05 17:48:53 +02:00
parent ba42f934fc
commit 1ef3a0176c
3 changed files with 156 additions and 26 deletions

View File

@ -126,30 +126,36 @@ public class OwnCloudClient extends HttpClient {
Log.e(TAG + " #" + mInstanceNumber, "BASE URL: " + mUri); Log.e(TAG + " #" + mInstanceNumber, "BASE URL: " + mUri);
Log.e(TAG + " #" + mInstanceNumber, "WebDAV URL: " + mWebdavUri); Log.e(TAG + " #" + mInstanceNumber, "WebDAV URL: " + mWebdavUri);
getParams().setAuthenticationPreemptive(false); if (accessToken != null && accessToken.length() > 0) {
mSsoSessionCookie = accessToken; getParams().setAuthenticationPreemptive(false);
mCredentials = null;
mSsoSessionCookie = accessToken;
Uri serverUri = (mUri != null)? mUri : mWebdavUri; mCredentials = null;
// TODO refactoring the mess of URIs
Uri serverUri = (mUri != null)? mUri : mWebdavUri;
String[] cookies = mSsoSessionCookie.split(";"); // TODO refactoring the mess of URIs
if (cookies.length > 0) {
//Cookie[] cookies = new Cookie[cookiesStr.length]; String[] cookies = mSsoSessionCookie.split(";");
for (int i=0; i<cookies.length; i++) { if (cookies.length > 0) {
Cookie cookie = new Cookie(); //Cookie[] cookies = new Cookie[cookiesStr.length];
int equalPos = cookies[i].indexOf('='); for (int i=0; i<cookies.length; i++) {
cookie.setName(cookies[i].substring(0, equalPos)); Cookie cookie = new Cookie();
//Log.d(TAG, "Set name for cookie: " + cookies[i].substring(0, equalPos)); int equalPos = cookies[i].indexOf('=');
cookie.setValue(cookies[i].substring(equalPos + 1)); cookie.setName(cookies[i].substring(0, equalPos));
//Log.d(TAG, "Set value for cookie: " + cookies[i].substring(equalPos + 1)); //Log.d(TAG, "Set name for cookie: " + cookies[i].substring(0, equalPos));
cookie.setDomain(serverUri.getHost()); // VERY IMPORTANT cookie.setValue(cookies[i].substring(equalPos + 1));
//Log.d(TAG, "Set domain for cookie: " + serverUri.getHost()); //Log.d(TAG, "Set value for cookie: " + cookies[i].substring(equalPos + 1));
cookie.setPath(serverUri.getPath()); // VERY IMPORTANT cookie.setDomain(serverUri.getHost()); // VERY IMPORTANT
Log.d(TAG, "Set path for cookie: " + serverUri.getPath()); //Log.d(TAG, "Set domain for cookie: " + serverUri.getHost());
getState().addCookie(cookie); cookie.setPath(serverUri.getPath()); // VERY IMPORTANT
} Log.d(TAG, "Set path for cookie: " + serverUri.getPath());
getState().addCookie(cookie);
}
}
} else {
Log.e(TAG, "Setting access token " + accessToken);
} }
} }

View File

@ -0,0 +1,76 @@
/* ownCloud Android Library is available under MIT license
* Copyright (C) 2014 ownCloud Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/
package com.owncloud.android.lib.common;
import java.io.IOException;
import java.util.concurrent.ConcurrentMap;
import android.accounts.Account;
import android.accounts.AuthenticatorException;
import android.accounts.OperationCanceledException;
import android.content.Context;
import com.owncloud.android.lib.common.OwnCloudClient;
import com.owncloud.android.lib.common.OwnCloudClientFactory;
import com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundException;
/**
* Map for {@link OwnCloudClient} instances associated to ownCloud {@link Account}s
*
* TODO check synchronization
*
* TODO consider converting into a non static object saved in the application context
* @author David A. Velasco
*/
public class OwnCloudClientMap {
private static ConcurrentMap<String, OwnCloudClient> mClients =
new java.util.concurrent.ConcurrentHashMap<String, OwnCloudClient>();
public static synchronized OwnCloudClient getClientFor(Account account, Context context)
throws OperationCanceledException, AuthenticatorException,
AccountNotFoundException, IOException {
OwnCloudClient client = mClients.get(account);
if (client == null) {
client = OwnCloudClientFactory.createOwnCloudClient(
account,
context.getApplicationContext());
mClients.putIfAbsent(account.name, client);
}
return client;
}
public static synchronized OwnCloudClient removeClientFor(Account account) {
return mClients.remove(account.name);
}
public static synchronized void clearPool() {
mClients.clear();
}
}

View File

@ -30,6 +30,7 @@ import org.apache.commons.httpclient.Credentials;
import com.owncloud.android.lib.common.OwnCloudClient; 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.OwnCloudClientMap;
import com.owncloud.android.lib.common.network.BearerCredentials; import com.owncloud.android.lib.common.network.BearerCredentials;
import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode; import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode;
@ -105,7 +106,7 @@ public abstract class RemoteOperation implements Runnable {
mAccount = account; mAccount = account;
mContext = context.getApplicationContext(); mContext = context.getApplicationContext();
try { try {
mClient = OwnCloudClientFactory.createOwnCloudClient(mAccount, mContext); mClient = OwnCloudClientMap.getClientFor(mAccount, mContext);
} catch (Exception e) { } catch (Exception e) {
Log.e(TAG, "Error while trying to access to " + mAccount.name, e); Log.e(TAG, "Error while trying to access to " + mAccount.name, e);
return new RemoteOperationResult(e); return new RemoteOperationResult(e);
@ -135,12 +136,17 @@ public abstract class RemoteOperation implements Runnable {
* *
* This method should be used whenever an ownCloud account is available, instead of {@link #execute(OwnCloudClient)}. * This method should be used whenever an ownCloud account is available, instead of {@link #execute(OwnCloudClient)}.
* *
* @deprecated This method will be removed in version 1.0.
* Use {@link #execute(Account, Context, OnRemoteOperationListener, Handler)}
* instead.
*
* @param account ownCloud account in remote ownCloud server to reach during the execution of the operation. * @param account ownCloud account in remote ownCloud server to reach during the execution of the operation.
* @param context Android context for the component calling the method. * @param context Android context for the component calling the method.
* @param listener Listener to be notified about the execution of the operation. * @param listener Listener to be notified about the execution of the operation.
* @param listenerHandler Handler associated to the thread where the methods of the listener objects must be called. * @param listenerHandler Handler associated to the thread where the methods of the listener objects must be called.
* @return Thread were the remote operation is executed. * @return Thread were the remote operation is executed.
*/ */
@Deprecated
public Thread execute(Account account, Context context, OnRemoteOperationListener listener, Handler listenerHandler, Activity callerActivity) { public Thread execute(Account account, Context context, OnRemoteOperationListener listener, Handler listenerHandler, Activity callerActivity) {
if (account == null) if (account == null)
throw new IllegalArgumentException("Trying to execute a remote operation with a NULL Account"); throw new IllegalArgumentException("Trying to execute a remote operation with a NULL Account");
@ -161,6 +167,42 @@ public abstract class RemoteOperation implements Runnable {
} }
/**
* Asynchronously executes the remote operation
*
* This method should be used whenever an ownCloud account is available,
* instead of {@link #execute(OwnCloudClient, OnRemoteOperationListener, Handler))}.
*
* @param account ownCloud account in remote ownCloud server to reach during the
* execution of the operation.
* @param context Android context for the component calling the method.
* @param listener Listener to be notified about the execution of the operation.
* @param listenerHandler Handler associated to the thread where the methods of the listener
* objects must be called.
* @return Thread were the remote operation is executed.
*/
public Thread execute(Account account, Context context, OnRemoteOperationListener listener,
Handler listenerHandler) {
if (account == null)
throw new IllegalArgumentException("Trying to execute a remote operation with a NULL Account");
if (context == null)
throw new IllegalArgumentException("Trying to execute a remote operation with a NULL Context");
mAccount = account;
mContext = context.getApplicationContext();
mCallerActivity = null;
mClient = null; // the client instance will be created from mAccount and mContext in the runnerThread to create below
mListener = listener;
mListenerHandler = listenerHandler;
Thread runnerThread = new Thread(this);
runnerThread.start();
return runnerThread;
}
/** /**
* Asynchronously executes the remote operation * Asynchronously executes the remote operation
* *
@ -205,10 +247,13 @@ public abstract class RemoteOperation implements Runnable {
try{ try{
if (mClient == null) { if (mClient == null) {
if (mAccount != null && mContext != null) { if (mAccount != null && mContext != null) {
/** DEPRECATED BLOCK - will be removed at version 1.0 */
if (mCallerActivity != null) { if (mCallerActivity != null) {
mClient = OwnCloudClientFactory.createOwnCloudClient(mAccount, mContext, mCallerActivity); mClient = OwnCloudClientFactory.createOwnCloudClient(
mAccount, mContext, mCallerActivity);
} else { } else {
mClient = OwnCloudClientFactory.createOwnCloudClient(mAccount, mContext); /** EOF DEPRECATED */
mClient = OwnCloudClientMap.getClientFor(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");
@ -228,6 +273,8 @@ public abstract class RemoteOperation implements Runnable {
result = run(mClient); result = run(mClient);
repeat = false; repeat = false;
/** DEPRECATED BLOCK - will be removed at version 1.0 ; don't trust in this code
* to trigger authentication update */
if (mCallerActivity != null && mAccount != null && mContext != null && !result.isSuccess() && if (mCallerActivity != null && mAccount != null && mContext != null && !result.isSuccess() &&
// (result.getCode() == ResultCode.UNAUTHORIZED || (result.isTemporalRedirection() && result.isIdPRedirection()))) { // (result.getCode() == ResultCode.UNAUTHORIZED || (result.isTemporalRedirection() && result.isIdPRedirection()))) {
(result.getCode() == ResultCode.UNAUTHORIZED || result.isIdPRedirection())) { (result.getCode() == ResultCode.UNAUTHORIZED || result.isIdPRedirection())) {
@ -251,6 +298,7 @@ public abstract class RemoteOperation implements Runnable {
result = null; result = null;
} }
} }
/** EOF DEPRECATED BLOCK **/
} while (repeat); } while (repeat);
final RemoteOperationResult resultToSend = result; final RemoteOperationResult resultToSend = result;