From 09375ce0539cef28cfda8c779fcdd7baba471893 Mon Sep 17 00:00:00 2001 From: Christian Schabesberger Date: Thu, 24 Mar 2022 17:45:54 +0100 Subject: [PATCH] rebase okhttp_signleton fix on top of connection validator fix token oauth token refresh error pleasure the linter --- .../android/lib/common/ConnectionValidator.kt | 29 +++++++++++++++++++ .../android/lib/common/OwnCloudClient.java | 15 ++++++---- .../lib/common/http/methods/HttpBaseMethod.kt | 6 ++-- .../CheckPathExistenceRemoteOperation.kt | 4 +-- .../oauth/GetOIDCDiscoveryRemoteOperation.kt | 2 +- .../lib/resources/status/StatusRequester.kt | 12 ++------ .../status/services/ServerInfoService.kt | 2 +- .../implementation/OCServerInfoService.kt | 2 +- .../lib/sampleclient/MainActivity.java | 24 ++------------- 9 files changed, 50 insertions(+), 46 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 1bc325c5..23a5a92f 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 @@ -1,3 +1,27 @@ +/* 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.AccountManager @@ -15,6 +39,11 @@ import timber.log.Timber import java.io.IOException import java.lang.Exception +/** + * ConnectionValidator + * + * @author Christian Schabesberger + */ class ConnectionValidator( val context: Context, val clearCookiesOnValidation: Boolean 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 2aefd06b..7d1a4409 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 @@ -150,12 +150,15 @@ public class OwnCloudClient extends HttpClient { } private boolean shouldConnectionValidatorBeCalled(HttpBaseMethod method, int status) { - return !mFollowRedirects && - !method.getFollowRedirects() && - mConnectionValidator != null && - (status == HttpConstants.HTTP_MOVED_TEMPORARILY || - (!(mCredentials instanceof OwnCloudAnonymousCredentials) && - status == HttpConstants.HTTP_UNAUTHORIZED)); + + return mConnectionValidator != null && ( + (!(mCredentials instanceof OwnCloudAnonymousCredentials) && + status == HttpConstants.HTTP_UNAUTHORIZED + ) || (!mFollowRedirects && + !method.getFollowRedirects() && + status == HttpConstants.HTTP_MOVED_TEMPORARILY + ) + ); } /** diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/HttpBaseMethod.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/HttpBaseMethod.kt index 29f9ea9a..bf58b0fd 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/HttpBaseMethod.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/HttpBaseMethod.kt @@ -40,12 +40,12 @@ import java.util.concurrent.TimeUnit abstract class HttpBaseMethod constructor(url: URL) { var httpUrl: HttpUrl = url.toHttpUrlOrNull() ?: throw MalformedURLException() var request: Request - private var _followPermanentRedirects = false + var followPermanentRedirects = false abstract var response: Response var call: Call? = null - var followRedirects: Boolean? = true - var retryOnConnectionFailure: Boolean? = false + var followRedirects: Boolean = true + var retryOnConnectionFailure: Boolean = false var connectionTimeoutVal: Long? = null var connectionTimeoutUnit: TimeUnit? = null var readTimeoutVal: Long? = null 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 afc88306..654f7143 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 @@ -62,10 +62,8 @@ class CheckPathExistenceRemoteOperation( setConnectionTimeout(TIMEOUT.toLong(), TimeUnit.SECONDS) } -<<<<<<< HEAD -======= propFindMethod.followRedirects = false ->>>>>>> 27ecf79c (remove redundant intercept of httpclient with httpmethod) + var status = client.executeHttpMethod(propFindMethod) /* PROPFIND method * 404 NOT FOUND: path doesn't exist, diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/oauth/GetOIDCDiscoveryRemoteOperation.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/oauth/GetOIDCDiscoveryRemoteOperation.kt index d2fae59e..c3f55dc9 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/oauth/GetOIDCDiscoveryRemoteOperation.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/oauth/GetOIDCDiscoveryRemoteOperation.kt @@ -55,7 +55,7 @@ class GetOIDCDiscoveryRemoteOperation : RemoteOperation() addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE) } - getMethod.setFollowRedirects(true) + getMethod.followRedirects = true val status = client.executeHttpMethod(getMethod) val responseBody = getMethod.getResponseBodyAsString() diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/status/StatusRequester.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/status/StatusRequester.kt index a63aec5a..9c246e9a 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/status/StatusRequester.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/status/StatusRequester.kt @@ -25,7 +25,6 @@ package com.owncloud.android.lib.resources.status import com.owncloud.android.lib.common.OwnCloudClient -import com.owncloud.android.lib.common.http.HttpClient import com.owncloud.android.lib.common.http.HttpConstants import com.owncloud.android.lib.common.http.methods.nonwebdav.GetMethod import com.owncloud.android.lib.common.operations.RemoteOperationResult @@ -64,7 +63,7 @@ internal class StatusRequester { return URL(oldLocationURL.protocol, oldLocationURL.host, oldLocationURL.port, redirectedLocation).toString() } - private fun getGetMethod(client: HttpClient, url: String): GetMethod { + private fun getGetMethod(url: String): GetMethod { return GetMethod(URL(url)).apply { setReadTimeout(TRY_CONNECTION_TIMEOUT, TimeUnit.SECONDS) setConnectionTimeout(TRY_CONNECTION_TIMEOUT, TimeUnit.SECONDS) @@ -80,15 +79,10 @@ internal class StatusRequester { fun request(baseLocation: String, client: OwnCloudClient): RequestResult { val currentLocation = baseLocation + OwnCloudClient.STATUS_PATH var status: Int - -<<<<<<< HEAD val getMethod = getGetMethod(currentLocation) - getMethod.setFollowPermanentRedirects(true) + + getMethod.followPermanentRedirects = true status = client.executeHttpMethod(getMethod) -======= - while (true) { - val getMethod = getGetMethod(client, currentLocation) ->>>>>>> 530f8644 (apply required fixes) return RequestResult(getMethod, status, getMethod.getFinalUrl().toString()) } diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/status/services/ServerInfoService.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/status/services/ServerInfoService.kt index 836bc13d..b1eb2871 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/status/services/ServerInfoService.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/status/services/ServerInfoService.kt @@ -29,7 +29,7 @@ import com.owncloud.android.lib.common.operations.RemoteOperationResult import com.owncloud.android.lib.resources.status.RemoteServerInfo interface ServerInfoService { - fun checkPathExistence(path: String, isUserLogged: Boolean, client: OwnCloudClient): RemoteOperationResult + fun checkPathExistence(path: String, isUserLoggedIn: Boolean, client: OwnCloudClient): RemoteOperationResult fun getRemoteStatus(path: String, client: OwnCloudClient): RemoteOperationResult } diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/status/services/implementation/OCServerInfoService.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/status/services/implementation/OCServerInfoService.kt index 732b7238..d2c891f0 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/status/services/implementation/OCServerInfoService.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/status/services/implementation/OCServerInfoService.kt @@ -38,7 +38,7 @@ class OCServerInfoService : ServerInfoService { override fun checkPathExistence( path: String, - isUserLogged: Boolean, + isUserLoggedIn: Boolean, client: OwnCloudClient ): RemoteOperationResult = CheckPathExistenceRemoteOperation( diff --git a/sample_client/src/main/java/com/owncloud/android/lib/sampleclient/MainActivity.java b/sample_client/src/main/java/com/owncloud/android/lib/sampleclient/MainActivity.java index 03ae360c..10128afa 100644 --- a/sample_client/src/main/java/com/owncloud/android/lib/sampleclient/MainActivity.java +++ b/sample_client/src/main/java/com/owncloud/android/lib/sampleclient/MainActivity.java @@ -37,14 +37,9 @@ import android.view.View; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; - -<<<<<<< HEAD import com.owncloud.android.lib.common.ConnectionValidator; import com.owncloud.android.lib.common.OwnCloudClient; -======= -import com.owncloud.android.lib.common.OwnCloudClient; import com.owncloud.android.lib.common.OwnCloudClientFactory; ->>>>>>> 5e555278 (get okhttp singleton removal changes to compile) import com.owncloud.android.lib.common.SingleSessionManager; import com.owncloud.android.lib.common.authentication.OwnCloudCredentialsFactory; import com.owncloud.android.lib.common.network.OnDatatransferProgressListener; @@ -55,13 +50,8 @@ import com.owncloud.android.lib.resources.files.DownloadRemoteFileOperation; import com.owncloud.android.lib.resources.files.ReadRemoteFolderOperation; import com.owncloud.android.lib.resources.files.RemoteFile; import com.owncloud.android.lib.resources.files.RemoveRemoteFileOperation; -import com.owncloud.android.lib.resources.files.UploadRemoteFileOperation; -<<<<<<< HEAD -======= import info.hannes.timber.DebugTree; ->>>>>>> 5e555278 (get okhttp singleton removal changes to compile) import timber.log.Timber; - import java.io.File; import java.io.FileOutputStream; import java.io.IOException; @@ -84,30 +74,20 @@ public class MainActivity extends Activity implements OnRemoteOperationListener, super.onCreate(savedInstanceState); setContentView(R.layout.main); -<<<<<<< HEAD - Timber.plant(); -======= Timber.plant(new DebugTree()); ->>>>>>> 5e555278 (get okhttp singleton removal changes to compile) mHandler = new Handler(); final Uri serverUri = Uri.parse(getString(R.string.server_base_url)); SingleSessionManager.setUserAgent(getUserAgent()); -<<<<<<< HEAD -<<<<<<< HEAD SingleSessionManager.setConnectionValidator(new ConnectionValidator(this, false)); mClient = new OwnCloudClient(serverUri, SingleSessionManager.getConnectionValidator(), true, SingleSessionManager.getDefaultSingleton()); -======= - mClient = OwnCloudClientFactory.createOwnCloudClient(serverUri, true); -======= - mClient = OwnCloudClientFactory.createOwnCloudClient(serverUri, this, true); ->>>>>>> 530f8644 (apply required fixes) ->>>>>>> 5e555278 (get okhttp singleton removal changes to compile) + mClient = OwnCloudClientFactory.createOwnCloudClient(serverUri, this, true); + mClient.setCredentials( OwnCloudCredentialsFactory.newBasicCredentials( getString(R.string.username),