Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
Signed-off-by: tobiasKaminsky <[email protected]>
  • Loading branch information
tobiasKaminsky committed Sep 4, 2023
1 parent 1f8bad5 commit b001071
Show file tree
Hide file tree
Showing 7 changed files with 168 additions and 26 deletions.
18 changes: 15 additions & 3 deletions library/src/androidTest/java/com/owncloud/android/Dav4JVM.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand All @@ -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
Expand Down
38 changes: 38 additions & 0 deletions library/src/main/java/com/nextcloud/common/DavMethod.kt
Original file line number Diff line number Diff line change
@@ -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 <https://www.gnu.org/licenses/>.
*/
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<T>(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
}
12 changes: 12 additions & 0 deletions library/src/main/java/com/nextcloud/common/NextcloudClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,18 @@ class NextcloudClient private constructor(
return method.execute(this)
}

fun <T> execute(method: DavMethod<T>): 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()
Expand Down
61 changes: 61 additions & 0 deletions library/src/main/java/com/nextcloud/operations/PropFindMethod.kt
Original file line number Diff line number Diff line change
@@ -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 <https://www.gnu.org/licenses/>.
*/
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<PropFindResult>(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
}
}
32 changes: 32 additions & 0 deletions library/src/main/java/com/nextcloud/operations/PropFindResult.kt
Original file line number Diff line number Diff line change
@@ -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 <https://www.gnu.org/licenses/>.
*/

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<RemoteFile> = mutableListOf()
)

Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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<Response> responses = new ArrayList<>();

new DavResource(
disabledRedirectClient,
client.disabledRedirectClient(),
HttpUrl.get(client.getDavUri().toString()))
.search(searchString, (response, hrefRelation) -> {
responses.add(response);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand Down Expand Up @@ -117,13 +116,6 @@ public RemoteOperationResult run(NextcloudClient client) {
List<Property.Name> removeProperties = new ArrayList<>();
Map<Property.Name, String> 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 {
Expand All @@ -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;
}
Expand Down

0 comments on commit b001071

Please sign in to comment.