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 Apr 29, 2022
1 parent 859fced commit 83fa188
Show file tree
Hide file tree
Showing 10 changed files with 284 additions and 78 deletions.
35 changes: 17 additions & 18 deletions library/src/androidTest/java/com/owncloud/android/FileIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
*/
package com.owncloud.android;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import android.net.Uri;

import com.owncloud.android.lib.common.operations.RemoteOperationResult;
Expand All @@ -43,10 +46,6 @@
import java.util.ArrayList;
import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

/**
* Tests related to file operations
*/
Expand All @@ -56,7 +55,7 @@ public void testCreateFolderSuccess() {
String path = "/testFolder/";

// create folder
assertTrue(new CreateFolderRemoteOperation(path, true).execute(client).isSuccess());
assertTrue(new CreateFolderRemoteOperation(path, true).execute(nextcloudClient).isSuccess());

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

// create folder
assertTrue(new CreateFolderRemoteOperation(path, true).execute(client).isSuccess());
assertTrue(new CreateFolderRemoteOperation(path, true).execute(nextcloudClient).isSuccess());

// create folder a second time will fail
assertFalse(new CreateFolderRemoteOperation(path, true).execute(client).isSuccess());
assertFalse(new CreateFolderRemoteOperation(path, true).execute(nextcloudClient).isSuccess());

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

assertTrue(new CreateFolderRemoteOperation(path, true).execute(client).isSuccess());
assertTrue(new CreateFolderRemoteOperation(path, true).execute(nextcloudClient).isSuccess());

// verify folder
assertTrue(new ReadFolderRemoteOperation(path).execute(client).isSuccess());
Expand All @@ -96,20 +95,20 @@ public void testCreateNonExistingSubFolder() {
@Test
public void testCreateFolderWithWrongURL() {
String path = "/testFolder/";
Uri uri = client.getBaseUri();
client.setBaseUri(Uri.parse(uri.toString() + "/remote.php/dav/files/"));
Uri uri = nextcloudClient.getBaseUri();
nextcloudClient.setBaseUri(Uri.parse(uri.toString() + "/remote.php/dav/files/"));

// create folder
assertFalse(new CreateFolderRemoteOperation(path, true).execute(client).isSuccess());
assertFalse(new CreateFolderRemoteOperation(path, true).execute(nextcloudClient).isSuccess());

client.setBaseUri(uri);
nextcloudClient.setBaseUri(uri);
}

@Test
public void testZeroSharees() {
// create & verify folder
String path = "/testFolder/";
assertTrue(new CreateFolderRemoteOperation(path, true).execute(client).isSuccess());
assertTrue(new CreateFolderRemoteOperation(path, true).execute(nextcloudClient).isSuccess());
assertTrue(new ReadFolderRemoteOperation(path).execute(client).isSuccess());

// verify
Expand All @@ -132,7 +131,7 @@ public void testZeroSharees() {
public void testShareViaLinkSharees() {
// create & verify folder
String path = "/testFolder/";
assertTrue(new CreateFolderRemoteOperation(path, true).execute(client).isSuccess());
assertTrue(new CreateFolderRemoteOperation(path, true).execute(nextcloudClient).isSuccess());
assertTrue(new ReadFolderRemoteOperation(path).execute(client).isSuccess());

// share folder
Expand Down Expand Up @@ -164,7 +163,7 @@ public void testShareViaLinkSharees() {
public void testShareToGroupSharees() {
// create & verify folder
String path = "/testFolder/";
assertTrue(new CreateFolderRemoteOperation(path, true).execute(client).isSuccess());
assertTrue(new CreateFolderRemoteOperation(path, true).execute(nextcloudClient).isSuccess());
assertTrue(new ReadFolderRemoteOperation(path).execute(client).isSuccess());

ShareeUser sharee = new ShareeUser("users", "", ShareType.GROUP);
Expand Down Expand Up @@ -199,7 +198,7 @@ public void testShareToGroupSharees() {
public void testOneSharees() {
// create & verify folder
String path = "/testFolder/";
assertTrue(new CreateFolderRemoteOperation(path, true).execute(client).isSuccess());
assertTrue(new CreateFolderRemoteOperation(path, true).execute(nextcloudClient).isSuccess());
assertTrue(new ReadFolderRemoteOperation(path).execute(client).isSuccess());

ShareeUser sharee = new ShareeUser("user1", "User One", ShareType.USER);
Expand Down Expand Up @@ -234,7 +233,7 @@ public void testOneSharees() {
public void testTwoShareesOnParent() {
// create & verify folder
String path = "/testFolder/";
assertTrue(new CreateFolderRemoteOperation(path, true).execute(client).isSuccess());
assertTrue(new CreateFolderRemoteOperation(path, true).execute(nextcloudClient).isSuccess());
assertTrue(new ReadFolderRemoteOperation(path).execute(client).isSuccess());

List<ShareeUser> sharees = new ArrayList<>();
Expand Down Expand Up @@ -282,7 +281,7 @@ public void testTwoShareesOnParent() {
public void testTwoSharees() {
// create & verify folder
String path = "/testFolder/";
assertTrue(new CreateFolderRemoteOperation(path, true).execute(client).isSuccess());
assertTrue(new CreateFolderRemoteOperation(path, true).execute(nextcloudClient).isSuccess());
assertTrue(new ReadFolderRemoteOperation(path).execute(client).isSuccess());

List<ShareeUser> sharees = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import com.owncloud.android.AbstractIT
import com.owncloud.android.lib.common.operations.RemoteOperationResult
import com.owncloud.android.lib.resources.files.model.RemoteFile
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Test

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

var eTagResult = CheckEtagRemoteOperation(remotePath, eTag).execute(client)
var eTagResult = CheckEtagRemoteOperation(remotePath, eTag).execute(nextcloudClient)
assertEquals(RemoteOperationResult.ResultCode.ETAG_UNCHANGED, eTagResult.code)

eTagResult = CheckEtagRemoteOperation(remotePath, "wrongEtag").execute(client)
eTagResult = CheckEtagRemoteOperation(remotePath, "wrongEtag").execute(nextcloudClient)
assertEquals(RemoteOperationResult.ResultCode.ETAG_CHANGED, eTagResult.code)

eTagResult = CheckEtagRemoteOperation("wrongPath", "wrongEtag").execute(nextcloudClient)
assertFalse(eTagResult.isSuccess)
assertEquals(RemoteOperationResult.ResultCode.FILE_NOT_FOUND, eTagResult.code)
}
}
17 changes: 16 additions & 1 deletion library/src/main/java/com/nextcloud/common/NextcloudClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,15 @@ package com.nextcloud.common

import android.content.Context
import android.net.Uri
import com.nextcloud.operations.dav.OkHttpDavMethodBase
import com.owncloud.android.lib.common.OwnCloudClient
import com.owncloud.android.lib.common.OwnCloudClientFactory.DEFAULT_CONNECTION_TIMEOUT_LONG
import com.owncloud.android.lib.common.OwnCloudClientFactory.DEFAULT_DATA_TIMEOUT_LONG
import com.owncloud.android.lib.common.accounts.AccountUtils
import com.owncloud.android.lib.common.network.AdvancedX509TrustManager
import com.owncloud.android.lib.common.network.NetworkUtils
import com.owncloud.android.lib.common.network.RedirectionPath
import com.owncloud.android.lib.common.network.WebdavUtils
import com.owncloud.android.lib.common.operations.RemoteOperation
import com.owncloud.android.lib.common.operations.RemoteOperationResult
import com.owncloud.android.lib.common.utils.Log_OC
Expand All @@ -54,7 +56,7 @@ class NextcloudClient(
var baseUri: Uri,
var userId: String,
var credentials: String,
val client: OkHttpClient
var client: OkHttpClient
) {
var followRedirects = true

Expand Down Expand Up @@ -105,6 +107,11 @@ class NextcloudClient(
return method.execute(this)
}

@Throws(Exception::class)
fun execute(method: OkHttpDavMethodBase) {
return method.execute(this)
}

internal fun execute(request: Request): ResponseOrError {
return try {
val response = client.newCall(request).execute()
Expand Down Expand Up @@ -177,4 +184,12 @@ class NextcloudClient(
fun getUserIdPlain(): String {
return userId
}

fun getFilesDavUri(path: String): String {
return getDavUri().toString() + "/files/" + userId + "/" + WebdavUtils.encodePath(path)
}

fun getDavUri(): Uri {
return Uri.parse(baseUri.toString() + AccountUtils.WEBDAV_PATH_9_0)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Nextcloud Android client application
*
* @author Tobias Kaminsky
* Copyright (C) 2022 Tobias Kaminsky
* Copyright (C) 2022 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.dav

import okhttp3.Interceptor
import okhttp3.Response

class BasicAuthInterceptor(val credentials: String) : Interceptor {
override fun intercept(chain: Interceptor.Chain): Response {
val request = chain.request()
val authRequest = request
.newBuilder()
.header("Authorization", credentials)
.build()

return chain.proceed(authRequest)
}
}
41 changes: 41 additions & 0 deletions library/src/main/java/com/nextcloud/operations/dav/MkColMethod.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Nextcloud Android client application
*
* @author Tobias Kaminsky
* Copyright (C) 2022 Tobias Kaminsky
* Copyright (C) 2022 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.dav

import at.bitfire.dav4jvm.DavResource
import com.nextcloud.common.NextcloudClient
import okhttp3.HttpUrl.Companion.toHttpUrl

/**
* Webdav MkCol method that uses OkHttp with new NextcloudClient
*/
class MkColMethod(val uri: String) : OkHttpDavMethodBase() {
override fun createDavResource(nextcloudClient: NextcloudClient) {
val httpURL = uri.toHttpUrl()
val davResource = DavResource(nextcloudClient.client, httpURL)
davResource.mkCol(null) {
// log response
response = it
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Nextcloud Android client application
*
* @author Tobias Kaminsky
* Copyright (C) 2022 Tobias Kaminsky
* Copyright (C) 2022 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.dav

import com.nextcloud.common.NextcloudClient
import com.nextcloud.common.OkHttpMethodBase
import okhttp3.Response

abstract class OkHttpDavMethodBase {
var response: Response? = null

fun execute(nextcloudClient: NextcloudClient) {
nextcloudClient.client = nextcloudClient.client
.newBuilder()
.followRedirects(false)
.addInterceptor(BasicAuthInterceptor(nextcloudClient.credentials))
.build()

createDavResource(nextcloudClient)
}

abstract fun createDavResource(nextcloudClient: NextcloudClient)

open fun statusCode(): Int {
return response?.code ?: OkHttpMethodBase.UNKNOWN_STATUS_CODE
}

fun releaseConnection() {
response?.body?.close()
}

fun succeeded(): Boolean {
return response?.isSuccessful ?: false
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Nextcloud Android client application
*
* @author Tobias Kaminsky
* Copyright (C) 2022 Tobias Kaminsky
* Copyright (C) 2022 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.dav

import at.bitfire.dav4jvm.DavResource
import at.bitfire.dav4jvm.DavResponseCallback
import at.bitfire.dav4jvm.Property
import at.bitfire.dav4jvm.Response
import com.nextcloud.common.NextcloudClient
import com.nextcloud.common.OkHttpMethodBase
import okhttp3.HttpUrl.Companion.toHttpUrl

/**
* Webdav Propfind method that uses OkHttp with new NextcloudClient
*/
class PropFindMethod(
val uri: String,
val properties: ArrayList<Property.Name>,
private val depth: Int
) : OkHttpDavMethodBase() {
var davResponse: Response? = null

override fun createDavResource(nextcloudClient: NextcloudClient) {
val httpURL = uri.toHttpUrl()
val davResource = DavResource(nextcloudClient.client, httpURL)
val callback: DavResponseCallback = { response, _ ->
davResponse = response
}
davResource.propfind(depth, *properties.toTypedArray(), callback = callback)
}

fun davResponse(): Response? {
return davResponse
}

override fun statusCode(): Int {
return davResponse?.status?.code ?: OkHttpMethodBase.UNKNOWN_STATUS_CODE
}
}
Loading

0 comments on commit 83fa188

Please sign in to comment.