Skip to content

Commit

Permalink
Move to NextcloudClient and OkHttp
Browse files Browse the repository at this point in the history
- Further replaced OwnCloudClient with NextcloudClient
- Moved to and implemented OkHttp/DAV methods
- Added DavResponse to encapsulate dav responses
- Added ArrayExtensions.kt to allow converting to legacy PropSet
- Ported some classes to Kotlin
- Improved typing on several remote operations
- Added some comments
- Minor fixes

Signed-off-by: ZetaTom <[email protected]>
  • Loading branch information
ZetaTom committed Mar 11, 2024
1 parent 818bfff commit 5ae2701
Show file tree
Hide file tree
Showing 39 changed files with 1,277 additions and 1,501 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ package com.owncloud.android.lib.resources.comments
import com.owncloud.android.AbstractIT
import com.owncloud.android.lib.resources.files.ReadFileRemoteOperation
import com.owncloud.android.lib.resources.files.UploadFileRemoteOperation
import com.owncloud.android.lib.resources.files.model.RemoteFile
import junit.framework.Assert.assertTrue
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertTrue
import org.junit.Test

class CommentFileRemoteOperationIT : AbstractIT() {
Expand All @@ -24,18 +24,20 @@ class CommentFileRemoteOperationIT : AbstractIT() {
.execute(client).isSuccess
)

val readResult = ReadFileRemoteOperation(remotePath).execute(client)
val remoteFile = readResult.data.get(0) as RemoteFile
val readResult = ReadFileRemoteOperation(remotePath).execute(nextcloudClient)
val remoteFile = readResult.getResultData()

assertNotNull(remoteFile)

assertTrue(
CommentFileRemoteOperation("test", remoteFile.localId)
CommentFileRemoteOperation("test", remoteFile!!.localId)
.execute(client)
.isSuccess
)

assertTrue(
MarkCommentsAsReadRemoteOperation(remoteFile.localId)
.execute(client)
.execute(nextcloudClient)
.isSuccess
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/
package com.owncloud.android.lib.resources.files.webdav

import com.nextcloud.extensions.toLegacyPropset
import com.owncloud.android.AbstractIT
import com.owncloud.android.lib.common.network.WebdavEntry
import com.owncloud.android.lib.common.network.WebdavUtils
Expand Down Expand Up @@ -139,7 +140,7 @@ class ChunkedFileUploadRemoteOperationIT : AbstractIT() {

private fun getRemoteSize(remotePath: String): Long {
val davPath = client.filesDavUri.toString() + "/" + WebdavUtils.encodePath(remotePath)
val propFindMethod = PropFindMethod(davPath, WebdavUtils.getFilePropSet(), 0)
val propFindMethod = PropFindMethod(davPath, WebdavUtils.PROPERTYSETS.FILE.toLegacyPropset(), 0)
client.executeMethod(propFindMethod)
assert(propFindMethod.succeeded())

Expand Down
39 changes: 39 additions & 0 deletions library/src/main/java/com/nextcloud/common/DavResponse.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Nextcloud Android Library
*
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2024 ZetaTom <[email protected]>
* SPDX-License-Identifier: MIT
*/
package com.nextcloud.common

import okhttp3.Headers
import okhttp3.internal.http.StatusLine

/**
* Encapsulates essential data returned as responses from various DAV calls.
*/
data class DavResponse(
var success: Boolean = false,
var status: StatusLine? = null,
var headers: Headers? = null
) {
/**
* Return value of specified header.
*
* Simple helper to aid with nullability when called from Java.
*
* @param key name of header to get
* @return value of header or `null` when header is not set
*/
fun getHeader(key: String): String? = headers?.get(key)

/**
* Return value of status code.
*
* Simple helper to aid with nullability when called from Java.
*
* @return HTTP status code or `0` if not set.
*/
fun getStatusCode(): Int = status?.code ?: 0
}
26 changes: 26 additions & 0 deletions library/src/main/java/com/nextcloud/extensions/ArrayExtensions.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Nextcloud Android Library
*
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2024 ZetaTom <[email protected]>
* SPDX-License-Identifier: MIT
*/
package com.nextcloud.extensions

import at.bitfire.dav4jvm.Property
import org.apache.jackrabbit.webdav.property.DavPropertyName
import org.apache.jackrabbit.webdav.property.DavPropertyNameSet
import org.apache.jackrabbit.webdav.xml.Namespace

/**
* Returns DavPropertyNameSet for given array of Property.Name.
*
* TODO: remove - only intended as a transitional aid
*/
fun Array<Property.Name>.toLegacyPropset(): DavPropertyNameSet {
val propertySet = DavPropertyNameSet()
for (property in this) {
propertySet.add(DavPropertyName.create(property.name, Namespace.getNamespace(property.namespace)))
}
return propertySet
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Nextcloud Android Library
*
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2024 ZetaTom <[email protected]>
* SPDX-License-Identifier: MIT
*/
package com.nextcloud.extensions

import android.os.Build
import android.os.Parcel

@Suppress("DEPRECATION")
inline fun <reified T> Parcel.readParcelableArrayBridge(type: Class<T>): Any? {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
this.readParcelableArray(type::class.java.classLoader, this::class.java)
} else {
this.readParcelableArray(type::class.java.classLoader)
}
}
41 changes: 41 additions & 0 deletions library/src/main/java/com/nextcloud/operations/MkColMethod.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Nextcloud Android Library
*
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2024 ZetaTom <[email protected]>
* SPDX-License-Identifier: MIT
*/
package com.nextcloud.operations

import android.net.Uri
import at.bitfire.dav4jvm.DavResource
import com.nextcloud.common.DavMethod
import com.nextcloud.common.DavResponse
import okhttp3.Headers.Companion.toHeaders
import okhttp3.HttpUrl
import okhttp3.OkHttpClient
import okhttp3.Response
import okhttp3.internal.http.StatusLine

class MkColMethod(httpUrl: HttpUrl) : DavMethod<DavResponse>(httpUrl){
private val headers = mutableMapOf<String, String>()

override fun apply(client: OkHttpClient, httpUrl: HttpUrl, filesDavUri: Uri): DavResponse {
val result = DavResponse()

DavResource(client, httpUrl).mkCol(
xmlBody = null,
headers = headers.toHeaders()
) { response: Response ->
result.success = response.isSuccessful
result.status = StatusLine.get(response)
result.headers = response.headers
}

return result
}

fun addRequestHeader(key: String, value: String) {
headers[key] = value
}
}
43 changes: 43 additions & 0 deletions library/src/main/java/com/nextcloud/operations/MoveMethod.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Nextcloud Android Library
*
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2024 ZetaTom <[email protected]>
* SPDX-License-Identifier: MIT
*/
package com.nextcloud.operations

import android.net.Uri
import at.bitfire.dav4jvm.DavResource
import com.nextcloud.common.DavMethod
import com.nextcloud.common.DavResponse
import okhttp3.HttpUrl
import okhttp3.OkHttpClient
import okhttp3.internal.http.StatusLine

class MoveMethod(
httpUrl: HttpUrl,
private val destination: HttpUrl,
private val forceOverwrite: Boolean = false
): DavMethod<DavResponse>(httpUrl) {
private val headers = mutableMapOf<String, String>()

override fun apply(client: OkHttpClient, httpUrl: HttpUrl, filesDavUri: Uri): DavResponse {
val result = DavResponse()

DavResource(client, httpUrl).move(
destination = destination,
overwrite = forceOverwrite
) { response ->
result.success = response.isSuccessful
result.status = StatusLine.get(response)
result.headers = response.headers
}

return result
}

fun addRequestHeader(key: String, value: String) {
headers[key] = value
}
}
11 changes: 7 additions & 4 deletions library/src/main/java/com/nextcloud/operations/PropFindMethod.kt
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,24 @@ class PropFindMethod
) : DavMethod<PropFindResult>(httpUrl) {

override fun apply(client: OkHttpClient, httpUrl: HttpUrl, filesDavUri: Uri): PropFindResult {
val webDavFileUtils = WebDavFileUtils()
val result = PropFindResult()

DavResource(client, httpUrl).propfind(
depth, *propertySet
) { response: Response, hrefRelation: Response.HrefRelation? ->
result.success = response.isSuccess()
result.davResponse.success = response.isSuccess()
response.status?.let { status ->
result.davResponse.status = status
}


when (hrefRelation) {
Response.HrefRelation.MEMBER -> result.children.add(
webDavFileUtils.parseResponse(response, filesDavUri)
WebDavFileUtils.parseResponse(response, filesDavUri)
)

Response.HrefRelation.SELF -> result.root =
webDavFileUtils.parseResponse(response, filesDavUri)
WebDavFileUtils.parseResponse(response, filesDavUri)

Response.HrefRelation.OTHER -> {}
else -> {}
Expand Down
10 changes: 7 additions & 3 deletions library/src/main/java/com/nextcloud/operations/PropFindResult.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,15 @@

package com.nextcloud.operations

import com.nextcloud.common.DavResponse
import com.owncloud.android.lib.resources.files.model.RemoteFile

data class PropFindResult(
var success: Boolean = false,
val davResponse: DavResponse = DavResponse(),
var root: RemoteFile = RemoteFile(),
val children: MutableList<RemoteFile> = mutableListOf()
)

) {
fun getContent(): List<RemoteFile> {
return children + root
}
}
36 changes: 36 additions & 0 deletions library/src/main/java/com/nextcloud/operations/PropPatchMethod.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Nextcloud Android Library
*
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2024 ZetaTom <[email protected]>
* SPDX-License-Identifier: MIT
*/
package com.nextcloud.operations

import android.net.Uri
import at.bitfire.dav4jvm.DavResource
import at.bitfire.dav4jvm.Property
import at.bitfire.dav4jvm.Response
import com.nextcloud.common.DavMethod
import com.nextcloud.common.DavResponse
import okhttp3.HttpUrl
import okhttp3.OkHttpClient

class PropPatchMethod
@JvmOverloads constructor(
httpUrl: HttpUrl,
private val setProperties: Map<Property.Name, String> = emptyMap(),
private val removeProperties: List<Property.Name> = emptyList()
) : DavMethod<DavResponse>(httpUrl){
override fun apply(client: OkHttpClient, httpUrl: HttpUrl, filesDavUri: Uri): DavResponse {
val result = DavResponse()
DavResource(client, httpUrl).proppatch(setProperties, removeProperties) { response: Response, hrefRelation: Response.HrefRelation? ->
result.success = response.isSuccess()
response.status?.let { status ->
result.status = status
}
}

return result
}
}
Original file line number Diff line number Diff line change
@@ -1,36 +1,17 @@
/* Nextcloud Android Library is available under MIT license
*
* @author ZetaZom
* Copyright (C) 2024 ZetaTom
* Copyright (C) 2024 Nextcloud 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.
*
*/

/*
* Nextcloud Android Library
*
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2024 ZetaTom <[email protected]>
* SPDX-License-Identifier: MIT
*/
package com.owncloud.android.lib.common.network

import at.bitfire.dav4jvm.Property

enum class ExtendedProperties(val value: String, val namespace: String) {
CREATION_TIME("creation_time", WebdavUtils.NAMESPACE_NC),
COMMENTS_READ_MARKER("readMarker", WebdavUtils.NAMESPACE_NC),
FAVORITE("favorite", WebdavUtils.NAMESPACE_OC),
HAS_PREVIEW("has-preview", WebdavUtils.NAMESPACE_NC),
HIDDEN("hidden", WebdavUtils.NAMESPACE_NC),
Expand Down
Loading

0 comments on commit 5ae2701

Please sign in to comment.