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:
parent
ba42f934fc
commit
1ef3a0176c
@ -126,30 +126,36 @@ public class OwnCloudClient extends HttpClient {
|
||||
Log.e(TAG + " #" + mInstanceNumber, "BASE URL: " + mUri);
|
||||
Log.e(TAG + " #" + mInstanceNumber, "WebDAV URL: " + mWebdavUri);
|
||||
|
||||
getParams().setAuthenticationPreemptive(false);
|
||||
if (accessToken != null && accessToken.length() > 0) {
|
||||
|
||||
mSsoSessionCookie = accessToken;
|
||||
mCredentials = null;
|
||||
|
||||
Uri serverUri = (mUri != null)? mUri : mWebdavUri;
|
||||
// TODO refactoring the mess of URIs
|
||||
|
||||
String[] cookies = mSsoSessionCookie.split(";");
|
||||
if (cookies.length > 0) {
|
||||
//Cookie[] cookies = new Cookie[cookiesStr.length];
|
||||
for (int i=0; i<cookies.length; i++) {
|
||||
Cookie cookie = new Cookie();
|
||||
int equalPos = cookies[i].indexOf('=');
|
||||
cookie.setName(cookies[i].substring(0, equalPos));
|
||||
//Log.d(TAG, "Set name for cookie: " + cookies[i].substring(0, equalPos));
|
||||
cookie.setValue(cookies[i].substring(equalPos + 1));
|
||||
//Log.d(TAG, "Set value for cookie: " + cookies[i].substring(equalPos + 1));
|
||||
cookie.setDomain(serverUri.getHost()); // VERY IMPORTANT
|
||||
//Log.d(TAG, "Set domain for cookie: " + serverUri.getHost());
|
||||
cookie.setPath(serverUri.getPath()); // VERY IMPORTANT
|
||||
Log.d(TAG, "Set path for cookie: " + serverUri.getPath());
|
||||
getState().addCookie(cookie);
|
||||
}
|
||||
getParams().setAuthenticationPreemptive(false);
|
||||
|
||||
mSsoSessionCookie = accessToken;
|
||||
mCredentials = null;
|
||||
|
||||
Uri serverUri = (mUri != null)? mUri : mWebdavUri;
|
||||
// TODO refactoring the mess of URIs
|
||||
|
||||
String[] cookies = mSsoSessionCookie.split(";");
|
||||
if (cookies.length > 0) {
|
||||
//Cookie[] cookies = new Cookie[cookiesStr.length];
|
||||
for (int i=0; i<cookies.length; i++) {
|
||||
Cookie cookie = new Cookie();
|
||||
int equalPos = cookies[i].indexOf('=');
|
||||
cookie.setName(cookies[i].substring(0, equalPos));
|
||||
//Log.d(TAG, "Set name for cookie: " + cookies[i].substring(0, equalPos));
|
||||
cookie.setValue(cookies[i].substring(equalPos + 1));
|
||||
//Log.d(TAG, "Set value for cookie: " + cookies[i].substring(equalPos + 1));
|
||||
cookie.setDomain(serverUri.getHost()); // VERY IMPORTANT
|
||||
//Log.d(TAG, "Set domain for cookie: " + serverUri.getHost());
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
76
src/com/owncloud/android/lib/common/OwnCloudClientMap.java
Normal file
76
src/com/owncloud/android/lib/common/OwnCloudClientMap.java
Normal 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();
|
||||
}
|
||||
|
||||
}
|
@ -30,6 +30,7 @@ import org.apache.commons.httpclient.Credentials;
|
||||
|
||||
import com.owncloud.android.lib.common.OwnCloudClient;
|
||||
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.operations.RemoteOperationResult.ResultCode;
|
||||
|
||||
@ -105,7 +106,7 @@ public abstract class RemoteOperation implements Runnable {
|
||||
mAccount = account;
|
||||
mContext = context.getApplicationContext();
|
||||
try {
|
||||
mClient = OwnCloudClientFactory.createOwnCloudClient(mAccount, mContext);
|
||||
mClient = OwnCloudClientMap.getClientFor(mAccount, mContext);
|
||||
} catch (Exception e) {
|
||||
Log.e(TAG, "Error while trying to access to " + mAccount.name, 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)}.
|
||||
*
|
||||
* @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 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.
|
||||
*/
|
||||
@Deprecated
|
||||
public Thread execute(Account account, Context context, OnRemoteOperationListener listener, Handler listenerHandler, Activity callerActivity) {
|
||||
if (account == null)
|
||||
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
|
||||
*
|
||||
@ -205,10 +247,13 @@ public abstract class RemoteOperation implements Runnable {
|
||||
try{
|
||||
if (mClient == null) {
|
||||
if (mAccount != null && mContext != null) {
|
||||
/** DEPRECATED BLOCK - will be removed at version 1.0 */
|
||||
if (mCallerActivity != null) {
|
||||
mClient = OwnCloudClientFactory.createOwnCloudClient(mAccount, mContext, mCallerActivity);
|
||||
mClient = OwnCloudClientFactory.createOwnCloudClient(
|
||||
mAccount, mContext, mCallerActivity);
|
||||
} else {
|
||||
mClient = OwnCloudClientFactory.createOwnCloudClient(mAccount, mContext);
|
||||
/** EOF DEPRECATED */
|
||||
mClient = OwnCloudClientMap.getClientFor(mAccount, mContext);
|
||||
}
|
||||
} else {
|
||||
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);
|
||||
|
||||
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() &&
|
||||
// (result.getCode() == ResultCode.UNAUTHORIZED || (result.isTemporalRedirection() && result.isIdPRedirection()))) {
|
||||
(result.getCode() == ResultCode.UNAUTHORIZED || result.isIdPRedirection())) {
|
||||
@ -251,6 +298,7 @@ public abstract class RemoteOperation implements Runnable {
|
||||
result = null;
|
||||
}
|
||||
}
|
||||
/** EOF DEPRECATED BLOCK **/
|
||||
} while (repeat);
|
||||
|
||||
final RemoteOperationResult resultToSend = result;
|
||||
|
Loading…
x
Reference in New Issue
Block a user