From c7f9c9d2014b88e31b583dea2f2e38c27880e149 Mon Sep 17 00:00:00 2001 From: davigonz Date: Tue, 4 Feb 2020 17:26:10 +0100 Subject: [PATCH] Clean up owncloud client factories --- .../android/lib/common/OwnCloudClient.java | 29 ++----- .../lib/common/OwnCloudClientManager.java | 52 ------------ .../common/OwnCloudClientManagerFactory.java | 85 ------------------- .../lib/common/SimpleFactoryManager.java | 72 ---------------- .../lib/common/SingleSessionManager.java | 38 +++++---- .../android/lib/common/http/HttpClient.java | 6 +- .../common/operations/RemoteOperation.java | 4 +- 7 files changed, 36 insertions(+), 250 deletions(-) delete mode 100644 owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/OwnCloudClientManager.java delete mode 100644 owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/OwnCloudClientManagerFactory.java delete mode 100644 owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/SimpleFactoryManager.java diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/OwnCloudClient.java b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/OwnCloudClient.java index ccfb8ebf..6f5bca83 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/OwnCloudClient.java +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/OwnCloudClient.java @@ -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. *

* {@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; } @@ -422,4 +409,4 @@ public class OwnCloudClient extends HttpClient { public void setFollowRedirects(boolean followRedirects) { this.mFollowRedirects = followRedirects; } -} \ No newline at end of file +} diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/OwnCloudClientManager.java b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/OwnCloudClientManager.java deleted file mode 100644 index fe793863..00000000 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/OwnCloudClientManager.java +++ /dev/null @@ -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; -} \ No newline at end of file diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/OwnCloudClientManagerFactory.java b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/OwnCloudClientManagerFactory.java deleted file mode 100644 index 31383d3d..00000000 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/OwnCloudClientManagerFactory.java +++ /dev/null @@ -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 - } -} diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/SimpleFactoryManager.java b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/SimpleFactoryManager.java deleted file mode 100644 index 4d5ebfa3..00000000 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/SimpleFactoryManager.java +++ /dev/null @@ -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! - } -} diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/SingleSessionManager.java b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/SingleSessionManager.java index e4b190a4..f448e45b 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/SingleSessionManager.java +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/SingleSessionManager.java @@ -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} - *

- * 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 mClientsWithKnownUsername = new ConcurrentHashMap<>(); - private ConcurrentMap 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... "); diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/HttpClient.java b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/HttpClient.java index 9a629bfe..27e83e62 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/HttpClient.java +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/HttpClient.java @@ -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); } diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/operations/RemoteOperation.java b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/operations/RemoteOperation.java index bcf57dc7..321907b6 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/operations/RemoteOperation.java +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/operations/RemoteOperation.java @@ -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 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 " +