From 7ccb86c153aab03a9f547584ebed0e485a2341af Mon Sep 17 00:00:00 2001 From: Christian Schabesberger Date: Fri, 10 Sep 2021 15:54:49 +0200 Subject: [PATCH] stop validation process if failing --- .../android/lib/common/ConnectionValidator.kt | 41 +++++++------------ .../android/lib/common/OwnCloudClient.java | 6 +-- .../CheckPathExistenceRemoteOperation.kt | 5 --- 3 files changed, 16 insertions(+), 36 deletions(-) diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/ConnectionValidator.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/ConnectionValidator.kt index 045bb990..3284a482 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/ConnectionValidator.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/ConnectionValidator.kt @@ -5,6 +5,7 @@ import com.owncloud.android.lib.common.authentication.OwnCloudCredentialsFactory import com.owncloud.android.lib.common.http.HttpConstants import com.owncloud.android.lib.common.http.methods.HttpBaseMethod import com.owncloud.android.lib.common.operations.RemoteOperationResult +import com.owncloud.android.lib.resources.files.CheckPathExistenceRemoteOperation import com.owncloud.android.lib.resources.status.GetRemoteStatusOperation import com.owncloud.android.lib.resources.status.RemoteServerInfo import org.apache.commons.lang3.exception.ExceptionUtils @@ -15,7 +16,7 @@ class ConnectionValidator ( val clearCookiesOnValidation: Boolean ){ - fun validate(method: HttpBaseMethod, baseClient: OwnCloudClient): Boolean { + fun validate(baseClient: OwnCloudClient): Boolean { try { var validationRetryCount = 0 val client = OwnCloudClient(baseClient.baseUri, null, false) @@ -26,27 +27,28 @@ class ConnectionValidator ( } client.credentials = baseClient.credentials - client.setFollowRedirects(true) while (validationRetryCount < 5) { var successCounter = 0 var failCounter = 0 + client.setFollowRedirects(true) if (isOnwCloudStatusOk(client)) { successCounter++ } else { failCounter++ } - val contentReply = accessRootFolder() - if (contentReply == HttpConstants.HTTP_OK) { - if (isRootFolderOk(contentReply)) { + client.setFollowRedirects(false) + val contentReply = canAccessRootFolder(client) + if (contentReply.httpCode == HttpConstants.HTTP_OK) { + if (contentReply.data == true) { //if data is true it means that the content reply was ok successCounter++ } else { failCounter++ } } else { failCounter++ - if (contentReply == HttpConstants.HTTP_UNAUTHORIZED) { + if (contentReply.hashCode() == HttpConstants.HTTP_UNAUTHORIZED) { triggerAuthRefresh() } } @@ -64,39 +66,24 @@ class ConnectionValidator ( } private fun isOnwCloudStatusOk(client: OwnCloudClient): Boolean { - //TODO: Implement me val reply = getOwnCloudStatus(client) - return if (reply.httpCode == HttpConstants.HTTP_OK) { - isOCStatusReplyValid(reply.data) - } else { - false - } + return reply.httpCode == HttpConstants.HTTP_OK && + !reply.isException && + reply.data != null } private fun getOwnCloudStatus(client: OwnCloudClient): RemoteOperationResult { val remoteStatusOperation = GetRemoteStatusOperation() - //TODO: follow redirects only 5 times return remoteStatusOperation.execute(client) } - private fun isOCStatusReplyValid(info: RemoteServerInfo): Boolean { - //TODO: Implement me - Timber.d("owncloud version %s", info.ownCloudVersion.version) - return true - } - private fun triggerAuthRefresh(): OwnCloudCredentials { //TODO: Implement me return OwnCloudCredentialsFactory.getAnonymousCredentials() } - private fun accessRootFolder(): Int { - //TODO: Implement me - return 55 - } - - private fun isRootFolderOk(content: Int): Boolean { - //TODO: Implement me - return true + private fun canAccessRootFolder(client: OwnCloudClient): RemoteOperationResult { + val checkPathExistenceRemoteOperation = CheckPathExistenceRemoteOperation("/", true) + return checkPathExistenceRemoteOperation.execute(client) } } \ No newline at end of file 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 39b2c8f2..b3aeac12 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 @@ -56,7 +56,6 @@ 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_PATH_4_0_AND_LATER = "/remote.php/dav"; public static final String STATUS_PATH = "/status.php"; private static final String WEBDAV_UPLOADS_PATH_4_0 = "/remote.php/dav/uploads/"; private static final int MAX_REDIRECTIONS_COUNT = 5; @@ -137,8 +136,7 @@ public class OwnCloudClient extends HttpClient { if (mConnectionValidator != null && status == HttpConstants.HTTP_MOVED_TEMPORARILY) { - mConnectionValidator.validate(method, this); - retry = true; + retry = mConnectionValidator.validate(this); // retry on success fail on no success } else if (mFollowRedirects) { status = followRedirection(method).getLastStatus(); } @@ -463,4 +461,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/resources/files/CheckPathExistenceRemoteOperation.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/CheckPathExistenceRemoteOperation.kt index 6106c74f..5e0d974c 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/CheckPathExistenceRemoteOperation.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/CheckPathExistenceRemoteOperation.kt @@ -98,11 +98,6 @@ class CheckPathExistenceRemoteOperation( } } - /** - * @return 'True' if the operation was executed and at least one redirection was followed. - */ - fun wasRedirected() = redirectionPath?.redirectionsCount ?: 0 > 0 - private fun isSuccess(status: Int) = status == HttpConstants.HTTP_OK || status == HttpConstants.HTTP_MULTI_STATUS companion object {