mirror of
https://github.com/owncloud/android-library.git
synced 2025-06-07 16:06:08 +00:00
Clean up owncloud client factories
This commit is contained in:
parent
5b3c21ba82
commit
c7f9c9d201
@ -1,5 +1,5 @@
|
||||
/* ownCloud Android Library is available under MIT license
|
||||
* Copyright (C) 2019 ownCloud GmbH.
|
||||
* Copyright (C) 2020 ownCloud GmbH.
|
||||
* Copyright (C) 2012 Bartek Przybylski
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
@ -53,9 +53,8 @@ import static com.owncloud.android.lib.common.http.HttpConstants.OC_X_REQUEST_ID
|
||||
public class OwnCloudClient extends HttpClient {
|
||||
|
||||
public static final String WEBDAV_FILES_PATH_4_0 = "/remote.php/dav/files/";
|
||||
public static final String WEBDAV_UPLOADS_PATH_4_0 = "/remote.php/dav/uploads/";
|
||||
private static final String WEBDAV_UPLOADS_PATH_4_0 = "/remote.php/dav/uploads/";
|
||||
public static final String STATUS_PATH = "/status.php";
|
||||
public static final String FILES_WEB_PATH = "/index.php/apps/files";
|
||||
|
||||
private static final int MAX_REDIRECTIONS_COUNT = 3;
|
||||
private static final int MAX_REPEAT_COUNT_WITH_FRESH_CREDENTIALS = 1;
|
||||
@ -68,13 +67,8 @@ public class OwnCloudClient extends HttpClient {
|
||||
private OwnCloudVersion mVersion = null;
|
||||
private OwnCloudAccount mAccount;
|
||||
|
||||
/**
|
||||
* {@link @OwnCloudClientManager} holding a reference to this object and delivering it to callers; might be
|
||||
* NULL
|
||||
*/
|
||||
private OwnCloudClientManager mOwnCloudClientManager = null;
|
||||
private SingleSessionManager mSingleSessionManager = null;
|
||||
|
||||
private String mRedirectedLocation;
|
||||
private boolean mFollowRedirects;
|
||||
|
||||
public OwnCloudClient(Uri baseUri) {
|
||||
@ -97,7 +91,7 @@ public class OwnCloudClient extends HttpClient {
|
||||
mCredentials.applyTo(this);
|
||||
}
|
||||
|
||||
public void applyCredentials() {
|
||||
void applyCredentials() {
|
||||
mCredentials.applyTo(this);
|
||||
}
|
||||
|
||||
@ -127,9 +121,6 @@ public class OwnCloudClient extends HttpClient {
|
||||
|
||||
private void checkFirstRedirection(HttpBaseMethod method) {
|
||||
final String location = method.getResponseHeader(HttpConstants.LOCATION_HEADER_LOWER);
|
||||
if (location != null && !location.isEmpty()) {
|
||||
mRedirectedLocation = location;
|
||||
}
|
||||
}
|
||||
|
||||
private int executeRedirectedHttpMethod(HttpBaseMethod method) throws Exception {
|
||||
@ -360,10 +351,10 @@ public class OwnCloudClient extends HttpClient {
|
||||
}
|
||||
}
|
||||
|
||||
if (!credentialsWereRefreshed && mOwnCloudClientManager != null) {
|
||||
if (!credentialsWereRefreshed && mSingleSessionManager != null) {
|
||||
// if credentials are not refreshed, client must be removed
|
||||
// from the OwnCloudClientManager to prevent it is reused once and again
|
||||
mOwnCloudClientManager.removeClientFor(mAccount);
|
||||
mSingleSessionManager.removeClientFor(mAccount);
|
||||
}
|
||||
}
|
||||
// else: onExecute will finish with status 401
|
||||
@ -395,7 +386,7 @@ public class OwnCloudClient extends HttpClient {
|
||||
|
||||
/**
|
||||
* Invalidates credentials stored for the given account in the system {@link AccountManager} and in
|
||||
* current {@link OwnCloudClientManagerFactory#getDefaultSingleton()} instance.
|
||||
* current {@link SingleSessionManager#getDefaultSingleton()} instance.
|
||||
* <p>
|
||||
* {@link #shouldInvalidateAccountCredentials(int)} should be called first.
|
||||
*
|
||||
@ -411,10 +402,6 @@ public class OwnCloudClient extends HttpClient {
|
||||
return true;
|
||||
}
|
||||
|
||||
void setOwnCloudClientManager(OwnCloudClientManager clientManager) {
|
||||
mOwnCloudClientManager = clientManager;
|
||||
}
|
||||
|
||||
public boolean followRedirects() {
|
||||
return mFollowRedirects;
|
||||
}
|
||||
|
@ -1,52 +0,0 @@
|
||||
/* ownCloud Android Library is available under MIT license
|
||||
* Copyright (C) 2019 ownCloud GmbH.
|
||||
*
|
||||
* 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 android.accounts.AuthenticatorException;
|
||||
import android.accounts.OperationCanceledException;
|
||||
import android.content.Context;
|
||||
|
||||
import com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundException;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* Manager to create and reuse OwnCloudClient instances to access remote OC servers.
|
||||
*
|
||||
* @author David A. Velasco
|
||||
* @author masensio
|
||||
* @author Christian Schabesberger
|
||||
*/
|
||||
|
||||
public interface OwnCloudClientManager {
|
||||
|
||||
OwnCloudClient getClientFor(OwnCloudAccount account, Context context) throws AccountNotFoundException,
|
||||
OperationCanceledException, AuthenticatorException, IOException;
|
||||
|
||||
OwnCloudClient removeClientFor(OwnCloudAccount account);
|
||||
|
||||
void saveAllClients(Context context, String accountType) throws AccountNotFoundException, AuthenticatorException, IOException,
|
||||
OperationCanceledException;
|
||||
}
|
@ -1,85 +0,0 @@
|
||||
/* ownCloud Android Library is available under MIT license
|
||||
* Copyright (C) 2019 ownCloud GmbH.
|
||||
*
|
||||
* 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;
|
||||
|
||||
public class OwnCloudClientManagerFactory {
|
||||
|
||||
private static Policy sDefaultPolicy = Policy.NEW_CLIENT;
|
||||
private static OwnCloudClientManager sDefaultSingleton;
|
||||
private static String sUserAgent;
|
||||
|
||||
private static OwnCloudClientManager newDefaultOwnCloudClientManager() {
|
||||
return newOwnCloudClientManager(sDefaultPolicy);
|
||||
}
|
||||
|
||||
private static OwnCloudClientManager newOwnCloudClientManager(Policy policy) {
|
||||
switch (policy) {
|
||||
case NEW_CLIENT:
|
||||
return new SimpleFactoryManager();
|
||||
|
||||
case SINGLE_SESSION_PER_ACCOUNT:
|
||||
return new SingleSessionManager();
|
||||
|
||||
default:
|
||||
throw new IllegalArgumentException("Unknown policy");
|
||||
}
|
||||
}
|
||||
|
||||
public static OwnCloudClientManager getDefaultSingleton() {
|
||||
if (sDefaultSingleton == null) {
|
||||
sDefaultSingleton = newDefaultOwnCloudClientManager();
|
||||
}
|
||||
return sDefaultSingleton;
|
||||
}
|
||||
|
||||
public static void setDefaultPolicy(Policy policy) {
|
||||
if (policy == null) {
|
||||
throw new IllegalArgumentException("Default policy cannot be NULL");
|
||||
}
|
||||
if (defaultSingletonMustBeUpdated(policy)) {
|
||||
sDefaultSingleton = null;
|
||||
}
|
||||
sDefaultPolicy = policy;
|
||||
}
|
||||
|
||||
public static String getUserAgent() {
|
||||
return sUserAgent;
|
||||
}
|
||||
|
||||
public static void setUserAgent(String userAgent) {
|
||||
sUserAgent = userAgent;
|
||||
}
|
||||
|
||||
private static boolean defaultSingletonMustBeUpdated(Policy policy) {
|
||||
if (sDefaultSingleton == null) {
|
||||
return false;
|
||||
}
|
||||
return policy == Policy.NEW_CLIENT && !(sDefaultSingleton instanceof SimpleFactoryManager);
|
||||
}
|
||||
|
||||
public enum Policy {
|
||||
NEW_CLIENT,
|
||||
SINGLE_SESSION_PER_ACCOUNT
|
||||
}
|
||||
}
|
@ -1,72 +0,0 @@
|
||||
/* ownCloud Android Library is available under MIT license
|
||||
* Copyright (C) 2016 ownCloud GmbH.
|
||||
*
|
||||
* 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 android.accounts.AuthenticatorException;
|
||||
import android.accounts.OperationCanceledException;
|
||||
import android.content.Context;
|
||||
|
||||
import com.owncloud.android.lib.common.accounts.AccountUtils;
|
||||
import timber.log.Timber;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class SimpleFactoryManager implements OwnCloudClientManager {
|
||||
|
||||
@Override
|
||||
public OwnCloudClient getClientFor(OwnCloudAccount account, Context context) throws
|
||||
OperationCanceledException, AuthenticatorException, IOException {
|
||||
|
||||
Timber.d("getClientFor(OwnCloudAccount ... : ");
|
||||
|
||||
OwnCloudClient client = OwnCloudClientFactory.createOwnCloudClient(
|
||||
account.getBaseUri(),
|
||||
context.getApplicationContext(),
|
||||
true);
|
||||
|
||||
Timber.v("new client {" + (account.getName() != null ? account.getName() :
|
||||
AccountUtils.buildAccountName(account.getBaseUri(), "")) + ", " + client.hashCode() + "}");
|
||||
|
||||
if (account.getCredentials() == null) {
|
||||
account.loadCredentials(context);
|
||||
}
|
||||
client.setCredentials(account.getCredentials());
|
||||
client.setAccount(account);
|
||||
client.setContext(context);
|
||||
client.setOwnCloudClientManager(this);
|
||||
return client;
|
||||
}
|
||||
|
||||
@Override
|
||||
public OwnCloudClient removeClientFor(OwnCloudAccount account) {
|
||||
// nothing to do - not taking care of tracking instances!
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void saveAllClients(Context context, String accountType) {
|
||||
// nothing to do - not taking care of tracking instances!
|
||||
}
|
||||
}
|
@ -1,5 +1,5 @@
|
||||
/* ownCloud Android Library is available under MIT license
|
||||
* Copyright (C) 2019 ownCloud GmbH.
|
||||
* Copyright (C) 2020 ownCloud GmbH.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
@ -41,23 +41,35 @@ import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ConcurrentMap;
|
||||
|
||||
/**
|
||||
* Implementation of {@link OwnCloudClientManager}
|
||||
* <p>
|
||||
* TODO check multithreading safety
|
||||
*
|
||||
* @author David A. Velasco
|
||||
* @author masensio
|
||||
* @author Christian Schabesberger
|
||||
* @author David González Verdugo
|
||||
*/
|
||||
|
||||
public class SingleSessionManager implements OwnCloudClientManager {
|
||||
public class SingleSessionManager {
|
||||
|
||||
private static SingleSessionManager sDefaultSingleton;
|
||||
private static String sUserAgent;
|
||||
|
||||
private ConcurrentMap<String, OwnCloudClient> mClientsWithKnownUsername = new ConcurrentHashMap<>();
|
||||
|
||||
private ConcurrentMap<String, OwnCloudClient> mClientsWithUnknownUsername = new ConcurrentHashMap<>();
|
||||
|
||||
@Override
|
||||
public static SingleSessionManager getDefaultSingleton() {
|
||||
if (sDefaultSingleton == null) {
|
||||
sDefaultSingleton = new SingleSessionManager();
|
||||
}
|
||||
return sDefaultSingleton;
|
||||
}
|
||||
|
||||
public static String getUserAgent() {
|
||||
return sUserAgent;
|
||||
}
|
||||
|
||||
public static void setUserAgent(String userAgent) {
|
||||
sUserAgent = userAgent;
|
||||
}
|
||||
|
||||
public OwnCloudClient getClientFor(OwnCloudAccount account, Context context) throws OperationCanceledException,
|
||||
AuthenticatorException, IOException {
|
||||
|
||||
@ -100,7 +112,6 @@ public class SingleSessionManager implements OwnCloudClientManager {
|
||||
true); // TODO remove dependency on OwnCloudClientFactory
|
||||
client.setAccount(account);
|
||||
HttpClient.setContext(context);
|
||||
client.setOwnCloudClientManager(this);
|
||||
|
||||
account.loadCredentials(context);
|
||||
client.setCredentials(account.getCredentials());
|
||||
@ -126,12 +137,11 @@ public class SingleSessionManager implements OwnCloudClientManager {
|
||||
return client;
|
||||
}
|
||||
|
||||
@Override
|
||||
public OwnCloudClient removeClientFor(OwnCloudAccount account) {
|
||||
public void removeClientFor(OwnCloudAccount account) {
|
||||
Timber.d("removeClientFor starting ");
|
||||
|
||||
if (account == null) {
|
||||
return null;
|
||||
return;
|
||||
}
|
||||
|
||||
OwnCloudClient client;
|
||||
@ -140,7 +150,7 @@ public class SingleSessionManager implements OwnCloudClientManager {
|
||||
client = mClientsWithKnownUsername.remove(accountName);
|
||||
if (client != null) {
|
||||
Timber.v("Removed client for account %s", accountName);
|
||||
return client;
|
||||
return;
|
||||
} else {
|
||||
Timber.v("No client tracked for account %s", accountName);
|
||||
}
|
||||
@ -149,10 +159,8 @@ public class SingleSessionManager implements OwnCloudClientManager {
|
||||
mClientsWithUnknownUsername.clear();
|
||||
|
||||
Timber.d("removeClientFor finishing ");
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void saveAllClients(Context context, String accountType) {
|
||||
Timber.d("Saving sessions... ");
|
||||
|
||||
|
@ -1,5 +1,5 @@
|
||||
/* ownCloud Android Library is available under MIT license
|
||||
* Copyright (C) 2019 ownCloud GmbH.
|
||||
* Copyright (C) 2020 ownCloud GmbH.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
@ -26,7 +26,7 @@ package com.owncloud.android.lib.common.http;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import com.owncloud.android.lib.common.OwnCloudClientManagerFactory;
|
||||
import com.owncloud.android.lib.common.SingleSessionManager;
|
||||
import com.owncloud.android.lib.common.http.interceptors.HttpInterceptor;
|
||||
import com.owncloud.android.lib.common.http.interceptors.RequestHeaderInterceptor;
|
||||
import com.owncloud.android.lib.common.network.AdvancedX509TrustManager;
|
||||
@ -132,7 +132,7 @@ public class HttpClient {
|
||||
private static HttpInterceptor getOkHttpInterceptor() {
|
||||
if (sOkHttpInterceptor == null) {
|
||||
sOkHttpInterceptor = new HttpInterceptor();
|
||||
addHeaderForAllRequests(HttpConstants.USER_AGENT_HEADER, OwnCloudClientManagerFactory.getUserAgent());
|
||||
addHeaderForAllRequests(HttpConstants.USER_AGENT_HEADER, SingleSessionManager.getUserAgent());
|
||||
addHeaderForAllRequests(HttpConstants.PARAM_SINGLE_COOKIE_HEADER, "true");
|
||||
addHeaderForAllRequests(HttpConstants.ACCEPT_ENCODING_HEADER, HttpConstants.ACCEPT_ENCODING_IDENTITY);
|
||||
}
|
||||
|
@ -9,7 +9,7 @@ import android.os.Handler;
|
||||
|
||||
import com.owncloud.android.lib.common.OwnCloudAccount;
|
||||
import com.owncloud.android.lib.common.OwnCloudClient;
|
||||
import com.owncloud.android.lib.common.OwnCloudClientManagerFactory;
|
||||
import com.owncloud.android.lib.common.SingleSessionManager;
|
||||
import com.owncloud.android.lib.common.accounts.AccountUtils;
|
||||
import okhttp3.OkHttpClient;
|
||||
import timber.log.Timber;
|
||||
@ -134,7 +134,7 @@ public abstract class RemoteOperation<T> implements Runnable {
|
||||
if (mClient == null) {
|
||||
if (mAccount != null && mContext != null) {
|
||||
OwnCloudAccount ocAccount = new OwnCloudAccount(mAccount, mContext);
|
||||
mClient = OwnCloudClientManagerFactory.getDefaultSingleton().
|
||||
mClient = SingleSessionManager.getDefaultSingleton().
|
||||
getClientFor(ocAccount, mContext);
|
||||
} else {
|
||||
throw new IllegalStateException("Trying to run a remote operation " +
|
||||
|
Loading…
x
Reference in New Issue
Block a user