From 390ef9fbd7ca42120b92b1741563e5ca3e305491 Mon Sep 17 00:00:00 2001 From: agarcia Date: Wed, 1 Jul 2020 10:41:21 +0200 Subject: [PATCH 01/15] Update versionCode and versionName --- owncloudComLibrary/build.gradle | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/owncloudComLibrary/build.gradle b/owncloudComLibrary/build.gradle index 4a16b6ba..3559bbf3 100644 --- a/owncloudComLibrary/build.gradle +++ b/owncloudComLibrary/build.gradle @@ -29,8 +29,8 @@ android { minSdkVersion 21 targetSdkVersion 29 - versionCode = 10000500 - versionName = "1.0.5" + versionCode = 10000600 + versionName = "1.0.6" // This is pretty ugly but manifest placeholders don't seem to work very well when using different modules // See https://github.com/openid/AppAuth-Android/issues/325 From ffe7d9d8e334f081fa0a4b20e3d4ebc8b78954b3 Mon Sep 17 00:00:00 2001 From: agarcia Date: Fri, 19 Jun 2020 14:27:00 +0200 Subject: [PATCH 02/15] Update dav4android and okttp versions to 4.6.0 --- README.md | 4 ++-- owncloudComLibrary/build.gradle | 4 ++-- .../android/lib/common/OwnCloudClient.java | 2 +- .../OwnCloudBasicCredentials.java | 5 +++-- .../common/http/methods/webdav/DavMethod.java | 20 +++++++++---------- .../common/http/methods/webdav/DavUtils.java | 4 ++-- .../http/methods/webdav/PropfindMethod.java | 6 +++--- .../common/http/methods/webdav/PutMethod.java | 2 +- .../operations/RemoteOperationResult.java | 4 ++-- .../CheckPathExistenceRemoteOperation.kt | 4 ++-- .../files/ReadRemoteFolderOperation.java | 2 +- .../lib/resources/files/RemoteFile.java | 16 +++------------ .../lib/resources/files/RemoteFileUtil.kt | 2 +- 13 files changed, 33 insertions(+), 42 deletions(-) diff --git a/README.md b/README.md index 7a29cb82..b86531b1 100644 --- a/README.md +++ b/README.md @@ -44,11 +44,11 @@ ownCloud Android Library is available under MIT license. See the file LICENSE.md #### Third party libraries -ownCloud Android Library uses OkHttp version 3.10, licensed under Apache License and version 2.0. Besides, it uses Dav4Android, licensed under Mozilla Public License, v. 2.0 +ownCloud Android Library uses OkHttp version 4.6.0, licensed under Apache License and version 2.0. Besides, it uses Dav4Android, licensed under Mozilla Public License, v. 2.0 ### Compatibility -ownCloud Android Library is valid for Android systems from version Android 2.2 (android:minSdkVersion="8" android:targetSdkVersion="19"). +ownCloud Android Library is valid for Android systems from version Android 5 (android:minSdkVersion="21" android:targetSdkVersion="29"). ownCloud Android library supports ownCloud server from version 4.5. diff --git a/owncloudComLibrary/build.gradle b/owncloudComLibrary/build.gradle index 3559bbf3..a75309e4 100644 --- a/owncloudComLibrary/build.gradle +++ b/owncloudComLibrary/build.gradle @@ -4,9 +4,9 @@ apply plugin: 'kotlin-kapt' apply plugin: 'kotlin-allopen' dependencies { - api 'com.squareup.okhttp3:okhttp:3.12.0' + api 'com.squareup.okhttp3:okhttp:4.6.0' implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlinVersion" - api 'com.gitlab.ownclouders:dav4android:oc_support_1.0.1' + api 'com.gitlab.ownclouders:dav4android:oc_support_2.1' api 'com.github.hannesa2:Logcat:1.6.0' api 'net.openid:appauth:0.7.1' 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 3ab812d4..7e2691d3 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 @@ -29,7 +29,7 @@ import android.accounts.AccountManager; import android.accounts.AccountsException; import android.net.Uri; -import at.bitfire.dav4android.exception.HttpException; +import at.bitfire.dav4jvm.exception.HttpException; import com.owncloud.android.lib.common.accounts.AccountUtils; import com.owncloud.android.lib.common.authentication.OwnCloudCredentials; import com.owncloud.android.lib.common.authentication.OwnCloudCredentialsFactory; diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/authentication/OwnCloudBasicCredentials.java b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/authentication/OwnCloudBasicCredentials.java index af3e9d75..bde053ce 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/authentication/OwnCloudBasicCredentials.java +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/authentication/OwnCloudBasicCredentials.java @@ -27,7 +27,8 @@ import com.owncloud.android.lib.common.OwnCloudClient; import com.owncloud.android.lib.common.http.HttpClient; import com.owncloud.android.lib.common.http.HttpConstants; import okhttp3.Credentials; -import okhttp3.internal.Util; + +import static java.nio.charset.StandardCharsets.UTF_8; public class OwnCloudBasicCredentials implements OwnCloudCredentials { @@ -51,7 +52,7 @@ public class OwnCloudBasicCredentials implements OwnCloudCredentials { HttpClient.deleteHeaderForAllRequests(HttpConstants.COOKIE_HEADER); HttpClient.addHeaderForAllRequests(HttpConstants.AUTHORIZATION_HEADER, - Credentials.basic(mUsername, mPassword, Util.UTF_8)); + Credentials.basic(mUsername, mPassword, UTF_8)); } @Override diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/DavMethod.java b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/DavMethod.java index 4255e1a6..1334e327 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/DavMethod.java +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/DavMethod.java @@ -24,10 +24,10 @@ package com.owncloud.android.lib.common.http.methods.webdav; -import at.bitfire.dav4android.Constants; -import at.bitfire.dav4android.DavOCResource; -import at.bitfire.dav4android.exception.HttpException; -import at.bitfire.dav4android.exception.RedirectException; +import at.bitfire.dav4jvm.Dav4jvm; +import at.bitfire.dav4jvm.DavOCResource; +import at.bitfire.dav4jvm.exception.HttpException; +import at.bitfire.dav4jvm.exception.RedirectException; import com.owncloud.android.lib.common.http.HttpConstants; import com.owncloud.android.lib.common.http.methods.HttpBaseMethod; import okhttp3.HttpUrl; @@ -52,7 +52,7 @@ public abstract class DavMethod extends HttpBaseMethod { mDavResource = new DavOCResource( mOkHttpClient, HttpUrl.parse(url.toString()), - Constants.INSTANCE.getLog()); + Dav4jvm.INSTANCE.getLog()); } @Override @@ -107,7 +107,7 @@ public abstract class DavMethod extends HttpBaseMethod { mDavResource = new DavOCResource( mOkHttpClient, HttpUrl.parse(mRequest.url().toString()), - Constants.INSTANCE.getLog()); + Dav4jvm.INSTANCE.getLog()); } @Override @@ -116,7 +116,7 @@ public abstract class DavMethod extends HttpBaseMethod { mDavResource = new DavOCResource( mOkHttpClient, HttpUrl.parse(mRequest.url().toString()), - Constants.INSTANCE.getLog()); + Dav4jvm.INSTANCE.getLog()); } @Override @@ -125,7 +125,7 @@ public abstract class DavMethod extends HttpBaseMethod { mDavResource = new DavOCResource( mOkHttpClient, HttpUrl.parse(mRequest.url().toString()), - Constants.INSTANCE.getLog()); + Dav4jvm.INSTANCE.getLog()); } @Override @@ -134,7 +134,7 @@ public abstract class DavMethod extends HttpBaseMethod { mDavResource = new DavOCResource( mOkHttpClient, HttpUrl.parse(mRequest.url().toString()), - Constants.INSTANCE.getLog()); + Dav4jvm.INSTANCE.getLog()); } @Override @@ -152,7 +152,7 @@ public abstract class DavMethod extends HttpBaseMethod { mDavResource = new DavOCResource( mOkHttpClient, HttpUrl.parse(mRequest.url().toString()), - Constants.INSTANCE.getLog()); + Dav4jvm.INSTANCE.getLog()); } @Override diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/DavUtils.java b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/DavUtils.java index 484c4ec1..abeca753 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/DavUtils.java +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/DavUtils.java @@ -1,7 +1,7 @@ package com.owncloud.android.lib.common.http.methods.webdav; -import at.bitfire.dav4android.Property; -import at.bitfire.dav4android.PropertyUtils; +import at.bitfire.dav4jvm.Property; +import at.bitfire.dav4jvm.PropertyUtils; public class DavUtils { diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/PropfindMethod.java b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/PropfindMethod.java index 6a48fc80..319b051c 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/PropfindMethod.java +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/PropfindMethod.java @@ -24,9 +24,9 @@ package com.owncloud.android.lib.common.http.methods.webdav; -import at.bitfire.dav4android.Property; -import at.bitfire.dav4android.Response; -import at.bitfire.dav4android.exception.DavException; +import at.bitfire.dav4jvm.Property; +import at.bitfire.dav4jvm.Response; +import at.bitfire.dav4jvm.exception.DavException; import kotlin.Unit; import java.io.IOException; diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/PutMethod.java b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/PutMethod.java index 243a4d5a..cb93bc87 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/PutMethod.java +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/PutMethod.java @@ -24,7 +24,7 @@ package com.owncloud.android.lib.common.http.methods.webdav; -import at.bitfire.dav4android.exception.HttpException; +import at.bitfire.dav4jvm.exception.HttpException; import com.owncloud.android.lib.common.http.HttpConstants; import kotlin.Unit; diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/operations/RemoteOperationResult.java b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/operations/RemoteOperationResult.java index 715ed32c..64386964 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/operations/RemoteOperationResult.java +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/operations/RemoteOperationResult.java @@ -27,8 +27,8 @@ package com.owncloud.android.lib.common.operations; import android.accounts.Account; import android.accounts.AccountsException; -import at.bitfire.dav4android.exception.DavException; -import at.bitfire.dav4android.exception.HttpException; +import at.bitfire.dav4jvm.exception.DavException; +import at.bitfire.dav4jvm.exception.HttpException; import com.owncloud.android.lib.common.accounts.AccountUtils; import com.owncloud.android.lib.common.http.HttpConstants; import com.owncloud.android.lib.common.http.methods.HttpBaseMethod; 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 aafa5fc7..e419cca4 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 @@ -25,7 +25,7 @@ package com.owncloud.android.lib.resources.files import com.owncloud.android.lib.common.OwnCloudClient import com.owncloud.android.lib.common.http.HttpConstants -import com.owncloud.android.lib.common.http.methods.webdav.DavUtils +import com.owncloud.android.lib.common.http.methods.webdav.DavUtils.getAllPropset import com.owncloud.android.lib.common.http.methods.webdav.PropfindMethod import com.owncloud.android.lib.common.network.RedirectionPath import com.owncloud.android.lib.common.network.WebdavUtils @@ -66,7 +66,7 @@ class CheckPathExistenceRemoteOperation( if (isUserLogged) client.baseFilesWebDavUri.toString() else client.userFilesWebDavUri.toString() + WebdavUtils.encodePath(remotePath) - val propFindMethod = PropfindMethod(URL(stringUrl), 0, DavUtils.getAllPropset()).apply { + val propFindMethod = PropfindMethod(URL(stringUrl), 0, getAllPropset()).apply { setReadTimeout(TIMEOUT.toLong(), TimeUnit.SECONDS) setConnectionTimeout(TIMEOUT.toLong(), TimeUnit.SECONDS) } diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/ReadRemoteFolderOperation.java b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/ReadRemoteFolderOperation.java index 730bd86b..838cfa00 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/ReadRemoteFolderOperation.java +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/ReadRemoteFolderOperation.java @@ -24,7 +24,7 @@ package com.owncloud.android.lib.resources.files; -import at.bitfire.dav4android.Response; +import at.bitfire.dav4jvm.Response; import com.owncloud.android.lib.common.OwnCloudClient; import com.owncloud.android.lib.common.accounts.AccountUtils; import com.owncloud.android.lib.common.http.HttpConstants; diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/RemoteFile.java b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/RemoteFile.java index d52ec1d8..cb390993 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/RemoteFile.java +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/RemoteFile.java @@ -27,19 +27,9 @@ package com.owncloud.android.lib.resources.files; import android.os.Parcel; import android.os.Parcelable; -import at.bitfire.dav4android.Property; -import at.bitfire.dav4android.Response; -import at.bitfire.dav4android.property.CreationDate; -import at.bitfire.dav4android.property.GetContentLength; -import at.bitfire.dav4android.property.GetContentType; -import at.bitfire.dav4android.property.GetETag; -import at.bitfire.dav4android.property.GetLastModified; -import at.bitfire.dav4android.property.QuotaAvailableBytes; -import at.bitfire.dav4android.property.QuotaUsedBytes; -import at.bitfire.dav4android.property.owncloud.OCId; -import at.bitfire.dav4android.property.owncloud.OCPermissions; -import at.bitfire.dav4android.property.owncloud.OCPrivatelink; -import at.bitfire.dav4android.property.owncloud.OCSize; +import at.bitfire.dav4jvm.Property; +import at.bitfire.dav4jvm.Response; +import at.bitfire.dav4jvm.property.*; import java.io.File; import java.io.Serializable; diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/RemoteFileUtil.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/RemoteFileUtil.kt index ace0675b..30550e70 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/RemoteFileUtil.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/RemoteFileUtil.kt @@ -41,7 +41,7 @@ class RemoteFileUtil { */ fun getRemotePathFromUrl(url: HttpUrl, userId: String): String? { val davFilesPath = OwnCloudClient.WEBDAV_FILES_PATH_4_0 + userId - val absoluteDavPath = Uri.decode(url.encodedPath()) + val absoluteDavPath = Uri.decode(url.encodedPath) val pathToOc = absoluteDavPath.split(davFilesPath)[0] return absoluteDavPath.replace(pathToOc + davFilesPath, "") } From f93887732c5c530c945d03e7174433d3bf4ef5a8 Mon Sep 17 00:00:00 2001 From: agarcia Date: Wed, 1 Jul 2020 11:03:20 +0200 Subject: [PATCH 03/15] Fix path dav4android -> dav4jvm --- .../lib/resources/users/GetRemoteUserQuotaOperation.kt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/users/GetRemoteUserQuotaOperation.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/users/GetRemoteUserQuotaOperation.kt index 4e6bd84a..de0999ae 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/users/GetRemoteUserQuotaOperation.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/users/GetRemoteUserQuotaOperation.kt @@ -26,9 +26,9 @@ */ package com.owncloud.android.lib.resources.users -import at.bitfire.dav4android.Property -import at.bitfire.dav4android.property.QuotaAvailableBytes -import at.bitfire.dav4android.property.QuotaUsedBytes +import at.bitfire.dav4jvm.Property +import at.bitfire.dav4jvm.property.QuotaAvailableBytes +import at.bitfire.dav4jvm.property.QuotaUsedBytes import com.owncloud.android.lib.common.OwnCloudClient import com.owncloud.android.lib.common.http.HttpConstants import com.owncloud.android.lib.common.http.methods.webdav.DavConstants From c5fd59c82595044ed323fdcedaf356d4930edcd1 Mon Sep 17 00:00:00 2001 From: agarcia Date: Tue, 7 Jul 2020 12:08:09 +0200 Subject: [PATCH 04/15] Add support for version.hide config --- .../owncloud/android/lib/resources/status/OwnCloudVersion.kt | 3 +++ 1 file changed, 3 insertions(+) diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/status/OwnCloudVersion.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/status/OwnCloudVersion.kt index c8c75653..c73805b8 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/status/OwnCloudVersion.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/status/OwnCloudVersion.kt @@ -38,6 +38,8 @@ class OwnCloudVersion(version: String) : Comparable, Parcelable var isVersionValid: Boolean = false set + var isVersionHidden: Boolean = false + val version: String get() = if (isVersionValid) { toString() @@ -55,6 +57,7 @@ class OwnCloudVersion(version: String) : Comparable, Parcelable var versionToParse = version mVersion = 0 isVersionValid = false + isVersionHidden = version.isBlank() val countDots = versionToParse.length - versionToParse.replace(".", "").length // Complete the version. Version must have 3 dots From 7f2d94bc7842ea3cc59a134f89fc474612db1caf Mon Sep 17 00:00:00 2001 From: agarcia Date: Thu, 25 Jun 2020 21:59:05 +0200 Subject: [PATCH 05/15] Migrate webdav wrappers to kotlin --- .../webdav/{CopyMethod.java => CopyMethod.kt} | 39 +++----- .../{DavConstants.java => DavConstants.kt} | 9 +- .../{MkColMethod.java => MkColMethod.kt} | 30 +++--- .../webdav/{MoveMethod.java => MoveMethod.kt} | 50 +++++----- .../http/methods/webdav/PropfindMethod.java | 94 ------------------- .../http/methods/webdav/PropfindMethod.kt | 78 +++++++++++++++ .../webdav/{PutMethod.java => PutMethod.kt} | 47 ++++------ .../files/ReadRemoteFileOperation.java | 1 - .../files/ReadRemoteFolderOperation.java | 1 - 9 files changed, 148 insertions(+), 201 deletions(-) rename owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/{CopyMethod.java => CopyMethod.kt} (66%) rename owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/{DavConstants.java => DavConstants.kt} (87%) rename owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/{MkColMethod.java => MkColMethod.kt} (75%) rename owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/{MoveMethod.java => MoveMethod.kt} (59%) delete mode 100644 owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/PropfindMethod.java create mode 100644 owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/PropfindMethod.kt rename owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/{PutMethod.java => PutMethod.kt} (59%) diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/CopyMethod.java b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/CopyMethod.kt similarity index 66% rename from owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/CopyMethod.java rename to owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/CopyMethod.kt index 718f948c..a49f7b6a 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/CopyMethod.java +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/CopyMethod.kt @@ -21,12 +21,10 @@ * THE SOFTWARE. * */ +package com.owncloud.android.lib.common.http.methods.webdav -package com.owncloud.android.lib.common.http.methods.webdav; - -import kotlin.Unit; - -import java.net.URL; +import okhttp3.Response +import java.net.URL /** * Copy calls wrapper @@ -34,24 +32,17 @@ import java.net.URL; * @author Christian Schabesberger * @author David González Verdugo */ -public class CopyMethod extends DavMethod { - - final String destinationUrl; - final boolean forceOverride; - - public CopyMethod(URL url, String destinationUrl, boolean forceOverride) { - super(url); - this.destinationUrl = destinationUrl; - this.forceOverride = forceOverride; +class CopyMethod( + val url: URL?, + private val destinationUrl: String, + private val forceOverride: Boolean +) : DavMethod(url) { + @Throws(Exception::class) + public override fun onExecute(): Int { + mDavResource.copy(destinationUrl, forceOverride) { response: Response -> + mResponse = response + } + return super.getStatusCode() } - @Override - public int onExecute() throws Exception { - mDavResource.copy(destinationUrl, forceOverride, response -> { - mResponse = response; - return Unit.INSTANCE; - }); - - return super.getStatusCode(); - } -} \ No newline at end of file +} diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/DavConstants.java b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/DavConstants.kt similarity index 87% rename from owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/DavConstants.java rename to owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/DavConstants.kt index 67fd90ba..03e36bdb 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/DavConstants.java +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/DavConstants.kt @@ -21,13 +21,12 @@ * THE SOFTWARE. * */ - -package com.owncloud.android.lib.common.http.methods.webdav; +package com.owncloud.android.lib.common.http.methods.webdav /** * @author David González Verdugo */ -public class DavConstants { - public static final int DEPTH_0 = 0; - public static final int DEPTH_1 = 1; +object DavConstants { + const val DEPTH_0 = 0 + const val DEPTH_1 = 1 } diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/MkColMethod.java b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/MkColMethod.kt similarity index 75% rename from owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/MkColMethod.java rename to owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/MkColMethod.kt index 9c4a3e34..ddcd85c2 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/MkColMethod.java +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/MkColMethod.kt @@ -21,12 +21,10 @@ * THE SOFTWARE. * */ +package com.owncloud.android.lib.common.http.methods.webdav -package com.owncloud.android.lib.common.http.methods.webdav; - -import kotlin.Unit; - -import java.net.URL; +import okhttp3.Response +import java.net.URL /** * MkCol calls wrapper @@ -34,18 +32,12 @@ import java.net.URL; * @author Christian Schabesberger * @author David González Verdugo */ -public class MkColMethod extends DavMethod { - public MkColMethod(URL url) { - super(url); +class MkColMethod(url: URL?) : DavMethod(url) { + @Throws(Exception::class) + public override fun onExecute(): Int { + mDavResource.mkCol(null) { response: Response -> + mResponse = response + } + return super.getStatusCode() } - - @Override - public int onExecute() throws Exception { - mDavResource.mkCol(null, response -> { - mResponse = response; - return Unit.INSTANCE; - }); - - return super.getStatusCode(); - } -} \ No newline at end of file +} diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/MoveMethod.java b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/MoveMethod.kt similarity index 59% rename from owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/MoveMethod.java rename to owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/MoveMethod.kt index a57a86b1..d2c45cfa 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/MoveMethod.java +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/MoveMethod.kt @@ -21,13 +21,11 @@ * THE SOFTWARE. * */ +package com.owncloud.android.lib.common.http.methods.webdav -package com.owncloud.android.lib.common.http.methods.webdav; - -import com.owncloud.android.lib.common.http.HttpConstants; -import kotlin.Unit; - -import java.net.URL; +import com.owncloud.android.lib.common.http.HttpConstants +import okhttp3.Response +import java.net.URL /** * Move calls wrapper @@ -35,27 +33,23 @@ import java.net.URL; * @author Christian Schabesberger * @author David González Verdugo */ -public class MoveMethod extends DavMethod { - final String destinationUrl; - final boolean forceOverride; - - public MoveMethod(URL url, String destinationUrl, boolean forceOverride) { - super(url); - this.destinationUrl = destinationUrl; - this.forceOverride = forceOverride; - } - - @Override - public int onExecute() throws Exception { +class MoveMethod( + url: URL?, + private val destinationUrl: String, + private val forceOverride: Boolean +) : + DavMethod(url) { + @Throws(Exception::class) + public override fun onExecute(): Int { mDavResource.move( - destinationUrl, - forceOverride, - super.getRequestHeader(HttpConstants.OC_TOTAL_LENGTH_HEADER), - super.getRequestHeader(HttpConstants.OC_X_OC_MTIME_HEADER), response -> { - mResponse = response; - return Unit.INSTANCE; - }); - - return super.getStatusCode(); + destinationUrl, + forceOverride, + super.getRequestHeader(HttpConstants.OC_TOTAL_LENGTH_HEADER), + super.getRequestHeader(HttpConstants.OC_X_OC_MTIME_HEADER) + ) { response: Response -> + mResponse = response + } + return super.getStatusCode() } -} \ No newline at end of file + +} diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/PropfindMethod.java b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/PropfindMethod.java deleted file mode 100644 index 319b051c..00000000 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/PropfindMethod.java +++ /dev/null @@ -1,94 +0,0 @@ -/* 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.common.http.methods.webdav; - -import at.bitfire.dav4jvm.Property; -import at.bitfire.dav4jvm.Response; -import at.bitfire.dav4jvm.exception.DavException; -import kotlin.Unit; - -import java.io.IOException; -import java.net.URL; -import java.util.ArrayList; -import java.util.List; - -/** - * Propfind calls wrapper - * - * @author David González Verdugo - */ -public class PropfindMethod extends DavMethod { - - // request - private final int mDepth; - private final Property.Name[] mPropertiesToRequest; - - // response - private final List mMembers; - private Response mRoot; - - public PropfindMethod(URL url, int depth, Property.Name[] propertiesToRequest) { - super(url); - mDepth = depth; - mPropertiesToRequest = propertiesToRequest; - mMembers = new ArrayList<>(); - mRoot = null; - } - - @Override - public int onExecute() throws IOException, DavException { - mDavResource.propfind(mDepth, mPropertiesToRequest, - (Response response, Response.HrefRelation hrefRelation) -> { - switch (hrefRelation) { - case MEMBER: - mMembers.add(response); - break; - case SELF: - mRoot = response; - break; - case OTHER: - default: - } - return Unit.INSTANCE; - }, response -> { - mResponse = response; - return Unit.INSTANCE; - }); - - return getStatusCode(); - } - - public int getDepth() { - return mDepth; - } - - public List getMembers() { - return mMembers; - } - - public Response getRoot() { - return mRoot; - } -} \ No newline at end of file diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/PropfindMethod.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/PropfindMethod.kt new file mode 100644 index 00000000..2a2913c0 --- /dev/null +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/PropfindMethod.kt @@ -0,0 +1,78 @@ +/* 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.common.http.methods.webdav + +import at.bitfire.dav4android.Property +import at.bitfire.dav4android.Response +import at.bitfire.dav4android.Response.HrefRelation +import at.bitfire.dav4android.exception.DavException +import java.io.IOException +import java.net.URL +import java.util.ArrayList + +/** + * Propfind calls wrapper + * + * @author David González Verdugo + */ +class PropfindMethod( + url: URL?, + // request + val depth: Int, + private val mPropertiesToRequest: Array +) : DavMethod(url) { + + // response + private val mMembers: MutableList + var root: Response? + private set + + @Throws(IOException::class, DavException::class) + public override fun onExecute(): Int { + mDavResource.propfind( + depth = depth, + reqProp = *mPropertiesToRequest, + callback = { response: Response, hrefRelation: HrefRelation? -> + when (hrefRelation) { + HrefRelation.MEMBER -> mMembers.add(response) + HrefRelation.SELF -> this.root = response + HrefRelation.OTHER -> { + } + else -> { + } + } + }, rawCallback = { response: okhttp3.Response -> + mResponse = response + }) + return statusCode + } + + val members: List + get() = mMembers + + init { + mMembers = ArrayList() + this.root = null + } +} diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/PutMethod.java b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/PutMethod.kt similarity index 59% rename from owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/PutMethod.java rename to owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/PutMethod.kt index cb93bc87..55736a6e 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/PutMethod.java +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/PutMethod.kt @@ -21,41 +21,30 @@ * THE SOFTWARE. * */ +package com.owncloud.android.lib.common.http.methods.webdav -package com.owncloud.android.lib.common.http.methods.webdav; - -import at.bitfire.dav4jvm.exception.HttpException; -import com.owncloud.android.lib.common.http.HttpConstants; -import kotlin.Unit; - -import java.io.IOException; -import java.net.URL; +import at.bitfire.dav4android.exception.HttpException +import com.owncloud.android.lib.common.http.HttpConstants +import java.io.IOException +import java.net.URL /** * Put calls wrapper * * @author David González Verdugo */ -public class PutMethod extends DavMethod { - - public PutMethod(URL url) { - super(url); - } - - ; - - @Override - public int onExecute() throws IOException, HttpException { +class PutMethod(url: URL?) : DavMethod(url) { + @Throws(IOException::class, HttpException::class) + public override fun onExecute(): Int { mDavResource.put( - mRequestBody, - super.getRequestHeader(HttpConstants.IF_MATCH_HEADER), - super.getRequestHeader(HttpConstants.CONTENT_TYPE_HEADER), - super.getRequestHeader(HttpConstants.OC_TOTAL_LENGTH_HEADER), - super.getRequestHeader(HttpConstants.OC_X_OC_MTIME_HEADER), response -> { - mResponse = response; - return Unit.INSTANCE; - }); - - return super.getStatusCode(); + mRequestBody, + super.getRequestHeader(HttpConstants.IF_MATCH_HEADER), + super.getRequestHeader(HttpConstants.CONTENT_TYPE_HEADER), + super.getRequestHeader(HttpConstants.OC_TOTAL_LENGTH_HEADER), + super.getRequestHeader(HttpConstants.OC_X_OC_MTIME_HEADER) + ) { response: Response -> + mResponse = response + } + return super.getStatusCode() } -} \ No newline at end of file +} diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/ReadRemoteFileOperation.java b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/ReadRemoteFileOperation.java index 9149e6d3..64c4481e 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/ReadRemoteFileOperation.java +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/ReadRemoteFileOperation.java @@ -27,7 +27,6 @@ import com.owncloud.android.lib.common.OwnCloudClient; import com.owncloud.android.lib.common.accounts.AccountUtils; import com.owncloud.android.lib.common.http.HttpConstants; import com.owncloud.android.lib.common.http.methods.webdav.DavUtils; -import com.owncloud.android.lib.common.http.methods.webdav.PropfindMethod; import com.owncloud.android.lib.common.network.WebdavUtils; import com.owncloud.android.lib.common.operations.RemoteOperation; import com.owncloud.android.lib.common.operations.RemoteOperationResult; diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/ReadRemoteFolderOperation.java b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/ReadRemoteFolderOperation.java index 838cfa00..f77c68c9 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/ReadRemoteFolderOperation.java +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/ReadRemoteFolderOperation.java @@ -30,7 +30,6 @@ import com.owncloud.android.lib.common.accounts.AccountUtils; import com.owncloud.android.lib.common.http.HttpConstants; import com.owncloud.android.lib.common.http.methods.webdav.DavConstants; import com.owncloud.android.lib.common.http.methods.webdav.DavUtils; -import com.owncloud.android.lib.common.http.methods.webdav.PropfindMethod; import com.owncloud.android.lib.common.network.WebdavUtils; import com.owncloud.android.lib.common.operations.RemoteOperation; import com.owncloud.android.lib.common.operations.RemoteOperationResult; From aebd7288cd59f76536b92fb95be36ef2ba1ce0d1 Mon Sep 17 00:00:00 2001 From: agarcia Date: Fri, 26 Jun 2020 00:04:48 +0200 Subject: [PATCH 06/15] Migrate DavUtils to kotlin and fix a compilation error --- .../common/http/methods/webdav/DavUtils.java | 15 -------- .../common/http/methods/webdav/DavUtils.kt | 36 +++++++++++++++++++ .../CheckPathExistenceRemoteOperation.kt | 4 +-- .../files/ReadRemoteFileOperation.java | 1 - .../files/ReadRemoteFolderOperation.java | 1 - .../users/GetRemoteUserQuotaOperation.kt | 13 +++++-- 6 files changed, 48 insertions(+), 22 deletions(-) delete mode 100644 owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/DavUtils.java create mode 100644 owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/DavUtils.kt diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/DavUtils.java b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/DavUtils.java deleted file mode 100644 index abeca753..00000000 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/DavUtils.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.owncloud.android.lib.common.http.methods.webdav; - -import at.bitfire.dav4jvm.Property; -import at.bitfire.dav4jvm.PropertyUtils; - -public class DavUtils { - - public static final Property.Name[] getAllPropset() { - return PropertyUtils.INSTANCE.getAllPropSet(); - } - - public static final Property.Name[] getQuotaPropSet() { - return PropertyUtils.INSTANCE.getQuotaPropset(); - } -} \ No newline at end of file diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/DavUtils.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/DavUtils.kt new file mode 100644 index 00000000..6da28798 --- /dev/null +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/DavUtils.kt @@ -0,0 +1,36 @@ +/* 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.common.http.methods.webdav + +import at.bitfire.dav4android.Property +import at.bitfire.dav4android.PropertyUtils.getAllPropSet +import at.bitfire.dav4android.PropertyUtils.getQuotaPropset + +object DavUtils { + @JvmStatic val allPropset: Array + get() = getAllPropSet() + + val quotaPropSet: Array + get() = getQuotaPropset() +} 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 e419cca4..f3da9a72 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 @@ -25,7 +25,7 @@ package com.owncloud.android.lib.resources.files import com.owncloud.android.lib.common.OwnCloudClient import com.owncloud.android.lib.common.http.HttpConstants -import com.owncloud.android.lib.common.http.methods.webdav.DavUtils.getAllPropset +import com.owncloud.android.lib.common.http.methods.webdav.DavUtils.allPropset import com.owncloud.android.lib.common.http.methods.webdav.PropfindMethod import com.owncloud.android.lib.common.network.RedirectionPath import com.owncloud.android.lib.common.network.WebdavUtils @@ -66,7 +66,7 @@ class CheckPathExistenceRemoteOperation( if (isUserLogged) client.baseFilesWebDavUri.toString() else client.userFilesWebDavUri.toString() + WebdavUtils.encodePath(remotePath) - val propFindMethod = PropfindMethod(URL(stringUrl), 0, getAllPropset()).apply { + val propFindMethod = PropfindMethod(URL(stringUrl), 0, allPropset).apply { setReadTimeout(TIMEOUT.toLong(), TimeUnit.SECONDS) setConnectionTimeout(TIMEOUT.toLong(), TimeUnit.SECONDS) } diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/ReadRemoteFileOperation.java b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/ReadRemoteFileOperation.java index 64c4481e..55facfe4 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/ReadRemoteFileOperation.java +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/ReadRemoteFileOperation.java @@ -26,7 +26,6 @@ package com.owncloud.android.lib.resources.files; import com.owncloud.android.lib.common.OwnCloudClient; import com.owncloud.android.lib.common.accounts.AccountUtils; import com.owncloud.android.lib.common.http.HttpConstants; -import com.owncloud.android.lib.common.http.methods.webdav.DavUtils; import com.owncloud.android.lib.common.network.WebdavUtils; import com.owncloud.android.lib.common.operations.RemoteOperation; import com.owncloud.android.lib.common.operations.RemoteOperationResult; diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/ReadRemoteFolderOperation.java b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/ReadRemoteFolderOperation.java index f77c68c9..8a11bc9c 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/ReadRemoteFolderOperation.java +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/ReadRemoteFolderOperation.java @@ -29,7 +29,6 @@ import com.owncloud.android.lib.common.OwnCloudClient; import com.owncloud.android.lib.common.accounts.AccountUtils; import com.owncloud.android.lib.common.http.HttpConstants; import com.owncloud.android.lib.common.http.methods.webdav.DavConstants; -import com.owncloud.android.lib.common.http.methods.webdav.DavUtils; import com.owncloud.android.lib.common.network.WebdavUtils; import com.owncloud.android.lib.common.operations.RemoteOperation; import com.owncloud.android.lib.common.operations.RemoteOperationResult; diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/users/GetRemoteUserQuotaOperation.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/users/GetRemoteUserQuotaOperation.kt index de0999ae..2607bd92 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/users/GetRemoteUserQuotaOperation.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/users/GetRemoteUserQuotaOperation.kt @@ -52,12 +52,12 @@ class GetRemoteUserQuotaOperation : RemoteOperation() { val propfindMethod = PropfindMethod( URL(client.userFilesWebDavUri.toString()), DavConstants.DEPTH_0, - DavUtils.getQuotaPropSet() + DavUtils.quotaPropSet ) with(client.executeHttpMethod(propfindMethod)) { if (isSuccess(this)) { RemoteOperationResult(ResultCode.OK).apply { - data = readData(propfindMethod.root.properties) + data = readData(propfindMethod.root?.properties) }.also { Timber.i("Get quota completed: ${it.data} and message: ${it.logMessage}") } @@ -81,9 +81,16 @@ class GetRemoteUserQuotaOperation : RemoteOperation() { * @param properties WebDAV properties containing quota data * @return new [RemoteQuota] instance representing the data read from the server */ - private fun readData(properties: List): RemoteQuota { + private fun readData(properties: List?): RemoteQuota { var quotaAvailable: Long = 0 var quotaUsed: Long = 0 + + if (properties == null) { + // Should not happen + Timber.d("Unable to get quota") + return RemoteQuota(0, 0, 0, 0.0) + } + for (property in properties) { if (property is QuotaAvailableBytes) { quotaAvailable = property.quotaAvailableBytes From c5cf0a8c0f49eff2e00528e5be35861f8d4f7237 Mon Sep 17 00:00:00 2001 From: agarcia Date: Fri, 26 Jun 2020 00:10:36 +0200 Subject: [PATCH 07/15] Migrate http methods to kotlin --- .../{DeleteMethod.java => DeleteMethod.kt} | 27 ++++++---------- .../{GetMethod.java => GetMethod.kt} | 27 ++++++---------- .../{PostMethod.java => PostMethod.kt} | 27 ++++++---------- .../{PutMethod.java => PutMethod.kt} | 32 +++++++++---------- 4 files changed, 45 insertions(+), 68 deletions(-) rename owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/{DeleteMethod.java => DeleteMethod.kt} (78%) rename owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/{GetMethod.java => GetMethod.kt} (78%) rename owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/{PostMethod.java => PostMethod.kt} (77%) rename owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/{PutMethod.java => PutMethod.kt} (76%) diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/DeleteMethod.java b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/DeleteMethod.kt similarity index 78% rename from owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/DeleteMethod.java rename to owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/DeleteMethod.kt index 42039dde..6db4092f 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/DeleteMethod.java +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/DeleteMethod.kt @@ -21,29 +21,22 @@ * THE SOFTWARE. * */ +package com.owncloud.android.lib.common.http.methods.nonwebdav -package com.owncloud.android.lib.common.http.methods.nonwebdav; - -import java.io.IOException; -import java.net.URL; +import java.io.IOException +import java.net.URL /** * OkHttp delete calls wrapper * * @author David González Verdugo */ -public class DeleteMethod extends HttpMethod { - - public DeleteMethod(URL url) { - super(url); - } - - @Override - public int onExecute() throws IOException { +class DeleteMethod(url: URL?) : HttpMethod(url) { + @Throws(IOException::class) + override fun onExecute(): Int { mRequest = mRequest.newBuilder() - .delete() - .build(); - - return super.onExecute(); + .delete() + .build() + return super.onExecute() } -} \ No newline at end of file +} diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/GetMethod.java b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/GetMethod.kt similarity index 78% rename from owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/GetMethod.java rename to owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/GetMethod.kt index 55022bbe..a73ffb9e 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/GetMethod.java +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/GetMethod.kt @@ -21,29 +21,22 @@ * THE SOFTWARE. * */ +package com.owncloud.android.lib.common.http.methods.nonwebdav -package com.owncloud.android.lib.common.http.methods.nonwebdav; - -import java.io.IOException; -import java.net.URL; +import java.io.IOException +import java.net.URL /** * OkHttp get calls wrapper * * @author David González Verdugo */ -public class GetMethod extends HttpMethod { - - public GetMethod(URL url) { - super(url); - } - - @Override - public int onExecute() throws IOException { +class GetMethod(url: URL?) : HttpMethod(url) { + @Throws(IOException::class) + override fun onExecute(): Int { mRequest = mRequest.newBuilder() - .get() - .build(); - - return super.onExecute(); + .get() + .build() + return super.onExecute() } -} \ No newline at end of file +} diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/PostMethod.java b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/PostMethod.kt similarity index 77% rename from owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/PostMethod.java rename to owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/PostMethod.kt index 1efb26d0..fb564d3d 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/PostMethod.java +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/PostMethod.kt @@ -21,29 +21,22 @@ * THE SOFTWARE. * */ +package com.owncloud.android.lib.common.http.methods.nonwebdav -package com.owncloud.android.lib.common.http.methods.nonwebdav; - -import java.io.IOException; -import java.net.URL; +import java.io.IOException +import java.net.URL /** * OkHttp post calls wrapper * * @author David González Verdugo */ -public class PostMethod extends HttpMethod { - - public PostMethod(URL url) { - super(url); - } - - @Override - public int onExecute() throws IOException { +class PostMethod(url: URL?) : HttpMethod(url) { + @Throws(IOException::class) + override fun onExecute(): Int { mRequest = mRequest.newBuilder() - .post(mRequestBody) - .build(); - - return super.onExecute(); + .post(mRequestBody) + .build() + return super.onExecute() } -} \ No newline at end of file +} diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/PutMethod.java b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/PutMethod.kt similarity index 76% rename from owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/PutMethod.java rename to owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/PutMethod.kt index c51a1958..6842befb 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/PutMethod.java +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/PutMethod.kt @@ -21,24 +21,22 @@ * THE SOFTWARE. * */ +package com.owncloud.android.lib.common.http.methods.nonwebdav -package com.owncloud.android.lib.common.http.methods.nonwebdav; +import java.io.IOException +import java.net.URL -import java.io.IOException; -import java.net.URL; - -public class PutMethod extends HttpMethod { - - public PutMethod(URL url) { - super(url); - } - - @Override - public int onExecute() throws IOException { +/** + * OkHttp put calls wrapper + * + * @author David González Verdugo + */ +class PutMethod(url: URL?) : HttpMethod(url) { + @Throws(IOException::class) + override fun onExecute(): Int { mRequest = mRequest.newBuilder() - .put(mRequestBody) - .build(); - - return super.onExecute(); + .put(mRequestBody) + .build() + return super.onExecute() } -} \ No newline at end of file +} From 93026e818024e3082a52a5292a62e26baf3a5888 Mon Sep 17 00:00:00 2001 From: agarcia Date: Mon, 29 Jun 2020 20:30:59 +0200 Subject: [PATCH 08/15] First step to replace interceptor --- .../android/lib/common/OwnCloudClient.java | 44 ++- .../lib/common/SingleSessionManager.java | 5 - .../OwnCloudBasicCredentials.java | 20 +- .../OwnCloudBearerCredentials.java | 17 +- .../authentication/OwnCloudCredentials.java | 6 +- .../OwnCloudCredentialsFactory.java | 8 +- .../android/lib/common/http/HttpClient.java | 25 -- .../http/interceptors/HttpInterceptor.java | 74 +--- .../RequestHeaderInterceptor.java | 54 --- .../common/http/methods/HttpBaseMethod.java | 191 ---------- .../lib/common/http/methods/HttpBaseMethod.kt | 163 +++++++++ .../http/methods/nonwebdav/DeleteMethod.kt | 4 +- .../http/methods/nonwebdav/GetMethod.kt | 4 +- .../{HttpMethod.java => HttpMethod.kt} | 22 +- .../http/methods/nonwebdav/PostMethod.kt | 8 +- .../http/methods/nonwebdav/PutMethod.kt | 8 +- .../common/http/methods/webdav/CopyMethod.kt | 13 +- .../common/http/methods/webdav/DavMethod.java | 162 --------- .../common/http/methods/webdav/DavMethod.kt | 164 +++++++++ .../common/http/methods/webdav/MkColMethod.kt | 11 +- .../common/http/methods/webdav/MoveMethod.kt | 14 +- .../http/methods/webdav/OCDavResource.kt | 340 ++++++++++++++++++ .../http/methods/webdav/PropfindMethod.kt | 7 +- .../common/http/methods/webdav/PutMethod.kt | 16 +- .../lib/common/network/WebdavUtils.java | 3 - .../shares/CreateRemoteShareOperation.kt | 8 +- .../shares/GetRemoteShareesOperation.kt | 2 +- .../shares/GetRemoteSharesForFileOperation.kt | 4 +- .../shares/RemoveRemoteShareOperation.kt | 6 +- .../shares/UpdateRemoteShareOperation.kt | 8 +- .../status/GetRemoteCapabilitiesOperation.kt | 2 +- .../status/GetRemoteStatusOperation.kt | 2 +- .../implementation/OCServerInfoService.kt | 3 +- .../users/GetRemoteUserAvatarOperation.kt | 8 +- .../users/GetRemoteUserInfoOperation.kt | 2 +- 35 files changed, 783 insertions(+), 645 deletions(-) delete mode 100644 owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/interceptors/RequestHeaderInterceptor.java delete mode 100644 owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/HttpBaseMethod.java create mode 100644 owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/HttpBaseMethod.kt rename owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/{HttpMethod.java => HttpMethod.kt} (79%) delete mode 100644 owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/DavMethod.java create mode 100644 owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/DavMethod.kt create mode 100644 owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/OCDavResource.kt 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 7e2691d3..2b2b5a7c 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 @@ -89,11 +89,6 @@ public class OwnCloudClient extends HttpClient { if (!(mCredentials instanceof OwnCloudAnonymousCredentials)) { mCredentials = OwnCloudCredentialsFactory.getAnonymousCredentials(); } - mCredentials.applyTo(this); - } - - void applyCredentials() { - mCredentials.applyTo(this); } public int executeHttpMethod(HttpBaseMethod method) throws Exception { @@ -102,8 +97,17 @@ public class OwnCloudClient extends HttpClient { int status; do { - setRequestId(method); + String requestId = RandomUtils.generateRandomUUID(); + // Header to allow tracing requests in apache and ownCloud logs + Timber.d("Executing in request with id %s", requestId); + method.setRequestHeader(HttpConstants.OC_X_REQUEST_ID,requestId); + method.setRequestHeader(HttpConstants.USER_AGENT_HEADER, SingleSessionManager.getUserAgent()); + method.setRequestHeader(HttpConstants.PARAM_SINGLE_COOKIE_HEADER, "true"); + method.setRequestHeader(HttpConstants.ACCEPT_ENCODING_HEADER, HttpConstants.ACCEPT_ENCODING_IDENTITY); + if(mCredentials.getHeaderAuth()!=null){ + method.setRequestHeader(HttpConstants.AUTHORIZATION_HEADER,mCredentials.getHeaderAuth()); + } status = method.execute(); if (mFollowRedirects) { @@ -125,8 +129,17 @@ public class OwnCloudClient extends HttpClient { int status; do { - setRequestId(method); + String requestId = RandomUtils.generateRandomUUID(); + // Header to allow tracing requests in apache and ownCloud logs + Timber.d("Executing in request with id %s", requestId); + method.setRequestHeader(OC_X_REQUEST_ID,requestId); + method.setRequestHeader(HttpConstants.USER_AGENT_HEADER, SingleSessionManager.getUserAgent()); + method.setRequestHeader(HttpConstants.PARAM_SINGLE_COOKIE_HEADER, "true"); + method.setRequestHeader(HttpConstants.ACCEPT_ENCODING_HEADER, HttpConstants.ACCEPT_ENCODING_IDENTITY); + if(mCredentials.getHeaderAuth()!=null){ + method.setRequestHeader(HttpConstants.AUTHORIZATION_HEADER,mCredentials.getHeaderAuth()); + } status = method.execute(); repeatWithFreshCredentials = checkUnauthorizedAccess(status, repeatCounter); @@ -138,19 +151,6 @@ public class OwnCloudClient extends HttpClient { return status; } - private void setRequestId(HttpBaseMethod method) { - // Clean previous request id. This is a bit hacky but is the only way to add request headers in WebDAV - // methods by using Dav4Android - deleteHeaderForAllRequests(OC_X_REQUEST_ID); - - String requestId = RandomUtils.generateRandomUUID(); - - // Header to allow tracing requests in apache and ownCloud logs - addHeaderForAllRequests(OC_X_REQUEST_ID, requestId); - - Timber.d("Executing in request with id %s", requestId); - } - public RedirectionPath followRedirection(HttpBaseMethod method) throws Exception { int redirectionsCount = 0; int status = method.getStatusCode(); @@ -215,9 +215,6 @@ public class OwnCloudClient extends HttpClient { public void exhaustResponse(InputStream responseBodyAsStream) { if (responseBodyAsStream != null) { try { - while (responseBodyAsStream.read(sExhaustBuffer) >= 0) { - ; - } responseBodyAsStream.close(); } catch (IOException io) { @@ -273,7 +270,6 @@ public class OwnCloudClient extends HttpClient { public void setCredentials(OwnCloudCredentials credentials) { if (credentials != null) { mCredentials = credentials; - mCredentials.applyTo(this); } else { clearCredentials(); } diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/SingleSessionManager.java b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/SingleSessionManager.java index a12f4796..e011a7fa 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/SingleSessionManager.java +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/SingleSessionManager.java @@ -130,7 +130,6 @@ public class SingleSessionManager { Timber.v("reusing client for session %s", sessionName); } - keepCredentialsUpdated(client); keepCookiesUpdated(context, account, client); keepUriUpdated(account, client); } @@ -177,10 +176,6 @@ public class SingleSessionManager { Timber.d("All sessions saved"); } - private void keepCredentialsUpdated(OwnCloudClient reusedClient) { - reusedClient.applyCredentials(); - } - private void keepCookiesUpdated(Context context, OwnCloudAccount account, OwnCloudClient reusedClient) { AccountManager am = AccountManager.get(context.getApplicationContext()); if (am != null && account.getSavedAccount() != null) { diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/authentication/OwnCloudBasicCredentials.java b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/authentication/OwnCloudBasicCredentials.java index bde053ce..96e8a233 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/authentication/OwnCloudBasicCredentials.java +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/authentication/OwnCloudBasicCredentials.java @@ -40,21 +40,6 @@ public class OwnCloudBasicCredentials implements OwnCloudCredentials { mPassword = password != null ? password : ""; } - public OwnCloudBasicCredentials(String username, String password, boolean preemptiveMode) { - mUsername = username != null ? username : ""; - mPassword = password != null ? password : ""; - } - - @Override - public void applyTo(OwnCloudClient client) { - // Clear previous basic credentials - HttpClient.deleteHeaderForAllRequests(HttpConstants.AUTHORIZATION_HEADER); - HttpClient.deleteHeaderForAllRequests(HttpConstants.COOKIE_HEADER); - - HttpClient.addHeaderForAllRequests(HttpConstants.AUTHORIZATION_HEADER, - Credentials.basic(mUsername, mPassword, UTF_8)); - } - @Override public String getUsername() { return mUsername; @@ -65,6 +50,11 @@ public class OwnCloudBasicCredentials implements OwnCloudCredentials { return mPassword; } + @Override + public String getHeaderAuth() { + return Credentials.basic(mUsername, mPassword, Util.UTF_8); + } + @Override public boolean authTokenExpires() { return false; diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/authentication/OwnCloudBearerCredentials.java b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/authentication/OwnCloudBearerCredentials.java index e455cb39..0dbf326a 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/authentication/OwnCloudBearerCredentials.java +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/authentication/OwnCloudBearerCredentials.java @@ -23,8 +23,6 @@ */ package com.owncloud.android.lib.common.authentication; -import com.owncloud.android.lib.common.OwnCloudClient; -import com.owncloud.android.lib.common.http.HttpClient; import com.owncloud.android.lib.common.http.HttpConstants; public class OwnCloudBearerCredentials implements OwnCloudCredentials { @@ -37,16 +35,6 @@ public class OwnCloudBearerCredentials implements OwnCloudCredentials { mAccessToken = accessToken != null ? accessToken : ""; } - @Override - public void applyTo(OwnCloudClient client) { - // Clear previous credentials - HttpClient.deleteHeaderForAllRequests(HttpConstants.AUTHORIZATION_HEADER); - HttpClient.deleteHeaderForAllRequests(HttpConstants.COOKIE_HEADER); - - HttpClient.addHeaderForAllRequests(HttpConstants.AUTHORIZATION_HEADER, - HttpConstants.BEARER_AUTHORIZATION_KEY + mAccessToken); - } - @Override public String getUsername() { // not relevant for authentication, but relevant for informational purposes @@ -58,6 +46,11 @@ public class OwnCloudBearerCredentials implements OwnCloudCredentials { return mAccessToken; } + @Override + public String getHeaderAuth() { + return HttpConstants.BEARER_AUTHORIZATION_KEY + mAccessToken; + } + @Override public boolean authTokenExpires() { return true; diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/authentication/OwnCloudCredentials.java b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/authentication/OwnCloudCredentials.java index 978dde8b..6ee06b58 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/authentication/OwnCloudCredentials.java +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/authentication/OwnCloudCredentials.java @@ -24,16 +24,14 @@ package com.owncloud.android.lib.common.authentication; -import com.owncloud.android.lib.common.OwnCloudClient; - public interface OwnCloudCredentials { - void applyTo(OwnCloudClient ownCloudClient); - String getUsername(); String getAuthToken(); + String getHeaderAuth(); + boolean authTokenExpires(); boolean authTokenCanBeRefreshed(); diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/authentication/OwnCloudCredentialsFactory.java b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/authentication/OwnCloudCredentialsFactory.java index 75b523cc..3d5dae91 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/authentication/OwnCloudCredentialsFactory.java +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/authentication/OwnCloudCredentialsFactory.java @@ -55,14 +55,12 @@ public class OwnCloudCredentialsFactory { } @Override - public void applyTo(OwnCloudClient client) { - // Clear previous basic credentials - HttpClient.deleteHeaderForAllRequests(HttpConstants.AUTHORIZATION_HEADER); - HttpClient.deleteHeaderForAllRequests(HttpConstants.COOKIE_HEADER); + public String getAuthToken() { + return ""; } @Override - public String getAuthToken() { + public String getHeaderAuth() { return ""; } diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/HttpClient.java b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/HttpClient.java index 6c6784f1..f9939385 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/HttpClient.java +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/HttpClient.java @@ -26,9 +26,7 @@ package com.owncloud.android.lib.common.http; import android.content.Context; -import com.owncloud.android.lib.common.SingleSessionManager; import com.owncloud.android.lib.common.http.interceptors.HttpInterceptor; -import com.owncloud.android.lib.common.http.interceptors.RequestHeaderInterceptor; import com.owncloud.android.lib.common.network.AdvancedX509TrustManager; import com.owncloud.android.lib.common.network.NetworkUtils; import okhttp3.Cookie; @@ -137,33 +135,10 @@ public class HttpClient { private static HttpInterceptor getOkHttpInterceptor() { if (sOkHttpInterceptor == null) { sOkHttpInterceptor = new HttpInterceptor(); - addHeaderForAllRequests(HttpConstants.USER_AGENT_HEADER, SingleSessionManager.getUserAgent()); - addHeaderForAllRequests(HttpConstants.PARAM_SINGLE_COOKIE_HEADER, "true"); - addHeaderForAllRequests(HttpConstants.ACCEPT_ENCODING_HEADER, HttpConstants.ACCEPT_ENCODING_IDENTITY); } return sOkHttpInterceptor; } - /** - * Add header that will be included for all the requests from now on - * - * @param headerName - * @param headerValue - */ - public static void addHeaderForAllRequests(String headerName, String headerValue) { - HttpInterceptor httpInterceptor = getOkHttpInterceptor(); - - if (getOkHttpInterceptor() != null) { - httpInterceptor.addRequestInterceptor( - new RequestHeaderInterceptor(headerName, headerValue) - ); - } - } - - public static void deleteHeaderForAllRequests(String headerName) { - getOkHttpInterceptor().deleteRequestHeaderInterceptor(headerName); - } - public Context getContext() { return sContext; } diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/interceptors/HttpInterceptor.java b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/interceptors/HttpInterceptor.java index b9965548..b7e6b6c9 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/interceptors/HttpInterceptor.java +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/interceptors/HttpInterceptor.java @@ -27,10 +27,10 @@ package com.owncloud.android.lib.common.http.interceptors; import okhttp3.Interceptor; import okhttp3.Request; import okhttp3.Response; +import org.jetbrains.annotations.NotNull; +import timber.log.Timber; import java.io.IOException; -import java.util.ArrayList; -import java.util.ListIterator; /** * Http interceptor to use multiple interceptors in the same {@link okhttp3.OkHttpClient} instance @@ -39,78 +39,14 @@ import java.util.ListIterator; */ public class HttpInterceptor implements Interceptor { - private final ArrayList mRequestInterceptors = new ArrayList<>(); - private final ArrayList mResponseInterceptors = new ArrayList<>(); - + @NotNull @Override public Response intercept(Chain chain) throws IOException { Request request = chain.request(); - ListIterator requestInterceptorIterator = mRequestInterceptors.listIterator(); + Timber.d("Executing in request headers %s", request.headers().toString()); - while (requestInterceptorIterator.hasNext()) { - RequestInterceptor currentRequestInterceptor = requestInterceptorIterator.next(); - request = currentRequestInterceptor.intercept(request); - } - - Response response = chain.proceed(request); - - ListIterator responseInterceptorIterator = mResponseInterceptors.listIterator(); - - while (responseInterceptorIterator.hasNext()) { - ResponseInterceptor currentResponseInterceptor = responseInterceptorIterator.next(); - response = currentResponseInterceptor.intercept(response); - } - - return response; + return chain.proceed(request); } - public HttpInterceptor addRequestInterceptor(RequestInterceptor requestInterceptor) { - mRequestInterceptors.listIterator().add(requestInterceptor); - return this; - } - - public HttpInterceptor addResponseInterceptor(ResponseInterceptor responseInterceptor) { - mResponseInterceptors.listIterator().add(responseInterceptor); - return this; - } - - public ArrayList getRequestInterceptors() { - return mRequestInterceptors; - } - - private ArrayList getRequestHeaderInterceptors() { - ArrayList requestHeaderInterceptors = new ArrayList<>(); - - for (RequestInterceptor requestInterceptor : mRequestInterceptors) { - if (requestInterceptor instanceof RequestHeaderInterceptor) { - requestHeaderInterceptors.add((RequestHeaderInterceptor) requestInterceptor); - } - } - - return requestHeaderInterceptors; - } - - public void deleteRequestHeaderInterceptor(String headerName) { - ListIterator requestInterceptorIterator = mRequestInterceptors.listIterator(); - while (requestInterceptorIterator.hasNext()) { - RequestInterceptor currentRequestInterceptor = requestInterceptorIterator.next(); - if (currentRequestInterceptor instanceof RequestHeaderInterceptor && - ((RequestHeaderInterceptor) currentRequestInterceptor).getHeaderName().equals(headerName)) { - requestInterceptorIterator.remove(); - } - } - } - - public ArrayList getResponseInterceptors() { - return mResponseInterceptors; - } - - public interface RequestInterceptor { - Request intercept(Request request) throws IOException; - } - - public interface ResponseInterceptor { - Response intercept(Response response) throws IOException; - } } diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/interceptors/RequestHeaderInterceptor.java b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/interceptors/RequestHeaderInterceptor.java deleted file mode 100644 index d7cb6dc4..00000000 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/interceptors/RequestHeaderInterceptor.java +++ /dev/null @@ -1,54 +0,0 @@ -/* 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.common.http.interceptors; - -import okhttp3.Request; - -/** - * Intercept requests to update their headers - */ -public class RequestHeaderInterceptor implements HttpInterceptor.RequestInterceptor { - - private String mHeaderName; - private String mHeaderValue; - - public RequestHeaderInterceptor(String headerName, String headerValue) { - this.mHeaderName = headerName; - this.mHeaderValue = headerValue; - } - - @Override - public Request intercept(Request request) { - return request.newBuilder().addHeader(mHeaderName, mHeaderValue).build(); - } - - public String getHeaderName() { - return mHeaderName; - } - - public String getHeaderValue() { - return mHeaderValue; - } -} \ No newline at end of file diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/HttpBaseMethod.java b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/HttpBaseMethod.java deleted file mode 100644 index 9dc8de97..00000000 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/HttpBaseMethod.java +++ /dev/null @@ -1,191 +0,0 @@ -/* 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.common.http.methods; - -import com.owncloud.android.lib.common.http.HttpClient; -import okhttp3.Call; -import okhttp3.Headers; -import okhttp3.HttpUrl; -import okhttp3.OkHttpClient; -import okhttp3.Request; -import okhttp3.RequestBody; -import okhttp3.Response; - -import java.io.IOException; -import java.io.InputStream; -import java.net.URL; -import java.util.concurrent.TimeUnit; - -/** - * Wrapper to perform http calls transparently by using: - * - OkHttp for non webdav methods - * - Dav4Android for webdav methods - * - * @author David González Verdugo - */ -public abstract class HttpBaseMethod { - protected OkHttpClient mOkHttpClient; - protected Request mRequest; - protected RequestBody mRequestBody; - protected Response mResponse; - protected String mResponseBodyString; - protected Call mCall; - - protected HttpBaseMethod(URL url) { - mOkHttpClient = HttpClient.getOkHttpClient(); - mRequest = new Request.Builder() - .url(HttpUrl.parse(url.toString())) - .build(); - } - - public int execute() throws Exception { - return onExecute(); - } - - public void abort() { - mCall.cancel(); - } - - public boolean isAborted() { - return mCall.isCanceled(); - } - - ////////////////////////////// - // For override - ////////////////////////////// - - protected abstract int onExecute() throws Exception; - - ////////////////////////////// - // Getter - ////////////////////////////// - - // Request - - public Headers getRequestHeaders() { - return mRequest.headers(); - } - - public String getRequestHeader(String name) { - return mRequest.header(name); - } - - // Response - - public int getStatusCode() { - return mResponse.code(); - } - - public String getStatusMessage() { - return mResponse.message(); - } - - public String getResponseBodyAsString() throws IOException { - if (mResponseBodyString == null && mResponse.body() != null) { - mResponseBodyString = mResponse.body().string(); - } - - return mResponseBodyString; - } - - public InputStream getResponseBodyAsStream() { - if (mResponse.body() != null) { - return mResponse.body().byteStream(); - } - return null; - } - - public Headers getResponseHeaders() { - return mResponse.headers(); - } - - public String getResponseHeader(String headerName) { - return mResponse.header(headerName); - } - - public boolean getRetryOnConnectionFailure() { - return mOkHttpClient.retryOnConnectionFailure(); - } - - ////////////////////////////// - // Setter - ////////////////////////////// - - // Connection parameters - - public void setRetryOnConnectionFailure(boolean retryOnConnectionFailure) { - mOkHttpClient = mOkHttpClient.newBuilder() - .retryOnConnectionFailure(retryOnConnectionFailure) - .build(); - } - - public void setReadTimeout(long readTimeout, TimeUnit timeUnit) { - mOkHttpClient = mOkHttpClient.newBuilder() - .readTimeout(readTimeout, timeUnit) - .build(); - } - - public void setConnectionTimeout(long connectionTimeout, TimeUnit timeUnit) { - mOkHttpClient = mOkHttpClient.newBuilder() - .readTimeout(connectionTimeout, timeUnit) - .build(); - } - - public void setFollowRedirects(boolean followRedirects) { - mOkHttpClient = mOkHttpClient.newBuilder() - .followRedirects(followRedirects) - .build(); - } - - // Request - - public void addRequestHeader(String name, String value) { - mRequest = mRequest.newBuilder() - .addHeader(name, value) - .build(); - } - - /** - * Sets a header and replace it if already exists with that name - * - * @param name header name - * @param value header value - */ - public void setRequestHeader(String name, String value) { - mRequest = mRequest.newBuilder() - .header(name, value) - .build(); - } - - public void setRequestBody(RequestBody requestBody) { - mRequestBody = requestBody; - } - - public void setUrl(HttpUrl url) { - mRequest = mRequest.newBuilder() - .url(url) - .build(); - } -} \ No newline at end of file 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 new file mode 100644 index 00000000..c71d065d --- /dev/null +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/HttpBaseMethod.kt @@ -0,0 +1,163 @@ +package com.owncloud.android.lib.common.http.methods + +import com.owncloud.android.lib.common.http.HttpClient +import okhttp3.Call +import okhttp3.Headers +import okhttp3.HttpUrl +import okhttp3.OkHttpClient +import okhttp3.Request +import okhttp3.RequestBody +import okhttp3.Response +import java.io.InputStream +import java.net.MalformedURLException +import java.net.URL +import java.util.concurrent.TimeUnit + +abstract class HttpBaseMethod constructor(url: URL) { + var okHttpClient: OkHttpClient + var httpUrl: HttpUrl = HttpUrl.parse(url.toString()) ?: throw MalformedURLException() + var request: Request + var requestBody: RequestBody? = null + lateinit var response: Response + var responseBodyString: String? = null + + var call: Call? = null + + init { + okHttpClient = HttpClient.getOkHttpClient() + request = Request.Builder() + .url(httpUrl) + .build() + } + + @Throws(Exception::class) + open fun execute(): Int { + return onExecute() + } + + open fun setUrl(url: HttpUrl) { + request = request.newBuilder() + .url(url) + .build() + } + + /**************** + *** Requests *** + ****************/ + + // Headers + val requestHeaders: Headers + get() = request.headers() + + fun getRequestHeader(name: String): String? { + return request.header(name) + } + + fun getRequestHeadersAsHashMap(): HashMap { + val headers: HashMap = HashMap() + val superHeaders: Set = requestHeaders.names() + superHeaders.forEach { + headers[it] = getRequestHeader(it) + } + return headers + } + + open fun addRequestHeader(name: String, value: String) { + request = request.newBuilder() + .addHeader(name, value) + .build() + } + + /** + * Sets a header and replace it if already exists with that name + * + * @param name header name + * @param value header value + */ + open fun setRequestHeader(name: String, value: String) { + request = request.newBuilder() + .header(name, value) + .build() + } + + /**************** + *** Response *** + ****************/ + val statusCode: Int + get() = response.code() + + val statusMessage: String + get() = response.message() + + // Headers + open fun getResponseHeaders(): Headers? { + return response.headers() + } + + open fun getResponseHeader(headerName: String): String? { + return response.header(headerName) + } + + // Body + fun getResponseBodyAsString(): String? { + if (responseBodyString == null && response.body() != null) { + responseBodyString = response.body()?.string() + } + return responseBodyString + } + + open fun getResponseBodyAsStream(): InputStream? { + return response.body()?.byteStream() + } + + /************************* + *** Connection Params *** + *************************/ + + ////////////////////////////// + // Setter + ////////////////////////////// + // Connection parameters + open fun setRetryOnConnectionFailure(retryOnConnectionFailure: Boolean) { + okHttpClient = okHttpClient.newBuilder() + .retryOnConnectionFailure(retryOnConnectionFailure) + .build() + } + + open fun setReadTimeout(readTimeout: Long, timeUnit: TimeUnit) { + okHttpClient = okHttpClient.newBuilder() + .readTimeout(readTimeout, timeUnit) + .build() + } + + open fun setConnectionTimeout( + connectionTimeout: Long, + timeUnit: TimeUnit + ) { + okHttpClient = okHttpClient.newBuilder() + .readTimeout(connectionTimeout, timeUnit) + .build() + } + + open fun setFollowRedirects(followRedirects: Boolean) { + okHttpClient = okHttpClient.newBuilder() + .followRedirects(followRedirects) + .build() + } + + /************ + *** Call *** + ************/ + open fun abort() { + call?.cancel() + } + + open val isAborted: Boolean + get() = call?.isCanceled ?: false + + ////////////////////////////// + // For override + ////////////////////////////// + @Throws(Exception::class) + protected abstract fun onExecute(): Int +} diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/DeleteMethod.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/DeleteMethod.kt index 6db4092f..51069bc2 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/DeleteMethod.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/DeleteMethod.kt @@ -31,10 +31,10 @@ import java.net.URL * * @author David González Verdugo */ -class DeleteMethod(url: URL?) : HttpMethod(url) { +class DeleteMethod(url: URL) : HttpMethod(url) { @Throws(IOException::class) override fun onExecute(): Int { - mRequest = mRequest.newBuilder() + request = request.newBuilder() .delete() .build() return super.onExecute() diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/GetMethod.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/GetMethod.kt index a73ffb9e..65bf40fc 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/GetMethod.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/GetMethod.kt @@ -31,10 +31,10 @@ import java.net.URL * * @author David González Verdugo */ -class GetMethod(url: URL?) : HttpMethod(url) { +class GetMethod(url: URL) : HttpMethod(url) { @Throws(IOException::class) override fun onExecute(): Int { - mRequest = mRequest.newBuilder() + request = request.newBuilder() .get() .build() return super.onExecute() diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/HttpMethod.java b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/HttpMethod.kt similarity index 79% rename from owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/HttpMethod.java rename to owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/HttpMethod.kt index 971923b7..52c874cb 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/HttpMethod.java +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/HttpMethod.kt @@ -21,8 +21,7 @@ * THE SOFTWARE. * */ - -package com.owncloud.android.lib.common.http.methods.nonwebdav; +package com.owncloud.android.lib.common.http.methods.nonwebdav import com.owncloud.android.lib.common.http.methods.HttpBaseMethod; @@ -34,16 +33,11 @@ import java.net.URL; * * @author David González Verdugo */ -public abstract class HttpMethod extends HttpBaseMethod { - - public HttpMethod(URL url) { - super(url); +abstract class HttpMethod(url: URL) : HttpBaseMethod(url) { + @Throws(IOException::class) + public override fun onExecute(): Int { + call = okHttpClient.newCall(request) + call?.let { response = it.execute() } + return super.statusCode } - - @Override - public int onExecute() throws IOException { - mCall = mOkHttpClient.newCall(mRequest); - mResponse = mCall.execute(); - return super.getStatusCode(); - } -} \ No newline at end of file +} diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/PostMethod.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/PostMethod.kt index fb564d3d..995b906e 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/PostMethod.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/PostMethod.kt @@ -23,6 +23,7 @@ */ package com.owncloud.android.lib.common.http.methods.nonwebdav +import okhttp3.RequestBody import java.io.IOException import java.net.URL @@ -31,11 +32,12 @@ import java.net.URL * * @author David González Verdugo */ -class PostMethod(url: URL?) : HttpMethod(url) { +class PostMethod(url: URL, private val postRequestBody: RequestBody) : HttpMethod(url) { @Throws(IOException::class) override fun onExecute(): Int { - mRequest = mRequest.newBuilder() - .post(mRequestBody) + requestBody = postRequestBody + request = request.newBuilder() + .post(postRequestBody) .build() return super.onExecute() } diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/PutMethod.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/PutMethod.kt index 6842befb..524f06a6 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/PutMethod.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/PutMethod.kt @@ -23,6 +23,7 @@ */ package com.owncloud.android.lib.common.http.methods.nonwebdav +import okhttp3.RequestBody import java.io.IOException import java.net.URL @@ -31,11 +32,12 @@ import java.net.URL * * @author David González Verdugo */ -class PutMethod(url: URL?) : HttpMethod(url) { +class PutMethod(url: URL, private val putRequestBody: RequestBody) : HttpMethod(url) { @Throws(IOException::class) override fun onExecute(): Int { - mRequest = mRequest.newBuilder() - .put(mRequestBody) + requestBody = putRequestBody + request = request.newBuilder() + .put(putRequestBody) .build() return super.onExecute() } diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/CopyMethod.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/CopyMethod.kt index a49f7b6a..9da7c22e 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/CopyMethod.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/CopyMethod.kt @@ -33,16 +33,19 @@ import java.net.URL * @author David González Verdugo */ class CopyMethod( - val url: URL?, + val url: URL, private val destinationUrl: String, private val forceOverride: Boolean ) : DavMethod(url) { @Throws(Exception::class) public override fun onExecute(): Int { - mDavResource.copy(destinationUrl, forceOverride) { response: Response -> - mResponse = response + mDavResource.copy( + destinationUrl, + forceOverride, + super.getRequestHeadersAsHashMap() + ) { callBackResponse: Response -> + response = callBackResponse } - return super.getStatusCode() + return super.statusCode } - } diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/DavMethod.java b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/DavMethod.java deleted file mode 100644 index 1334e327..00000000 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/DavMethod.java +++ /dev/null @@ -1,162 +0,0 @@ -/* 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.common.http.methods.webdav; - -import at.bitfire.dav4jvm.Dav4jvm; -import at.bitfire.dav4jvm.DavOCResource; -import at.bitfire.dav4jvm.exception.HttpException; -import at.bitfire.dav4jvm.exception.RedirectException; -import com.owncloud.android.lib.common.http.HttpConstants; -import com.owncloud.android.lib.common.http.methods.HttpBaseMethod; -import okhttp3.HttpUrl; -import okhttp3.Protocol; -import okhttp3.Response; -import okhttp3.ResponseBody; - -import java.net.URL; -import java.util.concurrent.TimeUnit; - -/** - * Wrapper to perform WebDAV (dav4android) calls - * - * @author David González Verdugo - */ -public abstract class DavMethod extends HttpBaseMethod { - - protected DavOCResource mDavResource; - - protected DavMethod(URL url) { - super(url); - mDavResource = new DavOCResource( - mOkHttpClient, - HttpUrl.parse(url.toString()), - Dav4jvm.INSTANCE.getLog()); - } - - @Override - public void abort() { - mDavResource.cancelCall(); - } - - @Override - public int execute() throws Exception { - try { - return onExecute(); - } catch (HttpException httpException) { - // Modify responses with information gathered from exceptions - if (httpException instanceof RedirectException) { - mResponse = new Response.Builder() - .header( - HttpConstants.LOCATION_HEADER, ((RedirectException) httpException).getRedirectLocation() - ) - .code(httpException.getCode()) - .request(mRequest) - .message(httpException.getMessage()) - .protocol(Protocol.HTTP_1_1) - .build(); - - } else if (mResponse != null) { - // The check below should be included in okhttp library, method ResponseBody.create( - // TODO check most recent versions of okhttp to see if this is already fixed and try to update if so - if (mResponse.body().contentType() != null) { - ResponseBody responseBody = ResponseBody.create( - mResponse.body().contentType(), - httpException.getResponseBody() - ); - - mResponse = mResponse.newBuilder() - .body(responseBody) - .build(); - } - } - - return httpException.getCode(); - } - } - - ////////////////////////////// - // Setter - ////////////////////////////// - - // Connection parameters - @Override - public void setReadTimeout(long readTimeout, TimeUnit timeUnit) { - super.setReadTimeout(readTimeout, timeUnit); - mDavResource = new DavOCResource( - mOkHttpClient, - HttpUrl.parse(mRequest.url().toString()), - Dav4jvm.INSTANCE.getLog()); - } - - @Override - public void setConnectionTimeout(long connectionTimeout, TimeUnit timeUnit) { - super.setConnectionTimeout(connectionTimeout, timeUnit); - mDavResource = new DavOCResource( - mOkHttpClient, - HttpUrl.parse(mRequest.url().toString()), - Dav4jvm.INSTANCE.getLog()); - } - - @Override - public void setFollowRedirects(boolean followRedirects) { - super.setFollowRedirects(followRedirects); - mDavResource = new DavOCResource( - mOkHttpClient, - HttpUrl.parse(mRequest.url().toString()), - Dav4jvm.INSTANCE.getLog()); - } - - @Override - public void setUrl(HttpUrl url) { - super.setUrl(url); - mDavResource = new DavOCResource( - mOkHttpClient, - HttpUrl.parse(mRequest.url().toString()), - Dav4jvm.INSTANCE.getLog()); - } - - @Override - public boolean getRetryOnConnectionFailure() { - return false; //TODO: implement me - } - - ////////////////////////////// - // Getter - ////////////////////////////// - - @Override - public void setRetryOnConnectionFailure(boolean retryOnConnectionFailure) { - super.setRetryOnConnectionFailure(retryOnConnectionFailure); - mDavResource = new DavOCResource( - mOkHttpClient, - HttpUrl.parse(mRequest.url().toString()), - Dav4jvm.INSTANCE.getLog()); - } - - @Override - public boolean isAborted() { - return mDavResource.isCallAborted(); - } -} \ No newline at end of file diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/DavMethod.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/DavMethod.kt new file mode 100644 index 00000000..ce98bb4f --- /dev/null +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/DavMethod.kt @@ -0,0 +1,164 @@ +/* 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.common.http.methods.webdav + +import at.bitfire.dav4android.Constants.log +import at.bitfire.dav4android.exception.HttpException +import at.bitfire.dav4android.exception.RedirectException +import com.owncloud.android.lib.common.http.HttpConstants +import com.owncloud.android.lib.common.http.methods.HttpBaseMethod +import okhttp3.HttpUrl +import okhttp3.Protocol +import okhttp3.Response +import okhttp3.ResponseBody +import java.net.MalformedURLException +import java.net.URL +import java.util.concurrent.TimeUnit + +/** + * Wrapper to perform WebDAV (dav4android) calls + * + * @author David González Verdugo + */ +abstract class DavMethod protected constructor(url: URL) : HttpBaseMethod(url) { + protected var mDavResource: OCDavResource + + init { + val httpUrl = HttpUrl.parse(url.toString()) ?: throw MalformedURLException() + mDavResource = OCDavResource( + okHttpClient, + httpUrl, + log + ) + } + + override fun abort() { + mDavResource.cancelCall() + } + + @Throws(Exception::class) + override fun execute(): Int { + return try { + onExecute() + } catch (httpException: HttpException) { + // Modify responses with information gathered from exceptions + if (httpException is RedirectException) { + response = Response.Builder() + .header( + HttpConstants.LOCATION_HEADER, httpException.redirectLocation + ) + .code(httpException.code) + .request(request) + .message(httpException.message ?: "") + .protocol(Protocol.HTTP_1_1) + .build() + } else { + // The check below should be included in okhttp library, method ResponseBody.create( + // TODO check most recent versions of okhttp to see if this is already fixed and try to update if so + if (response.body()?.contentType() != null) { + val responseBody = ResponseBody.create( + response.body()?.contentType(), + httpException.responseBody?:"" + ) + response = response.newBuilder() + .body(responseBody) + .build() + } + } + httpException.code + } + } + + ////////////////////////////// + // Setter + ////////////////////////////// + // Connection parameters + override fun setReadTimeout(readTimeout: Long, timeUnit: TimeUnit) { + super.setReadTimeout(readTimeout, timeUnit) + mDavResource = OCDavResource( + okHttpClient, + httpUrl, + log + ) + } + + override fun setConnectionTimeout( + connectionTimeout: Long, + timeUnit: TimeUnit + ) { + super.setConnectionTimeout(connectionTimeout, timeUnit) + mDavResource = OCDavResource( + okHttpClient, + httpUrl, + log + ) + } + + override fun setFollowRedirects(followRedirects: Boolean) { + super.setFollowRedirects(followRedirects) + mDavResource = OCDavResource( + okHttpClient, + httpUrl, + log + ) + } + + override fun setUrl(url: HttpUrl) { + super.setUrl(url) + mDavResource = OCDavResource( + okHttpClient, + httpUrl, + log + ) + } + + override fun setRequestHeader(name: String, value: String) { + super.setRequestHeader(name, value) + mDavResource = OCDavResource( + okHttpClient, + httpUrl, + log + ) + } + + fun getRetryOnConnectionFailure(): Boolean { + return false //TODO: implement me + } + + ////////////////////////////// + // Getter + ////////////////////////////// + override fun setRetryOnConnectionFailure(retryOnConnectionFailure: Boolean) { + super.setRetryOnConnectionFailure(retryOnConnectionFailure) + mDavResource = OCDavResource( + okHttpClient, + httpUrl, + log + ) + } + + override val isAborted: Boolean + get() = mDavResource.isCallAborted() + +} \ No newline at end of file diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/MkColMethod.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/MkColMethod.kt index ddcd85c2..088a2c6a 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/MkColMethod.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/MkColMethod.kt @@ -32,12 +32,15 @@ import java.net.URL * @author Christian Schabesberger * @author David González Verdugo */ -class MkColMethod(url: URL?) : DavMethod(url) { +class MkColMethod(url: URL) : DavMethod(url) { @Throws(Exception::class) public override fun onExecute(): Int { - mDavResource.mkCol(null) { response: Response -> - mResponse = response + mDavResource.mkCol( + xmlBody = null, + listOfHeaders = super.getRequestHeadersAsHashMap() + ) { callBackResponse: Response -> + response = callBackResponse } - return super.getStatusCode() + return super.statusCode } } diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/MoveMethod.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/MoveMethod.kt index d2c45cfa..9aaa5b20 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/MoveMethod.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/MoveMethod.kt @@ -34,22 +34,20 @@ import java.net.URL * @author David González Verdugo */ class MoveMethod( - url: URL?, + url: URL, private val destinationUrl: String, private val forceOverride: Boolean -) : - DavMethod(url) { +) : DavMethod(url) { @Throws(Exception::class) public override fun onExecute(): Int { mDavResource.move( destinationUrl, forceOverride, - super.getRequestHeader(HttpConstants.OC_TOTAL_LENGTH_HEADER), - super.getRequestHeader(HttpConstants.OC_X_OC_MTIME_HEADER) - ) { response: Response -> - mResponse = response + super.getRequestHeadersAsHashMap() + ) { callBackResponse: Response -> + response = callBackResponse } - return super.getStatusCode() + return super.statusCode } } diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/OCDavResource.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/OCDavResource.kt new file mode 100644 index 00000000..037f65ed --- /dev/null +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/OCDavResource.kt @@ -0,0 +1,340 @@ +package com.owncloud.android.lib.common.http.methods.webdav + +import at.bitfire.dav4android.DavResource +import at.bitfire.dav4android.DavResponseCallback +import at.bitfire.dav4android.IF_MATCH_HEADER +import at.bitfire.dav4android.Property +import at.bitfire.dav4android.QuotedStringUtils +import at.bitfire.dav4android.XmlUtils +import at.bitfire.dav4android.exception.DavException +import at.bitfire.dav4android.exception.HttpException +import okhttp3.HttpUrl +import okhttp3.OkHttpClient +import okhttp3.Request +import okhttp3.RequestBody +import okhttp3.Response +import java.io.IOException +import java.io.StringWriter +import java.util.logging.Logger + +class OCDavResource( + httpClient: OkHttpClient, + location: HttpUrl, + log: Logger +) : DavResource(httpClient, location, log) { + + /** + * Sends a PUT request to the resource. + * @param body new resource body to upload + * @param ifMatchETag value of "If-Match" header to set, or null to omit + * @param ifNoneMatch indicates whether "If-None-Match: *" ("don't overwrite anything existing") header shall be sent + * @param contentType + * @param ocTotalLength total length of resource body + * @param ocXOcMtimeHeader modification time + * @return true if the request was redirected successfully, i.e. #{@link #location} and maybe resource name may have changed + * @throws IOException on I/O error + * @throws HttpException on HTTP error + */ + @Throws(IOException::class, HttpException::class) + fun put( + body: RequestBody, + ifMatchETag: String?, + listOfHeaders: HashMap?, + callback: (response: Response) -> Unit + ) { + val requestBuilder = Request.Builder() + .put(body) + + listOfHeaders?.forEach { (name, value) -> + value?.let { + requestBuilder.header(name, value) + } + } + + if (ifMatchETag != null) + // only overwrite specific version + requestBuilder.header(IF_MATCH_HEADER, QuotedStringUtils.asQuotedString(ifMatchETag)) +// if (contentType != null) { +// requestBuilder.header(CONTENT_TYPE_HEADER, contentType) +// } +// if (ocTotalLength != null) { +// requestBuilder.header(OC_TOTAL_LENGTH_HEADER, ocTotalLength) +// } +// if (ocXOcMtimeHeader != null) { +// requestBuilder.header(OC_X_OC_MTIME_HEADER, ocXOcMtimeHeader) +// } + + followRedirects { + requestBuilder + .url(location) + val call = httpClient.newCall(requestBuilder.build()) + + this.call = call + call.execute() + }.use { response -> + callback(response) + checkStatus(response) + } + } + + /** + * Sends a MOVE request to the resource + * @param ocTotalLength total length of resource body + * @param ocXOcMtimeHeader modification time + */ + @Throws(IOException::class, HttpException::class, DavException::class) + fun move( + destination: String, + forceOverride: Boolean, + listOfHeaders: HashMap?, + callback: (response: Response) -> Unit + ) { + val requestBuilder = Request.Builder() + .method("MOVE", null) + .header("Content-Length", "0") + .header("Destination", destination) + + if (forceOverride) + requestBuilder.header("Overwrite", "F") + + listOfHeaders?.forEach { (name, value) -> + value?.let { + requestBuilder.header(name, value) + } + } +// if (ocTotalLength != null) +// requestBuilder.header(OC_TOTAL_LENGTH_HEADER, ocTotalLength) +// if (ocXOcMtimeHeader != null) +// requestBuilder.header(OC_X_OC_MTIME_HEADER, ocXOcMtimeHeader) + + followRedirects { + requestBuilder.url(location) + val call = httpClient.newCall(requestBuilder.build()) + this.call = call + call.execute() + }.use { response -> + callback(response) + checkStatus(response) + } + } + + @Throws(IOException::class, HttpException::class, DavException::class) + fun copy( + destination: String, + forceOverride: Boolean, + listOfHeaders: HashMap?, + callback: (response: Response) -> Unit + ) { + val requestBuilder = Request.Builder() + .method("COPY", null) + .header("Content-Length", "0") + .header("Destination", destination) + if (forceOverride) + requestBuilder.header("Overwrite", "F") + + listOfHeaders?.forEach { (name, value) -> + value?.let { + requestBuilder.header(name, value) + } + } + + followRedirects { + requestBuilder.url(location) + val call = httpClient.newCall(requestBuilder.build()) + + this.call = call + call.execute() + }.use { response -> + callback(response) + checkStatus(response) + } + } + + /** + * Sends a MKCOL request to this resource. Follows up to [MAX_REDIRECTS] redirects. + * + * @throws IOException on I/O error + * @throws HttpException on HTTP error + */ + @Throws(IOException::class, HttpException::class) + fun mkCol( + xmlBody: String?, + listOfHeaders: HashMap?, + callback: (response: Response) -> Unit + ) { + val rqBody = if (xmlBody != null) RequestBody.create(MIME_XML, xmlBody) else null + val requestBuilder = Request.Builder() + .method("MKCOL", rqBody) + .url(location) + listOfHeaders?.forEach { (name, value) -> + value?.let { + requestBuilder.header(name, value) + } + } + followRedirects { + val call = httpClient.newCall( + requestBuilder.build() + ) + this.call = call + call.execute() + }.use { response -> + callback(response) + checkStatus(response) + } + } + + /** + * Sends a GET request to the resource. Sends `Accept-Encoding: identity` to disable + * compression, because compression might change the ETag. + * + * Follows up to [MAX_REDIRECTS] redirects. + * + * @param accept value of Accept header (must not be null, but may be */*) + * @param callback called with server response unless an exception is thrown + * + * @throws IOException on I/O error + * @throws HttpException on HTTP error + */ + @Throws(IOException::class, HttpException::class) + fun get( + accept: String, + listOfHeaders: HashMap?, + callback: (response: Response) -> Unit + ) { + val requestBuilder = Request.Builder() + .get() + .url(location) + .header("Accept", accept) + .header("Accept-Encoding", "identity") // disable compression because it can change the ETag + + listOfHeaders?.forEach { (name, value) -> + value?.let { + requestBuilder.header(name, value) + } + } + + followRedirects { + val call = httpClient.newCall( + requestBuilder + .build() + ) + this.call = call + call.execute() + }.use { response -> + callback(response) + checkStatus(response) + } + } + + /** + * Sends a DELETE request to the resource. Warning: Sending this request to a collection will + * delete the collection with all its contents! + * + * Follows up to [MAX_REDIRECTS] redirects. + * + * @param ifMatchETag value of `If-Match` header to set, or null to omit + * @param callback called with server response unless an exception is thrown + * + * @throws IOException on I/O error + * @throws HttpException on HTTP errors, or when 207 Multi-Status is returned + * (because then there was probably a problem with a member resource) + */ + @Throws(IOException::class, HttpException::class) + fun delete( + ifMatchETag: String?, + listOfHeaders: HashMap?, + callback: (Response) -> Unit + ) { + followRedirects { + val requestBuilder = Request.Builder() + .delete() + .url(location) + if (ifMatchETag != null) + requestBuilder.header("If-Match", QuotedStringUtils.asQuotedString(ifMatchETag)) + listOfHeaders?.forEach { (name, value) -> + value?.let { + requestBuilder.header(name, value) + } + } + + val call = httpClient.newCall(requestBuilder.build()) + + this.call = call + call.execute() + }.use { response -> + callback(response) + checkStatus(response) + if (response.code() == 207) + /* If an error occurs deleting a member resource (a resource other than + the resource identified in the Request-URI), then the response can be + a 207 (Multi-Status). […] (RFC 4918 9.6.1. DELETE for Collections) */ + throw HttpException(response) + } + } + + /** + * Sends a PROPFIND request to the resource. Expects and processes a 207 Multi-Status response. + * + * Follows up to [MAX_REDIRECTS] redirects. + * + * @param depth "Depth" header to send (-1 for `infinity`) + * @param reqProp properties to request + * @param callback called for every XML response element in the Multi-Status response + * + * @throws IOException on I/O error + * @throws HttpException on HTTP error + * @throws DavException on WebDAV error (like no 207 Multi-Status response) + */ + @Throws(IOException::class, HttpException::class, DavException::class) + fun propfind( + depth: Int, + vararg reqProp: + Property.Name, + listOfHeaders: HashMap?, + callback: DavResponseCallback, + rawCallback: (response: Response) -> Unit + ) { + // build XML request body + val serializer = XmlUtils.newSerializer() + val writer = StringWriter() + serializer.setOutput(writer) + serializer.setPrefix("", XmlUtils.NS_WEBDAV) + serializer.setPrefix("CAL", XmlUtils.NS_CALDAV) + serializer.setPrefix("CARD", XmlUtils.NS_CARDDAV) + serializer.setPrefix("SABRE", XmlUtils.NS_SABREDAV) + serializer.setPrefix("OC", XmlUtils.NS_OWNCLOUD) + serializer.startDocument("UTF-8", null) + serializer.startTag(XmlUtils.NS_WEBDAV, "propfind") + serializer.startTag(XmlUtils.NS_WEBDAV, "prop") + for (prop in reqProp) { + serializer.startTag(prop.namespace, prop.name) + serializer.endTag(prop.namespace, prop.name) + } + serializer.endTag(XmlUtils.NS_WEBDAV, "prop") + serializer.endTag(XmlUtils.NS_WEBDAV, "propfind") + serializer.endDocument() + + val requestBuilder = Request.Builder() + .url(location) + .method("PROPFIND", RequestBody.create(MIME_XML, writer.toString())) + .header("Depth", if (depth >= 0) depth.toString() else "infinity") + + listOfHeaders?.forEach { (name, value) -> + value?.let { + requestBuilder.header(name, value) + } + } + + followRedirects { + val call = httpClient.newCall( + requestBuilder.build() + ) + this.call = call + call.execute() + }.use { response -> + rawCallback(response) + processMultiStatus(response, callback) + } + } + +} diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/PropfindMethod.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/PropfindMethod.kt index 2a2913c0..f99fd394 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/PropfindMethod.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/PropfindMethod.kt @@ -37,7 +37,7 @@ import java.util.ArrayList * @author David González Verdugo */ class PropfindMethod( - url: URL?, + url: URL, // request val depth: Int, private val mPropertiesToRequest: Array @@ -53,6 +53,7 @@ class PropfindMethod( mDavResource.propfind( depth = depth, reqProp = *mPropertiesToRequest, + listOfHeaders = super.getRequestHeadersAsHashMap(), callback = { response: Response, hrefRelation: HrefRelation? -> when (hrefRelation) { HrefRelation.MEMBER -> mMembers.add(response) @@ -62,8 +63,8 @@ class PropfindMethod( else -> { } } - }, rawCallback = { response: okhttp3.Response -> - mResponse = response + }, rawCallback = { callBackResponse: okhttp3.Response -> + response = callBackResponse }) return statusCode } diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/PutMethod.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/PutMethod.kt index 55736a6e..c63b7327 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/PutMethod.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/PutMethod.kt @@ -33,18 +33,18 @@ import java.net.URL * * @author David González Verdugo */ -class PutMethod(url: URL?) : DavMethod(url) { +class PutMethod( + url: URL +) : DavMethod(url) { @Throws(IOException::class, HttpException::class) public override fun onExecute(): Int { mDavResource.put( - mRequestBody, + requestBody!!, super.getRequestHeader(HttpConstants.IF_MATCH_HEADER), - super.getRequestHeader(HttpConstants.CONTENT_TYPE_HEADER), - super.getRequestHeader(HttpConstants.OC_TOTAL_LENGTH_HEADER), - super.getRequestHeader(HttpConstants.OC_X_OC_MTIME_HEADER) - ) { response: Response -> - mResponse = response + getRequestHeadersAsHashMap() + ) { callBackResponse -> + response = callBackResponse } - return super.getStatusCode() + return super.statusCode } } diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/network/WebdavUtils.java b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/network/WebdavUtils.java index 5647dec4..e86f89d7 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/network/WebdavUtils.java +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/network/WebdavUtils.java @@ -34,9 +34,6 @@ import java.text.SimpleDateFormat; import java.util.Date; import java.util.Locale; -import static com.owncloud.android.lib.common.OwnCloudClient.WEBDAV_FILES_PATH_4_0; -import static com.owncloud.android.lib.common.OwnCloudClient.WEBDAV_PATH_4_0_AND_LATER; - public class WebdavUtils { private static final SimpleDateFormat[] DATETIME_FORMATS = { diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/CreateRemoteShareOperation.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/CreateRemoteShareOperation.kt index 5d5eeb39..e188f7a7 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/CreateRemoteShareOperation.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/CreateRemoteShareOperation.kt @@ -116,9 +116,7 @@ class CreateRemoteShareOperation( val uriBuilder = requestUri.buildUpon() uriBuilder.appendEncodedPath(ShareUtils.SHARING_API_PATH) - val postMethod = PostMethod(URL(uriBuilder.build().toString())) - - postMethod.setRequestBody(formBodyBuilder.build()) + val postMethod = PostMethod(URL(uriBuilder.build().toString()), formBodyBuilder.build()) postMethod.setRequestHeader(HttpConstants.CONTENT_TYPE_HEADER, HttpConstants.CONTENT_TYPE_URLENCODED_UTF8) postMethod.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE) @@ -133,7 +131,7 @@ class CreateRemoteShareOperation( parser.oneOrMoreSharesRequired = true parser.ownCloudVersion = client.ownCloudVersion parser.serverBaseUri = client.baseUri - result = parser.parse(postMethod.responseBodyAsString) + result = parser.parse(postMethod.responseBodyString) if (result.isSuccess && retrieveShareDetails) { // retrieve more info - POST only returns the index of the new share @@ -145,7 +143,7 @@ class CreateRemoteShareOperation( } } else { - result = parser.parse(postMethod.responseBodyAsString) + result = parser.parse(postMethod.responseBodyString) } } catch (e: Exception) { diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/GetRemoteShareesOperation.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/GetRemoteShareesOperation.kt index 11449fb0..c1ec316e 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/GetRemoteShareesOperation.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/GetRemoteShareesOperation.kt @@ -98,7 +98,7 @@ class GetRemoteShareesOperation getMethod.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE) val status = client.executeHttpMethod(getMethod) - val response = getMethod.responseBodyAsString + val response = getMethod.responseBodyString if (isSuccess(status)) { Timber.d("Successful response: $response") diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/GetRemoteSharesForFileOperation.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/GetRemoteSharesForFileOperation.kt index ae09166b..d58918e4 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/GetRemoteSharesForFileOperation.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/GetRemoteSharesForFileOperation.kt @@ -86,10 +86,10 @@ class GetRemoteSharesForFileOperation( ) parser.ownCloudVersion = client.ownCloudVersion parser.serverBaseUri = client.baseUri - result = parser.parse(getMethod.responseBodyAsString) + result = parser.parse(getMethod.responseBodyString) if (result.isSuccess) { - Timber.d("Got " + result.data.shares.size + " shares") + Timber.d("Got ${result.data.shares.size} shares") } } else { result = RemoteOperationResult(getMethod) diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/RemoveRemoteShareOperation.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/RemoveRemoteShareOperation.kt index 034be286..0df2e332 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/RemoveRemoteShareOperation.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/RemoveRemoteShareOperation.kt @@ -73,9 +73,9 @@ class RemoveRemoteShareOperation(private val remoteShareId: Long) : RemoteOperat val parser = ShareToRemoteOperationResultParser( ShareXMLParser() ) - result = parser.parse(deleteMethod.responseBodyAsString) + result = parser.parse(deleteMethod.responseBodyString) - Timber.d("Unshare " + remoteShareId + ": " + result.logMessage) + Timber.d("Unshare $remoteShareId: ${result.logMessage}") } else { result = RemoteOperationResult(deleteMethod) @@ -83,7 +83,7 @@ class RemoveRemoteShareOperation(private val remoteShareId: Long) : RemoteOperat } catch (e: Exception) { result = RemoteOperationResult(e) - Timber.e(e, "Unshare Link Exception " + result.logMessage) + Timber.e(e, "Unshare Link Exception ${result.logMessage}") } return result diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/UpdateRemoteShareOperation.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/UpdateRemoteShareOperation.kt index 0b4a08e5..e2af8959 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/UpdateRemoteShareOperation.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/UpdateRemoteShareOperation.kt @@ -143,9 +143,7 @@ class UpdateRemoteShareOperation uriBuilder.appendEncodedPath(ShareUtils.SHARING_API_PATH) uriBuilder.appendEncodedPath(remoteId.toString()) - val putMethod = PutMethod(URL(uriBuilder.build().toString())) - - putMethod.setRequestBody(formBodyBuilder.build()) + val putMethod = PutMethod(URL(uriBuilder.build().toString()), formBodyBuilder.build()) putMethod.setRequestHeader(HttpConstants.CONTENT_TYPE_HEADER, HttpConstants.CONTENT_TYPE_URLENCODED_UTF8) putMethod.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE) @@ -158,12 +156,12 @@ class UpdateRemoteShareOperation ) if (!isSuccess(status)) { - return parser.parse(putMethod.responseBodyAsString) + return parser.parse(putMethod.getResponseBodyAsString()) } parser.ownCloudVersion = client.ownCloudVersion parser.serverBaseUri = client.baseUri - result = parser.parse(putMethod.responseBodyAsString) + result = parser.parse(putMethod.getResponseBodyAsString()) if (result.isSuccess && retrieveShareDetails) { // retrieve more info - PUT only returns the index of the new share diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/status/GetRemoteCapabilitiesOperation.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/status/GetRemoteCapabilitiesOperation.kt index 256e03b6..f9f164eb 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/status/GetRemoteCapabilitiesOperation.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/status/GetRemoteCapabilitiesOperation.kt @@ -65,7 +65,7 @@ class GetRemoteCapabilitiesOperation : RemoteOperation() { } val status = client.executeHttpMethod(getMethod) - val response = getMethod.responseBodyAsString + val response = getMethod.getResponseBodyAsString() if (status == HttpConstants.HTTP_OK) { Timber.d("Successful response $response") diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/status/GetRemoteStatusOperation.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/status/GetRemoteStatusOperation.kt index b6fd1b39..f47544b1 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/status/GetRemoteStatusOperation.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/status/GetRemoteStatusOperation.kt @@ -108,7 +108,7 @@ class GetRemoteStatusOperation : RemoteOperation() { } if (isSuccess(status)) { - val respJSON = JSONObject(getMethod.responseBodyAsString) + val respJSON = JSONObject(getMethod.getResponseBodyAsString()) if (!respJSON.getBoolean(NODE_INSTALLED)) { latestResult = RemoteOperationResult(ResultCode.INSTANCE_NOT_CONFIGURED) } else { 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 8efd9def..76f82a46 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 @@ -21,6 +21,7 @@ package com.owncloud.android.lib.resources.status.services.implementation import android.net.Uri import com.owncloud.android.lib.common.OwnCloudClient +import com.owncloud.android.lib.common.authentication.OwnCloudCredentialsFactory.getAnonymousCredentials import com.owncloud.android.lib.common.operations.RemoteOperationResult import com.owncloud.android.lib.resources.status.services.ServerInfoService import com.owncloud.android.lib.resources.files.CheckPathExistenceRemoteOperation @@ -38,6 +39,6 @@ class OCServerInfoService : ServerInfoService { GetRemoteStatusOperation().execute(createClientFromPath(path)) private fun createClientFromPath(path: String): OwnCloudClient { - return OwnCloudClient(Uri.parse(path)) + return OwnCloudClient(Uri.parse(path)).apply { credentials = getAnonymousCredentials() } } } diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/users/GetRemoteUserAvatarOperation.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/users/GetRemoteUserAvatarOperation.kt index 0e998ebc..2efbacb6 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/users/GetRemoteUserAvatarOperation.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/users/GetRemoteUserAvatarOperation.kt @@ -58,7 +58,7 @@ class GetRemoteUserAvatarOperation(private val avatarDimension: Int) : RemoteOpe if (isSuccess(status)) { // find out size of file to read - val contentLength = getMethod.getResponseHeader(HttpConstants.CONTENT_LENGTH_HEADER).toInt() + val contentLength = getMethod.getResponseHeader(HttpConstants.CONTENT_LENGTH_HEADER)?.toInt() // find out MIME-type! val mimeType = getMethod.getResponseHeader(HttpConstants.CONTENT_TYPE_HEADER) @@ -69,8 +69,8 @@ class GetRemoteUserAvatarOperation(private val avatarDimension: Int) : RemoteOpe } /// download will be performed to a buffer - inputStream = getMethod.responseBodyAsStream - val bytesArray = inputStream.readBytes() + inputStream = getMethod.getResponseBodyAsStream() + val bytesArray = inputStream?.readBytes()?: byteArrayOf() // TODO check total bytes transferred? Timber.d("Avatar size: Bytes received ${bytesArray.size} of $contentLength") @@ -87,7 +87,7 @@ class GetRemoteUserAvatarOperation(private val avatarDimension: Int) : RemoteOpe } else { result = RemoteOperationResult(getMethod) - client.exhaustResponse(getMethod.responseBodyAsStream) + client.exhaustResponse(getMethod.getResponseBodyAsStream()) } } catch (e: Exception) { diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/users/GetRemoteUserInfoOperation.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/users/GetRemoteUserInfoOperation.kt index 7ea63c69..53600d29 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/users/GetRemoteUserInfoOperation.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/users/GetRemoteUserInfoOperation.kt @@ -53,7 +53,7 @@ class GetRemoteUserInfoOperation : RemoteOperation() { try { val getMethod = GetMethod(URL(client.baseUri.toString() + OCS_ROUTE)) val status = client.executeHttpMethod(getMethod) - val response = getMethod.responseBodyAsString + val response = getMethod.getResponseBodyAsString() ?: "" if (status == HttpConstants.HTTP_OK) { Timber.d("Successful response $response") From b287cb91486092f95c483328235039a7ce013470 Mon Sep 17 00:00:00 2001 From: agarcia Date: Wed, 1 Jul 2020 17:16:10 +0200 Subject: [PATCH 09/15] Compile project with latest changes from OkHttp and dav4jvm --- .../OwnCloudBasicCredentials.java | 5 +--- .../lib/common/http/methods/HttpBaseMethod.kt | 19 +++++++------- .../common/http/methods/webdav/DavMethod.kt | 19 +++++++------- .../common/http/methods/webdav/DavUtils.kt | 6 ++--- .../http/methods/webdav/OCDavResource.kt | 25 ++++++++++--------- .../http/methods/webdav/PropfindMethod.kt | 8 +++--- .../common/http/methods/webdav/PutMethod.kt | 2 +- .../files/ReadRemoteFileOperation.java | 2 ++ .../files/ReadRemoteFolderOperation.java | 7 +++--- 9 files changed, 47 insertions(+), 46 deletions(-) diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/authentication/OwnCloudBasicCredentials.java b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/authentication/OwnCloudBasicCredentials.java index 96e8a233..3f1eeb4c 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/authentication/OwnCloudBasicCredentials.java +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/authentication/OwnCloudBasicCredentials.java @@ -23,9 +23,6 @@ */ package com.owncloud.android.lib.common.authentication; -import com.owncloud.android.lib.common.OwnCloudClient; -import com.owncloud.android.lib.common.http.HttpClient; -import com.owncloud.android.lib.common.http.HttpConstants; import okhttp3.Credentials; import static java.nio.charset.StandardCharsets.UTF_8; @@ -52,7 +49,7 @@ public class OwnCloudBasicCredentials implements OwnCloudCredentials { @Override public String getHeaderAuth() { - return Credentials.basic(mUsername, mPassword, Util.UTF_8); + return Credentials.basic(mUsername, mPassword, UTF_8); } @Override 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 c71d065d..f29420d8 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 @@ -4,6 +4,7 @@ import com.owncloud.android.lib.common.http.HttpClient import okhttp3.Call import okhttp3.Headers import okhttp3.HttpUrl +import okhttp3.HttpUrl.Companion.toHttpUrlOrNull import okhttp3.OkHttpClient import okhttp3.Request import okhttp3.RequestBody @@ -15,7 +16,7 @@ import java.util.concurrent.TimeUnit abstract class HttpBaseMethod constructor(url: URL) { var okHttpClient: OkHttpClient - var httpUrl: HttpUrl = HttpUrl.parse(url.toString()) ?: throw MalformedURLException() + var httpUrl: HttpUrl = url.toHttpUrlOrNull() ?: throw MalformedURLException() var request: Request var requestBody: RequestBody? = null lateinit var response: Response @@ -47,7 +48,7 @@ abstract class HttpBaseMethod constructor(url: URL) { // Headers val requestHeaders: Headers - get() = request.headers() + get() = request.headers fun getRequestHeader(name: String): String? { return request.header(name) @@ -84,14 +85,14 @@ abstract class HttpBaseMethod constructor(url: URL) { *** Response *** ****************/ val statusCode: Int - get() = response.code() + get() = response.code val statusMessage: String - get() = response.message() + get() = response.message // Headers open fun getResponseHeaders(): Headers? { - return response.headers() + return response.headers } open fun getResponseHeader(headerName: String): String? { @@ -100,14 +101,14 @@ abstract class HttpBaseMethod constructor(url: URL) { // Body fun getResponseBodyAsString(): String? { - if (responseBodyString == null && response.body() != null) { - responseBodyString = response.body()?.string() + if (responseBodyString == null && response.body != null) { + responseBodyString = response.body?.string() } return responseBodyString } open fun getResponseBodyAsStream(): InputStream? { - return response.body()?.byteStream() + return response.body?.byteStream() } /************************* @@ -153,7 +154,7 @@ abstract class HttpBaseMethod constructor(url: URL) { } open val isAborted: Boolean - get() = call?.isCanceled ?: false + get() = call?.isCanceled() ?: false ////////////////////////////// // For override diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/DavMethod.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/DavMethod.kt index ce98bb4f..aa160966 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/DavMethod.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/DavMethod.kt @@ -23,15 +23,16 @@ */ package com.owncloud.android.lib.common.http.methods.webdav -import at.bitfire.dav4android.Constants.log -import at.bitfire.dav4android.exception.HttpException -import at.bitfire.dav4android.exception.RedirectException +import at.bitfire.dav4jvm.Dav4jvm.log +import at.bitfire.dav4jvm.exception.HttpException +import at.bitfire.dav4jvm.exception.RedirectException import com.owncloud.android.lib.common.http.HttpConstants import com.owncloud.android.lib.common.http.methods.HttpBaseMethod import okhttp3.HttpUrl +import okhttp3.HttpUrl.Companion.toHttpUrlOrNull import okhttp3.Protocol import okhttp3.Response -import okhttp3.ResponseBody +import okhttp3.ResponseBody.Companion.toResponseBody import java.net.MalformedURLException import java.net.URL import java.util.concurrent.TimeUnit @@ -45,7 +46,7 @@ abstract class DavMethod protected constructor(url: URL) : HttpBaseMethod(url) { protected var mDavResource: OCDavResource init { - val httpUrl = HttpUrl.parse(url.toString()) ?: throw MalformedURLException() + val httpUrl = url.toHttpUrlOrNull() ?: throw MalformedURLException() mDavResource = OCDavResource( okHttpClient, httpUrl, @@ -76,11 +77,9 @@ abstract class DavMethod protected constructor(url: URL) : HttpBaseMethod(url) { } else { // The check below should be included in okhttp library, method ResponseBody.create( // TODO check most recent versions of okhttp to see if this is already fixed and try to update if so - if (response.body()?.contentType() != null) { - val responseBody = ResponseBody.create( - response.body()?.contentType(), - httpException.responseBody?:"" - ) + if (response.body?.contentType() != null) { + val responseBody = (httpException.responseBody ?: "" + ).toResponseBody(response.body?.contentType()) response = response.newBuilder() .body(responseBody) .build() diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/DavUtils.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/DavUtils.kt index 6da28798..22e1981f 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/DavUtils.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/DavUtils.kt @@ -23,9 +23,9 @@ */ package com.owncloud.android.lib.common.http.methods.webdav -import at.bitfire.dav4android.Property -import at.bitfire.dav4android.PropertyUtils.getAllPropSet -import at.bitfire.dav4android.PropertyUtils.getQuotaPropset +import at.bitfire.dav4jvm.Property +import at.bitfire.dav4jvm.PropertyUtils.getAllPropSet +import at.bitfire.dav4jvm.PropertyUtils.getQuotaPropset object DavUtils { @JvmStatic val allPropset: Array diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/OCDavResource.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/OCDavResource.kt index 037f65ed..d8a53583 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/OCDavResource.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/OCDavResource.kt @@ -1,17 +1,18 @@ package com.owncloud.android.lib.common.http.methods.webdav -import at.bitfire.dav4android.DavResource -import at.bitfire.dav4android.DavResponseCallback -import at.bitfire.dav4android.IF_MATCH_HEADER -import at.bitfire.dav4android.Property -import at.bitfire.dav4android.QuotedStringUtils -import at.bitfire.dav4android.XmlUtils -import at.bitfire.dav4android.exception.DavException -import at.bitfire.dav4android.exception.HttpException +import at.bitfire.dav4jvm.DavResource +import at.bitfire.dav4jvm.DavResponseCallback +import at.bitfire.dav4jvm.IF_MATCH_HEADER +import at.bitfire.dav4jvm.Property +import at.bitfire.dav4jvm.QuotedStringUtils +import at.bitfire.dav4jvm.XmlUtils +import at.bitfire.dav4jvm.exception.DavException +import at.bitfire.dav4jvm.exception.HttpException import okhttp3.HttpUrl import okhttp3.OkHttpClient import okhttp3.Request import okhttp3.RequestBody +import okhttp3.RequestBody.Companion.toRequestBody import okhttp3.Response import java.io.IOException import java.io.StringWriter @@ -162,9 +163,9 @@ class OCDavResource( listOfHeaders: HashMap?, callback: (response: Response) -> Unit ) { - val rqBody = if (xmlBody != null) RequestBody.create(MIME_XML, xmlBody) else null + val requestBody = xmlBody?.toRequestBody(MIME_XML) val requestBuilder = Request.Builder() - .method("MKCOL", rqBody) + .method("MKCOL", requestBody) .url(location) listOfHeaders?.forEach { (name, value) -> value?.let { @@ -264,7 +265,7 @@ class OCDavResource( }.use { response -> callback(response) checkStatus(response) - if (response.code() == 207) + if (response.code == 207) /* If an error occurs deleting a member resource (a resource other than the resource identified in the Request-URI), then the response can be a 207 (Multi-Status). […] (RFC 4918 9.6.1. DELETE for Collections) */ @@ -316,7 +317,7 @@ class OCDavResource( val requestBuilder = Request.Builder() .url(location) - .method("PROPFIND", RequestBody.create(MIME_XML, writer.toString())) + .method("PROPFIND", writer.toString().toRequestBody(MIME_XML)) .header("Depth", if (depth >= 0) depth.toString() else "infinity") listOfHeaders?.forEach { (name, value) -> diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/PropfindMethod.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/PropfindMethod.kt index f99fd394..7fb0b05b 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/PropfindMethod.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/PropfindMethod.kt @@ -23,10 +23,10 @@ */ package com.owncloud.android.lib.common.http.methods.webdav -import at.bitfire.dav4android.Property -import at.bitfire.dav4android.Response -import at.bitfire.dav4android.Response.HrefRelation -import at.bitfire.dav4android.exception.DavException +import at.bitfire.dav4jvm.Property +import at.bitfire.dav4jvm.Response +import at.bitfire.dav4jvm.Response.HrefRelation +import at.bitfire.dav4jvm.exception.DavException import java.io.IOException import java.net.URL import java.util.ArrayList diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/PutMethod.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/PutMethod.kt index c63b7327..d19e7118 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/PutMethod.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/PutMethod.kt @@ -23,7 +23,7 @@ */ package com.owncloud.android.lib.common.http.methods.webdav -import at.bitfire.dav4android.exception.HttpException +import at.bitfire.dav4jvm.exception.HttpException import com.owncloud.android.lib.common.http.HttpConstants import java.io.IOException import java.net.URL diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/ReadRemoteFileOperation.java b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/ReadRemoteFileOperation.java index 55facfe4..9149e6d3 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/ReadRemoteFileOperation.java +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/ReadRemoteFileOperation.java @@ -26,6 +26,8 @@ package com.owncloud.android.lib.resources.files; import com.owncloud.android.lib.common.OwnCloudClient; import com.owncloud.android.lib.common.accounts.AccountUtils; import com.owncloud.android.lib.common.http.HttpConstants; +import com.owncloud.android.lib.common.http.methods.webdav.DavUtils; +import com.owncloud.android.lib.common.http.methods.webdav.PropfindMethod; import com.owncloud.android.lib.common.network.WebdavUtils; import com.owncloud.android.lib.common.operations.RemoteOperation; import com.owncloud.android.lib.common.operations.RemoteOperationResult; diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/ReadRemoteFolderOperation.java b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/ReadRemoteFolderOperation.java index 8a11bc9c..03cb3aa7 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/ReadRemoteFolderOperation.java +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/ReadRemoteFolderOperation.java @@ -29,6 +29,8 @@ import com.owncloud.android.lib.common.OwnCloudClient; import com.owncloud.android.lib.common.accounts.AccountUtils; import com.owncloud.android.lib.common.http.HttpConstants; import com.owncloud.android.lib.common.http.methods.webdav.DavConstants; +import com.owncloud.android.lib.common.http.methods.webdav.DavUtils; +import com.owncloud.android.lib.common.http.methods.webdav.PropfindMethod; import com.owncloud.android.lib.common.network.WebdavUtils; import com.owncloud.android.lib.common.operations.RemoteOperation; import com.owncloud.android.lib.common.operations.RemoteOperationResult; @@ -120,7 +122,6 @@ public class ReadRemoteFolderOperation extends RemoteOperation Date: Thu, 2 Jul 2020 12:42:21 +0200 Subject: [PATCH 10/15] Remove interceptor and fix some errors related with shares --- .../android/lib/common/http/HttpClient.java | 10 ---- .../http/interceptors/HttpInterceptor.java | 52 ------------------- .../lib/common/http/methods/HttpBaseMethod.kt | 2 +- .../http/methods/webdav/OCDavResource.kt | 3 +- .../shares/CreateRemoteShareOperation.kt | 4 +- .../shares/GetRemoteShareesOperation.kt | 2 +- .../shares/GetRemoteSharesForFileOperation.kt | 2 +- .../shares/RemoveRemoteShareOperation.kt | 2 +- 8 files changed, 7 insertions(+), 70 deletions(-) delete mode 100644 owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/interceptors/HttpInterceptor.java diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/HttpClient.java b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/HttpClient.java index f9939385..12571a88 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/HttpClient.java +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/HttpClient.java @@ -26,7 +26,6 @@ package com.owncloud.android.lib.common.http; import android.content.Context; -import com.owncloud.android.lib.common.http.interceptors.HttpInterceptor; import com.owncloud.android.lib.common.network.AdvancedX509TrustManager; import com.owncloud.android.lib.common.network.NetworkUtils; import okhttp3.Cookie; @@ -56,7 +55,6 @@ import java.util.concurrent.TimeUnit; */ public class HttpClient { private static OkHttpClient sOkHttpClient; - private static HttpInterceptor sOkHttpInterceptor; private static Context sContext; private static HashMap> sCookieStore = new HashMap<>(); @@ -112,7 +110,6 @@ public class HttpClient { }; OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder() - .addInterceptor(getOkHttpInterceptor()) .protocols(Arrays.asList(Protocol.HTTP_1_1)) .readTimeout(HttpConstants.DEFAULT_DATA_TIMEOUT, TimeUnit.MILLISECONDS) .writeTimeout(HttpConstants.DEFAULT_DATA_TIMEOUT, TimeUnit.MILLISECONDS) @@ -132,13 +129,6 @@ public class HttpClient { return sOkHttpClient; } - private static HttpInterceptor getOkHttpInterceptor() { - if (sOkHttpInterceptor == null) { - sOkHttpInterceptor = new HttpInterceptor(); - } - return sOkHttpInterceptor; - } - public Context getContext() { return sContext; } diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/interceptors/HttpInterceptor.java b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/interceptors/HttpInterceptor.java deleted file mode 100644 index b7e6b6c9..00000000 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/interceptors/HttpInterceptor.java +++ /dev/null @@ -1,52 +0,0 @@ -/* 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.common.http.interceptors; - -import okhttp3.Interceptor; -import okhttp3.Request; -import okhttp3.Response; -import org.jetbrains.annotations.NotNull; -import timber.log.Timber; - -import java.io.IOException; - -/** - * Http interceptor to use multiple interceptors in the same {@link okhttp3.OkHttpClient} instance - * - * @author David González Verdugo - */ -public class HttpInterceptor implements Interceptor { - - @NotNull - @Override - public Response intercept(Chain chain) throws IOException { - Request request = chain.request(); - - Timber.d("Executing in request headers %s", request.headers().toString()); - - return chain.proceed(request); - } - -} 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 f29420d8..bf67caf6 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 @@ -20,7 +20,7 @@ abstract class HttpBaseMethod constructor(url: URL) { var request: Request var requestBody: RequestBody? = null lateinit var response: Response - var responseBodyString: String? = null + private var responseBodyString: String? = null var call: Call? = null diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/OCDavResource.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/OCDavResource.kt index d8a53583..7edf43f8 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/OCDavResource.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/OCDavResource.kt @@ -289,8 +289,7 @@ class OCDavResource( @Throws(IOException::class, HttpException::class, DavException::class) fun propfind( depth: Int, - vararg reqProp: - Property.Name, + vararg reqProp: Property.Name, listOfHeaders: HashMap?, callback: DavResponseCallback, rawCallback: (response: Response) -> Unit diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/CreateRemoteShareOperation.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/CreateRemoteShareOperation.kt index e188f7a7..896f570a 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/CreateRemoteShareOperation.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/CreateRemoteShareOperation.kt @@ -131,7 +131,7 @@ class CreateRemoteShareOperation( parser.oneOrMoreSharesRequired = true parser.ownCloudVersion = client.ownCloudVersion parser.serverBaseUri = client.baseUri - result = parser.parse(postMethod.responseBodyString) + result = parser.parse(postMethod.getResponseBodyAsString()) if (result.isSuccess && retrieveShareDetails) { // retrieve more info - POST only returns the index of the new share @@ -143,7 +143,7 @@ class CreateRemoteShareOperation( } } else { - result = parser.parse(postMethod.responseBodyString) + result = parser.parse(postMethod.getResponseBodyAsString()) } } catch (e: Exception) { diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/GetRemoteShareesOperation.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/GetRemoteShareesOperation.kt index c1ec316e..e3432b5b 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/GetRemoteShareesOperation.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/GetRemoteShareesOperation.kt @@ -98,7 +98,7 @@ class GetRemoteShareesOperation getMethod.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE) val status = client.executeHttpMethod(getMethod) - val response = getMethod.responseBodyString + val response = getMethod.getResponseBodyAsString() if (isSuccess(status)) { Timber.d("Successful response: $response") diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/GetRemoteSharesForFileOperation.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/GetRemoteSharesForFileOperation.kt index d58918e4..ea1d7e2a 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/GetRemoteSharesForFileOperation.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/GetRemoteSharesForFileOperation.kt @@ -86,7 +86,7 @@ class GetRemoteSharesForFileOperation( ) parser.ownCloudVersion = client.ownCloudVersion parser.serverBaseUri = client.baseUri - result = parser.parse(getMethod.responseBodyString) + result = parser.parse(getMethod.getResponseBodyAsString()) if (result.isSuccess) { Timber.d("Got ${result.data.shares.size} shares") diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/RemoveRemoteShareOperation.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/RemoveRemoteShareOperation.kt index 0df2e332..5fa18169 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/RemoveRemoteShareOperation.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/RemoveRemoteShareOperation.kt @@ -73,7 +73,7 @@ class RemoveRemoteShareOperation(private val remoteShareId: Long) : RemoteOperat val parser = ShareToRemoteOperationResultParser( ShareXMLParser() ) - result = parser.parse(deleteMethod.responseBodyString) + result = parser.parse(deleteMethod.getResponseBodyAsString()) Timber.d("Unshare $remoteShareId: ${result.logMessage}") From 69497645ec3703828c26f7e319975cecd62fb425 Mon Sep 17 00:00:00 2001 From: agarcia Date: Fri, 3 Jul 2020 15:27:55 +0200 Subject: [PATCH 11/15] Update dav4android and fix timestamp error depending on locale --- owncloudComLibrary/build.gradle | 2 +- .../common/http/methods/webdav/DavMethod.kt | 17 +- .../http/methods/webdav/OCDavResource.kt | 340 ------------------ 3 files changed, 10 insertions(+), 349 deletions(-) delete mode 100644 owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/OCDavResource.kt diff --git a/owncloudComLibrary/build.gradle b/owncloudComLibrary/build.gradle index a75309e4..ca70bc23 100644 --- a/owncloudComLibrary/build.gradle +++ b/owncloudComLibrary/build.gradle @@ -6,7 +6,7 @@ apply plugin: 'kotlin-allopen' dependencies { api 'com.squareup.okhttp3:okhttp:4.6.0' implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlinVersion" - api 'com.gitlab.ownclouders:dav4android:oc_support_2.1' + api 'com.gitlab.ownclouders:dav4android:oc_support_2.1.2' api 'com.github.hannesa2:Logcat:1.6.0' api 'net.openid:appauth:0.7.1' diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/DavMethod.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/DavMethod.kt index aa160966..09efda64 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/DavMethod.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/DavMethod.kt @@ -24,6 +24,7 @@ package com.owncloud.android.lib.common.http.methods.webdav import at.bitfire.dav4jvm.Dav4jvm.log +import at.bitfire.dav4jvm.DavOCResource import at.bitfire.dav4jvm.exception.HttpException import at.bitfire.dav4jvm.exception.RedirectException import com.owncloud.android.lib.common.http.HttpConstants @@ -43,11 +44,11 @@ import java.util.concurrent.TimeUnit * @author David González Verdugo */ abstract class DavMethod protected constructor(url: URL) : HttpBaseMethod(url) { - protected var mDavResource: OCDavResource + protected var mDavResource: DavOCResource init { val httpUrl = url.toHttpUrlOrNull() ?: throw MalformedURLException() - mDavResource = OCDavResource( + mDavResource = DavOCResource( okHttpClient, httpUrl, log @@ -95,7 +96,7 @@ abstract class DavMethod protected constructor(url: URL) : HttpBaseMethod(url) { // Connection parameters override fun setReadTimeout(readTimeout: Long, timeUnit: TimeUnit) { super.setReadTimeout(readTimeout, timeUnit) - mDavResource = OCDavResource( + mDavResource = DavOCResource( okHttpClient, httpUrl, log @@ -107,7 +108,7 @@ abstract class DavMethod protected constructor(url: URL) : HttpBaseMethod(url) { timeUnit: TimeUnit ) { super.setConnectionTimeout(connectionTimeout, timeUnit) - mDavResource = OCDavResource( + mDavResource = DavOCResource( okHttpClient, httpUrl, log @@ -116,7 +117,7 @@ abstract class DavMethod protected constructor(url: URL) : HttpBaseMethod(url) { override fun setFollowRedirects(followRedirects: Boolean) { super.setFollowRedirects(followRedirects) - mDavResource = OCDavResource( + mDavResource = DavOCResource( okHttpClient, httpUrl, log @@ -125,7 +126,7 @@ abstract class DavMethod protected constructor(url: URL) : HttpBaseMethod(url) { override fun setUrl(url: HttpUrl) { super.setUrl(url) - mDavResource = OCDavResource( + mDavResource = DavOCResource( okHttpClient, httpUrl, log @@ -134,7 +135,7 @@ abstract class DavMethod protected constructor(url: URL) : HttpBaseMethod(url) { override fun setRequestHeader(name: String, value: String) { super.setRequestHeader(name, value) - mDavResource = OCDavResource( + mDavResource = DavOCResource( okHttpClient, httpUrl, log @@ -150,7 +151,7 @@ abstract class DavMethod protected constructor(url: URL) : HttpBaseMethod(url) { ////////////////////////////// override fun setRetryOnConnectionFailure(retryOnConnectionFailure: Boolean) { super.setRetryOnConnectionFailure(retryOnConnectionFailure) - mDavResource = OCDavResource( + mDavResource = DavOCResource( okHttpClient, httpUrl, log diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/OCDavResource.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/OCDavResource.kt deleted file mode 100644 index 7edf43f8..00000000 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/OCDavResource.kt +++ /dev/null @@ -1,340 +0,0 @@ -package com.owncloud.android.lib.common.http.methods.webdav - -import at.bitfire.dav4jvm.DavResource -import at.bitfire.dav4jvm.DavResponseCallback -import at.bitfire.dav4jvm.IF_MATCH_HEADER -import at.bitfire.dav4jvm.Property -import at.bitfire.dav4jvm.QuotedStringUtils -import at.bitfire.dav4jvm.XmlUtils -import at.bitfire.dav4jvm.exception.DavException -import at.bitfire.dav4jvm.exception.HttpException -import okhttp3.HttpUrl -import okhttp3.OkHttpClient -import okhttp3.Request -import okhttp3.RequestBody -import okhttp3.RequestBody.Companion.toRequestBody -import okhttp3.Response -import java.io.IOException -import java.io.StringWriter -import java.util.logging.Logger - -class OCDavResource( - httpClient: OkHttpClient, - location: HttpUrl, - log: Logger -) : DavResource(httpClient, location, log) { - - /** - * Sends a PUT request to the resource. - * @param body new resource body to upload - * @param ifMatchETag value of "If-Match" header to set, or null to omit - * @param ifNoneMatch indicates whether "If-None-Match: *" ("don't overwrite anything existing") header shall be sent - * @param contentType - * @param ocTotalLength total length of resource body - * @param ocXOcMtimeHeader modification time - * @return true if the request was redirected successfully, i.e. #{@link #location} and maybe resource name may have changed - * @throws IOException on I/O error - * @throws HttpException on HTTP error - */ - @Throws(IOException::class, HttpException::class) - fun put( - body: RequestBody, - ifMatchETag: String?, - listOfHeaders: HashMap?, - callback: (response: Response) -> Unit - ) { - val requestBuilder = Request.Builder() - .put(body) - - listOfHeaders?.forEach { (name, value) -> - value?.let { - requestBuilder.header(name, value) - } - } - - if (ifMatchETag != null) - // only overwrite specific version - requestBuilder.header(IF_MATCH_HEADER, QuotedStringUtils.asQuotedString(ifMatchETag)) -// if (contentType != null) { -// requestBuilder.header(CONTENT_TYPE_HEADER, contentType) -// } -// if (ocTotalLength != null) { -// requestBuilder.header(OC_TOTAL_LENGTH_HEADER, ocTotalLength) -// } -// if (ocXOcMtimeHeader != null) { -// requestBuilder.header(OC_X_OC_MTIME_HEADER, ocXOcMtimeHeader) -// } - - followRedirects { - requestBuilder - .url(location) - val call = httpClient.newCall(requestBuilder.build()) - - this.call = call - call.execute() - }.use { response -> - callback(response) - checkStatus(response) - } - } - - /** - * Sends a MOVE request to the resource - * @param ocTotalLength total length of resource body - * @param ocXOcMtimeHeader modification time - */ - @Throws(IOException::class, HttpException::class, DavException::class) - fun move( - destination: String, - forceOverride: Boolean, - listOfHeaders: HashMap?, - callback: (response: Response) -> Unit - ) { - val requestBuilder = Request.Builder() - .method("MOVE", null) - .header("Content-Length", "0") - .header("Destination", destination) - - if (forceOverride) - requestBuilder.header("Overwrite", "F") - - listOfHeaders?.forEach { (name, value) -> - value?.let { - requestBuilder.header(name, value) - } - } -// if (ocTotalLength != null) -// requestBuilder.header(OC_TOTAL_LENGTH_HEADER, ocTotalLength) -// if (ocXOcMtimeHeader != null) -// requestBuilder.header(OC_X_OC_MTIME_HEADER, ocXOcMtimeHeader) - - followRedirects { - requestBuilder.url(location) - val call = httpClient.newCall(requestBuilder.build()) - this.call = call - call.execute() - }.use { response -> - callback(response) - checkStatus(response) - } - } - - @Throws(IOException::class, HttpException::class, DavException::class) - fun copy( - destination: String, - forceOverride: Boolean, - listOfHeaders: HashMap?, - callback: (response: Response) -> Unit - ) { - val requestBuilder = Request.Builder() - .method("COPY", null) - .header("Content-Length", "0") - .header("Destination", destination) - if (forceOverride) - requestBuilder.header("Overwrite", "F") - - listOfHeaders?.forEach { (name, value) -> - value?.let { - requestBuilder.header(name, value) - } - } - - followRedirects { - requestBuilder.url(location) - val call = httpClient.newCall(requestBuilder.build()) - - this.call = call - call.execute() - }.use { response -> - callback(response) - checkStatus(response) - } - } - - /** - * Sends a MKCOL request to this resource. Follows up to [MAX_REDIRECTS] redirects. - * - * @throws IOException on I/O error - * @throws HttpException on HTTP error - */ - @Throws(IOException::class, HttpException::class) - fun mkCol( - xmlBody: String?, - listOfHeaders: HashMap?, - callback: (response: Response) -> Unit - ) { - val requestBody = xmlBody?.toRequestBody(MIME_XML) - val requestBuilder = Request.Builder() - .method("MKCOL", requestBody) - .url(location) - listOfHeaders?.forEach { (name, value) -> - value?.let { - requestBuilder.header(name, value) - } - } - followRedirects { - val call = httpClient.newCall( - requestBuilder.build() - ) - this.call = call - call.execute() - }.use { response -> - callback(response) - checkStatus(response) - } - } - - /** - * Sends a GET request to the resource. Sends `Accept-Encoding: identity` to disable - * compression, because compression might change the ETag. - * - * Follows up to [MAX_REDIRECTS] redirects. - * - * @param accept value of Accept header (must not be null, but may be */*) - * @param callback called with server response unless an exception is thrown - * - * @throws IOException on I/O error - * @throws HttpException on HTTP error - */ - @Throws(IOException::class, HttpException::class) - fun get( - accept: String, - listOfHeaders: HashMap?, - callback: (response: Response) -> Unit - ) { - val requestBuilder = Request.Builder() - .get() - .url(location) - .header("Accept", accept) - .header("Accept-Encoding", "identity") // disable compression because it can change the ETag - - listOfHeaders?.forEach { (name, value) -> - value?.let { - requestBuilder.header(name, value) - } - } - - followRedirects { - val call = httpClient.newCall( - requestBuilder - .build() - ) - this.call = call - call.execute() - }.use { response -> - callback(response) - checkStatus(response) - } - } - - /** - * Sends a DELETE request to the resource. Warning: Sending this request to a collection will - * delete the collection with all its contents! - * - * Follows up to [MAX_REDIRECTS] redirects. - * - * @param ifMatchETag value of `If-Match` header to set, or null to omit - * @param callback called with server response unless an exception is thrown - * - * @throws IOException on I/O error - * @throws HttpException on HTTP errors, or when 207 Multi-Status is returned - * (because then there was probably a problem with a member resource) - */ - @Throws(IOException::class, HttpException::class) - fun delete( - ifMatchETag: String?, - listOfHeaders: HashMap?, - callback: (Response) -> Unit - ) { - followRedirects { - val requestBuilder = Request.Builder() - .delete() - .url(location) - if (ifMatchETag != null) - requestBuilder.header("If-Match", QuotedStringUtils.asQuotedString(ifMatchETag)) - listOfHeaders?.forEach { (name, value) -> - value?.let { - requestBuilder.header(name, value) - } - } - - val call = httpClient.newCall(requestBuilder.build()) - - this.call = call - call.execute() - }.use { response -> - callback(response) - checkStatus(response) - if (response.code == 207) - /* If an error occurs deleting a member resource (a resource other than - the resource identified in the Request-URI), then the response can be - a 207 (Multi-Status). […] (RFC 4918 9.6.1. DELETE for Collections) */ - throw HttpException(response) - } - } - - /** - * Sends a PROPFIND request to the resource. Expects and processes a 207 Multi-Status response. - * - * Follows up to [MAX_REDIRECTS] redirects. - * - * @param depth "Depth" header to send (-1 for `infinity`) - * @param reqProp properties to request - * @param callback called for every XML response element in the Multi-Status response - * - * @throws IOException on I/O error - * @throws HttpException on HTTP error - * @throws DavException on WebDAV error (like no 207 Multi-Status response) - */ - @Throws(IOException::class, HttpException::class, DavException::class) - fun propfind( - depth: Int, - vararg reqProp: Property.Name, - listOfHeaders: HashMap?, - callback: DavResponseCallback, - rawCallback: (response: Response) -> Unit - ) { - // build XML request body - val serializer = XmlUtils.newSerializer() - val writer = StringWriter() - serializer.setOutput(writer) - serializer.setPrefix("", XmlUtils.NS_WEBDAV) - serializer.setPrefix("CAL", XmlUtils.NS_CALDAV) - serializer.setPrefix("CARD", XmlUtils.NS_CARDDAV) - serializer.setPrefix("SABRE", XmlUtils.NS_SABREDAV) - serializer.setPrefix("OC", XmlUtils.NS_OWNCLOUD) - serializer.startDocument("UTF-8", null) - serializer.startTag(XmlUtils.NS_WEBDAV, "propfind") - serializer.startTag(XmlUtils.NS_WEBDAV, "prop") - for (prop in reqProp) { - serializer.startTag(prop.namespace, prop.name) - serializer.endTag(prop.namespace, prop.name) - } - serializer.endTag(XmlUtils.NS_WEBDAV, "prop") - serializer.endTag(XmlUtils.NS_WEBDAV, "propfind") - serializer.endDocument() - - val requestBuilder = Request.Builder() - .url(location) - .method("PROPFIND", writer.toString().toRequestBody(MIME_XML)) - .header("Depth", if (depth >= 0) depth.toString() else "infinity") - - listOfHeaders?.forEach { (name, value) -> - value?.let { - requestBuilder.header(name, value) - } - } - - followRedirects { - val call = httpClient.newCall( - requestBuilder.build() - ) - this.call = call - call.execute() - }.use { response -> - rawCallback(response) - processMultiStatus(response, callback) - } - } - -} From f89cfd91a947991f0554bcde539c2f17b67dd1ef Mon Sep 17 00:00:00 2001 From: agarcia Date: Wed, 8 Jul 2020 17:51:14 +0200 Subject: [PATCH 12/15] Reformat some code thanks to code review --- .../android/lib/common/OwnCloudClient.java | 12 +++---- .../lib/common/http/methods/HttpBaseMethod.kt | 10 ++---- .../http/methods/nonwebdav/HttpMethod.kt | 15 +++++---- .../http/methods/nonwebdav/PutMethod.kt | 5 ++- .../common/http/methods/webdav/CopyMethod.kt | 2 +- .../common/http/methods/webdav/DavMethod.kt | 31 +++++++++---------- .../common/http/methods/webdav/MkColMethod.kt | 2 +- .../common/http/methods/webdav/MoveMethod.kt | 3 +- .../http/methods/webdav/PropfindMethod.kt | 21 +++++-------- .../common/http/methods/webdav/PutMethod.kt | 2 +- .../network/AdvancedX509TrustManager.java | 3 +- .../lib/common/network/FileRequestBody.java | 2 +- .../ChunkedUploadRemoteFileOperation.java | 1 - 13 files changed, 49 insertions(+), 60 deletions(-) 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 2b2b5a7c..7491e0ae 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 @@ -101,12 +101,12 @@ public class OwnCloudClient extends HttpClient { // Header to allow tracing requests in apache and ownCloud logs Timber.d("Executing in request with id %s", requestId); - method.setRequestHeader(HttpConstants.OC_X_REQUEST_ID,requestId); + method.setRequestHeader(HttpConstants.OC_X_REQUEST_ID, requestId); method.setRequestHeader(HttpConstants.USER_AGENT_HEADER, SingleSessionManager.getUserAgent()); method.setRequestHeader(HttpConstants.PARAM_SINGLE_COOKIE_HEADER, "true"); method.setRequestHeader(HttpConstants.ACCEPT_ENCODING_HEADER, HttpConstants.ACCEPT_ENCODING_IDENTITY); - if(mCredentials.getHeaderAuth()!=null){ - method.setRequestHeader(HttpConstants.AUTHORIZATION_HEADER,mCredentials.getHeaderAuth()); + if (mCredentials.getHeaderAuth() != null) { + method.setRequestHeader(HttpConstants.AUTHORIZATION_HEADER, mCredentials.getHeaderAuth()); } status = method.execute(); @@ -133,12 +133,12 @@ public class OwnCloudClient extends HttpClient { // Header to allow tracing requests in apache and ownCloud logs Timber.d("Executing in request with id %s", requestId); - method.setRequestHeader(OC_X_REQUEST_ID,requestId); + method.setRequestHeader(OC_X_REQUEST_ID, requestId); method.setRequestHeader(HttpConstants.USER_AGENT_HEADER, SingleSessionManager.getUserAgent()); method.setRequestHeader(HttpConstants.PARAM_SINGLE_COOKIE_HEADER, "true"); method.setRequestHeader(HttpConstants.ACCEPT_ENCODING_HEADER, HttpConstants.ACCEPT_ENCODING_IDENTITY); - if(mCredentials.getHeaderAuth()!=null){ - method.setRequestHeader(HttpConstants.AUTHORIZATION_HEADER,mCredentials.getHeaderAuth()); + if (mCredentials.getHeaderAuth() != null) { + method.setRequestHeader(HttpConstants.AUTHORIZATION_HEADER, mCredentials.getHeaderAuth()); } status = method.execute(); 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 bf67caf6..4f9541ef 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 @@ -19,8 +19,7 @@ abstract class HttpBaseMethod constructor(url: URL) { var httpUrl: HttpUrl = url.toHttpUrlOrNull() ?: throw MalformedURLException() var request: Request var requestBody: RequestBody? = null - lateinit var response: Response - private var responseBodyString: String? = null + abstract var response: Response var call: Call? = null @@ -100,12 +99,7 @@ abstract class HttpBaseMethod constructor(url: URL) { } // Body - fun getResponseBodyAsString(): String? { - if (responseBodyString == null && response.body != null) { - responseBodyString = response.body?.string() - } - return responseBodyString - } + fun getResponseBodyAsString(): String? = response.body?.string() open fun getResponseBodyAsStream(): InputStream? { return response.body?.byteStream() diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/HttpMethod.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/HttpMethod.kt index 52c874cb..da759ada 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/HttpMethod.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/HttpMethod.kt @@ -23,18 +23,21 @@ */ package com.owncloud.android.lib.common.http.methods.nonwebdav -import com.owncloud.android.lib.common.http.methods.HttpBaseMethod; - -import java.io.IOException; -import java.net.URL; +import com.owncloud.android.lib.common.http.methods.HttpBaseMethod +import okhttp3.Response +import java.net.URL /** * Wrapper to perform OkHttp calls * * @author David González Verdugo */ -abstract class HttpMethod(url: URL) : HttpBaseMethod(url) { - @Throws(IOException::class) +abstract class HttpMethod( + url: URL +) : HttpBaseMethod(url) { + + override lateinit var response: Response + public override fun onExecute(): Int { call = okHttpClient.newCall(request) call?.let { response = it.execute() } diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/PutMethod.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/PutMethod.kt index 524f06a6..a8fdd67e 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/PutMethod.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/PutMethod.kt @@ -32,7 +32,10 @@ import java.net.URL * * @author David González Verdugo */ -class PutMethod(url: URL, private val putRequestBody: RequestBody) : HttpMethod(url) { +class PutMethod( + url: URL, + private val putRequestBody: RequestBody +) : HttpMethod(url) { @Throws(IOException::class) override fun onExecute(): Int { requestBody = putRequestBody diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/CopyMethod.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/CopyMethod.kt index 9da7c22e..2c816d75 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/CopyMethod.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/CopyMethod.kt @@ -39,7 +39,7 @@ class CopyMethod( ) : DavMethod(url) { @Throws(Exception::class) public override fun onExecute(): Int { - mDavResource.copy( + davResource.copy( destinationUrl, forceOverride, super.getRequestHeadersAsHashMap() diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/DavMethod.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/DavMethod.kt index 09efda64..b943877c 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/DavMethod.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/DavMethod.kt @@ -44,11 +44,13 @@ import java.util.concurrent.TimeUnit * @author David González Verdugo */ abstract class DavMethod protected constructor(url: URL) : HttpBaseMethod(url) { - protected var mDavResource: DavOCResource + protected var davResource: DavOCResource + + override lateinit var response: Response init { val httpUrl = url.toHttpUrlOrNull() ?: throw MalformedURLException() - mDavResource = DavOCResource( + davResource = DavOCResource( okHttpClient, httpUrl, log @@ -56,7 +58,7 @@ abstract class DavMethod protected constructor(url: URL) : HttpBaseMethod(url) { } override fun abort() { - mDavResource.cancelCall() + davResource.cancelCall() } @Throws(Exception::class) @@ -79,8 +81,7 @@ abstract class DavMethod protected constructor(url: URL) : HttpBaseMethod(url) { // The check below should be included in okhttp library, method ResponseBody.create( // TODO check most recent versions of okhttp to see if this is already fixed and try to update if so if (response.body?.contentType() != null) { - val responseBody = (httpException.responseBody ?: "" - ).toResponseBody(response.body?.contentType()) + val responseBody = (httpException.responseBody ?: "").toResponseBody(response.body?.contentType()) response = response.newBuilder() .body(responseBody) .build() @@ -96,7 +97,7 @@ abstract class DavMethod protected constructor(url: URL) : HttpBaseMethod(url) { // Connection parameters override fun setReadTimeout(readTimeout: Long, timeUnit: TimeUnit) { super.setReadTimeout(readTimeout, timeUnit) - mDavResource = DavOCResource( + davResource = DavOCResource( okHttpClient, httpUrl, log @@ -108,7 +109,7 @@ abstract class DavMethod protected constructor(url: URL) : HttpBaseMethod(url) { timeUnit: TimeUnit ) { super.setConnectionTimeout(connectionTimeout, timeUnit) - mDavResource = DavOCResource( + davResource = DavOCResource( okHttpClient, httpUrl, log @@ -117,7 +118,7 @@ abstract class DavMethod protected constructor(url: URL) : HttpBaseMethod(url) { override fun setFollowRedirects(followRedirects: Boolean) { super.setFollowRedirects(followRedirects) - mDavResource = DavOCResource( + davResource = DavOCResource( okHttpClient, httpUrl, log @@ -126,7 +127,7 @@ abstract class DavMethod protected constructor(url: URL) : HttpBaseMethod(url) { override fun setUrl(url: HttpUrl) { super.setUrl(url) - mDavResource = DavOCResource( + davResource = DavOCResource( okHttpClient, httpUrl, log @@ -135,23 +136,19 @@ abstract class DavMethod protected constructor(url: URL) : HttpBaseMethod(url) { override fun setRequestHeader(name: String, value: String) { super.setRequestHeader(name, value) - mDavResource = DavOCResource( + davResource = DavOCResource( okHttpClient, httpUrl, log ) } - fun getRetryOnConnectionFailure(): Boolean { - return false //TODO: implement me - } - ////////////////////////////// // Getter ////////////////////////////// override fun setRetryOnConnectionFailure(retryOnConnectionFailure: Boolean) { super.setRetryOnConnectionFailure(retryOnConnectionFailure) - mDavResource = DavOCResource( + davResource = DavOCResource( okHttpClient, httpUrl, log @@ -159,6 +156,6 @@ abstract class DavMethod protected constructor(url: URL) : HttpBaseMethod(url) { } override val isAborted: Boolean - get() = mDavResource.isCallAborted() + get() = davResource.isCallAborted() -} \ No newline at end of file +} diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/MkColMethod.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/MkColMethod.kt index 088a2c6a..03c8aff0 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/MkColMethod.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/MkColMethod.kt @@ -35,7 +35,7 @@ import java.net.URL class MkColMethod(url: URL) : DavMethod(url) { @Throws(Exception::class) public override fun onExecute(): Int { - mDavResource.mkCol( + davResource.mkCol( xmlBody = null, listOfHeaders = super.getRequestHeadersAsHashMap() ) { callBackResponse: Response -> diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/MoveMethod.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/MoveMethod.kt index 9aaa5b20..4c65cb98 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/MoveMethod.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/MoveMethod.kt @@ -23,7 +23,6 @@ */ package com.owncloud.android.lib.common.http.methods.webdav -import com.owncloud.android.lib.common.http.HttpConstants import okhttp3.Response import java.net.URL @@ -40,7 +39,7 @@ class MoveMethod( ) : DavMethod(url) { @Throws(Exception::class) public override fun onExecute(): Int { - mDavResource.move( + davResource.move( destinationUrl, forceOverride, super.getRequestHeadersAsHashMap() diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/PropfindMethod.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/PropfindMethod.kt index 7fb0b05b..66cc313b 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/PropfindMethod.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/PropfindMethod.kt @@ -29,7 +29,6 @@ import at.bitfire.dav4jvm.Response.HrefRelation import at.bitfire.dav4jvm.exception.DavException import java.io.IOException import java.net.URL -import java.util.ArrayList /** * Propfind calls wrapper @@ -38,30 +37,27 @@ import java.util.ArrayList */ class PropfindMethod( url: URL, - // request - val depth: Int, - private val mPropertiesToRequest: Array + private val depth: Int, + private val propertiesToRequest: Array ) : DavMethod(url) { // response - private val mMembers: MutableList + val members: MutableList var root: Response? private set @Throws(IOException::class, DavException::class) public override fun onExecute(): Int { - mDavResource.propfind( + davResource.propfind( depth = depth, - reqProp = *mPropertiesToRequest, + reqProp = *propertiesToRequest, listOfHeaders = super.getRequestHeadersAsHashMap(), callback = { response: Response, hrefRelation: HrefRelation? -> when (hrefRelation) { - HrefRelation.MEMBER -> mMembers.add(response) + HrefRelation.MEMBER -> members.add(response) HrefRelation.SELF -> this.root = response HrefRelation.OTHER -> { } - else -> { - } } }, rawCallback = { callBackResponse: okhttp3.Response -> response = callBackResponse @@ -69,11 +65,8 @@ class PropfindMethod( return statusCode } - val members: List - get() = mMembers - init { - mMembers = ArrayList() + members = arrayListOf() this.root = null } } diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/PutMethod.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/PutMethod.kt index d19e7118..4d482652 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/PutMethod.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/PutMethod.kt @@ -38,7 +38,7 @@ class PutMethod( ) : DavMethod(url) { @Throws(IOException::class, HttpException::class) public override fun onExecute(): Int { - mDavResource.put( + davResource.put( requestBody!!, super.getRequestHeader(HttpConstants.IF_MATCH_HEADER), getRequestHeadersAsHashMap() diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/network/AdvancedX509TrustManager.java b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/network/AdvancedX509TrustManager.java index d76ecbca..b84c03de 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/network/AdvancedX509TrustManager.java +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/network/AdvancedX509TrustManager.java @@ -45,6 +45,7 @@ public class AdvancedX509TrustManager implements X509TrustManager { private X509TrustManager mStandardTrustManager; private KeyStore mKnownServersKeyStore; + /** * Constructor for AdvancedX509TrustManager * @@ -66,7 +67,7 @@ public class AdvancedX509TrustManager implements X509TrustManager { * @return The first X509TrustManager found in factory. */ private X509TrustManager findX509TrustManager(TrustManagerFactory factory) { - TrustManager tms[] = factory.getTrustManagers(); + TrustManager[] tms = factory.getTrustManagers(); for (TrustManager tm : tms) { if (tm instanceof X509TrustManager) { return (X509TrustManager) tm; diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/network/FileRequestBody.java b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/network/FileRequestBody.java index 63d07bec..2df9884d 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/network/FileRequestBody.java +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/network/FileRequestBody.java @@ -44,9 +44,9 @@ import java.util.Set; */ public class FileRequestBody extends RequestBody implements ProgressiveDataTransferer { + final Set mDataTransferListeners = new HashSet<>(); protected File mFile; private MediaType mContentType; - final Set mDataTransferListeners = new HashSet<>(); public FileRequestBody(File file, MediaType contentType) { mFile = file; diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/chunks/ChunkedUploadRemoteFileOperation.java b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/chunks/ChunkedUploadRemoteFileOperation.java index 0072b5be..a7f33549 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/chunks/ChunkedUploadRemoteFileOperation.java +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/chunks/ChunkedUploadRemoteFileOperation.java @@ -29,7 +29,6 @@ import com.owncloud.android.lib.common.http.methods.webdav.PutMethod; import com.owncloud.android.lib.common.network.ChunkFromFileRequestBody; import com.owncloud.android.lib.common.operations.OperationCancelledException; import com.owncloud.android.lib.common.operations.RemoteOperationResult; -import com.owncloud.android.lib.resources.files.FileUtils; import com.owncloud.android.lib.resources.files.UploadRemoteFileOperation; import okhttp3.MediaType; import timber.log.Timber; From 31d3bfbde958ac6cd7b0fe479def3840820a6913 Mon Sep 17 00:00:00 2001 From: agarcia Date: Wed, 8 Jul 2020 18:15:28 +0200 Subject: [PATCH 13/15] Move RequestBody to constructor to avoid npe --- .../lib/common/http/methods/HttpBaseMethod.kt | 8 +------- .../lib/common/http/methods/nonwebdav/PostMethod.kt | 6 ++++-- .../lib/common/http/methods/nonwebdav/PutMethod.kt | 1 - .../lib/common/http/methods/webdav/PutMethod.kt | 6 ++++-- .../resources/files/UploadRemoteFileOperation.java | 11 +++++------ .../chunks/ChunkedUploadRemoteFileOperation.java | 13 ++++++------- 6 files changed, 20 insertions(+), 25 deletions(-) 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 4f9541ef..0c4c7ada 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 @@ -7,7 +7,6 @@ import okhttp3.HttpUrl import okhttp3.HttpUrl.Companion.toHttpUrlOrNull import okhttp3.OkHttpClient import okhttp3.Request -import okhttp3.RequestBody import okhttp3.Response import java.io.InputStream import java.net.MalformedURLException @@ -18,7 +17,6 @@ abstract class HttpBaseMethod constructor(url: URL) { var okHttpClient: OkHttpClient var httpUrl: HttpUrl = url.toHttpUrlOrNull() ?: throw MalformedURLException() var request: Request - var requestBody: RequestBody? = null abstract var response: Response var call: Call? = null @@ -45,17 +43,13 @@ abstract class HttpBaseMethod constructor(url: URL) { *** Requests *** ****************/ - // Headers - val requestHeaders: Headers - get() = request.headers - fun getRequestHeader(name: String): String? { return request.header(name) } fun getRequestHeadersAsHashMap(): HashMap { val headers: HashMap = HashMap() - val superHeaders: Set = requestHeaders.names() + val superHeaders: Set = request.headers.names() superHeaders.forEach { headers[it] = getRequestHeader(it) } diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/PostMethod.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/PostMethod.kt index 995b906e..ba4f6d4d 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/PostMethod.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/PostMethod.kt @@ -32,10 +32,12 @@ import java.net.URL * * @author David González Verdugo */ -class PostMethod(url: URL, private val postRequestBody: RequestBody) : HttpMethod(url) { +class PostMethod( + url: URL, + private val postRequestBody: RequestBody +) : HttpMethod(url) { @Throws(IOException::class) override fun onExecute(): Int { - requestBody = postRequestBody request = request.newBuilder() .post(postRequestBody) .build() diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/PutMethod.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/PutMethod.kt index a8fdd67e..98be1503 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/PutMethod.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/PutMethod.kt @@ -38,7 +38,6 @@ class PutMethod( ) : HttpMethod(url) { @Throws(IOException::class) override fun onExecute(): Int { - requestBody = putRequestBody request = request.newBuilder() .put(putRequestBody) .build() diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/PutMethod.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/PutMethod.kt index 4d482652..70dea1de 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/PutMethod.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/PutMethod.kt @@ -25,6 +25,7 @@ package com.owncloud.android.lib.common.http.methods.webdav import at.bitfire.dav4jvm.exception.HttpException import com.owncloud.android.lib.common.http.HttpConstants +import okhttp3.RequestBody import java.io.IOException import java.net.URL @@ -34,12 +35,13 @@ import java.net.URL * @author David González Verdugo */ class PutMethod( - url: URL + url: URL, + private val putRequestBody: RequestBody ) : DavMethod(url) { @Throws(IOException::class, HttpException::class) public override fun onExecute(): Int { davResource.put( - requestBody!!, + putRequestBody, super.getRequestHeader(HttpConstants.IF_MATCH_HEADER), getRequestHeadersAsHashMap() ) { callBackResponse -> diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/UploadRemoteFileOperation.java b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/UploadRemoteFileOperation.java index 939eafee..43181b28 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/UploadRemoteFileOperation.java +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/UploadRemoteFileOperation.java @@ -84,10 +84,6 @@ public class UploadRemoteFileOperation extends RemoteOperation { RemoteOperationResult result; try { - mPutMethod = new PutMethod( - new URL(client.getUserFilesWebDavUri() + WebdavUtils.encodePath(mRemotePath))); - - mPutMethod.setRetryOnConnectionFailure(false); if (mCancellationRequested.get()) { // the operation was cancelled before getting it's turn to be executed in the queue of uploads @@ -125,6 +121,11 @@ public class UploadRemoteFileOperation extends RemoteOperation { mFileRequestBody.addDatatransferProgressListeners(mDataTransferListeners); } + mPutMethod = new PutMethod( + new URL(client.getUserFilesWebDavUri() + WebdavUtils.encodePath(mRemotePath)), mFileRequestBody); + + mPutMethod.setRetryOnConnectionFailure(false); + if (mRequiredEtag != null && mRequiredEtag.length() > 0) { mPutMethod.addRequestHeader(HttpConstants.IF_MATCH_HEADER, mRequiredEtag); } @@ -132,8 +133,6 @@ public class UploadRemoteFileOperation extends RemoteOperation { mPutMethod.addRequestHeader(HttpConstants.OC_TOTAL_LENGTH_HEADER, String.valueOf(fileToUpload.length())); mPutMethod.addRequestHeader(HttpConstants.OC_X_OC_MTIME_HEADER, mFileLastModifTimestamp); - mPutMethod.setRequestBody(mFileRequestBody); - int status = client.executeHttpMethod(mPutMethod); if (isSuccess(status)) { diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/chunks/ChunkedUploadRemoteFileOperation.java b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/chunks/ChunkedUploadRemoteFileOperation.java index a7f33549..14d0fbf5 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/chunks/ChunkedUploadRemoteFileOperation.java +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/chunks/ChunkedUploadRemoteFileOperation.java @@ -86,11 +86,6 @@ public class ChunkedUploadRemoteFileOperation extends UploadRemoteFileOperation long chunkCount = (long) Math.ceil((double) totalLength / CHUNK_SIZE); for (int chunkIndex = 0; chunkIndex < chunkCount; chunkIndex++, offset += CHUNK_SIZE) { - mPutMethod = new PutMethod(new URL(uriPrefix + File.separator + chunkIndex)); - - if (mRequiredEtag != null && mRequiredEtag.length() > 0) { - mPutMethod.addRequestHeader(IF_MATCH_HEADER, "\"" + mRequiredEtag + "\""); - } ((ChunkFromFileRequestBody) mFileRequestBody).setOffset(offset); @@ -98,6 +93,12 @@ public class ChunkedUploadRemoteFileOperation extends UploadRemoteFileOperation result = new RemoteOperationResult<>(new OperationCancelledException()); break; } else { + mPutMethod = new PutMethod(new URL(uriPrefix + File.separator + chunkIndex), mFileRequestBody); + + if (mRequiredEtag != null && mRequiredEtag.length() > 0) { + mPutMethod.addRequestHeader(IF_MATCH_HEADER, "\"" + mRequiredEtag + "\""); + } + if (chunkIndex == chunkCount - 1) { // Added a high timeout to the last chunk due to when the last chunk // arrives to the server with the last PUT, all chunks get assembled @@ -105,8 +106,6 @@ public class ChunkedUploadRemoteFileOperation extends UploadRemoteFileOperation mPutMethod.setReadTimeout(LAST_CHUNK_TIMEOUT, TimeUnit.MILLISECONDS); } - mPutMethod.setRequestBody(mFileRequestBody); - status = client.executeHttpMethod(mPutMethod); Timber.d("Upload of " + mLocalPath + " to " + mRemotePath + From f09162b213372f8d6ed54991eada6ca3fe0ba5a9 Mon Sep 17 00:00:00 2001 From: agarcia Date: Tue, 14 Jul 2020 13:03:31 +0200 Subject: [PATCH 14/15] Fix redirections --- .../lib/common/http/methods/webdav/DavMethod.kt | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/DavMethod.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/DavMethod.kt index b943877c..b9bc14cd 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/DavMethod.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/DavMethod.kt @@ -99,7 +99,7 @@ abstract class DavMethod protected constructor(url: URL) : HttpBaseMethod(url) { super.setReadTimeout(readTimeout, timeUnit) davResource = DavOCResource( okHttpClient, - httpUrl, + request.url, log ) } @@ -111,7 +111,7 @@ abstract class DavMethod protected constructor(url: URL) : HttpBaseMethod(url) { super.setConnectionTimeout(connectionTimeout, timeUnit) davResource = DavOCResource( okHttpClient, - httpUrl, + request.url, log ) } @@ -120,7 +120,7 @@ abstract class DavMethod protected constructor(url: URL) : HttpBaseMethod(url) { super.setFollowRedirects(followRedirects) davResource = DavOCResource( okHttpClient, - httpUrl, + request.url, log ) } @@ -129,7 +129,7 @@ abstract class DavMethod protected constructor(url: URL) : HttpBaseMethod(url) { super.setUrl(url) davResource = DavOCResource( okHttpClient, - httpUrl, + request.url, log ) } @@ -138,7 +138,7 @@ abstract class DavMethod protected constructor(url: URL) : HttpBaseMethod(url) { super.setRequestHeader(name, value) davResource = DavOCResource( okHttpClient, - httpUrl, + request.url, log ) } @@ -150,7 +150,7 @@ abstract class DavMethod protected constructor(url: URL) : HttpBaseMethod(url) { super.setRetryOnConnectionFailure(retryOnConnectionFailure) davResource = DavOCResource( okHttpClient, - httpUrl, + request.url, log ) } From a7a269a9e4b247d6e96b1e436f534191a5f8f459 Mon Sep 17 00:00:00 2001 From: agarcia Date: Tue, 11 Aug 2020 18:40:17 +0200 Subject: [PATCH 15/15] Fix timestamps --- owncloudComLibrary/build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/owncloudComLibrary/build.gradle b/owncloudComLibrary/build.gradle index ca70bc23..ef04982b 100644 --- a/owncloudComLibrary/build.gradle +++ b/owncloudComLibrary/build.gradle @@ -6,7 +6,7 @@ apply plugin: 'kotlin-allopen' dependencies { api 'com.squareup.okhttp3:okhttp:4.6.0' implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlinVersion" - api 'com.gitlab.ownclouders:dav4android:oc_support_2.1.2' + api 'com.gitlab.ownclouders:dav4android:oc_support_2.1.5' api 'com.github.hannesa2:Logcat:1.6.0' api 'net.openid:appauth:0.7.1'