From ec5c9fc4aad325b3a6f2ddfffb19ecaca7da872b Mon Sep 17 00:00:00 2001 From: abelgardep Date: Mon, 27 Jan 2020 22:50:10 +0100 Subject: [PATCH] Remove ugly dependency from GetRemoteStatusOperation Create AnonymousService for operations that not require authentication --- .../lib/common/accounts/AccountUtils.java | 2 + .../lib/resources/server/AnonymousService.kt | 33 ++++++++++++ .../server/CheckPathExistenceOperation.kt | 9 ++-- .../server/GetRemoteStatusOperation.kt | 50 +++++++------------ .../lib/resources/server/ServerService.kt | 2 - 5 files changed, 56 insertions(+), 40 deletions(-) create mode 100644 owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/server/AnonymousService.kt diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/accounts/AccountUtils.java b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/accounts/AccountUtils.java index b2ab1880..fbe4d338 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/accounts/AccountUtils.java +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/accounts/AccountUtils.java @@ -329,5 +329,7 @@ public class AccountUtils { * OAuth2 scope */ public static final String KEY_OAUTH2_SCOPE = "oc_oauth2_scope"; + + public static final String WEBDAV_PATH_4_0_AND_LATER = "/remote.php/dav"; } } diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/server/AnonymousService.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/server/AnonymousService.kt new file mode 100644 index 00000000..ee3c5fc8 --- /dev/null +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/server/AnonymousService.kt @@ -0,0 +1,33 @@ +/* ownCloud Android Library is available under MIT license + * 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 + * 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.resources.server + +import com.owncloud.android.lib.common.operations.RemoteOperationResult +import com.owncloud.android.lib.resources.status.OwnCloudVersion + +interface AnonymousService { + fun checkPathExistence(path: String, isUserLogged: Boolean): RemoteOperationResult + + fun getRemoteStatus(path: String): RemoteOperationResult +} diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/server/CheckPathExistenceOperation.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/server/CheckPathExistenceOperation.kt index 0e900dde..051d7691 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/server/CheckPathExistenceOperation.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/server/CheckPathExistenceOperation.kt @@ -56,7 +56,7 @@ class CheckPathExistenceOperation( * * @return Sequence of redirections followed, if any, or NULL if the operation was not executed. */ - var redirectionPath: RedirectionPath? = null + lateinit var redirectionPath: RedirectionPath private set override fun run(client: OwnCloudClient): RemoteOperationResult { @@ -75,7 +75,7 @@ class CheckPathExistenceOperation( var status = client.executeHttpMethod(propFindMethod) if (previousFollowRedirects) { redirectionPath = client.followRedirection(propFindMethod) - status = redirectionPath?.lastStatus!! + status = redirectionPath.lastStatus } /* PROPFIND method * 404 NOT FOUND: path doesn't exist, @@ -103,10 +103,9 @@ class CheckPathExistenceOperation( /** * @return 'True' if the operation was executed and at least one redirection was followed. */ - fun wasRedirected() = redirectionPath != null && redirectionPath!!.redirectionsCount > 0 + fun wasRedirected() = redirectionPath.redirectionsCount > 0 - private fun isSuccess(status: Int) = - status == HttpConstants.HTTP_OK || status == HttpConstants.HTTP_MULTI_STATUS || status == HttpConstants.HTTP_MULTI_STATUS + private fun isSuccess(status: Int) = status == HttpConstants.HTTP_OK || status == HttpConstants.HTTP_MULTI_STATUS companion object { /** diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/server/GetRemoteStatusOperation.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/server/GetRemoteStatusOperation.kt index 6b903e33..4a92e93b 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/server/GetRemoteStatusOperation.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/server/GetRemoteStatusOperation.kt @@ -23,9 +23,6 @@ */ package com.owncloud.android.lib.resources.server -import android.content.Context -import android.net.ConnectivityManager -import android.net.NetworkInfo import android.net.Uri import com.owncloud.android.lib.common.OwnCloudClient import com.owncloud.android.lib.common.http.HttpConstants @@ -42,21 +39,18 @@ import java.util.concurrent.TimeUnit import javax.net.ssl.SSLException /** - * Checks if the server is valid and if the server supports the Share API + * Checks if the server is valid * * @author David A. Velasco * @author masensio * @author David González Verdugo + * @author Abel García de Prada */ -class GetRemoteStatusOperation(private var context: Context) : RemoteOperation() { +class GetRemoteStatusOperation : RemoteOperation() { private lateinit var latestResult: RemoteOperationResult override fun run(client: OwnCloudClient): RemoteOperationResult { - if (!isOnline) { - return RemoteOperationResult(ResultCode.NO_NETWORK_CONNECTION) - } - val baseUriStr = client.baseUri.toString() if (baseUriStr.startsWith(HTTP_PREFIX) || baseUriStr.startsWith(HTTPS_PREFIX)) { tryConnection(client) @@ -73,7 +67,7 @@ class GetRemoteStatusOperation(private var context: Context) : RemoteOperation { - Timber.i("Connection check at $baseUrlSt: ${latestResult.logMessage}") - } - latestResult.exception != null -> { - Timber.e(latestResult.exception, "Connection check at $baseUrlSt: ${latestResult.logMessage}") - } - else -> { - Timber.e("Connection check at $baseUrlSt: ${latestResult.logMessage}") - } - } - return retval - } + latestResult.isSuccess -> Timber.i("Connection check at $baseUrlSt: ${latestResult.logMessage}") - private val isOnline: Boolean - get() { - val cm = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager - val activeNetwork: NetworkInfo? = cm.activeNetworkInfo - return activeNetwork?.isConnected == true + latestResult.isException -> + Timber.e(latestResult.exception, "Connection check at $baseUrlSt: ${latestResult.logMessage}") + + else -> Timber.e("Connection check at $baseUrlSt: ${latestResult.logMessage}") } + return successfulConnection + } private fun isSuccess(status: Int): Boolean = status == HttpConstants.HTTP_OK @@ -161,7 +145,7 @@ class GetRemoteStatusOperation(private var context: Context) : RemoteOperation - }