Skip to content

Commit 83fa188

Browse files
wip
Signed-off-by: tobiasKaminsky <[email protected]>
1 parent 859fced commit 83fa188

File tree

10 files changed

+284
-78
lines changed

10 files changed

+284
-78
lines changed

library/src/androidTest/java/com/owncloud/android/FileIT.java

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626
*/
2727
package com.owncloud.android;
2828

29+
import static org.junit.Assert.assertFalse;
30+
import static org.junit.Assert.assertTrue;
31+
2932
import android.net.Uri;
3033

3134
import com.owncloud.android.lib.common.operations.RemoteOperationResult;
@@ -43,10 +46,6 @@
4346
import java.util.ArrayList;
4447
import java.util.List;
4548

46-
import static org.junit.Assert.assertEquals;
47-
import static org.junit.Assert.assertFalse;
48-
import static org.junit.Assert.assertTrue;
49-
5049
/**
5150
* Tests related to file operations
5251
*/
@@ -56,7 +55,7 @@ public void testCreateFolderSuccess() {
5655
String path = "/testFolder/";
5756

5857
// create folder
59-
assertTrue(new CreateFolderRemoteOperation(path, true).execute(client).isSuccess());
58+
assertTrue(new CreateFolderRemoteOperation(path, true).execute(nextcloudClient).isSuccess());
6059

6160
// verify folder
6261
assertTrue(new ReadFolderRemoteOperation(path).execute(client).isSuccess());
@@ -70,10 +69,10 @@ public void testCreateFolderFailure() {
7069
String path = "/testFolder/";
7170

7271
// create folder
73-
assertTrue(new CreateFolderRemoteOperation(path, true).execute(client).isSuccess());
72+
assertTrue(new CreateFolderRemoteOperation(path, true).execute(nextcloudClient).isSuccess());
7473

7574
// create folder a second time will fail
76-
assertFalse(new CreateFolderRemoteOperation(path, true).execute(client).isSuccess());
75+
assertFalse(new CreateFolderRemoteOperation(path, true).execute(nextcloudClient).isSuccess());
7776

7877
// remove folder
7978
assertTrue(new RemoveFileRemoteOperation(path).execute(client).isSuccess());
@@ -84,7 +83,7 @@ public void testCreateNonExistingSubFolder() {
8483
String path = "/testFolder/1/2/3/4/5/";
8584
String top = "/testFolder/";
8685

87-
assertTrue(new CreateFolderRemoteOperation(path, true).execute(client).isSuccess());
86+
assertTrue(new CreateFolderRemoteOperation(path, true).execute(nextcloudClient).isSuccess());
8887

8988
// verify folder
9089
assertTrue(new ReadFolderRemoteOperation(path).execute(client).isSuccess());
@@ -96,20 +95,20 @@ public void testCreateNonExistingSubFolder() {
9695
@Test
9796
public void testCreateFolderWithWrongURL() {
9897
String path = "/testFolder/";
99-
Uri uri = client.getBaseUri();
100-
client.setBaseUri(Uri.parse(uri.toString() + "/remote.php/dav/files/"));
98+
Uri uri = nextcloudClient.getBaseUri();
99+
nextcloudClient.setBaseUri(Uri.parse(uri.toString() + "/remote.php/dav/files/"));
101100

102101
// create folder
103-
assertFalse(new CreateFolderRemoteOperation(path, true).execute(client).isSuccess());
102+
assertFalse(new CreateFolderRemoteOperation(path, true).execute(nextcloudClient).isSuccess());
104103

105-
client.setBaseUri(uri);
104+
nextcloudClient.setBaseUri(uri);
106105
}
107106

108107
@Test
109108
public void testZeroSharees() {
110109
// create & verify folder
111110
String path = "/testFolder/";
112-
assertTrue(new CreateFolderRemoteOperation(path, true).execute(client).isSuccess());
111+
assertTrue(new CreateFolderRemoteOperation(path, true).execute(nextcloudClient).isSuccess());
113112
assertTrue(new ReadFolderRemoteOperation(path).execute(client).isSuccess());
114113

115114
// verify
@@ -132,7 +131,7 @@ public void testZeroSharees() {
132131
public void testShareViaLinkSharees() {
133132
// create & verify folder
134133
String path = "/testFolder/";
135-
assertTrue(new CreateFolderRemoteOperation(path, true).execute(client).isSuccess());
134+
assertTrue(new CreateFolderRemoteOperation(path, true).execute(nextcloudClient).isSuccess());
136135
assertTrue(new ReadFolderRemoteOperation(path).execute(client).isSuccess());
137136

138137
// share folder
@@ -164,7 +163,7 @@ public void testShareViaLinkSharees() {
164163
public void testShareToGroupSharees() {
165164
// create & verify folder
166165
String path = "/testFolder/";
167-
assertTrue(new CreateFolderRemoteOperation(path, true).execute(client).isSuccess());
166+
assertTrue(new CreateFolderRemoteOperation(path, true).execute(nextcloudClient).isSuccess());
168167
assertTrue(new ReadFolderRemoteOperation(path).execute(client).isSuccess());
169168

170169
ShareeUser sharee = new ShareeUser("users", "", ShareType.GROUP);
@@ -199,7 +198,7 @@ public void testShareToGroupSharees() {
199198
public void testOneSharees() {
200199
// create & verify folder
201200
String path = "/testFolder/";
202-
assertTrue(new CreateFolderRemoteOperation(path, true).execute(client).isSuccess());
201+
assertTrue(new CreateFolderRemoteOperation(path, true).execute(nextcloudClient).isSuccess());
203202
assertTrue(new ReadFolderRemoteOperation(path).execute(client).isSuccess());
204203

205204
ShareeUser sharee = new ShareeUser("user1", "User One", ShareType.USER);
@@ -234,7 +233,7 @@ public void testOneSharees() {
234233
public void testTwoShareesOnParent() {
235234
// create & verify folder
236235
String path = "/testFolder/";
237-
assertTrue(new CreateFolderRemoteOperation(path, true).execute(client).isSuccess());
236+
assertTrue(new CreateFolderRemoteOperation(path, true).execute(nextcloudClient).isSuccess());
238237
assertTrue(new ReadFolderRemoteOperation(path).execute(client).isSuccess());
239238

240239
List<ShareeUser> sharees = new ArrayList<>();
@@ -282,7 +281,7 @@ public void testTwoShareesOnParent() {
282281
public void testTwoSharees() {
283282
// create & verify folder
284283
String path = "/testFolder/";
285-
assertTrue(new CreateFolderRemoteOperation(path, true).execute(client).isSuccess());
284+
assertTrue(new CreateFolderRemoteOperation(path, true).execute(nextcloudClient).isSuccess());
286285
assertTrue(new ReadFolderRemoteOperation(path).execute(client).isSuccess());
287286

288287
List<ShareeUser> sharees = new ArrayList<>();

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import com.owncloud.android.AbstractIT
3030
import com.owncloud.android.lib.common.operations.RemoteOperationResult
3131
import com.owncloud.android.lib.resources.files.model.RemoteFile
3232
import org.junit.Assert.assertEquals
33+
import org.junit.Assert.assertFalse
3334
import org.junit.Assert.assertTrue
3435
import org.junit.Test
3536

@@ -47,10 +48,14 @@ class CheckEtagRemoteOperationIT : AbstractIT() {
4748
val remoteFile = readResult.data[0] as RemoteFile
4849
val eTag = remoteFile.etag
4950

50-
var eTagResult = CheckEtagRemoteOperation(remotePath, eTag).execute(client)
51+
var eTagResult = CheckEtagRemoteOperation(remotePath, eTag).execute(nextcloudClient)
5152
assertEquals(RemoteOperationResult.ResultCode.ETAG_UNCHANGED, eTagResult.code)
5253

53-
eTagResult = CheckEtagRemoteOperation(remotePath, "wrongEtag").execute(client)
54+
eTagResult = CheckEtagRemoteOperation(remotePath, "wrongEtag").execute(nextcloudClient)
5455
assertEquals(RemoteOperationResult.ResultCode.ETAG_CHANGED, eTagResult.code)
56+
57+
eTagResult = CheckEtagRemoteOperation("wrongPath", "wrongEtag").execute(nextcloudClient)
58+
assertFalse(eTagResult.isSuccess)
59+
assertEquals(RemoteOperationResult.ResultCode.FILE_NOT_FOUND, eTagResult.code)
5560
}
5661
}

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,15 @@ package com.nextcloud.common
3030

3131
import android.content.Context
3232
import android.net.Uri
33+
import com.nextcloud.operations.dav.OkHttpDavMethodBase
3334
import com.owncloud.android.lib.common.OwnCloudClient
3435
import com.owncloud.android.lib.common.OwnCloudClientFactory.DEFAULT_CONNECTION_TIMEOUT_LONG
3536
import com.owncloud.android.lib.common.OwnCloudClientFactory.DEFAULT_DATA_TIMEOUT_LONG
3637
import com.owncloud.android.lib.common.accounts.AccountUtils
3738
import com.owncloud.android.lib.common.network.AdvancedX509TrustManager
3839
import com.owncloud.android.lib.common.network.NetworkUtils
3940
import com.owncloud.android.lib.common.network.RedirectionPath
41+
import com.owncloud.android.lib.common.network.WebdavUtils
4042
import com.owncloud.android.lib.common.operations.RemoteOperation
4143
import com.owncloud.android.lib.common.operations.RemoteOperationResult
4244
import com.owncloud.android.lib.common.utils.Log_OC
@@ -54,7 +56,7 @@ class NextcloudClient(
5456
var baseUri: Uri,
5557
var userId: String,
5658
var credentials: String,
57-
val client: OkHttpClient
59+
var client: OkHttpClient
5860
) {
5961
var followRedirects = true
6062

@@ -105,6 +107,11 @@ class NextcloudClient(
105107
return method.execute(this)
106108
}
107109

110+
@Throws(Exception::class)
111+
fun execute(method: OkHttpDavMethodBase) {
112+
return method.execute(this)
113+
}
114+
108115
internal fun execute(request: Request): ResponseOrError {
109116
return try {
110117
val response = client.newCall(request).execute()
@@ -177,4 +184,12 @@ class NextcloudClient(
177184
fun getUserIdPlain(): String {
178185
return userId
179186
}
187+
188+
fun getFilesDavUri(path: String): String {
189+
return getDavUri().toString() + "/files/" + userId + "/" + WebdavUtils.encodePath(path)
190+
}
191+
192+
fun getDavUri(): Uri {
193+
return Uri.parse(baseUri.toString() + AccountUtils.WEBDAV_PATH_9_0)
194+
}
180195
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Nextcloud Android client application
3+
*
4+
* @author Tobias Kaminsky
5+
* Copyright (C) 2022 Tobias Kaminsky
6+
* Copyright (C) 2022 Nextcloud GmbH
7+
*
8+
* This program is free software: you can redistribute it and/or modify
9+
* it under the terms of the GNU Affero General Public License as published by
10+
* the Free Software Foundation, either version 3 of the License, or
11+
* (at your option) any later version.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* GNU Affero General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU Affero General Public License
19+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
20+
*
21+
*/
22+
23+
package com.nextcloud.operations.dav
24+
25+
import okhttp3.Interceptor
26+
import okhttp3.Response
27+
28+
class BasicAuthInterceptor(val credentials: String) : Interceptor {
29+
override fun intercept(chain: Interceptor.Chain): Response {
30+
val request = chain.request()
31+
val authRequest = request
32+
.newBuilder()
33+
.header("Authorization", credentials)
34+
.build()
35+
36+
return chain.proceed(authRequest)
37+
}
38+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Nextcloud Android client application
3+
*
4+
* @author Tobias Kaminsky
5+
* Copyright (C) 2022 Tobias Kaminsky
6+
* Copyright (C) 2022 Nextcloud GmbH
7+
*
8+
* This program is free software: you can redistribute it and/or modify
9+
* it under the terms of the GNU Affero General Public License as published by
10+
* the Free Software Foundation, either version 3 of the License, or
11+
* (at your option) any later version.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* GNU Affero General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU Affero General Public License
19+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
20+
*
21+
*/
22+
23+
package com.nextcloud.operations.dav
24+
25+
import at.bitfire.dav4jvm.DavResource
26+
import com.nextcloud.common.NextcloudClient
27+
import okhttp3.HttpUrl.Companion.toHttpUrl
28+
29+
/**
30+
* Webdav MkCol method that uses OkHttp with new NextcloudClient
31+
*/
32+
class MkColMethod(val uri: String) : OkHttpDavMethodBase() {
33+
override fun createDavResource(nextcloudClient: NextcloudClient) {
34+
val httpURL = uri.toHttpUrl()
35+
val davResource = DavResource(nextcloudClient.client, httpURL)
36+
davResource.mkCol(null) {
37+
// log response
38+
response = it
39+
}
40+
}
41+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Nextcloud Android client application
3+
*
4+
* @author Tobias Kaminsky
5+
* Copyright (C) 2022 Tobias Kaminsky
6+
* Copyright (C) 2022 Nextcloud GmbH
7+
*
8+
* This program is free software: you can redistribute it and/or modify
9+
* it under the terms of the GNU Affero General Public License as published by
10+
* the Free Software Foundation, either version 3 of the License, or
11+
* (at your option) any later version.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* GNU Affero General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU Affero General Public License
19+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
20+
*
21+
*/
22+
23+
package com.nextcloud.operations.dav
24+
25+
import com.nextcloud.common.NextcloudClient
26+
import com.nextcloud.common.OkHttpMethodBase
27+
import okhttp3.Response
28+
29+
abstract class OkHttpDavMethodBase {
30+
var response: Response? = null
31+
32+
fun execute(nextcloudClient: NextcloudClient) {
33+
nextcloudClient.client = nextcloudClient.client
34+
.newBuilder()
35+
.followRedirects(false)
36+
.addInterceptor(BasicAuthInterceptor(nextcloudClient.credentials))
37+
.build()
38+
39+
createDavResource(nextcloudClient)
40+
}
41+
42+
abstract fun createDavResource(nextcloudClient: NextcloudClient)
43+
44+
open fun statusCode(): Int {
45+
return response?.code ?: OkHttpMethodBase.UNKNOWN_STATUS_CODE
46+
}
47+
48+
fun releaseConnection() {
49+
response?.body?.close()
50+
}
51+
52+
fun succeeded(): Boolean {
53+
return response?.isSuccessful ?: false
54+
}
55+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Nextcloud Android client application
3+
*
4+
* @author Tobias Kaminsky
5+
* Copyright (C) 2022 Tobias Kaminsky
6+
* Copyright (C) 2022 Nextcloud GmbH
7+
*
8+
* This program is free software: you can redistribute it and/or modify
9+
* it under the terms of the GNU Affero General Public License as published by
10+
* the Free Software Foundation, either version 3 of the License, or
11+
* (at your option) any later version.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* GNU Affero General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU Affero General Public License
19+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
20+
*
21+
*/
22+
23+
package com.nextcloud.operations.dav
24+
25+
import at.bitfire.dav4jvm.DavResource
26+
import at.bitfire.dav4jvm.DavResponseCallback
27+
import at.bitfire.dav4jvm.Property
28+
import at.bitfire.dav4jvm.Response
29+
import com.nextcloud.common.NextcloudClient
30+
import com.nextcloud.common.OkHttpMethodBase
31+
import okhttp3.HttpUrl.Companion.toHttpUrl
32+
33+
/**
34+
* Webdav Propfind method that uses OkHttp with new NextcloudClient
35+
*/
36+
class PropFindMethod(
37+
val uri: String,
38+
val properties: ArrayList<Property.Name>,
39+
private val depth: Int
40+
) : OkHttpDavMethodBase() {
41+
var davResponse: Response? = null
42+
43+
override fun createDavResource(nextcloudClient: NextcloudClient) {
44+
val httpURL = uri.toHttpUrl()
45+
val davResource = DavResource(nextcloudClient.client, httpURL)
46+
val callback: DavResponseCallback = { response, _ ->
47+
davResponse = response
48+
}
49+
davResource.propfind(depth, *properties.toTypedArray(), callback = callback)
50+
}
51+
52+
fun davResponse(): Response? {
53+
return davResponse
54+
}
55+
56+
override fun statusCode(): Int {
57+
return davResponse?.status?.code ?: OkHttpMethodBase.UNKNOWN_STATUS_CODE
58+
}
59+
}

0 commit comments

Comments
 (0)