Skip to content

Commit 5ae2701

Browse files
committed
Move to NextcloudClient and OkHttp
- 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]>
1 parent 818bfff commit 5ae2701

39 files changed

+1277
-1501
lines changed

library/src/androidTest/java/com/owncloud/android/lib/resources/comments/CommentFileRemoteOperationIT.kt

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ package com.owncloud.android.lib.resources.comments
1010
import com.owncloud.android.AbstractIT
1111
import com.owncloud.android.lib.resources.files.ReadFileRemoteOperation
1212
import com.owncloud.android.lib.resources.files.UploadFileRemoteOperation
13-
import com.owncloud.android.lib.resources.files.model.RemoteFile
14-
import junit.framework.Assert.assertTrue
13+
import org.junit.Assert.assertNotNull
14+
import org.junit.Assert.assertTrue
1515
import org.junit.Test
1616

1717
class CommentFileRemoteOperationIT : AbstractIT() {
@@ -24,18 +24,20 @@ class CommentFileRemoteOperationIT : AbstractIT() {
2424
.execute(client).isSuccess
2525
)
2626

27-
val readResult = ReadFileRemoteOperation(remotePath).execute(client)
28-
val remoteFile = readResult.data.get(0) as RemoteFile
27+
val readResult = ReadFileRemoteOperation(remotePath).execute(nextcloudClient)
28+
val remoteFile = readResult.getResultData()
29+
30+
assertNotNull(remoteFile)
2931

3032
assertTrue(
31-
CommentFileRemoteOperation("test", remoteFile.localId)
33+
CommentFileRemoteOperation("test", remoteFile!!.localId)
3234
.execute(client)
3335
.isSuccess
3436
)
3537

3638
assertTrue(
3739
MarkCommentsAsReadRemoteOperation(remoteFile.localId)
38-
.execute(client)
40+
.execute(nextcloudClient)
3941
.isSuccess
4042
)
4143
}

library/src/androidTest/java/com/owncloud/android/lib/resources/files/webdav/ChunkedFileUploadRemoteOperationIT.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88
package com.owncloud.android.lib.resources.files.webdav
99

10+
import com.nextcloud.extensions.toLegacyPropset
1011
import com.owncloud.android.AbstractIT
1112
import com.owncloud.android.lib.common.network.WebdavEntry
1213
import com.owncloud.android.lib.common.network.WebdavUtils
@@ -139,7 +140,7 @@ class ChunkedFileUploadRemoteOperationIT : AbstractIT() {
139140

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Nextcloud Android Library
3+
*
4+
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
5+
* SPDX-FileCopyrightText: 2024 ZetaTom <[email protected]>
6+
* SPDX-License-Identifier: MIT
7+
*/
8+
package com.nextcloud.common
9+
10+
import okhttp3.Headers
11+
import okhttp3.internal.http.StatusLine
12+
13+
/**
14+
* Encapsulates essential data returned as responses from various DAV calls.
15+
*/
16+
data class DavResponse(
17+
var success: Boolean = false,
18+
var status: StatusLine? = null,
19+
var headers: Headers? = null
20+
) {
21+
/**
22+
* Return value of specified header.
23+
*
24+
* Simple helper to aid with nullability when called from Java.
25+
*
26+
* @param key name of header to get
27+
* @return value of header or `null` when header is not set
28+
*/
29+
fun getHeader(key: String): String? = headers?.get(key)
30+
31+
/**
32+
* Return value of status code.
33+
*
34+
* Simple helper to aid with nullability when called from Java.
35+
*
36+
* @return HTTP status code or `0` if not set.
37+
*/
38+
fun getStatusCode(): Int = status?.code ?: 0
39+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Nextcloud Android Library
3+
*
4+
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
5+
* SPDX-FileCopyrightText: 2024 ZetaTom <[email protected]>
6+
* SPDX-License-Identifier: MIT
7+
*/
8+
package com.nextcloud.extensions
9+
10+
import at.bitfire.dav4jvm.Property
11+
import org.apache.jackrabbit.webdav.property.DavPropertyName
12+
import org.apache.jackrabbit.webdav.property.DavPropertyNameSet
13+
import org.apache.jackrabbit.webdav.xml.Namespace
14+
15+
/**
16+
* Returns DavPropertyNameSet for given array of Property.Name.
17+
*
18+
* TODO: remove - only intended as a transitional aid
19+
*/
20+
fun Array<Property.Name>.toLegacyPropset(): DavPropertyNameSet {
21+
val propertySet = DavPropertyNameSet()
22+
for (property in this) {
23+
propertySet.add(DavPropertyName.create(property.name, Namespace.getNamespace(property.namespace)))
24+
}
25+
return propertySet
26+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Nextcloud Android Library
3+
*
4+
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
5+
* SPDX-FileCopyrightText: 2024 ZetaTom <[email protected]>
6+
* SPDX-License-Identifier: MIT
7+
*/
8+
package com.nextcloud.extensions
9+
10+
import android.os.Build
11+
import android.os.Parcel
12+
13+
@Suppress("DEPRECATION")
14+
inline fun <reified T> Parcel.readParcelableArrayBridge(type: Class<T>): Any? {
15+
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
16+
this.readParcelableArray(type::class.java.classLoader, this::class.java)
17+
} else {
18+
this.readParcelableArray(type::class.java.classLoader)
19+
}
20+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Nextcloud Android Library
3+
*
4+
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
5+
* SPDX-FileCopyrightText: 2024 ZetaTom <[email protected]>
6+
* SPDX-License-Identifier: MIT
7+
*/
8+
package com.nextcloud.operations
9+
10+
import android.net.Uri
11+
import at.bitfire.dav4jvm.DavResource
12+
import com.nextcloud.common.DavMethod
13+
import com.nextcloud.common.DavResponse
14+
import okhttp3.Headers.Companion.toHeaders
15+
import okhttp3.HttpUrl
16+
import okhttp3.OkHttpClient
17+
import okhttp3.Response
18+
import okhttp3.internal.http.StatusLine
19+
20+
class MkColMethod(httpUrl: HttpUrl) : DavMethod<DavResponse>(httpUrl){
21+
private val headers = mutableMapOf<String, String>()
22+
23+
override fun apply(client: OkHttpClient, httpUrl: HttpUrl, filesDavUri: Uri): DavResponse {
24+
val result = DavResponse()
25+
26+
DavResource(client, httpUrl).mkCol(
27+
xmlBody = null,
28+
headers = headers.toHeaders()
29+
) { response: Response ->
30+
result.success = response.isSuccessful
31+
result.status = StatusLine.get(response)
32+
result.headers = response.headers
33+
}
34+
35+
return result
36+
}
37+
38+
fun addRequestHeader(key: String, value: String) {
39+
headers[key] = value
40+
}
41+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Nextcloud Android Library
3+
*
4+
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
5+
* SPDX-FileCopyrightText: 2024 ZetaTom <[email protected]>
6+
* SPDX-License-Identifier: MIT
7+
*/
8+
package com.nextcloud.operations
9+
10+
import android.net.Uri
11+
import at.bitfire.dav4jvm.DavResource
12+
import com.nextcloud.common.DavMethod
13+
import com.nextcloud.common.DavResponse
14+
import okhttp3.HttpUrl
15+
import okhttp3.OkHttpClient
16+
import okhttp3.internal.http.StatusLine
17+
18+
class MoveMethod(
19+
httpUrl: HttpUrl,
20+
private val destination: HttpUrl,
21+
private val forceOverwrite: Boolean = false
22+
): DavMethod<DavResponse>(httpUrl) {
23+
private val headers = mutableMapOf<String, String>()
24+
25+
override fun apply(client: OkHttpClient, httpUrl: HttpUrl, filesDavUri: Uri): DavResponse {
26+
val result = DavResponse()
27+
28+
DavResource(client, httpUrl).move(
29+
destination = destination,
30+
overwrite = forceOverwrite
31+
) { response ->
32+
result.success = response.isSuccessful
33+
result.status = StatusLine.get(response)
34+
result.headers = response.headers
35+
}
36+
37+
return result
38+
}
39+
40+
fun addRequestHeader(key: String, value: String) {
41+
headers[key] = value
42+
}
43+
}

library/src/main/java/com/nextcloud/operations/PropFindMethod.kt

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,21 +45,24 @@ class PropFindMethod
4545
) : DavMethod<PropFindResult>(httpUrl) {
4646

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

5150
DavResource(client, httpUrl).propfind(
5251
depth, *propertySet
5352
) { response: Response, hrefRelation: Response.HrefRelation? ->
54-
result.success = response.isSuccess()
53+
result.davResponse.success = response.isSuccess()
54+
response.status?.let { status ->
55+
result.davResponse.status = status
56+
}
57+
5558

5659
when (hrefRelation) {
5760
Response.HrefRelation.MEMBER -> result.children.add(
58-
webDavFileUtils.parseResponse(response, filesDavUri)
61+
WebDavFileUtils.parseResponse(response, filesDavUri)
5962
)
6063

6164
Response.HrefRelation.SELF -> result.root =
62-
webDavFileUtils.parseResponse(response, filesDavUri)
65+
WebDavFileUtils.parseResponse(response, filesDavUri)
6366

6467
Response.HrefRelation.OTHER -> {}
6568
else -> {}

library/src/main/java/com/nextcloud/operations/PropFindResult.kt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,15 @@
2222

2323
package com.nextcloud.operations
2424

25+
import com.nextcloud.common.DavResponse
2526
import com.owncloud.android.lib.resources.files.model.RemoteFile
2627

2728
data class PropFindResult(
28-
var success: Boolean = false,
29+
val davResponse: DavResponse = DavResponse(),
2930
var root: RemoteFile = RemoteFile(),
3031
val children: MutableList<RemoteFile> = mutableListOf()
31-
)
32-
32+
) {
33+
fun getContent(): List<RemoteFile> {
34+
return children + root
35+
}
36+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Nextcloud Android Library
3+
*
4+
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
5+
* SPDX-FileCopyrightText: 2024 ZetaTom <[email protected]>
6+
* SPDX-License-Identifier: MIT
7+
*/
8+
package com.nextcloud.operations
9+
10+
import android.net.Uri
11+
import at.bitfire.dav4jvm.DavResource
12+
import at.bitfire.dav4jvm.Property
13+
import at.bitfire.dav4jvm.Response
14+
import com.nextcloud.common.DavMethod
15+
import com.nextcloud.common.DavResponse
16+
import okhttp3.HttpUrl
17+
import okhttp3.OkHttpClient
18+
19+
class PropPatchMethod
20+
@JvmOverloads constructor(
21+
httpUrl: HttpUrl,
22+
private val setProperties: Map<Property.Name, String> = emptyMap(),
23+
private val removeProperties: List<Property.Name> = emptyList()
24+
) : DavMethod<DavResponse>(httpUrl){
25+
override fun apply(client: OkHttpClient, httpUrl: HttpUrl, filesDavUri: Uri): DavResponse {
26+
val result = DavResponse()
27+
DavResource(client, httpUrl).proppatch(setProperties, removeProperties) { response: Response, hrefRelation: Response.HrefRelation? ->
28+
result.success = response.isSuccess()
29+
response.status?.let { status ->
30+
result.status = status
31+
}
32+
}
33+
34+
return result
35+
}
36+
}

0 commit comments

Comments
 (0)