From a90a4b7dab4d918d98defacf11468c2148ea14f6 Mon Sep 17 00:00:00 2001 From: tobiasKaminsky Date: Fri, 1 Sep 2023 14:31:37 +0200 Subject: [PATCH] wip Signed-off-by: tobiasKaminsky --- .../java/com/owncloud/android/Dav4JVM.kt | 18 +++++- .../java/com/nextcloud/common/DavMethod.kt | 38 ++++++++++++ .../com/nextcloud/common/NextcloudClient.kt | 12 ++++ .../nextcloud/operations/PropFindMethod.kt | 61 +++++++++++++++++++ .../nextcloud/operations/PropFindResult.kt | 32 ++++++++++ .../files/SearchRemoteOperation.java | 11 +--- .../files/ToggleFavoriteRemoteOperation.java | 22 +++---- 7 files changed, 168 insertions(+), 26 deletions(-) create mode 100644 library/src/main/java/com/nextcloud/common/DavMethod.kt create mode 100644 library/src/main/java/com/nextcloud/operations/PropFindMethod.kt create mode 100644 library/src/main/java/com/nextcloud/operations/PropFindResult.kt diff --git a/library/src/androidTest/java/com/owncloud/android/Dav4JVM.kt b/library/src/androidTest/java/com/owncloud/android/Dav4JVM.kt index fc7cce27f7..8f788267db 100644 --- a/library/src/androidTest/java/com/owncloud/android/Dav4JVM.kt +++ b/library/src/androidTest/java/com/owncloud/android/Dav4JVM.kt @@ -30,6 +30,7 @@ package com.owncloud.android import at.bitfire.dav4jvm.DavResource import at.bitfire.dav4jvm.Response import com.nextcloud.common.NextcloudAuthenticator +import com.nextcloud.operations.PropFindMethod import com.owncloud.android.lib.common.network.WebdavUtils import com.owncloud.android.lib.common.utils.WebDavFileUtils import com.owncloud.android.lib.resources.files.CreateFolderRemoteOperation @@ -57,7 +58,7 @@ class Dav4JVM : AbstractIT() { @Throws(IOException::class) fun singlePropfind() { val path = "/testFolder/" - val subFolder = "$path subfolder/" + val subFolder = path + "subfolder/" // create folder CreateFolderRemoteOperation( @@ -123,7 +124,6 @@ class Dav4JVM : AbstractIT() { WebdavUtils.registerCustomFactories() // TODO use DavResource().propfind in ReadFileRemoteOperation/ReadFolderRemoteOperation - // TODO extract in own class for convenient use // TODO test all properties on server! DavResource(client, httpUrl) .propfind( @@ -144,8 +144,20 @@ class Dav4JVM : AbstractIT() { assertEquals(1, memberElements.size) val remoteFile = WebDavFileUtils().parseResponse(rootElement, nextcloudClient.filesDavUri) - assertTrue(oldRemoteFile == remoteFile) + + val subfolderFile = + WebDavFileUtils().parseResponse(memberElements[0], nextcloudClient.filesDavUri) + assertTrue(oldSubFolderFile == subfolderFile) + + // new propfind + val newResult = nextcloudClient.execute(PropFindMethod(httpUrl)) + + assertTrue(newResult.success) + assertTrue(oldRemoteFile == newResult.root) + + assertEquals(1, newResult.children.size) + assertTrue(oldSubFolderFile == newResult.children[0]) } @Test diff --git a/library/src/main/java/com/nextcloud/common/DavMethod.kt b/library/src/main/java/com/nextcloud/common/DavMethod.kt new file mode 100644 index 0000000000..dd3395fc39 --- /dev/null +++ b/library/src/main/java/com/nextcloud/common/DavMethod.kt @@ -0,0 +1,38 @@ +/* + * + * Nextcloud Android client application + * + * @author Tobias Kaminsky + * Copyright (C) 2023 Tobias Kaminsky + * Copyright (C) 2023 Nextcloud GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +package com.nextcloud.common + +import android.net.Uri +import com.owncloud.android.lib.common.network.WebdavUtils +import okhttp3.HttpUrl +import okhttp3.OkHttpClient + +abstract class DavMethod(private val httpUrl: HttpUrl) { + fun execute(nextcloudClient: NextcloudClient): T { + // register custom property + WebdavUtils.registerCustomFactories() + + return apply(nextcloudClient.disabledRedirectClient(), httpUrl, nextcloudClient.filesDavUri) + } + + abstract fun apply(client: OkHttpClient, httpUrl: HttpUrl, filesDavUri: Uri): T +} diff --git a/library/src/main/java/com/nextcloud/common/NextcloudClient.kt b/library/src/main/java/com/nextcloud/common/NextcloudClient.kt index d2359395f2..26565e53ca 100644 --- a/library/src/main/java/com/nextcloud/common/NextcloudClient.kt +++ b/library/src/main/java/com/nextcloud/common/NextcloudClient.kt @@ -114,6 +114,18 @@ class NextcloudClient private constructor( return method.execute(this) } + fun execute(method: DavMethod): T { + return method.execute(this) + } + + fun disabledRedirectClient(): OkHttpClient { + return client + .newBuilder() + .followRedirects(false) + .authenticator(NextcloudAuthenticator(credentials, "Authorization")) + .build() + } + internal fun execute(request: Request): ResponseOrError { return try { val response = client.newCall(request).execute() diff --git a/library/src/main/java/com/nextcloud/operations/PropFindMethod.kt b/library/src/main/java/com/nextcloud/operations/PropFindMethod.kt new file mode 100644 index 0000000000..f4e69fb1e7 --- /dev/null +++ b/library/src/main/java/com/nextcloud/operations/PropFindMethod.kt @@ -0,0 +1,61 @@ +/* + * + * Nextcloud Android client application + * + * @author Tobias Kaminsky + * Copyright (C) 2023 Tobias Kaminsky + * Copyright (C) 2023 Nextcloud GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +package com.nextcloud.operations + +import android.net.Uri +import at.bitfire.dav4jvm.DavResource +import at.bitfire.dav4jvm.Response +import com.nextcloud.common.DavMethod +import com.owncloud.android.lib.common.network.WebdavUtils +import com.owncloud.android.lib.common.utils.WebDavFileUtils +import okhttp3.HttpUrl +import okhttp3.OkHttpClient +import org.apache.jackrabbit.webdav.DavConstants + +class PropFindMethod(httpUrl: HttpUrl) : DavMethod(httpUrl) { + override fun apply(client: OkHttpClient, httpUrl: HttpUrl, filesDavUri: Uri): PropFindResult { + val webDavFileUtils = WebDavFileUtils() + val result = PropFindResult() + + DavResource(client, httpUrl) + .propfind( + DavConstants.DEPTH_1, + *WebdavUtils.getAllPropertiesList() + ) { response: Response, hrefRelation: Response.HrefRelation? -> + result.success = response.isSuccess() + + when (hrefRelation) { + Response.HrefRelation.MEMBER -> result.children.add( + webDavFileUtils.parseResponse(response, filesDavUri) + ) + + Response.HrefRelation.SELF -> result.root = + webDavFileUtils.parseResponse(response, filesDavUri) + + Response.HrefRelation.OTHER -> {} + else -> {} + } + } + + return result + } +} diff --git a/library/src/main/java/com/nextcloud/operations/PropFindResult.kt b/library/src/main/java/com/nextcloud/operations/PropFindResult.kt new file mode 100644 index 0000000000..c9f5ff22ff --- /dev/null +++ b/library/src/main/java/com/nextcloud/operations/PropFindResult.kt @@ -0,0 +1,32 @@ +/* + * + * Nextcloud Android client application + * + * @author Tobias Kaminsky + * Copyright (C) 2023 Tobias Kaminsky + * Copyright (C) 2023 Nextcloud GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package com.nextcloud.operations + +import com.owncloud.android.lib.resources.files.model.RemoteFile + +data class PropFindResult( + var success: Boolean = false, + var root: RemoteFile = RemoteFile(), + val children: MutableList = mutableListOf() +) + diff --git a/library/src/main/java/com/owncloud/android/lib/resources/files/SearchRemoteOperation.java b/library/src/main/java/com/owncloud/android/lib/resources/files/SearchRemoteOperation.java index bc92c380e9..d761d379fb 100644 --- a/library/src/main/java/com/owncloud/android/lib/resources/files/SearchRemoteOperation.java +++ b/library/src/main/java/com/owncloud/android/lib/resources/files/SearchRemoteOperation.java @@ -27,7 +27,6 @@ package com.owncloud.android.lib.resources.files; -import com.nextcloud.common.NextcloudAuthenticator; import com.nextcloud.common.NextcloudClient; import com.owncloud.android.lib.common.OwnCloudClient; import com.owncloud.android.lib.common.operations.RemoteOperation; @@ -56,7 +55,6 @@ import at.bitfire.dav4jvm.DavResource; import at.bitfire.dav4jvm.Response; import okhttp3.HttpUrl; -import okhttp3.OkHttpClient; /** * Remote operation performing the search in the Nextcloud server. @@ -199,20 +197,13 @@ public RemoteOperationResult run(NextcloudClient client) { startDate, endDate); - // disable redirect - OkHttpClient disabledRedirectClient = client.getClient() - .newBuilder() - .followRedirects(false) - .authenticator(new NextcloudAuthenticator(client.getCredentials(), "Authorization")) - .build(); - Document searchDocument = searchMethod.getDocumentQuery(searchInfo); String searchString = transformDocumentToString(searchDocument); ArrayList responses = new ArrayList<>(); new DavResource( - disabledRedirectClient, + client.disabledRedirectClient(), HttpUrl.get(client.getDavUri().toString())) .search(searchString, (response, hrefRelation) -> { responses.add(response); diff --git a/library/src/main/java/com/owncloud/android/lib/resources/files/ToggleFavoriteRemoteOperation.java b/library/src/main/java/com/owncloud/android/lib/resources/files/ToggleFavoriteRemoteOperation.java index 8e3938b7ac..e2d4a7f5ec 100644 --- a/library/src/main/java/com/owncloud/android/lib/resources/files/ToggleFavoriteRemoteOperation.java +++ b/library/src/main/java/com/owncloud/android/lib/resources/files/ToggleFavoriteRemoteOperation.java @@ -29,7 +29,6 @@ import android.net.Uri; -import com.nextcloud.common.NextcloudAuthenticator; import com.nextcloud.common.NextcloudClient; import com.owncloud.android.lib.common.OwnCloudClient; import com.owncloud.android.lib.common.network.WebdavEntry; @@ -49,11 +48,11 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.concurrent.atomic.AtomicBoolean; import at.bitfire.dav4jvm.DavResource; import at.bitfire.dav4jvm.Property; import okhttp3.HttpUrl; -import okhttp3.OkHttpClient; /** * Favorite or unfavorite a file. @@ -117,13 +116,6 @@ public RemoteOperationResult run(NextcloudClient client) { List removeProperties = new ArrayList<>(); Map newProperties = new HashMap<>(); - // disable redirect - OkHttpClient disabledRedirectClient = client.getClient() - .newBuilder() - .followRedirects(false) - .authenticator(new NextcloudAuthenticator(client.getCredentials(), "Authorization")) - .build(); - if (makeItFavorited) { newProperties.put(NCFavorite.NAME, "1"); } else { @@ -134,15 +126,19 @@ public RemoteOperationResult run(NextcloudClient client) { String encodedPath = (client.getUserId() + Uri.encode(filePath)).replace("%2F", "/"); String fullFilePath = webDavUrl + "/files/" + encodedPath; - boolean resultCode = false; + AtomicBoolean resultCode = new AtomicBoolean(false); - new DavResource(disabledRedirectClient, + new DavResource(client.disabledRedirectClient(), HttpUrl.get(fullFilePath)) .proppatch(newProperties, removeProperties, (response, hrefRelation) -> { - //resultCode = response.isSuccess(); + resultCode.set(response.isSuccess()); }); - result = new RemoteOperationResult<>(RemoteOperationResult.ResultCode.OK); + if (resultCode.get()) { + result = new RemoteOperationResult<>(RemoteOperationResult.ResultCode.OK); + } else { + result = new RemoteOperationResult<>(RemoteOperationResult.ResultCode.WRONG_SERVER_RESPONSE); + } return result; }