From 10e53571895d95af67f1d1cfe04419605acc289b Mon Sep 17 00:00:00 2001 From: tobiasKaminsky Date: Thu, 5 Jan 2023 09:28:33 +0100 Subject: [PATCH 1/2] fix findings of detekt do no allow any issues on detekt Signed-off-by: tobiasKaminsky --- library/detekt.yml | 2 +- .../nextcloud/common/NextcloudUriDelegateIT.kt | 8 ++++---- .../lib/resources/shares/ShareXMLParserIT.kt | 1 + .../java/com/nextcloud/common/NextcloudClient.kt | 15 +++++++-------- .../java/com/nextcloud/common/OkHttpMethodBase.kt | 4 ++-- 5 files changed, 15 insertions(+), 15 deletions(-) diff --git a/library/detekt.yml b/library/detekt.yml index f4efaa121..110f92fc3 100644 --- a/library/detekt.yml +++ b/library/detekt.yml @@ -1,5 +1,5 @@ build: - maxIssues: 8 + maxIssues: 0 weights: # complexity: 2 # LongParameterList: 1 diff --git a/library/src/androidTest/java/com/nextcloud/common/NextcloudUriDelegateIT.kt b/library/src/androidTest/java/com/nextcloud/common/NextcloudUriDelegateIT.kt index 0cb081f5d..f1f60b83c 100644 --- a/library/src/androidTest/java/com/nextcloud/common/NextcloudUriDelegateIT.kt +++ b/library/src/androidTest/java/com/nextcloud/common/NextcloudUriDelegateIT.kt @@ -15,28 +15,28 @@ class NextcloudUriDelegateIT { } @Test - fun testFilesDavUri_leadingSlashInPath() { + fun testFilesDavUriLeadingSlashInPath() { val expected = "$EXPECTED_FILES_DAV/path/to/file.txt" val actual = sut.getFilesDavUri("/path/to/file.txt") Assert.assertEquals("Wrong URL", expected, actual) } @Test - fun testFilesDavUri_noLeadingSlashInPath() { + fun testFilesDavUriLoLeadingSlashInPath() { val expected = "$EXPECTED_FILES_DAV/path/to/file.txt" val actual = sut.getFilesDavUri("path/to/file.txt") Assert.assertEquals("Wrong URL", expected, actual) } @Test - fun testFilesDavUri_emptyPath() { + fun testFilesDavUriEmptyPath() { val expected = "$EXPECTED_FILES_DAV/" val actual = sut.getFilesDavUri("") Assert.assertEquals("Wrong URL", expected, actual) } @Test - fun testFilesDavUri_rootPath() { + fun testFilesDavUriRootPath() { val expected = "$EXPECTED_FILES_DAV/" val actual = sut.getFilesDavUri("/") Assert.assertEquals("Wrong URL", expected, actual) diff --git a/library/src/androidTest/java/com/owncloud/android/lib/resources/shares/ShareXMLParserIT.kt b/library/src/androidTest/java/com/owncloud/android/lib/resources/shares/ShareXMLParserIT.kt index ea746d2ab..e782b455d 100644 --- a/library/src/androidTest/java/com/owncloud/android/lib/resources/shares/ShareXMLParserIT.kt +++ b/library/src/androidTest/java/com/owncloud/android/lib/resources/shares/ShareXMLParserIT.kt @@ -30,6 +30,7 @@ import junit.framework.Assert.assertEquals import junit.framework.Assert.assertTrue import org.junit.Test +@Suppress("LargeClass", "LongMethod") class ShareXMLParserIT { @Test fun testOCShareResponse() { diff --git a/library/src/main/java/com/nextcloud/common/NextcloudClient.kt b/library/src/main/java/com/nextcloud/common/NextcloudClient.kt index d2359395f..4d2eba553 100644 --- a/library/src/main/java/com/nextcloud/common/NextcloudClient.kt +++ b/library/src/main/java/com/nextcloud/common/NextcloudClient.kt @@ -124,6 +124,7 @@ class NextcloudClient private constructor( } @Throws(IOException::class) + @Suppress("NestedBlockDepth") fun followRedirection(method: OkHttpMethodBase): RedirectionPath { var redirectionsCount = 0 var status = method.getStatusCode() @@ -133,10 +134,9 @@ class NextcloudClient private constructor( status == HttpStatus.SC_MOVED_TEMPORARILY || status == HttpStatus.SC_TEMPORARY_REDIRECT while (redirectionsCount < OwnCloudClient.MAX_REDIRECTIONS_COUNT && statusIsRedirection) { - var location = method.getResponseHeader("Location") - if (location == null) { - location = method.getResponseHeader("location") - } + val location = method.getResponseHeader("Location") + ?: method.getResponseHeader("location") + if (location != null) { Log_OC.d(TAG, "Location to redirect: $location") result.addLocation(location) @@ -145,10 +145,7 @@ class NextcloudClient private constructor( method.releaseConnection() method.uri = location var destination = method.getRequestHeader("Destination") - - if (destination == null) { - destination = method.getRequestHeader("destination") - } + ?: method.getRequestHeader("destination") if (destination != null) { val suffixIndex = location.lastIndexOf(AccountUtils.WEBDAV_PATH_9_0) @@ -164,12 +161,14 @@ class NextcloudClient private constructor( method.addRequestHeader("Destination", destination) } } + status = method.execute(this) result.addStatus(status) redirectionsCount++ } else { Log_OC.d(TAG, "No location to redirect!") status = HttpStatus.SC_NOT_FOUND + result.addStatus(status) } } return result diff --git a/library/src/main/java/com/nextcloud/common/OkHttpMethodBase.kt b/library/src/main/java/com/nextcloud/common/OkHttpMethodBase.kt index 16a71d730..18c977cfa 100644 --- a/library/src/main/java/com/nextcloud/common/OkHttpMethodBase.kt +++ b/library/src/main/java/com/nextcloud/common/OkHttpMethodBase.kt @@ -124,8 +124,8 @@ abstract class OkHttpMethodBase( return response?.header(name) } - fun getRequestHeader(name: String): String { - return request?.header(name) ?: "" + fun getRequestHeader(name: String): String? { + return request?.header(name) } /** From ac81c09b34771f1e8531ce37fb4923f80b7b8d1b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20Brey?= Date: Mon, 9 Jan 2023 13:13:04 +0100 Subject: [PATCH 2/2] NextcloudClient: reduce nested block depth instead of suppressing it MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Álvaro Brey --- .../com/nextcloud/common/NextcloudClient.kt | 33 +++++++++++-------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/library/src/main/java/com/nextcloud/common/NextcloudClient.kt b/library/src/main/java/com/nextcloud/common/NextcloudClient.kt index 4d2eba553..16eb9588d 100644 --- a/library/src/main/java/com/nextcloud/common/NextcloudClient.kt +++ b/library/src/main/java/com/nextcloud/common/NextcloudClient.kt @@ -124,7 +124,6 @@ class NextcloudClient private constructor( } @Throws(IOException::class) - @Suppress("NestedBlockDepth") fun followRedirection(method: OkHttpMethodBase): RedirectionPath { var redirectionsCount = 0 var status = method.getStatusCode() @@ -144,22 +143,11 @@ class NextcloudClient private constructor( // due to it will be set a different url method.releaseConnection() method.uri = location - var destination = method.getRequestHeader("Destination") + val destination = method.getRequestHeader("Destination") ?: method.getRequestHeader("destination") if (destination != null) { - val suffixIndex = location.lastIndexOf(AccountUtils.WEBDAV_PATH_9_0) - val redirectionBase = location.substring(0, suffixIndex) - val destinationStr = destination - val destinationPath = destinationStr.substring(baseUri.toString().length) - val redirectedDestination = redirectionBase + destinationPath - destination = redirectedDestination - - if (method.getRequestHeader("Destination").isNullOrEmpty()) { - method.addRequestHeader("destination", destination) - } else { - method.addRequestHeader("Destination", destination) - } + setRedirectedDestinationHeader(method, location, destination) } status = method.execute(this) @@ -174,6 +162,23 @@ class NextcloudClient private constructor( return result } + private fun setRedirectedDestinationHeader( + method: OkHttpMethodBase, + location: String, + destination: String + ) { + val suffixIndex = location.lastIndexOf(AccountUtils.WEBDAV_PATH_9_0) + val redirectionBase = location.substring(0, suffixIndex) + val destinationPath = destination.substring(baseUri.toString().length) + val redirectedDestination = redirectionBase + destinationPath + + if (method.getRequestHeader("Destination").isNullOrEmpty()) { + method.addRequestHeader("destination", redirectedDestination) + } else { + method.addRequestHeader("Destination", redirectedDestination) + } + } + fun getUserIdEncoded(): String { return delegate.userIdEncoded!! }