1
0
mirror of https://github.com/owncloud/android-library.git synced 2025-06-07 16:06:08 +00:00

stop validation process if failing

This commit is contained in:
Christian Schabesberger 2021-09-10 15:54:49 +02:00
parent e878ad3931
commit 7ccb86c153
3 changed files with 16 additions and 36 deletions

View File

@ -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<RemoteServerInfo> {
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<Boolean> {
val checkPathExistenceRemoteOperation = CheckPathExistenceRemoteOperation("/", true)
return checkPathExistenceRemoteOperation.execute(client)
}
}

View File

@ -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;
}
}
}

View File

@ -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 {