Skip to content

Commit a90a4b7

Browse files
wip
Signed-off-by: tobiasKaminsky <[email protected]>
1 parent 2374f29 commit a90a4b7

File tree

7 files changed

+168
-26
lines changed

7 files changed

+168
-26
lines changed

library/src/androidTest/java/com/owncloud/android/Dav4JVM.kt

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ package com.owncloud.android
3030
import at.bitfire.dav4jvm.DavResource
3131
import at.bitfire.dav4jvm.Response
3232
import com.nextcloud.common.NextcloudAuthenticator
33+
import com.nextcloud.operations.PropFindMethod
3334
import com.owncloud.android.lib.common.network.WebdavUtils
3435
import com.owncloud.android.lib.common.utils.WebDavFileUtils
3536
import com.owncloud.android.lib.resources.files.CreateFolderRemoteOperation
@@ -57,7 +58,7 @@ class Dav4JVM : AbstractIT() {
5758
@Throws(IOException::class)
5859
fun singlePropfind() {
5960
val path = "/testFolder/"
60-
val subFolder = "$path subfolder/"
61+
val subFolder = path + "subfolder/"
6162

6263
// create folder
6364
CreateFolderRemoteOperation(
@@ -123,7 +124,6 @@ class Dav4JVM : AbstractIT() {
123124
WebdavUtils.registerCustomFactories()
124125

125126
// TODO use DavResource().propfind in ReadFileRemoteOperation/ReadFolderRemoteOperation
126-
// TODO extract in own class for convenient use
127127
// TODO test all properties on server!
128128
DavResource(client, httpUrl)
129129
.propfind(
@@ -144,8 +144,20 @@ class Dav4JVM : AbstractIT() {
144144
assertEquals(1, memberElements.size)
145145

146146
val remoteFile = WebDavFileUtils().parseResponse(rootElement, nextcloudClient.filesDavUri)
147-
148147
assertTrue(oldRemoteFile == remoteFile)
148+
149+
val subfolderFile =
150+
WebDavFileUtils().parseResponse(memberElements[0], nextcloudClient.filesDavUri)
151+
assertTrue(oldSubFolderFile == subfolderFile)
152+
153+
// new propfind
154+
val newResult = nextcloudClient.execute(PropFindMethod(httpUrl))
155+
156+
assertTrue(newResult.success)
157+
assertTrue(oldRemoteFile == newResult.root)
158+
159+
assertEquals(1, newResult.children.size)
160+
assertTrue(oldSubFolderFile == newResult.children[0])
149161
}
150162

151163
@Test
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
*
3+
* Nextcloud Android client application
4+
*
5+
* @author Tobias Kaminsky
6+
* Copyright (C) 2023 Tobias Kaminsky
7+
* Copyright (C) 2023 Nextcloud GmbH
8+
*
9+
* This program is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU Affero General Public License as published by
11+
* the Free Software Foundation, either version 3 of the License, or
12+
* (at your option) any later version.
13+
*
14+
* This program is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* GNU Affero General Public License for more details.
18+
*
19+
* You should have received a copy of the GNU Affero General Public License
20+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
21+
*/
22+
package com.nextcloud.common
23+
24+
import android.net.Uri
25+
import com.owncloud.android.lib.common.network.WebdavUtils
26+
import okhttp3.HttpUrl
27+
import okhttp3.OkHttpClient
28+
29+
abstract class DavMethod<T>(private val httpUrl: HttpUrl) {
30+
fun execute(nextcloudClient: NextcloudClient): T {
31+
// register custom property
32+
WebdavUtils.registerCustomFactories()
33+
34+
return apply(nextcloudClient.disabledRedirectClient(), httpUrl, nextcloudClient.filesDavUri)
35+
}
36+
37+
abstract fun apply(client: OkHttpClient, httpUrl: HttpUrl, filesDavUri: Uri): T
38+
}

library/src/main/java/com/nextcloud/common/NextcloudClient.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,18 @@ class NextcloudClient private constructor(
114114
return method.execute(this)
115115
}
116116

117+
fun <T> execute(method: DavMethod<T>): T {
118+
return method.execute(this)
119+
}
120+
121+
fun disabledRedirectClient(): OkHttpClient {
122+
return client
123+
.newBuilder()
124+
.followRedirects(false)
125+
.authenticator(NextcloudAuthenticator(credentials, "Authorization"))
126+
.build()
127+
}
128+
117129
internal fun execute(request: Request): ResponseOrError {
118130
return try {
119131
val response = client.newCall(request).execute()
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
*
3+
* Nextcloud Android client application
4+
*
5+
* @author Tobias Kaminsky
6+
* Copyright (C) 2023 Tobias Kaminsky
7+
* Copyright (C) 2023 Nextcloud GmbH
8+
*
9+
* This program is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU Affero General Public License as published by
11+
* the Free Software Foundation, either version 3 of the License, or
12+
* (at your option) any later version.
13+
*
14+
* This program is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* GNU Affero General Public License for more details.
18+
*
19+
* You should have received a copy of the GNU Affero General Public License
20+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
21+
*/
22+
package com.nextcloud.operations
23+
24+
import android.net.Uri
25+
import at.bitfire.dav4jvm.DavResource
26+
import at.bitfire.dav4jvm.Response
27+
import com.nextcloud.common.DavMethod
28+
import com.owncloud.android.lib.common.network.WebdavUtils
29+
import com.owncloud.android.lib.common.utils.WebDavFileUtils
30+
import okhttp3.HttpUrl
31+
import okhttp3.OkHttpClient
32+
import org.apache.jackrabbit.webdav.DavConstants
33+
34+
class PropFindMethod(httpUrl: HttpUrl) : DavMethod<PropFindResult>(httpUrl) {
35+
override fun apply(client: OkHttpClient, httpUrl: HttpUrl, filesDavUri: Uri): PropFindResult {
36+
val webDavFileUtils = WebDavFileUtils()
37+
val result = PropFindResult()
38+
39+
DavResource(client, httpUrl)
40+
.propfind(
41+
DavConstants.DEPTH_1,
42+
*WebdavUtils.getAllPropertiesList()
43+
) { response: Response, hrefRelation: Response.HrefRelation? ->
44+
result.success = response.isSuccess()
45+
46+
when (hrefRelation) {
47+
Response.HrefRelation.MEMBER -> result.children.add(
48+
webDavFileUtils.parseResponse(response, filesDavUri)
49+
)
50+
51+
Response.HrefRelation.SELF -> result.root =
52+
webDavFileUtils.parseResponse(response, filesDavUri)
53+
54+
Response.HrefRelation.OTHER -> {}
55+
else -> {}
56+
}
57+
}
58+
59+
return result
60+
}
61+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
*
3+
* Nextcloud Android client application
4+
*
5+
* @author Tobias Kaminsky
6+
* Copyright (C) 2023 Tobias Kaminsky
7+
* Copyright (C) 2023 Nextcloud GmbH
8+
*
9+
* This program is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU Affero General Public License as published by
11+
* the Free Software Foundation, either version 3 of the License, or
12+
* (at your option) any later version.
13+
*
14+
* This program is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* GNU Affero General Public License for more details.
18+
*
19+
* You should have received a copy of the GNU Affero General Public License
20+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
21+
*/
22+
23+
package com.nextcloud.operations
24+
25+
import com.owncloud.android.lib.resources.files.model.RemoteFile
26+
27+
data class PropFindResult(
28+
var success: Boolean = false,
29+
var root: RemoteFile = RemoteFile(),
30+
val children: MutableList<RemoteFile> = mutableListOf()
31+
)
32+

library/src/main/java/com/owncloud/android/lib/resources/files/SearchRemoteOperation.java

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727

2828
package com.owncloud.android.lib.resources.files;
2929

30-
import com.nextcloud.common.NextcloudAuthenticator;
3130
import com.nextcloud.common.NextcloudClient;
3231
import com.owncloud.android.lib.common.OwnCloudClient;
3332
import com.owncloud.android.lib.common.operations.RemoteOperation;
@@ -56,7 +55,6 @@
5655
import at.bitfire.dav4jvm.DavResource;
5756
import at.bitfire.dav4jvm.Response;
5857
import okhttp3.HttpUrl;
59-
import okhttp3.OkHttpClient;
6058

6159
/**
6260
* Remote operation performing the search in the Nextcloud server.
@@ -199,20 +197,13 @@ public RemoteOperationResult run(NextcloudClient client) {
199197
startDate,
200198
endDate);
201199

202-
// disable redirect
203-
OkHttpClient disabledRedirectClient = client.getClient()
204-
.newBuilder()
205-
.followRedirects(false)
206-
.authenticator(new NextcloudAuthenticator(client.getCredentials(), "Authorization"))
207-
.build();
208-
209200
Document searchDocument = searchMethod.getDocumentQuery(searchInfo);
210201
String searchString = transformDocumentToString(searchDocument);
211202

212203
ArrayList<Response> responses = new ArrayList<>();
213204

214205
new DavResource(
215-
disabledRedirectClient,
206+
client.disabledRedirectClient(),
216207
HttpUrl.get(client.getDavUri().toString()))
217208
.search(searchString, (response, hrefRelation) -> {
218209
responses.add(response);

library/src/main/java/com/owncloud/android/lib/resources/files/ToggleFavoriteRemoteOperation.java

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929

3030
import android.net.Uri;
3131

32-
import com.nextcloud.common.NextcloudAuthenticator;
3332
import com.nextcloud.common.NextcloudClient;
3433
import com.owncloud.android.lib.common.OwnCloudClient;
3534
import com.owncloud.android.lib.common.network.WebdavEntry;
@@ -49,11 +48,11 @@
4948
import java.util.HashMap;
5049
import java.util.List;
5150
import java.util.Map;
51+
import java.util.concurrent.atomic.AtomicBoolean;
5252

5353
import at.bitfire.dav4jvm.DavResource;
5454
import at.bitfire.dav4jvm.Property;
5555
import okhttp3.HttpUrl;
56-
import okhttp3.OkHttpClient;
5756

5857
/**
5958
* Favorite or unfavorite a file.
@@ -117,13 +116,6 @@ public RemoteOperationResult run(NextcloudClient client) {
117116
List<Property.Name> removeProperties = new ArrayList<>();
118117
Map<Property.Name, String> newProperties = new HashMap<>();
119118

120-
// disable redirect
121-
OkHttpClient disabledRedirectClient = client.getClient()
122-
.newBuilder()
123-
.followRedirects(false)
124-
.authenticator(new NextcloudAuthenticator(client.getCredentials(), "Authorization"))
125-
.build();
126-
127119
if (makeItFavorited) {
128120
newProperties.put(NCFavorite.NAME, "1");
129121
} else {
@@ -134,15 +126,19 @@ public RemoteOperationResult run(NextcloudClient client) {
134126
String encodedPath = (client.getUserId() + Uri.encode(filePath)).replace("%2F", "/");
135127
String fullFilePath = webDavUrl + "/files/" + encodedPath;
136128

137-
boolean resultCode = false;
129+
AtomicBoolean resultCode = new AtomicBoolean(false);
138130

139-
new DavResource(disabledRedirectClient,
131+
new DavResource(client.disabledRedirectClient(),
140132
HttpUrl.get(fullFilePath))
141133
.proppatch(newProperties, removeProperties, (response, hrefRelation) -> {
142-
//resultCode = response.isSuccess();
134+
resultCode.set(response.isSuccess());
143135
});
144136

145-
result = new RemoteOperationResult<>(RemoteOperationResult.ResultCode.OK);
137+
if (resultCode.get()) {
138+
result = new RemoteOperationResult<>(RemoteOperationResult.ResultCode.OK);
139+
} else {
140+
result = new RemoteOperationResult<>(RemoteOperationResult.ResultCode.WRONG_SERVER_RESPONSE);
141+
}
146142

147143
return result;
148144
}

0 commit comments

Comments
 (0)