From ec5bcdbf3bb4853fac4cb88435681fb46291d011 Mon Sep 17 00:00:00 2001 From: ZetaTom <70907959+ZetaTom@users.noreply.github.com> Date: Mon, 8 Jan 2024 13:52:11 +0100 Subject: [PATCH 1/7] Deprecate OwnCloudClient - Sharing Signed-off-by: ZetaTom <70907959+ZetaTom@users.noreply.github.com> --- ...ashboardGetWidgetItemsRemoteOperationIT.kt | 4 +- .../java/com/owncloud/android/AbstractIT.java | 18 ++-- .../java/com/owncloud/android/FileIT.java | 22 ++--- .../lib/common/operations/CreateShareIT.java | 30 +++---- .../lib/common/operations/GetSharesIT.java | 12 +-- .../lib/common/operations/RemoveShareIT.java | 6 +- .../files/SearchRemoteOperationIT.java | 12 +-- .../shares/CreateShareRemoteOperationIT.kt | 2 +- .../shares/GetSharesRemoteOperationIT.java | 88 ++++++++----------- .../shares/UpdateShareRemoteOperationIT.kt | 20 ++--- .../common/OwnCloudAnonymousCredentials.java | 2 +- .../files/ExistenceCheckRemoteOperation.java | 36 +++----- .../shares/CreateShareRemoteOperation.java | 42 ++++----- .../shares/GetShareRemoteOperation.java | 22 +++-- .../shares/GetShareesRemoteOperation.java | 18 ++-- .../GetSharesForFileRemoteOperation.java | 52 ++++++----- .../shares/GetSharesRemoteOperation.java | 18 ++-- .../shares/RemoveShareRemoteOperation.java | 26 +++--- .../lib/resources/shares/ShareUtils.java | 4 +- .../shares/UpdateShareRemoteOperation.java | 30 +++---- 20 files changed, 213 insertions(+), 251 deletions(-) diff --git a/library/src/androidTest/java/com/nextcloud/android/lib/resources/dashboard/DashboardGetWidgetItemsRemoteOperationIT.kt b/library/src/androidTest/java/com/nextcloud/android/lib/resources/dashboard/DashboardGetWidgetItemsRemoteOperationIT.kt index a2e9aed38..a1a4495b2 100644 --- a/library/src/androidTest/java/com/nextcloud/android/lib/resources/dashboard/DashboardGetWidgetItemsRemoteOperationIT.kt +++ b/library/src/androidTest/java/com/nextcloud/android/lib/resources/dashboard/DashboardGetWidgetItemsRemoteOperationIT.kt @@ -23,7 +23,7 @@ class DashboardGetWidgetItemsRemoteOperationIT : AbstractIT() { testOnlyOnServer(NextcloudVersion.nextcloud_25) // create folder to have some content - assertTrue(CreateFolderRemoteOperation("/testFolder", false).execute(client2).isSuccess) + assertTrue(CreateFolderRemoteOperation("/testFolder", false).execute(nextcloudClient2).isSuccess) assertTrue( CreateShareRemoteOperation( "/testFolder", @@ -32,7 +32,7 @@ class DashboardGetWidgetItemsRemoteOperationIT : AbstractIT() { false, "", OCShare.MAXIMUM_PERMISSIONS_FOR_FOLDER - ).execute(client2) + ).execute(nextcloudClient2) .isSuccess ) diff --git a/library/src/androidTest/java/com/owncloud/android/AbstractIT.java b/library/src/androidTest/java/com/owncloud/android/AbstractIT.java index 629747cce..8c738d7cf 100644 --- a/library/src/androidTest/java/com/owncloud/android/AbstractIT.java +++ b/library/src/androidTest/java/com/owncloud/android/AbstractIT.java @@ -7,17 +7,10 @@ */ package com.owncloud.android; -import static junit.framework.TestCase.assertTrue; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; -import static org.junit.Assume.assumeTrue; - import android.content.Context; import android.net.Uri; import android.os.Bundle; -import androidx.test.platform.app.InstrumentationRegistry; - import com.nextcloud.common.NextcloudClient; import com.owncloud.android.lib.common.OwnCloudBasicCredentials; import com.owncloud.android.lib.common.OwnCloudClient; @@ -54,8 +47,14 @@ import java.security.cert.CertificateException; import java.security.cert.X509Certificate; +import androidx.test.platform.app.InstrumentationRegistry; import okhttp3.Credentials; +import static junit.framework.TestCase.assertTrue; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; +import static org.junit.Assume.assumeTrue; + /** * Common base for all integration tests */ @@ -70,6 +69,7 @@ public abstract class AbstractIT { public static OwnCloudClient client; public static OwnCloudClient client2; protected static NextcloudClient nextcloudClient; + protected static NextcloudClient nextcloudClient2; protected static Context context; protected static Uri url; @@ -109,6 +109,10 @@ public static void beforeAll() throws InterruptedException, String credentials = Credentials.basic(loginName, password); nextcloudClient = new NextcloudClient(url, userId, credentials, context); + String userId2 = loginName; // for test same as userId + String credentials2 = Credentials.basic(loginName2, password2); + nextcloudClient2 = new NextcloudClient(url, userId2, credentials2, context); + waitForServer(client, url); testConnection(); } diff --git a/library/src/androidTest/java/com/owncloud/android/FileIT.java b/library/src/androidTest/java/com/owncloud/android/FileIT.java index 2cadacc7c..65bae644a 100644 --- a/library/src/androidTest/java/com/owncloud/android/FileIT.java +++ b/library/src/androidTest/java/com/owncloud/android/FileIT.java @@ -7,10 +7,6 @@ */ package com.owncloud.android; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - import android.net.Uri; import com.owncloud.android.lib.common.operations.RemoteOperationResult; @@ -31,6 +27,10 @@ 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 */ @@ -126,7 +126,7 @@ public void testShareViaLinkSharees() { false, "", OCShare.NO_PERMISSION) - .execute(client).isSuccess()); + .execute(nextcloudClient).isSuccess()); // verify RemoteOperationResult result = new ReadFolderRemoteOperation("/").execute(client); @@ -167,7 +167,7 @@ public void testShareToGroupSharees() { false, "", OCShare.NO_PERMISSION) - .execute(client).isSuccess()); + .execute(nextcloudClient).isSuccess()); // verify RemoteOperationResult result = new ReadFolderRemoteOperation("/").execute(client); @@ -202,7 +202,7 @@ public void testOneSharees() { false, "", OCShare.NO_PERMISSION) - .execute(client).isSuccess()); + .execute(nextcloudClient).isSuccess()); // verify RemoteOperationResult result = new ReadFolderRemoteOperation("/").execute(client); @@ -239,7 +239,7 @@ public void testTwoShareesOnParent() { false, "", OCShare.NO_PERMISSION) - .execute(client).isSuccess()); + .execute(nextcloudClient).isSuccess()); assertTrue(new CreateShareRemoteOperation(path, ShareType.USER, @@ -247,7 +247,7 @@ public void testTwoShareesOnParent() { false, "", OCShare.NO_PERMISSION) - .execute(client).isSuccess()); + .execute(nextcloudClient).isSuccess()); // verify RemoteOperationResult result = new ReadFolderRemoteOperation("/").execute(client); @@ -287,7 +287,7 @@ public void testTwoSharees() { false, "", OCShare.NO_PERMISSION) - .execute(client).isSuccess()); + .execute(nextcloudClient).isSuccess()); assertTrue(new CreateShareRemoteOperation(path, ShareType.USER, @@ -295,7 +295,7 @@ public void testTwoSharees() { false, "", OCShare.NO_PERMISSION) - .execute(client).isSuccess()); + .execute(nextcloudClient).isSuccess()); // verify RemoteOperationResult result = new ReadFolderRemoteOperation(path).execute(client); diff --git a/library/src/androidTest/java/com/owncloud/android/lib/common/operations/CreateShareIT.java b/library/src/androidTest/java/com/owncloud/android/lib/common/operations/CreateShareIT.java index d4a327a64..ac71c2b0f 100644 --- a/library/src/androidTest/java/com/owncloud/android/lib/common/operations/CreateShareIT.java +++ b/library/src/androidTest/java/com/owncloud/android/lib/common/operations/CreateShareIT.java @@ -7,10 +7,6 @@ */ package com.owncloud.android.lib.common.operations; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - import com.owncloud.android.AbstractIT; import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode; import com.owncloud.android.lib.resources.files.UploadFileRemoteOperation; @@ -22,6 +18,10 @@ import java.io.File; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + /** * Test create share */ @@ -56,7 +56,7 @@ public void testCreatePublicShareSuccessful() { "", false, "", - 1).execute(client); + 1).execute(nextcloudClient); assertTrue(result.isSuccess()); } @@ -67,7 +67,7 @@ public void testCreatePublicShareFailure() { "", false, "", - 1).execute(client); + 1).execute(nextcloudClient); assertFalse(result.isSuccess()); assertEquals(ResultCode.FILE_NOT_FOUND, result.getCode()); @@ -83,7 +83,7 @@ public void testCreatePrivateShareWithUserSuccessful() { "admin", false, "", - 31).execute(client); + 31).execute(nextcloudClient); assertTrue(result.isSuccess()); } @@ -97,7 +97,7 @@ public void testCreatePrivateShareWithUserNotExists() { "no_exist", false, "", - 31).execute(client); + 31).execute(nextcloudClient); assertFalse(result.isSuccess()); // TODO 404 is File not found, but actually it is "user not found" @@ -114,7 +114,7 @@ public void testCreatePrivateShareWithFileNotExists() { "admin", false, "", - 31).execute(client); + 31).execute(nextcloudClient); assertFalse(result.isSuccess()); assertEquals(ResultCode.FILE_NOT_FOUND, result.getCode()); } @@ -129,7 +129,7 @@ public void testCreatePrivateShareWithGroupSuccessful() { "admin", false, "", - 1).execute(client); + 1).execute(nextcloudClient); assertTrue(result.isSuccess()); } @@ -143,7 +143,7 @@ public void testCreatePrivateShareWithNonExistingGroupSharee() { "no_exist", false, "", - 31).execute(client); + 31).execute(nextcloudClient); assertFalse(result.isSuccess()); // TODO 404 is File not found, but actually it is "user not found" @@ -160,7 +160,7 @@ public void testCreatePrivateShareWithNonExistingFile() { "admin", false, "", - 31).execute(client); + 31).execute(nextcloudClient); assertFalse(result.isSuccess()); assertEquals(ResultCode.FILE_NOT_FOUND, result.getCode()); } @@ -189,7 +189,7 @@ public void testCreateFederatedShareWithNonExistingSharee() { "no_exist@" + serverUri2, false, "", - 31).execute(client); + 31).execute(nextcloudClient); assertFalse("sharee doesn't exist in an existing remote server", result.isSuccess()); assertEquals("sharee doesn't exist in an existing remote server, forbidden", @@ -206,7 +206,7 @@ public void testCreateFederatedShareWithNonExistingRemoteServer() { "no_exist", false, "", - 31).execute(client); + 31).execute(nextcloudClient); assertFalse(result.isSuccess()); // TODO expected: but was: assertEquals("remote server doesn't exist", ResultCode.SHARE_FORBIDDEN, result.getCode()); @@ -222,7 +222,7 @@ public void testCreateFederatedShareWithNonExistingFile() { "admin@" + serverUri2, false, "", - 31).execute(client); + 31).execute(nextcloudClient); assertFalse("file doesn't exist", result.isSuccess()); assertEquals("file doesn't exist", ResultCode.FILE_NOT_FOUND, result.getCode()); diff --git a/library/src/androidTest/java/com/owncloud/android/lib/common/operations/GetSharesIT.java b/library/src/androidTest/java/com/owncloud/android/lib/common/operations/GetSharesIT.java index 5a8061708..e8c971e31 100644 --- a/library/src/androidTest/java/com/owncloud/android/lib/common/operations/GetSharesIT.java +++ b/library/src/androidTest/java/com/owncloud/android/lib/common/operations/GetSharesIT.java @@ -8,9 +8,6 @@ */ package com.owncloud.android.lib.common.operations; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; - import com.owncloud.android.AbstractIT; import com.owncloud.android.lib.resources.files.CreateFolderRemoteOperation; import com.owncloud.android.lib.resources.shares.CreateShareRemoteOperation; @@ -22,6 +19,9 @@ import java.util.List; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + /** * Class to test Get Shares Operation * @@ -36,7 +36,7 @@ public void testGetShares() { "", false, "", - 1).execute(client).isSuccess()); + 1).execute(nextcloudClient).isSuccess()); assertTrue(new CreateFolderRemoteOperation("/2/", true).execute(client).isSuccess()); assertTrue(new CreateShareRemoteOperation("/2/", @@ -44,9 +44,9 @@ public void testGetShares() { "", false, "", - 1).execute(client).isSuccess()); + 1).execute(nextcloudClient).isSuccess()); - RemoteOperationResult> result = new GetSharesRemoteOperation().execute(client); + RemoteOperationResult> result = new GetSharesRemoteOperation().execute(nextcloudClient); assertTrue(result.isSuccess()); assertEquals(2, result.getResultData().size()); assertEquals("/1/", result.getResultData().get(0).getPath()); diff --git a/library/src/androidTest/java/com/owncloud/android/lib/common/operations/RemoveShareIT.java b/library/src/androidTest/java/com/owncloud/android/lib/common/operations/RemoveShareIT.java index 70171e560..e168e4527 100644 --- a/library/src/androidTest/java/com/owncloud/android/lib/common/operations/RemoveShareIT.java +++ b/library/src/androidTest/java/com/owncloud/android/lib/common/operations/RemoveShareIT.java @@ -7,8 +7,6 @@ */ package com.owncloud.android.lib.common.operations; -import static org.junit.Assert.assertTrue; - import com.owncloud.android.AbstractIT; import com.owncloud.android.lib.resources.files.UploadFileRemoteOperation; import com.owncloud.android.lib.resources.shares.CreateShareRemoteOperation; @@ -22,6 +20,8 @@ import java.io.IOException; import java.util.List; +import static org.junit.Assert.assertTrue; + public class RemoveShareIT extends AbstractIT { private static final String FILE_TO_UNSHARE = "/fileToUnshare.txt"; @@ -42,7 +42,7 @@ public void testRemoveShare() throws IOException { ShareType.PUBLIC_LINK, "", false, - "", 1).execute(client); + "", 1).execute(nextcloudClient); assertTrue(result.isSuccess()); diff --git a/library/src/androidTest/java/com/owncloud/android/lib/resources/files/SearchRemoteOperationIT.java b/library/src/androidTest/java/com/owncloud/android/lib/resources/files/SearchRemoteOperationIT.java index f16c50e8d..cc19ea9f5 100644 --- a/library/src/androidTest/java/com/owncloud/android/lib/resources/files/SearchRemoteOperationIT.java +++ b/library/src/androidTest/java/com/owncloud/android/lib/resources/files/SearchRemoteOperationIT.java @@ -7,14 +7,9 @@ */ package com.owncloud.android.lib.resources.files; -import static junit.framework.TestCase.assertEquals; -import static junit.framework.TestCase.assertTrue; - import android.net.Uri; import android.os.Bundle; -import androidx.test.platform.app.InstrumentationRegistry; - import com.owncloud.android.AbstractIT; import com.owncloud.android.lib.common.OwnCloudBasicCredentials; import com.owncloud.android.lib.common.OwnCloudClient; @@ -32,6 +27,11 @@ import java.io.IOException; import java.util.List; +import androidx.test.platform.app.InstrumentationRegistry; + +import static junit.framework.TestCase.assertEquals; +import static junit.framework.TestCase.assertTrue; + public class SearchRemoteOperationIT extends AbstractIT { private static OCCapability capability; @@ -177,7 +177,7 @@ public void favoriteFiles() throws IOException { client.getUserId(), false, "", - 31).execute(client2) + 31).execute(nextcloudClient2) .isSuccess() ); diff --git a/library/src/androidTest/java/com/owncloud/android/lib/resources/shares/CreateShareRemoteOperationIT.kt b/library/src/androidTest/java/com/owncloud/android/lib/resources/shares/CreateShareRemoteOperationIT.kt index 06bfbe701..32d191992 100644 --- a/library/src/androidTest/java/com/owncloud/android/lib/resources/shares/CreateShareRemoteOperationIT.kt +++ b/library/src/androidTest/java/com/owncloud/android/lib/resources/shares/CreateShareRemoteOperationIT.kt @@ -50,7 +50,7 @@ class CreateShareRemoteOperationIT : AbstractIT() { OCShare.MAXIMUM_PERMISSIONS_FOR_FOLDER, true, note - ).execute(client) + ).execute(nextcloudClient) junit.framework.Assert.assertTrue(sut.isSuccess) diff --git a/library/src/androidTest/java/com/owncloud/android/lib/resources/shares/GetSharesRemoteOperationIT.java b/library/src/androidTest/java/com/owncloud/android/lib/resources/shares/GetSharesRemoteOperationIT.java index 58ed2ac25..9d95b6c81 100644 --- a/library/src/androidTest/java/com/owncloud/android/lib/resources/shares/GetSharesRemoteOperationIT.java +++ b/library/src/androidTest/java/com/owncloud/android/lib/resources/shares/GetSharesRemoteOperationIT.java @@ -7,16 +7,10 @@ */ package com.owncloud.android.lib.resources.shares; -import static junit.framework.TestCase.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; -import static org.junit.Assume.assumeTrue; - import android.net.Uri; import android.os.Bundle; -import androidx.test.platform.app.InstrumentationRegistry; - +import com.nextcloud.common.NextcloudClient; import com.owncloud.android.AbstractIT; import com.owncloud.android.lib.common.OwnCloudBasicCredentials; import com.owncloud.android.lib.common.OwnCloudClient; @@ -34,6 +28,14 @@ import java.util.List; +import androidx.test.platform.app.InstrumentationRegistry; +import okhttp3.Credentials; + +import static junit.framework.TestCase.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.junit.Assume.assumeTrue; + public class GetSharesRemoteOperationIT extends AbstractIT { @Test public void searchSharedFiles() { @@ -52,31 +54,16 @@ public void searchSharedFiles() { assertEquals(0, result.getResultData().size()); // share folder to user "admin" - assertTrue(new CreateShareRemoteOperation("/shareToAdmin/", - ShareType.USER, - "admin", - false, - "", - OCShare.MAXIMUM_PERMISSIONS_FOR_FOLDER) - .execute(client).isSuccess()); + assertTrue(new CreateShareRemoteOperation("/shareToAdmin/", ShareType.USER, "admin", false, "", + OCShare.MAXIMUM_PERMISSIONS_FOR_FOLDER).execute(nextcloudClient).isSuccess()); // share folder via public link - assertTrue(new CreateShareRemoteOperation("/shareViaLink/", - ShareType.PUBLIC_LINK, - "", - true, - "", - OCShare.READ_PERMISSION_FLAG) - .execute(client).isSuccess()); + assertTrue(new CreateShareRemoteOperation("/shareViaLink/", ShareType.PUBLIC_LINK, "", true, "", + OCShare.READ_PERMISSION_FLAG).execute(nextcloudClient).isSuccess()); // share folder to group - assertTrue(new CreateShareRemoteOperation("/shareToGroup/", - ShareType.GROUP, - "users", - false, - "", - OCShare.NO_PERMISSION) - .execute(client).isSuccess()); + assertTrue(new CreateShareRemoteOperation("/shareToGroup/", ShareType.GROUP, "users", false, "", + OCShare.NO_PERMISSION).execute(nextcloudClient).isSuccess()); // share folder to circle // get share @@ -106,7 +93,7 @@ public void searchSharedFiles() { sut = new GetSharesRemoteOperation(); - result = sut.execute(client); + result = sut.execute(nextcloudClient); assertTrue(result.isSuccess()); assertEquals(3, result.getResultData().size()); @@ -157,30 +144,25 @@ public void sharedWithMe() { GetSharesRemoteOperation sut = new GetSharesRemoteOperation(); GetSharesRemoteOperation sutSharedWithMe = new GetSharesRemoteOperation(true); - RemoteOperationResult> result = sut.execute(client); + RemoteOperationResult> result = sut.execute(nextcloudClient); assertTrue(result.isSuccess()); assertEquals(0, result.getResultData().size()); - RemoteOperationResult> resultSharedWithMe = sutSharedWithMe.execute(client); + RemoteOperationResult> resultSharedWithMe = sutSharedWithMe.execute(nextcloudClient); assertTrue(resultSharedWithMe.isSuccess()); assertEquals(0, resultSharedWithMe.getResultData().size()); // share folder to user "admin" assertTrue(new CreateFolderRemoteOperation("/shareToAdmin/", true).execute(client).isSuccess()); - assertTrue(new CreateShareRemoteOperation("/shareToAdmin/", - ShareType.USER, - "admin", - false, - "", - OCShare.MAXIMUM_PERMISSIONS_FOR_FOLDER) - .execute(client).isSuccess()); + assertTrue(new CreateShareRemoteOperation("/shareToAdmin/", ShareType.USER, "admin", false, "", + OCShare.MAXIMUM_PERMISSIONS_FOR_FOLDER).execute(nextcloudClient).isSuccess()); // Expect one file shared by me, no file shared with me - result = sut.execute(client); + result = sut.execute(nextcloudClient); assertEquals(1, result.getResultData().size()); - resultSharedWithMe = sutSharedWithMe.execute(client); + resultSharedWithMe = sutSharedWithMe.execute(nextcloudClient); assertEquals(0, resultSharedWithMe.getResultData().size()); // create client for user "user1" @@ -188,26 +170,26 @@ public void sharedWithMe() { url = Uri.parse(arguments.getString("TEST_SERVER_URL")); String loginName = "user1"; String password = "user1"; + String credentials = Credentials.basic(loginName, password); OwnCloudClient clientUser1 = OwnCloudClientFactory.createOwnCloudClient(url, context, true); clientUser1.setCredentials(new OwnCloudBasicCredentials(loginName, password)); clientUser1.setUserId(loginName); // for test same as userId + NextcloudClient nextcloudClientUser1 = OwnCloudClientFactory.createNextcloudClient(url, loginName, + credentials, context, true); + // share folder to previous user assertTrue(new CreateFolderRemoteOperation("/shareToUser/", true).execute(clientUser1).isSuccess()); - assertTrue(new CreateShareRemoteOperation("/shareToUser/", - ShareType.USER, - client.getCredentials().getUsername(), - false, - "", - OCShare.MAXIMUM_PERMISSIONS_FOR_FOLDER) - .execute(clientUser1).isSuccess()); + assertTrue(new CreateShareRemoteOperation("/shareToUser/", ShareType.USER, + client.getCredentials().getUsername(), false, "", + OCShare.MAXIMUM_PERMISSIONS_FOR_FOLDER).execute(nextcloudClientUser1).isSuccess()); // Expect one file shared by me, one file shared with me - result = sut.execute(client); + result = sut.execute(nextcloudClient); assertEquals(1, result.getResultData().size()); - resultSharedWithMe = sutSharedWithMe.execute(client); + resultSharedWithMe = sutSharedWithMe.execute(nextcloudClient); assertEquals(1, resultSharedWithMe.getResultData().size()); } @@ -225,7 +207,7 @@ public void favorites() { "", OCShare.MAXIMUM_PERMISSIONS_FOR_FOLDER, true) - .execute(client); + .execute(nextcloudClient); assertTrue(createResult.isSuccess()); @@ -243,7 +225,7 @@ public void favorites() { "", OCShare.MAXIMUM_PERMISSIONS_FOR_FOLDER, true) - .execute(client); + .execute(nextcloudClient); assertTrue(createResult.isSuccess()); @@ -269,7 +251,7 @@ public void noFavorite() { "", OCShare.MAXIMUM_PERMISSIONS_FOR_FOLDER, true) - .execute(client); + .execute(nextcloudClient); assertTrue(createResult.isSuccess()); @@ -299,7 +281,7 @@ public void favorite() { "", OCShare.MAXIMUM_PERMISSIONS_FOR_FOLDER, true) - .execute(client); + .execute(nextcloudClient); assertTrue(createResult.isSuccess()); diff --git a/library/src/androidTest/java/com/owncloud/android/lib/resources/shares/UpdateShareRemoteOperationIT.kt b/library/src/androidTest/java/com/owncloud/android/lib/resources/shares/UpdateShareRemoteOperationIT.kt index 368d9dc59..8c00fac09 100644 --- a/library/src/androidTest/java/com/owncloud/android/lib/resources/shares/UpdateShareRemoteOperationIT.kt +++ b/library/src/androidTest/java/com/owncloud/android/lib/resources/shares/UpdateShareRemoteOperationIT.kt @@ -58,7 +58,7 @@ class UpdateShareRemoteOperationIT : AbstractIT() { OCShare.MAXIMUM_PERMISSIONS_FOR_FOLDER, true, "" - ).execute(client) + ).execute(nextcloudClient) assertTrue(createOperationResult.isSuccess) @@ -67,10 +67,10 @@ class UpdateShareRemoteOperationIT : AbstractIT() { val sut = UpdateShareRemoteOperation(share.remoteId) sut.setNote(note) - assertTrue(sut.execute(client).isSuccess) + assertTrue(sut.execute(nextcloudClient).isSuccess) // verify - val getShareOperationResult = GetShareRemoteOperation(share.remoteId).execute(client) + val getShareOperationResult = GetShareRemoteOperation(share.remoteId).execute(nextcloudClient) assertTrue(getShareOperationResult.isSuccess) val updatedShare = getShareOperationResult.resultData[0] @@ -94,7 +94,7 @@ class UpdateShareRemoteOperationIT : AbstractIT() { true, "", OCShare.READ_PERMISSION_FLAG - ).execute(client) + ).execute(nextcloudClient) assertTrue(createOperationResult.isSuccess) @@ -103,10 +103,10 @@ class UpdateShareRemoteOperationIT : AbstractIT() { val sut = UpdateShareRemoteOperation(share.remoteId) sut.setLabel(label) - assertTrue(sut.execute(client).isSuccess) + assertTrue(sut.execute(nextcloudClient).isSuccess) // verify - val getShareOperationResult = GetShareRemoteOperation(share.remoteId).execute(client) + val getShareOperationResult = GetShareRemoteOperation(share.remoteId).execute(nextcloudClient) assertTrue(getShareOperationResult.isSuccess) val updatedShare = getShareOperationResult.resultData[0] @@ -131,7 +131,7 @@ class UpdateShareRemoteOperationIT : AbstractIT() { true, "", OCShare.READ_PERMISSION_FLAG - ).execute(client) + ).execute(nextcloudClient) assertTrue(createOperationResult.isSuccess) @@ -140,7 +140,7 @@ class UpdateShareRemoteOperationIT : AbstractIT() { val sut = UpdateShareRemoteOperation(share.remoteId) sut.setPassword("1") - val result = sut.execute(client) + val result = sut.execute(nextcloudClient) assertFalse(result.isSuccess) val capabilityResult = GetCapabilitiesRemoteOperation().execute(nextcloudClient) @@ -181,7 +181,7 @@ class UpdateShareRemoteOperationIT : AbstractIT() { true, "", OCShare.READ_PERMISSION_FLAG - ).execute(client) + ).execute(nextcloudClient) assertTrue(createOperationResult.isSuccess) @@ -190,7 +190,7 @@ class UpdateShareRemoteOperationIT : AbstractIT() { val sut = UpdateShareRemoteOperation(share.remoteId) sut.setPassword("arnservcvcbtp234") - assertTrue(sut.execute(client).isSuccess) + assertTrue(sut.execute(nextcloudClient).isSuccess) assertTrue(RemoveFileRemoteOperation(folder).execute(client).isSuccess) } } diff --git a/library/src/main/java/com/owncloud/android/lib/common/OwnCloudAnonymousCredentials.java b/library/src/main/java/com/owncloud/android/lib/common/OwnCloudAnonymousCredentials.java index 2012e697e..8d04d3955 100644 --- a/library/src/main/java/com/owncloud/android/lib/common/OwnCloudAnonymousCredentials.java +++ b/library/src/main/java/com/owncloud/android/lib/common/OwnCloudAnonymousCredentials.java @@ -35,7 +35,7 @@ public boolean authTokenExpires() { @Override public String toOkHttpCredentials() { - return OkHttpCredentialsUtil.basic(getUsername(), getAuthToken()); + return OkHttpCredentialsUtil.basic("", ""); } @Override diff --git a/library/src/main/java/com/owncloud/android/lib/resources/files/ExistenceCheckRemoteOperation.java b/library/src/main/java/com/owncloud/android/lib/resources/files/ExistenceCheckRemoteOperation.java index 32270bc7a..4a84591c1 100644 --- a/library/src/main/java/com/owncloud/android/lib/resources/files/ExistenceCheckRemoteOperation.java +++ b/library/src/main/java/com/owncloud/android/lib/resources/files/ExistenceCheckRemoteOperation.java @@ -8,30 +8,25 @@ import android.content.Context; -import com.owncloud.android.lib.common.OwnCloudAnonymousCredentials; -import com.owncloud.android.lib.common.OwnCloudClient; +import com.nextcloud.common.NextcloudClient; +import com.nextcloud.operations.HeadMethod; import com.owncloud.android.lib.common.network.RedirectionPath; 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; import org.apache.commons.httpclient.HttpStatus; -import org.apache.commons.httpclient.methods.HeadMethod; /** * Operation to check the existence or absence of a path in a remote server. * * @author David A. Velasco */ -public class ExistenceCheckRemoteOperation extends RemoteOperation { - - /** Maximum time to wait for a response from the server in MILLISECONDs. */ - public static final int TIMEOUT = 50000; - +public class ExistenceCheckRemoteOperation extends RemoteOperation { private static final String TAG = ExistenceCheckRemoteOperation.class.getSimpleName(); - private String mPath; - private boolean mSuccessIfAbsent; + private final String mPath; + private final boolean mSuccessIfAbsent; /** Sequence of redirections followed. Available only after executing the operation */ private RedirectionPath mRedirectionPath = null; @@ -63,26 +58,21 @@ public ExistenceCheckRemoteOperation(String remotePath, Context context, boolean } @Override - protected RemoteOperationResult run(OwnCloudClient client) { - RemoteOperationResult result = null; - HeadMethod head = null; - boolean previousFollowRedirects = client.isFollowRedirects(); + public RemoteOperationResult run(NextcloudClient client) { + RemoteOperationResult result; + com.nextcloud.operations.HeadMethod head = null; + boolean previousFollowRedirects = client.getFollowRedirects(); try { - if (client.getCredentials() instanceof OwnCloudAnonymousCredentials) { - head = new HeadMethod(client.getDavUri().toString()); - } else { - head = new HeadMethod(client.getFilesDavUri(mPath)); - } + head = new HeadMethod(client.getFilesDavUri(mPath), false); client.setFollowRedirects(false); - int status = client.executeMethod(head, TIMEOUT, TIMEOUT); + int status = client.execute(head); if (previousFollowRedirects) { mRedirectionPath = client.followRedirection(head); status = mRedirectionPath.getLastStatus(); } - client.exhaustResponse(head.getResponseBodyAsStream()); boolean success = (status == HttpStatus.SC_OK && !mSuccessIfAbsent) || (status == HttpStatus.SC_NOT_FOUND && mSuccessIfAbsent); - result = new RemoteOperationResult( + result = new RemoteOperationResult<>( success, status, head.getStatusText(), @@ -93,7 +83,7 @@ protected RemoteOperationResult run(OwnCloudClient client) { "finished with HTTP status " + status + (!success ? "(FAIL)" : "")); } catch (Exception e) { - result = new RemoteOperationResult(e); + result = new RemoteOperationResult<>(e); Log_OC.e(TAG, "Existence check for " + client.getFilesDavUri(mPath) + " targeting for " + (mSuccessIfAbsent ? " absence " : " existence ") + ": " + result.getLogMessage(), result.getException()); diff --git a/library/src/main/java/com/owncloud/android/lib/resources/shares/CreateShareRemoteOperation.java b/library/src/main/java/com/owncloud/android/lib/resources/shares/CreateShareRemoteOperation.java index 0ef9a46e9..128f9db6f 100644 --- a/library/src/main/java/com/owncloud/android/lib/resources/shares/CreateShareRemoteOperation.java +++ b/library/src/main/java/com/owncloud/android/lib/resources/shares/CreateShareRemoteOperation.java @@ -12,13 +12,14 @@ import android.text.TextUtils; -import com.owncloud.android.lib.common.OwnCloudClient; +import com.nextcloud.common.JSONRequestBody; +import com.nextcloud.common.NextcloudClient; +import com.nextcloud.operations.PostMethod; 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; import org.apache.commons.httpclient.HttpStatus; -import org.apache.commons.httpclient.methods.Utf8PostMethod; import java.io.IOException; import java.util.List; @@ -45,7 +46,7 @@ public class CreateShareRemoteOperation extends RemoteOperation> { private final String password; private final int permissions; private boolean getShareDetails; - private String note; + private final String note; /** * Constructor @@ -129,45 +130,44 @@ public void setGetShareDetails(boolean set) { } @Override - protected RemoteOperationResult> run(OwnCloudClient client) { + public RemoteOperationResult> run(NextcloudClient client) { RemoteOperationResult> result; int status; - Utf8PostMethod post = null; + PostMethod post = null; try { - // Post Method - post = new Utf8PostMethod(client.getBaseUri() + ShareUtils.SHARING_API_PATH); + // request body + JSONRequestBody jsonRequestBody = new JSONRequestBody(); - post.setRequestHeader(CONTENT_TYPE, FORM_URLENCODED); - - post.addParameter(PARAM_PATH, remoteFilePath); - post.addParameter(PARAM_SHARE_TYPE, Integer.toString(shareType.getValue())); - post.addParameter(PARAM_SHARE_WITH, shareWith); + jsonRequestBody.put(PARAM_PATH, remoteFilePath); + jsonRequestBody.put(PARAM_SHARE_TYPE, Integer.toString(shareType.getValue())); + jsonRequestBody.put(PARAM_SHARE_WITH, shareWith); if (publicUpload) { - post.addParameter(PARAM_PUBLIC_UPLOAD, Boolean.toString(true)); + jsonRequestBody.put(PARAM_PUBLIC_UPLOAD, Boolean.toString(true)); } if (password != null && password.length() > 0) { - post.addParameter(PARAM_PASSWORD, password); + jsonRequestBody.put(PARAM_PASSWORD, password); } if (OCShare.NO_PERMISSION != permissions) { - post.addParameter(PARAM_PERMISSIONS, Integer.toString(permissions)); + jsonRequestBody.put(PARAM_PERMISSIONS, Integer.toString(permissions)); } if (!TextUtils.isEmpty(note)) { - post.addParameter(PARAM_NOTE, note); + jsonRequestBody.put(PARAM_NOTE, note); } - post.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE); + // post method + post = new PostMethod(client.getBaseUri() + ShareUtils.SHARING_API_PATH, true, jsonRequestBody.get()); + post.addRequestHeader(CONTENT_TYPE, FORM_URLENCODED); - status = client.executeMethod(post); + status = client.execute(post); if (isSuccess(status)) { String response = post.getResponseBodyAsString(); - ShareToRemoteOperationResultParser parser = new ShareToRemoteOperationResultParser( - new ShareXMLParser() - ); + ShareToRemoteOperationResultParser parser = + new ShareToRemoteOperationResultParser(new ShareXMLParser()); parser.setOneOrMoreSharesRequired(true); parser.setServerBaseUri(client.getBaseUri()); result = parser.parse(response); diff --git a/library/src/main/java/com/owncloud/android/lib/resources/shares/GetShareRemoteOperation.java b/library/src/main/java/com/owncloud/android/lib/resources/shares/GetShareRemoteOperation.java index 3ed737b86..76822138c 100644 --- a/library/src/main/java/com/owncloud/android/lib/resources/shares/GetShareRemoteOperation.java +++ b/library/src/main/java/com/owncloud/android/lib/resources/shares/GetShareRemoteOperation.java @@ -9,19 +9,19 @@ */ package com.owncloud.android.lib.resources.shares; -import static com.owncloud.android.lib.resources.shares.ShareUtils.INCLUDE_TAGS; -import static com.owncloud.android.lib.resources.shares.ShareUtils.SHARING_API_PATH; - -import com.owncloud.android.lib.common.OwnCloudClient; +import com.nextcloud.common.NextcloudClient; +import com.nextcloud.operations.GetMethod; 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; import org.apache.commons.httpclient.HttpStatus; -import org.apache.commons.httpclient.methods.GetMethod; import java.util.List; +import static com.owncloud.android.lib.resources.shares.ShareUtils.INCLUDE_TAGS; +import static com.owncloud.android.lib.resources.shares.ShareUtils.SHARING_API_PATH; + /** * Get the data about a Share resource, known its remote ID. */ @@ -38,20 +38,18 @@ public GetShareRemoteOperation(long remoteId) { @Override - protected RemoteOperationResult> run(OwnCloudClient client) { + public RemoteOperationResult> run(NextcloudClient client) { RemoteOperationResult> result; int status; - // Get Method - GetMethod get = null; + // get method + com.nextcloud.operations.GetMethod get = null; - // Get the response try { - get = new GetMethod(client.getBaseUri() + SHARING_API_PATH + "/" + remoteId); + get = new GetMethod(client.getBaseUri() + SHARING_API_PATH + "/" + remoteId, true); get.setQueryString(INCLUDE_TAGS); - get.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE); - status = client.executeMethod(get); + status = client.execute(get); if (isSuccess(status)) { String response = get.getResponseBodyAsString(); diff --git a/library/src/main/java/com/owncloud/android/lib/resources/shares/GetShareesRemoteOperation.java b/library/src/main/java/com/owncloud/android/lib/resources/shares/GetShareesRemoteOperation.java index f7f152392..2d7fc1d01 100644 --- a/library/src/main/java/com/owncloud/android/lib/resources/shares/GetShareesRemoteOperation.java +++ b/library/src/main/java/com/owncloud/android/lib/resources/shares/GetShareesRemoteOperation.java @@ -12,13 +12,13 @@ import android.net.Uri; -import com.owncloud.android.lib.common.OwnCloudClient; +import com.nextcloud.common.NextcloudClient; +import com.nextcloud.operations.GetMethod; 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; import org.apache.commons.httpclient.HttpStatus; -import org.apache.commons.httpclient.methods.GetMethod; import org.json.JSONArray; import org.json.JSONObject; @@ -103,7 +103,7 @@ public GetShareesRemoteOperation(String searchString, int page, int perPage) { } @Override - protected RemoteOperationResult> run(OwnCloudClient client) { + public RemoteOperationResult> run(NextcloudClient client) { RemoteOperationResult> result; int status; GetMethod get = null; @@ -120,10 +120,9 @@ protected RemoteOperationResult> run(OwnCloudClient client uriBuilder.appendQueryParameter(PARAM_LOOKUP, VALUE_FALSE); // Get Method - get = new GetMethod(uriBuilder.build().toString()); - get.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE); + get = new GetMethod(uriBuilder.build().toString(), true); - status = client.executeMethod(get); + status = client.execute(get); if (isSuccess(status)) { String response = get.getResponseBodyAsString(); @@ -205,12 +204,7 @@ protected RemoteOperationResult> run(OwnCloudClient client result = new RemoteOperationResult<>(false, get); String response = get.getResponseBodyAsString(); Log_OC.e(TAG, "Failed response while getting users/groups from the server"); - - if (response != null) { - Log_OC.e(TAG, "*** status code: " + status + "; response message: " + response); - } else { - Log_OC.e(TAG, "*** status code: " + status); - } + Log_OC.e(TAG, "*** status code: " + status + "; response message: " + response); } } catch (Exception e) { diff --git a/library/src/main/java/com/owncloud/android/lib/resources/shares/GetSharesForFileRemoteOperation.java b/library/src/main/java/com/owncloud/android/lib/resources/shares/GetSharesForFileRemoteOperation.java index b579e65ba..6faa01937 100644 --- a/library/src/main/java/com/owncloud/android/lib/resources/shares/GetSharesForFileRemoteOperation.java +++ b/library/src/main/java/com/owncloud/android/lib/resources/shares/GetSharesForFileRemoteOperation.java @@ -10,21 +10,23 @@ */ package com.owncloud.android.lib.resources.shares; -import com.owncloud.android.lib.common.OwnCloudClient; +import com.nextcloud.common.NextcloudClient; +import com.nextcloud.operations.GetMethod; 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; import org.apache.commons.httpclient.HttpStatus; -import org.apache.commons.httpclient.NameValuePair; -import org.apache.commons.httpclient.methods.GetMethod; + +import java.util.List; +import java.util.Map; /** * Provide a list shares for a specific file. * The input is the full path of the desired file. * The output is a list of everyone who has the file shared with them. */ -public class GetSharesForFileRemoteOperation extends RemoteOperation { +public class GetSharesForFileRemoteOperation extends RemoteOperation> { private static final String TAG = GetSharesForFileRemoteOperation.class.getSimpleName(); @@ -32,9 +34,9 @@ public class GetSharesForFileRemoteOperation extends RemoteOperation { private static final String PARAM_RESHARES = "reshares"; private static final String PARAM_SUBFILES = "subfiles"; - private String mRemoteFilePath; - private boolean mReshares; - private boolean mSubfiles; + private final String mRemoteFilePath; + private final boolean mReshares; + private final boolean mSubfiles; /** * Constructor @@ -46,35 +48,31 @@ public class GetSharesForFileRemoteOperation extends RemoteOperation { * @param subfiles If set to false (default), lists only the folder being shared * If set to true, all shared files within the folder are returned. */ - public GetSharesForFileRemoteOperation(String remoteFilePath, boolean reshares, - boolean subfiles) { + public GetSharesForFileRemoteOperation(String remoteFilePath, boolean reshares, boolean subfiles) { mRemoteFilePath = remoteFilePath; mReshares = reshares; mSubfiles = subfiles; } @Override - protected RemoteOperationResult run(OwnCloudClient client) { - RemoteOperationResult result = null; - int status = -1; + public RemoteOperationResult> run(NextcloudClient client) { + RemoteOperationResult> result; + int status; GetMethod get = null; try { - // Get Method - get = new GetMethod(client.getBaseUri() + ShareUtils.SHARING_API_PATH); - - // Add Parameters to Get Method - get.setQueryString(new NameValuePair[]{ - new NameValuePair(PARAM_PATH, mRemoteFilePath), - new NameValuePair(PARAM_RESHARES, String.valueOf(mReshares)), - new NameValuePair(PARAM_SUBFILES, String.valueOf(mSubfiles)) //, - //new NameValuePair("shared_with_me", "true") - }); + // get method + get = new com.nextcloud.operations.GetMethod(client.getBaseUri() + ShareUtils.SHARING_API_PATH, true); - get.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE); + // add parameters to get method + get.setQueryString(Map.of( + PARAM_PATH, mRemoteFilePath, + PARAM_RESHARES, String.valueOf(mReshares), + PARAM_SUBFILES, String.valueOf(mSubfiles) + )); - status = client.executeMethod(get); + status = client.execute(get); if (isSuccess(status)) { String response = get.getResponseBodyAsString(); @@ -87,15 +85,15 @@ protected RemoteOperationResult run(OwnCloudClient client) { result = parser.parse(response); if (result.isSuccess()) { - Log_OC.d(TAG, "Got " + result.getData().size() + " shares"); + Log_OC.d(TAG, "Got " + result.getResultData().size() + " shares"); } } else { - result = new RemoteOperationResult(false, get); + result = new RemoteOperationResult<>(false, get); } } catch (Exception e) { - result = new RemoteOperationResult(e); + result = new RemoteOperationResult<>(e); Log_OC.e(TAG, "Exception while getting shares", e); } finally { diff --git a/library/src/main/java/com/owncloud/android/lib/resources/shares/GetSharesRemoteOperation.java b/library/src/main/java/com/owncloud/android/lib/resources/shares/GetSharesRemoteOperation.java index 5e5090a66..a9993827f 100644 --- a/library/src/main/java/com/owncloud/android/lib/resources/shares/GetSharesRemoteOperation.java +++ b/library/src/main/java/com/owncloud/android/lib/resources/shares/GetSharesRemoteOperation.java @@ -10,15 +10,16 @@ */ package com.owncloud.android.lib.resources.shares; -import com.owncloud.android.lib.common.OwnCloudClient; +import com.nextcloud.common.NextcloudClient; +import com.nextcloud.operations.GetMethod; 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; import org.apache.commons.httpclient.HttpStatus; -import org.apache.commons.httpclient.methods.GetMethod; import java.util.List; +import java.util.Map; import static com.owncloud.android.lib.resources.shares.ShareUtils.INCLUDE_TAGS; @@ -28,7 +29,7 @@ public class GetSharesRemoteOperation extends RemoteOperation> { private static final String TAG = GetSharesRemoteOperation.class.getSimpleName(); - private boolean sharedWithMe = false; + private final boolean sharedWithMe; public GetSharesRemoteOperation() { this(false); @@ -39,24 +40,23 @@ public GetSharesRemoteOperation(boolean sharedWithMe) { } @Override - protected RemoteOperationResult> run(OwnCloudClient client) { + public RemoteOperationResult> run(NextcloudClient client) { RemoteOperationResult> result; int status; // Get Method - GetMethod get = null; + com.nextcloud.operations.GetMethod get = null; // Get the response try { - get = new GetMethod(client.getBaseUri() + ShareUtils.SHARING_API_PATH); + get = new GetMethod(client.getBaseUri() + ShareUtils.SHARING_API_PATH, true); get.setQueryString(INCLUDE_TAGS); - get.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE); if (sharedWithMe) { - get.setQueryString("shared_with_me=true"); + get.setQueryString(Map.of("shared_with_me", "true")); } - status = client.executeMethod(get); + status = client.execute(get); if (isSuccess(status)) { String response = get.getResponseBodyAsString(); diff --git a/library/src/main/java/com/owncloud/android/lib/resources/shares/RemoveShareRemoteOperation.java b/library/src/main/java/com/owncloud/android/lib/resources/shares/RemoveShareRemoteOperation.java index b0c33bc35..e1866e644 100644 --- a/library/src/main/java/com/owncloud/android/lib/resources/shares/RemoveShareRemoteOperation.java +++ b/library/src/main/java/com/owncloud/android/lib/resources/shares/RemoveShareRemoteOperation.java @@ -10,23 +10,25 @@ */ package com.owncloud.android.lib.resources.shares; -import com.owncloud.android.lib.common.OwnCloudClient; +import com.nextcloud.common.NextcloudClient; +import com.nextcloud.operations.DeleteMethod; 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; import org.apache.commons.httpclient.HttpStatus; -import org.apache.jackrabbit.webdav.client.methods.DeleteMethod; + +import java.util.List; /** * Remove a share */ -public class RemoveShareRemoteOperation extends RemoteOperation { +public class RemoveShareRemoteOperation extends RemoteOperation> { private static final String TAG = RemoveShareRemoteOperation.class.getSimpleName(); - private long remoteShareId; + private final long remoteShareId; /** * Constructor @@ -40,16 +42,14 @@ public RemoveShareRemoteOperation(long remoteShareId) { } @Override - protected RemoteOperationResult run(OwnCloudClient client) { - RemoteOperationResult result; - DeleteMethod delete = null; + public RemoteOperationResult> run(NextcloudClient client) { + RemoteOperationResult> result; + com.nextcloud.operations.DeleteMethod delete = null; try { - delete = new DeleteMethod(client.getBaseUri() + ShareUtils.SHARING_API_PATH + "/" + remoteShareId); - - delete.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE); + delete = new DeleteMethod(client.getBaseUri() + ShareUtils.SHARING_API_PATH + "/" + remoteShareId, true); - int status = client.executeMethod(delete); + int status = client.execute(delete); if (isSuccess(status)) { String response = delete.getResponseBodyAsString(); @@ -63,10 +63,10 @@ protected RemoteOperationResult run(OwnCloudClient client) { Log_OC.d(TAG, "Unshare " + remoteShareId + ": " + result.getLogMessage()); } else { - result = new RemoteOperationResult(false, delete); + result = new RemoteOperationResult<>(false, delete); } } catch (Exception e) { - result = new RemoteOperationResult(e); + result = new RemoteOperationResult<>(e); Log_OC.e(TAG, "Unshare Link Exception " + result.getLogMessage(), e); } finally { diff --git a/library/src/main/java/com/owncloud/android/lib/resources/shares/ShareUtils.java b/library/src/main/java/com/owncloud/android/lib/resources/shares/ShareUtils.java index eb201184b..0cead8dba 100644 --- a/library/src/main/java/com/owncloud/android/lib/resources/shares/ShareUtils.java +++ b/library/src/main/java/com/owncloud/android/lib/resources/shares/ShareUtils.java @@ -9,6 +9,8 @@ */ package com.owncloud.android.lib.resources.shares; +import java.util.Map; + /** * Contains Constants for Share Operation * @@ -21,7 +23,7 @@ public class ShareUtils { // OCS Route public static final String SHARING_API_PATH = "/ocs/v2.php/apps/files_sharing/api/v1/shares"; - public static final String INCLUDE_TAGS = "include_tags=true"; + public static final Map INCLUDE_TAGS = Map.of("include_tags", "true"); // String to build the link with the token of a share: public static final String SHARING_LINK_PATH_AFTER_VERSION_8 = "/index.php/s/"; diff --git a/library/src/main/java/com/owncloud/android/lib/resources/shares/UpdateShareRemoteOperation.java b/library/src/main/java/com/owncloud/android/lib/resources/shares/UpdateShareRemoteOperation.java index 830a67724..402504bd4 100644 --- a/library/src/main/java/com/owncloud/android/lib/resources/shares/UpdateShareRemoteOperation.java +++ b/library/src/main/java/com/owncloud/android/lib/resources/shares/UpdateShareRemoteOperation.java @@ -13,14 +13,14 @@ import android.net.Uri; import android.util.Pair; -import com.owncloud.android.lib.common.OwnCloudClient; +import com.nextcloud.common.JSONRequestBody; +import com.nextcloud.common.NextcloudClient; +import com.nextcloud.operations.PutMethod; 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; import org.apache.commons.httpclient.HttpStatus; -import org.apache.commons.httpclient.methods.PutMethod; -import org.apache.commons.httpclient.methods.StringRequestEntity; import java.net.URLEncoder; import java.text.DateFormat; @@ -36,7 +36,7 @@ * * Allow updating several parameters, triggering a request to the server per parameter. */ -public class UpdateShareRemoteOperation extends RemoteOperation { +public class UpdateShareRemoteOperation extends RemoteOperation> { private static final String TAG = GetShareRemoteOperation.class.getSimpleName(); @@ -47,14 +47,12 @@ public class UpdateShareRemoteOperation extends RemoteOperation { private static final String PARAM_HIDE_DOWNLOAD = "hideDownload"; private static final String PARAM_LABEL = "label"; private static final String FORMAT_EXPIRATION_DATE = "yyyy-MM-dd"; - private static final String ENTITY_CONTENT_TYPE = "application/x-www-form-urlencoded"; - private static final String ENTITY_CHARSET = "UTF-8"; /** * Identifier of the share to update */ - private long remoteId; + private final long remoteId; /** * Password to set for the public link @@ -142,7 +140,7 @@ public void setNote(String note) { } @Override - protected RemoteOperationResult> run(OwnCloudClient client) { + public RemoteOperationResult> run(NextcloudClient client) { RemoteOperationResult> result = null; int status; @@ -182,7 +180,7 @@ protected RemoteOperationResult> run(OwnCloudClient client) { } /// perform required PUT requests - PutMethod put = null; + com.nextcloud.operations.PutMethod put = null; String uriString; try { @@ -196,15 +194,11 @@ protected RemoteOperationResult> run(OwnCloudClient client) { if (put != null) { put.releaseConnection(); } - put = new PutMethod(uriString); - put.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE); - put.setRequestEntity(new StringRequestEntity( - parameter.first + "=" + parameter.second, - ENTITY_CONTENT_TYPE, - ENTITY_CHARSET - )); - - status = client.executeMethod(put); + JSONRequestBody jsonRequestBody = new JSONRequestBody(parameter.first, parameter.second); + + put = new PutMethod(uriString, true, jsonRequestBody.get()); + + status = client.execute(put); if (status == HttpStatus.SC_OK || status == HttpStatus.SC_BAD_REQUEST) { String response = put.getResponseBodyAsString(); From c9e166a9f8fde21e5163ca833576dff4877d3c2f Mon Sep 17 00:00:00 2001 From: ZetaTom <70907959+ZetaTom@users.noreply.github.com> Date: Wed, 10 Jan 2024 09:49:17 +0100 Subject: [PATCH 2/7] Fix GetServerInfo and GetStatus operations Signed-off-by: ZetaTom <70907959+ZetaTom@users.noreply.github.com> --- .../status/GetStatusRemoteOperation.java | 81 ++++++++----------- 1 file changed, 32 insertions(+), 49 deletions(-) diff --git a/library/src/main/java/com/owncloud/android/lib/resources/status/GetStatusRemoteOperation.java b/library/src/main/java/com/owncloud/android/lib/resources/status/GetStatusRemoteOperation.java index 423abcbe3..c204dc62e 100644 --- a/library/src/main/java/com/owncloud/android/lib/resources/status/GetStatusRemoteOperation.java +++ b/library/src/main/java/com/owncloud/android/lib/resources/status/GetStatusRemoteOperation.java @@ -15,21 +15,18 @@ import android.net.ConnectivityManager; import android.net.Uri; -import com.owncloud.android.lib.common.OwnCloudClient; -import com.owncloud.android.lib.common.OwnCloudClientManagerFactory; +import com.nextcloud.common.NextcloudClient; +import com.nextcloud.operations.GetMethod; import com.owncloud.android.lib.common.accounts.AccountUtils; 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; import org.apache.commons.httpclient.HttpStatus; -import org.apache.commons.httpclient.methods.GetMethod; -import org.apache.commons.httpclient.params.HttpMethodParams; -import org.apache.commons.httpclient.params.HttpParams; import org.json.JSONException; import org.json.JSONObject; -import java.util.ArrayList; +import kotlin.Pair; /** * Checks if the server is valid and if the server supports the Share API @@ -37,13 +34,7 @@ * @author David A. Velasco * @author masensio */ -public class GetStatusRemoteOperation extends RemoteOperation { - - /** - * Maximum time to wait for a response from the server when the connection is being tested, in MILLISECONDs. - */ - private static final int TRY_CONNECTION_TIMEOUT = 50000; - +public class GetStatusRemoteOperation extends RemoteOperation> { private static final String TAG = GetStatusRemoteOperation.class.getSimpleName(); private static final String NODE_INSTALLED = "installed"; @@ -53,28 +44,24 @@ public class GetStatusRemoteOperation extends RemoteOperation { private static final String PROTOCOL_HTTP = "http://"; private static final int UNTRUSTED_DOMAIN_ERROR_CODE = 15; - private RemoteOperationResult mLatestResult; - private Context mContext; + private RemoteOperationResult> mLatestResult; + private final Context mContext; public GetStatusRemoteOperation(Context context) { mContext = context; } - private boolean tryConnection(OwnCloudClient client) { + private boolean tryConnection(NextcloudClient client) { boolean retval = false; - GetMethod get = null; - String baseUrlSt = client.getBaseUri().toString(); + com.nextcloud.operations.GetMethod get = null; + String baseUrlSt = String.valueOf(client.getBaseUri()); try { - get = new GetMethod(baseUrlSt + AccountUtils.STATUS_PATH); - - HttpParams params = HttpMethodParams.getDefaultParams(); - params.setParameter(HttpMethodParams.USER_AGENT, OwnCloudClientManagerFactory.getUserAgent()); - get.getParams().setDefaults(params); + get = new com.nextcloud.operations.GetMethod(baseUrlSt + AccountUtils.STATUS_PATH, false); client.setFollowRedirects(false); boolean isRedirectToNonSecureConnection = false; - int status = client.executeMethod(get, TRY_CONNECTION_TIMEOUT, TRY_CONNECTION_TIMEOUT); - mLatestResult = new RemoteOperationResult((status == HttpStatus.SC_OK), get); + int status = client.execute(get); + mLatestResult = new RemoteOperationResult<>((status == HttpStatus.SC_OK), get); String redirectedLocation = mLatestResult.getRedirectedLocation(); while (redirectedLocation != null && redirectedLocation.length() > 0 @@ -85,9 +72,9 @@ private boolean tryConnection(OwnCloudClient client) { redirectedLocation.startsWith(PROTOCOL_HTTP) ); get.releaseConnection(); - get = new GetMethod(redirectedLocation); - status = client.executeMethod(get, TRY_CONNECTION_TIMEOUT, TRY_CONNECTION_TIMEOUT); - mLatestResult = new RemoteOperationResult((status == HttpStatus.SC_OK), get); + get = new GetMethod(redirectedLocation, false); + status = client.execute(get); + mLatestResult = new RemoteOperationResult<>((status == HttpStatus.SC_OK), get); redirectedLocation = mLatestResult.getRedirectedLocation(); } @@ -96,7 +83,7 @@ private boolean tryConnection(OwnCloudClient client) { if (status == HttpStatus.SC_OK) { JSONObject json = new JSONObject(response); if (!json.getBoolean(NODE_INSTALLED)) { - mLatestResult = new RemoteOperationResult( + mLatestResult = new RemoteOperationResult<>( RemoteOperationResult.ResultCode.INSTANCE_NOT_CONFIGURED); } else { boolean extendedSupport = false; @@ -108,24 +95,22 @@ private boolean tryConnection(OwnCloudClient client) { OwnCloudVersion ocVersion = new OwnCloudVersion(version); if (!ocVersion.isVersionValid()) { - mLatestResult = new RemoteOperationResult(RemoteOperationResult.ResultCode.BAD_OC_VERSION); + mLatestResult = new RemoteOperationResult<>(RemoteOperationResult.ResultCode.BAD_OC_VERSION); } else { // success if (isRedirectToNonSecureConnection) { - mLatestResult = new RemoteOperationResult( + mLatestResult = new RemoteOperationResult<>( RemoteOperationResult.ResultCode.OK_REDIRECT_TO_NON_SECURE_CONNECTION); } else { - mLatestResult = new RemoteOperationResult( + mLatestResult = new RemoteOperationResult<>( baseUrlSt.startsWith(PROTOCOL_HTTPS) ? RemoteOperationResult.ResultCode.OK_SSL : RemoteOperationResult.ResultCode.OK_NO_SSL ); } - ArrayList data = new ArrayList<>(); - data.add(ocVersion); - data.add(extendedSupport); - mLatestResult.setData(data); + Pair data = new Pair<>(ocVersion, extendedSupport); + mLatestResult.setResultData(data); retval = true; } } @@ -134,22 +119,22 @@ private boolean tryConnection(OwnCloudClient client) { JSONObject json = new JSONObject(response); if (json.getInt("code") == UNTRUSTED_DOMAIN_ERROR_CODE) { - mLatestResult = new RemoteOperationResult(RemoteOperationResult.ResultCode.UNTRUSTED_DOMAIN); + mLatestResult = new RemoteOperationResult<>(RemoteOperationResult.ResultCode.UNTRUSTED_DOMAIN); } else { - mLatestResult = new RemoteOperationResult(false, status, get.getResponseHeaders()); + mLatestResult = new RemoteOperationResult<>(false, get); } } catch (JSONException e) { - mLatestResult = new RemoteOperationResult(false, status, get.getResponseHeaders()); + mLatestResult = new RemoteOperationResult<>(false, get); } } else { - mLatestResult = new RemoteOperationResult(false, status, get.getResponseHeaders()); + mLatestResult = new RemoteOperationResult<>(false, get); } } catch (JSONException e) { - mLatestResult = new RemoteOperationResult(RemoteOperationResult.ResultCode.INSTANCE_NOT_CONFIGURED); + mLatestResult = new RemoteOperationResult<>(RemoteOperationResult.ResultCode.INSTANCE_NOT_CONFIGURED); } catch (Exception e) { - mLatestResult = new RemoteOperationResult(e); + mLatestResult = new RemoteOperationResult<>(e); } finally { if (get != null) @@ -171,18 +156,16 @@ private boolean tryConnection(OwnCloudClient client) { } private boolean isOnline() { - ConnectivityManager cm = (ConnectivityManager) mContext - .getSystemService(Context.CONNECTIVITY_SERVICE); - return cm != null && cm.getActiveNetworkInfo() != null - && cm.getActiveNetworkInfo().isConnectedOrConnecting(); + ConnectivityManager cm = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE); + return cm != null && cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isConnectedOrConnecting(); } @Override - protected RemoteOperationResult run(OwnCloudClient client) { + public RemoteOperationResult> run(NextcloudClient client) { if (!isOnline()) { - return new RemoteOperationResult(RemoteOperationResult.ResultCode.NO_NETWORK_CONNECTION); + return new RemoteOperationResult<>(RemoteOperationResult.ResultCode.NO_NETWORK_CONNECTION); } - String baseUriStr = client.getBaseUri().toString(); + String baseUriStr = String.valueOf(client.getBaseUri()); if (baseUriStr.startsWith(PROTOCOL_HTTP) || baseUriStr.startsWith(PROTOCOL_HTTPS)) { tryConnection(client); From fabe2c9e703dc158ab7743af6b30d5406d5f31fb Mon Sep 17 00:00:00 2001 From: ZetaTom <70907959+ZetaTom@users.noreply.github.com> Date: Tue, 30 Jan 2024 14:19:26 +0100 Subject: [PATCH 3/7] WIP Signed-off-by: ZetaTom <70907959+ZetaTom@users.noreply.github.com> --- .../e2ee/GetMetadataRemoteOperation.java | 29 +++++++------------ .../e2ee/LockFileRemoteOperation.java | 21 +++++++------- .../e2ee/UnlockFileRemoteOperation.java | 17 ++++------- 3 files changed, 27 insertions(+), 40 deletions(-) diff --git a/library/src/main/java/com/owncloud/android/lib/resources/e2ee/GetMetadataRemoteOperation.java b/library/src/main/java/com/owncloud/android/lib/resources/e2ee/GetMetadataRemoteOperation.java index 2d666baa3..13f6dd0cd 100644 --- a/library/src/main/java/com/owncloud/android/lib/resources/e2ee/GetMetadataRemoteOperation.java +++ b/library/src/main/java/com/owncloud/android/lib/resources/e2ee/GetMetadataRemoteOperation.java @@ -7,14 +7,13 @@ */ package com.owncloud.android.lib.resources.e2ee; -import com.owncloud.android.lib.common.OwnCloudClient; +import com.nextcloud.common.NextcloudClient; +import com.nextcloud.operations.GetMethod; 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; -import org.apache.commons.httpclient.Header; import org.apache.commons.httpclient.HttpStatus; -import org.apache.commons.httpclient.methods.GetMethod; import org.json.JSONObject; @@ -25,8 +24,6 @@ public class GetMetadataRemoteOperation extends RemoteOperation { private static final String TAG = GetMetadataRemoteOperation.class.getSimpleName(); - private static final int SYNC_READ_TIMEOUT = 40000; - private static final int SYNC_CONNECTION_TIMEOUT = 5000; private static final String METADATA_V1_URL = "/ocs/v2.php/apps/end_to_end_encryption/api/v1/meta-data/"; private static final String METADATA_V2_URL = "/ocs/v2.php/apps/end_to_end_encryption/api/v2/meta-data/"; @@ -49,32 +46,29 @@ public GetMetadataRemoteOperation(long fileId) { * @param client Client object */ @Override - protected RemoteOperationResult run(OwnCloudClient client) { - GetMethod getMethod = null; + public RemoteOperationResult run(NextcloudClient client) { + com.nextcloud.operations.GetMethod getMethod = null; RemoteOperationResult result; try { // remote request - getMethod = new GetMethod(client.getBaseUri() + METADATA_V2_URL + fileId + JSON_FORMAT); - getMethod.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE); + getMethod = new com.nextcloud.operations.GetMethod(client.getBaseUri() + METADATA_V2_URL + fileId + JSON_FORMAT, true); - int status = client.executeMethod(getMethod, SYNC_READ_TIMEOUT, SYNC_CONNECTION_TIMEOUT); + int status = client.execute(getMethod); if (status == HttpStatus.SC_NOT_FOUND || status == HttpStatus.SC_INTERNAL_SERVER_ERROR) { // retry with v1 - getMethod = new GetMethod(client.getBaseUri() + METADATA_V1_URL + fileId + JSON_FORMAT); - getMethod.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE); + getMethod = new GetMethod(client.getBaseUri() + METADATA_V1_URL + fileId + JSON_FORMAT, true); - status = client.executeMethod(getMethod, SYNC_READ_TIMEOUT, SYNC_CONNECTION_TIMEOUT); + status = client.execute(getMethod); } if (status == HttpStatus.SC_OK) { String response = getMethod.getResponseBodyAsString(); - Header signatureHeader = getMethod.getResponseHeader(HEADER_SIGNATURE); + String signature = getMethod.getResponseHeader(HEADER_SIGNATURE); - String signature = ""; - if (signatureHeader != null) { - signature = signatureHeader.getValue(); + if (signature == null) { + signature = ""; } // Parse the response @@ -90,7 +84,6 @@ protected RemoteOperationResult run(OwnCloudClient client) { result.setResultData(metadataResponse); } else { result = new RemoteOperationResult<>(false, getMethod); - client.exhaustResponse(getMethod.getResponseBodyAsStream()); } } catch (Exception e) { result = new RemoteOperationResult<>(e); diff --git a/library/src/main/java/com/owncloud/android/lib/resources/e2ee/LockFileRemoteOperation.java b/library/src/main/java/com/owncloud/android/lib/resources/e2ee/LockFileRemoteOperation.java index 27a265f7e..bb4e299b7 100644 --- a/library/src/main/java/com/owncloud/android/lib/resources/e2ee/LockFileRemoteOperation.java +++ b/library/src/main/java/com/owncloud/android/lib/resources/e2ee/LockFileRemoteOperation.java @@ -7,15 +7,17 @@ */ package com.owncloud.android.lib.resources.e2ee; -import com.owncloud.android.lib.common.OwnCloudClient; +import com.nextcloud.common.NextcloudClient; +import com.nextcloud.operations.PostMethod; 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; import org.apache.commons.httpclient.HttpStatus; -import org.apache.commons.httpclient.methods.Utf8PostMethod; import org.json.JSONObject; +import okhttp3.RequestBody; + /** * Lock a file @@ -23,8 +25,6 @@ public class LockFileRemoteOperation extends RemoteOperation { private static final String TAG = LockFileRemoteOperation.class.getSimpleName(); - private static final int SYNC_READ_TIMEOUT = 40000; - private static final int SYNC_CONNECTION_TIMEOUT = 5000; private static final String LOCK_FILE_URL = "/ocs/v2.php/apps/end_to_end_encryption/api/v1/lock/"; private static final String COUNTER_HEADER = "X-NC-E2EE-COUNTER"; @@ -52,22 +52,22 @@ public LockFileRemoteOperation(long localId) { * @param client Client object */ @Override - protected RemoteOperationResult run(OwnCloudClient client) { + public RemoteOperationResult run(NextcloudClient client) { RemoteOperationResult result; - Utf8PostMethod postMethod = null; + PostMethod postMethod = null; try { - postMethod = new Utf8PostMethod(client.getBaseUri() + LOCK_FILE_URL + localId + JSON_FORMAT); + RequestBody requestBody = RequestBody.create(new byte[] {}); + + postMethod = new PostMethod(client.getBaseUri() + LOCK_FILE_URL + localId + JSON_FORMAT, true, requestBody); - // remote request - postMethod.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE); postMethod.addRequestHeader(CONTENT_TYPE, FORM_URLENCODED); if (counter > 0) { postMethod.addRequestHeader(COUNTER_HEADER, String.valueOf(counter)); } - int status = client.executeMethod(postMethod, SYNC_READ_TIMEOUT, SYNC_CONNECTION_TIMEOUT); + int status = client.execute(postMethod); if (status == HttpStatus.SC_OK) { String response = postMethod.getResponseBodyAsString(); @@ -83,7 +83,6 @@ protected RemoteOperationResult run(OwnCloudClient client) { result.setResultData(token); } else { result = new RemoteOperationResult<>(false, postMethod); - client.exhaustResponse(postMethod.getResponseBodyAsStream()); } } catch (Exception e) { result = new RemoteOperationResult<>(e); diff --git a/library/src/main/java/com/owncloud/android/lib/resources/e2ee/UnlockFileRemoteOperation.java b/library/src/main/java/com/owncloud/android/lib/resources/e2ee/UnlockFileRemoteOperation.java index db110881e..9f83c4402 100644 --- a/library/src/main/java/com/owncloud/android/lib/resources/e2ee/UnlockFileRemoteOperation.java +++ b/library/src/main/java/com/owncloud/android/lib/resources/e2ee/UnlockFileRemoteOperation.java @@ -7,13 +7,13 @@ */ package com.owncloud.android.lib.resources.e2ee; -import com.owncloud.android.lib.common.OwnCloudClient; +import com.nextcloud.common.NextcloudClient; +import com.nextcloud.operations.DeleteMethod; 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; import org.apache.commons.httpclient.HttpStatus; -import org.apache.commons.httpclient.methods.DeleteMethod; /** @@ -22,8 +22,6 @@ public class UnlockFileRemoteOperation extends RemoteOperation { private static final String TAG = UnlockFileRemoteOperation.class.getSimpleName(); - private static final int SYNC_READ_TIMEOUT = 40000; - private static final int SYNC_CONNECTION_TIMEOUT = 5000; private static final String LOCK_FILE_URL = "/ocs/v2.php/apps/end_to_end_encryption/api/v2/lock/"; private final long localId; @@ -41,22 +39,19 @@ public UnlockFileRemoteOperation(long localId, String token) { * @param client Client object */ @Override - protected RemoteOperationResult run(OwnCloudClient client) { + public RemoteOperationResult run(NextcloudClient client) { RemoteOperationResult result; - DeleteMethod deleteMethod = null; + com.nextcloud.operations.DeleteMethod deleteMethod = null; try { // remote request - deleteMethod = new DeleteMethod(client.getBaseUri() + LOCK_FILE_URL + localId); - deleteMethod.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE); + deleteMethod = new DeleteMethod(client.getBaseUri() + LOCK_FILE_URL + localId, true); deleteMethod.addRequestHeader(CONTENT_TYPE, FORM_URLENCODED); deleteMethod.addRequestHeader(E2E_TOKEN, token); - int status = client.executeMethod(deleteMethod, SYNC_READ_TIMEOUT, SYNC_CONNECTION_TIMEOUT); + int status = client.execute(deleteMethod); result = new RemoteOperationResult<>(status == HttpStatus.SC_OK, deleteMethod); - - client.exhaustResponse(deleteMethod.getResponseBodyAsStream()); } catch (Exception e) { result = new RemoteOperationResult<>(e); Log_OC.e(TAG, "Unlock file with id " + localId + " failed: " + result.getLogMessage(), From f68c9d47695628461cd6fd8ea08fb531ef400836 Mon Sep 17 00:00:00 2001 From: tobiasKaminsky Date: Tue, 11 Jun 2024 14:58:31 +0200 Subject: [PATCH 4/7] update Signed-off-by: tobiasKaminsky --- .../java/com/owncloud/android/AbstractIT.java | 15 ++++++++------- .../files/RenameFileRemoteOperationIT.kt | 4 ++-- .../android/lib/common/OwnCloudClient.java | 18 ++++++++++++++++++ .../files/RenameFileRemoteOperation.java | 5 ++++- 4 files changed, 32 insertions(+), 10 deletions(-) diff --git a/library/src/androidTest/java/com/owncloud/android/AbstractIT.java b/library/src/androidTest/java/com/owncloud/android/AbstractIT.java index 8c738d7cf..a8210ea45 100644 --- a/library/src/androidTest/java/com/owncloud/android/AbstractIT.java +++ b/library/src/androidTest/java/com/owncloud/android/AbstractIT.java @@ -7,10 +7,17 @@ */ package com.owncloud.android; +import static junit.framework.TestCase.assertTrue; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; +import static org.junit.Assume.assumeTrue; + import android.content.Context; import android.net.Uri; import android.os.Bundle; +import androidx.test.platform.app.InstrumentationRegistry; + import com.nextcloud.common.NextcloudClient; import com.owncloud.android.lib.common.OwnCloudBasicCredentials; import com.owncloud.android.lib.common.OwnCloudClient; @@ -47,14 +54,8 @@ import java.security.cert.CertificateException; import java.security.cert.X509Certificate; -import androidx.test.platform.app.InstrumentationRegistry; import okhttp3.Credentials; -import static junit.framework.TestCase.assertTrue; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; -import static org.junit.Assume.assumeTrue; - /** * Common base for all integration tests */ @@ -148,7 +149,7 @@ private static void testConnection() throws KeyStoreException, InterruptedException { GetStatusRemoteOperation getStatus = new GetStatusRemoteOperation(context); - RemoteOperationResult result = getStatus.execute(client); + RemoteOperationResult result = getStatus.execute(nextcloudClient); if (result.isSuccess()) { Log_OC.d("AbstractIT", "Connection to server successful"); diff --git a/library/src/androidTest/java/com/owncloud/android/lib/resources/files/RenameFileRemoteOperationIT.kt b/library/src/androidTest/java/com/owncloud/android/lib/resources/files/RenameFileRemoteOperationIT.kt index 63237f70f..42c0695e0 100644 --- a/library/src/androidTest/java/com/owncloud/android/lib/resources/files/RenameFileRemoteOperationIT.kt +++ b/library/src/androidTest/java/com/owncloud/android/lib/resources/files/RenameFileRemoteOperationIT.kt @@ -32,8 +32,8 @@ class RenameFileRemoteOperationIT : AbstractIT() { .isSuccess ) - assertFalse(ExistenceCheckRemoteOperation(oldRemotePath, false).execute(client).isSuccess) - assertTrue(ExistenceCheckRemoteOperation(newRemotePath, false).execute(client).isSuccess) + assertFalse(ExistenceCheckRemoteOperation(oldRemotePath, false).execute(nextcloudClient).isSuccess) + assertTrue(ExistenceCheckRemoteOperation(newRemotePath, false).execute(nextcloudClient).isSuccess) } @Test diff --git a/library/src/main/java/com/owncloud/android/lib/common/OwnCloudClient.java b/library/src/main/java/com/owncloud/android/lib/common/OwnCloudClient.java index 3d10638ee..7b7c13039 100644 --- a/library/src/main/java/com/owncloud/android/lib/common/OwnCloudClient.java +++ b/library/src/main/java/com/owncloud/android/lib/common/OwnCloudClient.java @@ -21,6 +21,7 @@ import android.text.TextUtils; import com.nextcloud.common.DNSCache; +import com.nextcloud.common.NextcloudClient; import com.nextcloud.common.NextcloudUriDelegate; import com.owncloud.android.lib.common.accounts.AccountUtils; import com.owncloud.android.lib.common.network.AdvancedX509KeyManager; @@ -67,6 +68,8 @@ public class OwnCloudClient extends HttpClient { private AdvancedX509KeyManager keyManager; + private Context context; + /** * Constructor */ @@ -76,6 +79,7 @@ public OwnCloudClient(Uri baseUri, HttpConnectionManager connectionMgr, Context if (baseUri == null) { throw new IllegalArgumentException("Parameter 'baseUri' cannot be NULL"); } + this.context = context; this.keyManager = new AdvancedX509KeyManager(context); nextcloudUriDelegate = new NextcloudUriDelegate(baseUri); @@ -449,4 +453,18 @@ public OwnCloudCredentials getCredentials() { public void setFollowRedirects(boolean followRedirects) { this.followRedirects = followRedirects; } + + public Context getContext() { + return context; + } + + public NextcloudClient toNextcloudClient() { + return OwnCloudClientFactory.createNextcloudClient( + getBaseUri(), + getUserId(), + getCredentials().toOkHttpCredentials(), + getContext(), + isFollowRedirects() + ); + } } diff --git a/library/src/main/java/com/owncloud/android/lib/resources/files/RenameFileRemoteOperation.java b/library/src/main/java/com/owncloud/android/lib/resources/files/RenameFileRemoteOperation.java index ec3300f0e..fb449b760 100644 --- a/library/src/main/java/com/owncloud/android/lib/resources/files/RenameFileRemoteOperation.java +++ b/library/src/main/java/com/owncloud/android/lib/resources/files/RenameFileRemoteOperation.java @@ -6,6 +6,7 @@ */ package com.owncloud.android.lib.resources.files; +import com.nextcloud.common.NextcloudClient; import com.owncloud.android.lib.common.OwnCloudClient; import com.owncloud.android.lib.common.operations.RemoteOperation; import com.owncloud.android.lib.common.operations.RemoteOperationResult; @@ -75,8 +76,10 @@ protected RemoteOperationResult run(OwnCloudClient client) { } // check if a file with the new name already exists + NextcloudClient nextcloudClient = client.toNextcloudClient(); + RemoteOperationResult existenceResult = new ExistenceCheckRemoteOperation(mNewRemotePath, false) - .execute(client); + .execute(nextcloudClient); if (existenceResult.isSuccess()) { return new RemoteOperationResult(ResultCode.INVALID_OVERWRITE); } From 35ea4a5aa612d13fd277c6cee855123929ec254c Mon Sep 17 00:00:00 2001 From: tobiasKaminsky Date: Tue, 11 Jun 2024 15:56:46 +0200 Subject: [PATCH 5/7] wip Signed-off-by: tobiasKaminsky --- .../lib/resources/e2ee/StoreMetadataV2RemoteOperation.kt | 2 +- .../android/lib/resources/e2ee/UnlockFileV1RemoteOperation.kt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/library/src/main/java/com/owncloud/android/lib/resources/e2ee/StoreMetadataV2RemoteOperation.kt b/library/src/main/java/com/owncloud/android/lib/resources/e2ee/StoreMetadataV2RemoteOperation.kt index 85be51a80..d94285bd4 100644 --- a/library/src/main/java/com/owncloud/android/lib/resources/e2ee/StoreMetadataV2RemoteOperation.kt +++ b/library/src/main/java/com/owncloud/android/lib/resources/e2ee/StoreMetadataV2RemoteOperation.kt @@ -30,7 +30,7 @@ class StoreMetadataV2RemoteOperation( */ @Deprecated("Deprecated in Java") @Suppress("Detekt.TooGenericExceptionCaught") - override fun run(client: OwnCloudClient): RemoteOperationResult { + fun run(client: OwnCloudClient): RemoteOperationResult { var postMethod: Utf8PostMethod? = null var result: RemoteOperationResult try { diff --git a/library/src/main/java/com/owncloud/android/lib/resources/e2ee/UnlockFileV1RemoteOperation.kt b/library/src/main/java/com/owncloud/android/lib/resources/e2ee/UnlockFileV1RemoteOperation.kt index c13e24694..48eae2bc4 100644 --- a/library/src/main/java/com/owncloud/android/lib/resources/e2ee/UnlockFileV1RemoteOperation.kt +++ b/library/src/main/java/com/owncloud/android/lib/resources/e2ee/UnlockFileV1RemoteOperation.kt @@ -26,7 +26,7 @@ class UnlockFileV1RemoteOperation( */ @Deprecated("Deprecated in Java") @Suppress("Detekt.TooGenericExceptionCaught") - override fun run(client: OwnCloudClient): RemoteOperationResult { + fun run(client: OwnCloudClient): RemoteOperationResult { var result: RemoteOperationResult var deleteMethod: DeleteMethod? = null try { From faa485d6994a2ba2e36c3dbe78966abc4e96bb2f Mon Sep 17 00:00:00 2001 From: tobiasKaminsky Date: Wed, 12 Jun 2024 15:27:51 +0200 Subject: [PATCH 6/7] wip Signed-off-by: tobiasKaminsky --- .../lib/resources/e2ee/StoreMetadataV2RemoteOperation.kt | 2 +- .../android/lib/resources/e2ee/UnlockFileV1RemoteOperation.kt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/library/src/main/java/com/owncloud/android/lib/resources/e2ee/StoreMetadataV2RemoteOperation.kt b/library/src/main/java/com/owncloud/android/lib/resources/e2ee/StoreMetadataV2RemoteOperation.kt index d94285bd4..85be51a80 100644 --- a/library/src/main/java/com/owncloud/android/lib/resources/e2ee/StoreMetadataV2RemoteOperation.kt +++ b/library/src/main/java/com/owncloud/android/lib/resources/e2ee/StoreMetadataV2RemoteOperation.kt @@ -30,7 +30,7 @@ class StoreMetadataV2RemoteOperation( */ @Deprecated("Deprecated in Java") @Suppress("Detekt.TooGenericExceptionCaught") - fun run(client: OwnCloudClient): RemoteOperationResult { + override fun run(client: OwnCloudClient): RemoteOperationResult { var postMethod: Utf8PostMethod? = null var result: RemoteOperationResult try { diff --git a/library/src/main/java/com/owncloud/android/lib/resources/e2ee/UnlockFileV1RemoteOperation.kt b/library/src/main/java/com/owncloud/android/lib/resources/e2ee/UnlockFileV1RemoteOperation.kt index 48eae2bc4..c13e24694 100644 --- a/library/src/main/java/com/owncloud/android/lib/resources/e2ee/UnlockFileV1RemoteOperation.kt +++ b/library/src/main/java/com/owncloud/android/lib/resources/e2ee/UnlockFileV1RemoteOperation.kt @@ -26,7 +26,7 @@ class UnlockFileV1RemoteOperation( */ @Deprecated("Deprecated in Java") @Suppress("Detekt.TooGenericExceptionCaught") - fun run(client: OwnCloudClient): RemoteOperationResult { + override fun run(client: OwnCloudClient): RemoteOperationResult { var result: RemoteOperationResult var deleteMethod: DeleteMethod? = null try { From 2e3aabe6c155b428881b2e7dc74c64397beb71c6 Mon Sep 17 00:00:00 2001 From: tobiasKaminsky Date: Mon, 17 Jun 2024 15:20:45 +0200 Subject: [PATCH 7/7] wip Signed-off-by: tobiasKaminsky --- ...ashboardGetWidgetItemsRemoteOperationIT.kt | 4 +- .../java/com/owncloud/android/AbstractIT.java | 7 +- .../java/com/owncloud/android/FileIT.java | 22 ++--- .../lib/common/operations/CreateShareIT.java | 30 +++---- .../lib/common/operations/GetSharesIT.java | 12 +-- .../lib/common/operations/RemoveShareIT.java | 6 +- .../files/RenameFileRemoteOperationIT.kt | 4 +- .../files/SearchRemoteOperationIT.java | 12 +-- .../shares/CreateShareRemoteOperationIT.kt | 2 +- .../shares/GetSharesRemoteOperationIT.java | 88 +++++++++++-------- .../shares/UpdateShareRemoteOperationIT.kt | 20 ++--- .../common/OwnCloudAnonymousCredentials.java | 2 +- .../android/lib/common/OwnCloudClient.java | 18 ---- .../e2ee/GetMetadataRemoteOperation.java | 29 +++--- .../e2ee/LockFileRemoteOperation.java | 21 ++--- .../e2ee/UnlockFileRemoteOperation.java | 17 ++-- .../files/ExistenceCheckRemoteOperation.java | 40 ++++++--- .../files/RenameFileRemoteOperation.java | 5 +- .../shares/CreateShareRemoteOperation.java | 42 ++++----- .../shares/GetShareRemoteOperation.java | 22 ++--- .../shares/GetShareesRemoteOperation.java | 18 ++-- .../GetSharesForFileRemoteOperation.java | 52 +++++------ .../shares/GetSharesRemoteOperation.java | 22 ++--- .../shares/RemoveShareRemoteOperation.java | 26 +++--- .../lib/resources/shares/ShareUtils.java | 4 +- .../shares/UpdateShareRemoteOperation.java | 30 ++++--- .../status/GetStatusRemoteOperation.java | 81 ++++++++++------- 27 files changed, 342 insertions(+), 294 deletions(-) diff --git a/library/src/androidTest/java/com/nextcloud/android/lib/resources/dashboard/DashboardGetWidgetItemsRemoteOperationIT.kt b/library/src/androidTest/java/com/nextcloud/android/lib/resources/dashboard/DashboardGetWidgetItemsRemoteOperationIT.kt index a1a4495b2..a2e9aed38 100644 --- a/library/src/androidTest/java/com/nextcloud/android/lib/resources/dashboard/DashboardGetWidgetItemsRemoteOperationIT.kt +++ b/library/src/androidTest/java/com/nextcloud/android/lib/resources/dashboard/DashboardGetWidgetItemsRemoteOperationIT.kt @@ -23,7 +23,7 @@ class DashboardGetWidgetItemsRemoteOperationIT : AbstractIT() { testOnlyOnServer(NextcloudVersion.nextcloud_25) // create folder to have some content - assertTrue(CreateFolderRemoteOperation("/testFolder", false).execute(nextcloudClient2).isSuccess) + assertTrue(CreateFolderRemoteOperation("/testFolder", false).execute(client2).isSuccess) assertTrue( CreateShareRemoteOperation( "/testFolder", @@ -32,7 +32,7 @@ class DashboardGetWidgetItemsRemoteOperationIT : AbstractIT() { false, "", OCShare.MAXIMUM_PERMISSIONS_FOR_FOLDER - ).execute(nextcloudClient2) + ).execute(client2) .isSuccess ) diff --git a/library/src/androidTest/java/com/owncloud/android/AbstractIT.java b/library/src/androidTest/java/com/owncloud/android/AbstractIT.java index a8210ea45..629747cce 100644 --- a/library/src/androidTest/java/com/owncloud/android/AbstractIT.java +++ b/library/src/androidTest/java/com/owncloud/android/AbstractIT.java @@ -70,7 +70,6 @@ public abstract class AbstractIT { public static OwnCloudClient client; public static OwnCloudClient client2; protected static NextcloudClient nextcloudClient; - protected static NextcloudClient nextcloudClient2; protected static Context context; protected static Uri url; @@ -110,10 +109,6 @@ public static void beforeAll() throws InterruptedException, String credentials = Credentials.basic(loginName, password); nextcloudClient = new NextcloudClient(url, userId, credentials, context); - String userId2 = loginName; // for test same as userId - String credentials2 = Credentials.basic(loginName2, password2); - nextcloudClient2 = new NextcloudClient(url, userId2, credentials2, context); - waitForServer(client, url); testConnection(); } @@ -149,7 +144,7 @@ private static void testConnection() throws KeyStoreException, InterruptedException { GetStatusRemoteOperation getStatus = new GetStatusRemoteOperation(context); - RemoteOperationResult result = getStatus.execute(nextcloudClient); + RemoteOperationResult result = getStatus.execute(client); if (result.isSuccess()) { Log_OC.d("AbstractIT", "Connection to server successful"); diff --git a/library/src/androidTest/java/com/owncloud/android/FileIT.java b/library/src/androidTest/java/com/owncloud/android/FileIT.java index 65bae644a..1c75ea5b9 100644 --- a/library/src/androidTest/java/com/owncloud/android/FileIT.java +++ b/library/src/androidTest/java/com/owncloud/android/FileIT.java @@ -7,6 +7,10 @@ */ package com.owncloud.android; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + import android.net.Uri; import com.owncloud.android.lib.common.operations.RemoteOperationResult; @@ -27,10 +31,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 */ @@ -126,7 +126,7 @@ public void testShareViaLinkSharees() { false, "", OCShare.NO_PERMISSION) - .execute(nextcloudClient).isSuccess()); + .execute(client).isSuccess()); // verify RemoteOperationResult result = new ReadFolderRemoteOperation("/").execute(client); @@ -167,7 +167,7 @@ public void testShareToGroupSharees() { false, "", OCShare.NO_PERMISSION) - .execute(nextcloudClient).isSuccess()); + .execute(client).isSuccess()); // verify RemoteOperationResult result = new ReadFolderRemoteOperation("/").execute(client); @@ -202,7 +202,7 @@ public void testOneSharees() { false, "", OCShare.NO_PERMISSION) - .execute(nextcloudClient).isSuccess()); + .execute(client).isSuccess()); // verify RemoteOperationResult result = new ReadFolderRemoteOperation("/").execute(client); @@ -239,7 +239,7 @@ public void testTwoShareesOnParent() { false, "", OCShare.NO_PERMISSION) - .execute(nextcloudClient).isSuccess()); + .execute(client).isSuccess()); assertTrue(new CreateShareRemoteOperation(path, ShareType.USER, @@ -247,7 +247,7 @@ public void testTwoShareesOnParent() { false, "", OCShare.NO_PERMISSION) - .execute(nextcloudClient).isSuccess()); + .execute(client).isSuccess()); // verify RemoteOperationResult result = new ReadFolderRemoteOperation("/").execute(client); @@ -287,7 +287,7 @@ public void testTwoSharees() { false, "", OCShare.NO_PERMISSION) - .execute(nextcloudClient).isSuccess()); + .execute(client).isSuccess()); assertTrue(new CreateShareRemoteOperation(path, ShareType.USER, @@ -295,7 +295,7 @@ public void testTwoSharees() { false, "", OCShare.NO_PERMISSION) - .execute(nextcloudClient).isSuccess()); + .execute(client).isSuccess()); // verify RemoteOperationResult result = new ReadFolderRemoteOperation(path).execute(client); diff --git a/library/src/androidTest/java/com/owncloud/android/lib/common/operations/CreateShareIT.java b/library/src/androidTest/java/com/owncloud/android/lib/common/operations/CreateShareIT.java index ac71c2b0f..fe9e5cc9e 100644 --- a/library/src/androidTest/java/com/owncloud/android/lib/common/operations/CreateShareIT.java +++ b/library/src/androidTest/java/com/owncloud/android/lib/common/operations/CreateShareIT.java @@ -7,6 +7,10 @@ */ package com.owncloud.android.lib.common.operations; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + import com.owncloud.android.AbstractIT; import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode; import com.owncloud.android.lib.resources.files.UploadFileRemoteOperation; @@ -18,10 +22,6 @@ import java.io.File; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - /** * Test create share */ @@ -56,7 +56,7 @@ public void testCreatePublicShareSuccessful() { "", false, "", - 1).execute(nextcloudClient); + 1).execute(client); assertTrue(result.isSuccess()); } @@ -67,7 +67,7 @@ public void testCreatePublicShareFailure() { "", false, "", - 1).execute(nextcloudClient); + 1).execute(client); assertFalse(result.isSuccess()); assertEquals(ResultCode.FILE_NOT_FOUND, result.getCode()); @@ -83,7 +83,7 @@ public void testCreatePrivateShareWithUserSuccessful() { "admin", false, "", - 31).execute(nextcloudClient); + 31).execute(client); assertTrue(result.isSuccess()); } @@ -97,7 +97,7 @@ public void testCreatePrivateShareWithUserNotExists() { "no_exist", false, "", - 31).execute(nextcloudClient); + 31).execute(client); assertFalse(result.isSuccess()); // TODO 404 is File not found, but actually it is "user not found" @@ -114,7 +114,7 @@ public void testCreatePrivateShareWithFileNotExists() { "admin", false, "", - 31).execute(nextcloudClient); + 31).execute(client); assertFalse(result.isSuccess()); assertEquals(ResultCode.FILE_NOT_FOUND, result.getCode()); } @@ -129,7 +129,7 @@ public void testCreatePrivateShareWithGroupSuccessful() { "admin", false, "", - 1).execute(nextcloudClient); + 1).execute(client); assertTrue(result.isSuccess()); } @@ -143,7 +143,7 @@ public void testCreatePrivateShareWithNonExistingGroupSharee() { "no_exist", false, "", - 31).execute(nextcloudClient); + 31).execute(client); assertFalse(result.isSuccess()); // TODO 404 is File not found, but actually it is "user not found" @@ -160,7 +160,7 @@ public void testCreatePrivateShareWithNonExistingFile() { "admin", false, "", - 31).execute(nextcloudClient); + 31).execute(client); assertFalse(result.isSuccess()); assertEquals(ResultCode.FILE_NOT_FOUND, result.getCode()); } @@ -189,7 +189,7 @@ public void testCreateFederatedShareWithNonExistingSharee() { "no_exist@" + serverUri2, false, "", - 31).execute(nextcloudClient); + 31).execute(client); assertFalse("sharee doesn't exist in an existing remote server", result.isSuccess()); assertEquals("sharee doesn't exist in an existing remote server, forbidden", @@ -206,7 +206,7 @@ public void testCreateFederatedShareWithNonExistingRemoteServer() { "no_exist", false, "", - 31).execute(nextcloudClient); + 31).execute(client); assertFalse(result.isSuccess()); // TODO expected: but was: assertEquals("remote server doesn't exist", ResultCode.SHARE_FORBIDDEN, result.getCode()); @@ -222,7 +222,7 @@ public void testCreateFederatedShareWithNonExistingFile() { "admin@" + serverUri2, false, "", - 31).execute(nextcloudClient); + 31).execute(client); assertFalse("file doesn't exist", result.isSuccess()); assertEquals("file doesn't exist", ResultCode.FILE_NOT_FOUND, result.getCode()); diff --git a/library/src/androidTest/java/com/owncloud/android/lib/common/operations/GetSharesIT.java b/library/src/androidTest/java/com/owncloud/android/lib/common/operations/GetSharesIT.java index e8c971e31..55d72ca41 100644 --- a/library/src/androidTest/java/com/owncloud/android/lib/common/operations/GetSharesIT.java +++ b/library/src/androidTest/java/com/owncloud/android/lib/common/operations/GetSharesIT.java @@ -8,6 +8,9 @@ */ package com.owncloud.android.lib.common.operations; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + import com.owncloud.android.AbstractIT; import com.owncloud.android.lib.resources.files.CreateFolderRemoteOperation; import com.owncloud.android.lib.resources.shares.CreateShareRemoteOperation; @@ -19,9 +22,6 @@ import java.util.List; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; - /** * Class to test Get Shares Operation * @@ -36,7 +36,7 @@ public void testGetShares() { "", false, "", - 1).execute(nextcloudClient).isSuccess()); + 1).execute(client).isSuccess()); assertTrue(new CreateFolderRemoteOperation("/2/", true).execute(client).isSuccess()); assertTrue(new CreateShareRemoteOperation("/2/", @@ -44,9 +44,9 @@ public void testGetShares() { "", false, "", - 1).execute(nextcloudClient).isSuccess()); + 1).execute(client).isSuccess()); - RemoteOperationResult> result = new GetSharesRemoteOperation().execute(nextcloudClient); + RemoteOperationResult> result = new GetSharesRemoteOperation().execute(client); assertTrue(result.isSuccess()); assertEquals(2, result.getResultData().size()); assertEquals("/1/", result.getResultData().get(0).getPath()); diff --git a/library/src/androidTest/java/com/owncloud/android/lib/common/operations/RemoveShareIT.java b/library/src/androidTest/java/com/owncloud/android/lib/common/operations/RemoveShareIT.java index e168e4527..f7986b37d 100644 --- a/library/src/androidTest/java/com/owncloud/android/lib/common/operations/RemoveShareIT.java +++ b/library/src/androidTest/java/com/owncloud/android/lib/common/operations/RemoveShareIT.java @@ -7,6 +7,8 @@ */ package com.owncloud.android.lib.common.operations; +import static org.junit.Assert.assertTrue; + import com.owncloud.android.AbstractIT; import com.owncloud.android.lib.resources.files.UploadFileRemoteOperation; import com.owncloud.android.lib.resources.shares.CreateShareRemoteOperation; @@ -20,8 +22,6 @@ import java.io.IOException; import java.util.List; -import static org.junit.Assert.assertTrue; - public class RemoveShareIT extends AbstractIT { private static final String FILE_TO_UNSHARE = "/fileToUnshare.txt"; @@ -42,7 +42,7 @@ public void testRemoveShare() throws IOException { ShareType.PUBLIC_LINK, "", false, - "", 1).execute(nextcloudClient); + "", 1).execute(client); assertTrue(result.isSuccess()); diff --git a/library/src/androidTest/java/com/owncloud/android/lib/resources/files/RenameFileRemoteOperationIT.kt b/library/src/androidTest/java/com/owncloud/android/lib/resources/files/RenameFileRemoteOperationIT.kt index 42c0695e0..63237f70f 100644 --- a/library/src/androidTest/java/com/owncloud/android/lib/resources/files/RenameFileRemoteOperationIT.kt +++ b/library/src/androidTest/java/com/owncloud/android/lib/resources/files/RenameFileRemoteOperationIT.kt @@ -32,8 +32,8 @@ class RenameFileRemoteOperationIT : AbstractIT() { .isSuccess ) - assertFalse(ExistenceCheckRemoteOperation(oldRemotePath, false).execute(nextcloudClient).isSuccess) - assertTrue(ExistenceCheckRemoteOperation(newRemotePath, false).execute(nextcloudClient).isSuccess) + assertFalse(ExistenceCheckRemoteOperation(oldRemotePath, false).execute(client).isSuccess) + assertTrue(ExistenceCheckRemoteOperation(newRemotePath, false).execute(client).isSuccess) } @Test diff --git a/library/src/androidTest/java/com/owncloud/android/lib/resources/files/SearchRemoteOperationIT.java b/library/src/androidTest/java/com/owncloud/android/lib/resources/files/SearchRemoteOperationIT.java index cc19ea9f5..4004ae1bc 100644 --- a/library/src/androidTest/java/com/owncloud/android/lib/resources/files/SearchRemoteOperationIT.java +++ b/library/src/androidTest/java/com/owncloud/android/lib/resources/files/SearchRemoteOperationIT.java @@ -7,9 +7,14 @@ */ package com.owncloud.android.lib.resources.files; +import static junit.framework.TestCase.assertEquals; +import static junit.framework.TestCase.assertTrue; + import android.net.Uri; import android.os.Bundle; +import androidx.test.platform.app.InstrumentationRegistry; + import com.owncloud.android.AbstractIT; import com.owncloud.android.lib.common.OwnCloudBasicCredentials; import com.owncloud.android.lib.common.OwnCloudClient; @@ -27,11 +32,6 @@ import java.io.IOException; import java.util.List; -import androidx.test.platform.app.InstrumentationRegistry; - -import static junit.framework.TestCase.assertEquals; -import static junit.framework.TestCase.assertTrue; - public class SearchRemoteOperationIT extends AbstractIT { private static OCCapability capability; @@ -177,7 +177,7 @@ public void favoriteFiles() throws IOException { client.getUserId(), false, "", - 31).execute(nextcloudClient2) + 31).execute(client2) .isSuccess() ); diff --git a/library/src/androidTest/java/com/owncloud/android/lib/resources/shares/CreateShareRemoteOperationIT.kt b/library/src/androidTest/java/com/owncloud/android/lib/resources/shares/CreateShareRemoteOperationIT.kt index 32d191992..06bfbe701 100644 --- a/library/src/androidTest/java/com/owncloud/android/lib/resources/shares/CreateShareRemoteOperationIT.kt +++ b/library/src/androidTest/java/com/owncloud/android/lib/resources/shares/CreateShareRemoteOperationIT.kt @@ -50,7 +50,7 @@ class CreateShareRemoteOperationIT : AbstractIT() { OCShare.MAXIMUM_PERMISSIONS_FOR_FOLDER, true, note - ).execute(nextcloudClient) + ).execute(client) junit.framework.Assert.assertTrue(sut.isSuccess) diff --git a/library/src/androidTest/java/com/owncloud/android/lib/resources/shares/GetSharesRemoteOperationIT.java b/library/src/androidTest/java/com/owncloud/android/lib/resources/shares/GetSharesRemoteOperationIT.java index 9d95b6c81..2d173ad2a 100644 --- a/library/src/androidTest/java/com/owncloud/android/lib/resources/shares/GetSharesRemoteOperationIT.java +++ b/library/src/androidTest/java/com/owncloud/android/lib/resources/shares/GetSharesRemoteOperationIT.java @@ -7,10 +7,16 @@ */ package com.owncloud.android.lib.resources.shares; +import static junit.framework.TestCase.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.junit.Assume.assumeTrue; + import android.net.Uri; import android.os.Bundle; -import com.nextcloud.common.NextcloudClient; +import androidx.test.platform.app.InstrumentationRegistry; + import com.owncloud.android.AbstractIT; import com.owncloud.android.lib.common.OwnCloudBasicCredentials; import com.owncloud.android.lib.common.OwnCloudClient; @@ -28,14 +34,6 @@ import java.util.List; -import androidx.test.platform.app.InstrumentationRegistry; -import okhttp3.Credentials; - -import static junit.framework.TestCase.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; -import static org.junit.Assume.assumeTrue; - public class GetSharesRemoteOperationIT extends AbstractIT { @Test public void searchSharedFiles() { @@ -54,16 +52,31 @@ public void searchSharedFiles() { assertEquals(0, result.getResultData().size()); // share folder to user "admin" - assertTrue(new CreateShareRemoteOperation("/shareToAdmin/", ShareType.USER, "admin", false, "", - OCShare.MAXIMUM_PERMISSIONS_FOR_FOLDER).execute(nextcloudClient).isSuccess()); + assertTrue(new CreateShareRemoteOperation("/shareToAdmin/", + ShareType.USER, + "admin", + false, + "", + OCShare.MAXIMUM_PERMISSIONS_FOR_FOLDER) + .execute(client).isSuccess()); // share folder via public link - assertTrue(new CreateShareRemoteOperation("/shareViaLink/", ShareType.PUBLIC_LINK, "", true, "", - OCShare.READ_PERMISSION_FLAG).execute(nextcloudClient).isSuccess()); + assertTrue(new CreateShareRemoteOperation("/shareViaLink/", + ShareType.PUBLIC_LINK, + "", + true, + "", + OCShare.READ_PERMISSION_FLAG) + .execute(client).isSuccess()); // share folder to group - assertTrue(new CreateShareRemoteOperation("/shareToGroup/", ShareType.GROUP, "users", false, "", - OCShare.NO_PERMISSION).execute(nextcloudClient).isSuccess()); + assertTrue(new CreateShareRemoteOperation("/shareToGroup/", + ShareType.GROUP, + "users", + false, + "", + OCShare.NO_PERMISSION) + .execute(client).isSuccess()); // share folder to circle // get share @@ -93,7 +106,7 @@ public void searchSharedFiles() { sut = new GetSharesRemoteOperation(); - result = sut.execute(nextcloudClient); + result = sut.execute(client); assertTrue(result.isSuccess()); assertEquals(3, result.getResultData().size()); @@ -144,25 +157,30 @@ public void sharedWithMe() { GetSharesRemoteOperation sut = new GetSharesRemoteOperation(); GetSharesRemoteOperation sutSharedWithMe = new GetSharesRemoteOperation(true); - RemoteOperationResult> result = sut.execute(nextcloudClient); + RemoteOperationResult> result = sut.execute(client); assertTrue(result.isSuccess()); assertEquals(0, result.getResultData().size()); - RemoteOperationResult> resultSharedWithMe = sutSharedWithMe.execute(nextcloudClient); + RemoteOperationResult> resultSharedWithMe = sutSharedWithMe.execute(client); assertTrue(resultSharedWithMe.isSuccess()); assertEquals(0, resultSharedWithMe.getResultData().size()); // share folder to user "admin" assertTrue(new CreateFolderRemoteOperation("/shareToAdmin/", true).execute(client).isSuccess()); - assertTrue(new CreateShareRemoteOperation("/shareToAdmin/", ShareType.USER, "admin", false, "", - OCShare.MAXIMUM_PERMISSIONS_FOR_FOLDER).execute(nextcloudClient).isSuccess()); + assertTrue(new CreateShareRemoteOperation("/shareToAdmin/", + ShareType.USER, + "admin", + false, + "", + OCShare.MAXIMUM_PERMISSIONS_FOR_FOLDER) + .execute(client).isSuccess()); // Expect one file shared by me, no file shared with me - result = sut.execute(nextcloudClient); + result = sut.execute(client); assertEquals(1, result.getResultData().size()); - resultSharedWithMe = sutSharedWithMe.execute(nextcloudClient); + resultSharedWithMe = sutSharedWithMe.execute(client); assertEquals(0, resultSharedWithMe.getResultData().size()); // create client for user "user1" @@ -170,26 +188,26 @@ public void sharedWithMe() { url = Uri.parse(arguments.getString("TEST_SERVER_URL")); String loginName = "user1"; String password = "user1"; - String credentials = Credentials.basic(loginName, password); OwnCloudClient clientUser1 = OwnCloudClientFactory.createOwnCloudClient(url, context, true); clientUser1.setCredentials(new OwnCloudBasicCredentials(loginName, password)); clientUser1.setUserId(loginName); // for test same as userId - NextcloudClient nextcloudClientUser1 = OwnCloudClientFactory.createNextcloudClient(url, loginName, - credentials, context, true); - // share folder to previous user assertTrue(new CreateFolderRemoteOperation("/shareToUser/", true).execute(clientUser1).isSuccess()); - assertTrue(new CreateShareRemoteOperation("/shareToUser/", ShareType.USER, - client.getCredentials().getUsername(), false, "", - OCShare.MAXIMUM_PERMISSIONS_FOR_FOLDER).execute(nextcloudClientUser1).isSuccess()); + assertTrue(new CreateShareRemoteOperation("/shareToUser/", + ShareType.USER, + client.getCredentials().getUsername(), + false, + "", + OCShare.MAXIMUM_PERMISSIONS_FOR_FOLDER) + .execute(clientUser1).isSuccess()); // Expect one file shared by me, one file shared with me - result = sut.execute(nextcloudClient); + result = sut.execute(client); assertEquals(1, result.getResultData().size()); - resultSharedWithMe = sutSharedWithMe.execute(nextcloudClient); + resultSharedWithMe = sutSharedWithMe.execute(client); assertEquals(1, resultSharedWithMe.getResultData().size()); } @@ -207,7 +225,7 @@ public void favorites() { "", OCShare.MAXIMUM_PERMISSIONS_FOR_FOLDER, true) - .execute(nextcloudClient); + .execute(client); assertTrue(createResult.isSuccess()); @@ -225,7 +243,7 @@ public void favorites() { "", OCShare.MAXIMUM_PERMISSIONS_FOR_FOLDER, true) - .execute(nextcloudClient); + .execute(client); assertTrue(createResult.isSuccess()); @@ -251,7 +269,7 @@ public void noFavorite() { "", OCShare.MAXIMUM_PERMISSIONS_FOR_FOLDER, true) - .execute(nextcloudClient); + .execute(client); assertTrue(createResult.isSuccess()); @@ -281,7 +299,7 @@ public void favorite() { "", OCShare.MAXIMUM_PERMISSIONS_FOR_FOLDER, true) - .execute(nextcloudClient); + .execute(client); assertTrue(createResult.isSuccess()); diff --git a/library/src/androidTest/java/com/owncloud/android/lib/resources/shares/UpdateShareRemoteOperationIT.kt b/library/src/androidTest/java/com/owncloud/android/lib/resources/shares/UpdateShareRemoteOperationIT.kt index 8c00fac09..368d9dc59 100644 --- a/library/src/androidTest/java/com/owncloud/android/lib/resources/shares/UpdateShareRemoteOperationIT.kt +++ b/library/src/androidTest/java/com/owncloud/android/lib/resources/shares/UpdateShareRemoteOperationIT.kt @@ -58,7 +58,7 @@ class UpdateShareRemoteOperationIT : AbstractIT() { OCShare.MAXIMUM_PERMISSIONS_FOR_FOLDER, true, "" - ).execute(nextcloudClient) + ).execute(client) assertTrue(createOperationResult.isSuccess) @@ -67,10 +67,10 @@ class UpdateShareRemoteOperationIT : AbstractIT() { val sut = UpdateShareRemoteOperation(share.remoteId) sut.setNote(note) - assertTrue(sut.execute(nextcloudClient).isSuccess) + assertTrue(sut.execute(client).isSuccess) // verify - val getShareOperationResult = GetShareRemoteOperation(share.remoteId).execute(nextcloudClient) + val getShareOperationResult = GetShareRemoteOperation(share.remoteId).execute(client) assertTrue(getShareOperationResult.isSuccess) val updatedShare = getShareOperationResult.resultData[0] @@ -94,7 +94,7 @@ class UpdateShareRemoteOperationIT : AbstractIT() { true, "", OCShare.READ_PERMISSION_FLAG - ).execute(nextcloudClient) + ).execute(client) assertTrue(createOperationResult.isSuccess) @@ -103,10 +103,10 @@ class UpdateShareRemoteOperationIT : AbstractIT() { val sut = UpdateShareRemoteOperation(share.remoteId) sut.setLabel(label) - assertTrue(sut.execute(nextcloudClient).isSuccess) + assertTrue(sut.execute(client).isSuccess) // verify - val getShareOperationResult = GetShareRemoteOperation(share.remoteId).execute(nextcloudClient) + val getShareOperationResult = GetShareRemoteOperation(share.remoteId).execute(client) assertTrue(getShareOperationResult.isSuccess) val updatedShare = getShareOperationResult.resultData[0] @@ -131,7 +131,7 @@ class UpdateShareRemoteOperationIT : AbstractIT() { true, "", OCShare.READ_PERMISSION_FLAG - ).execute(nextcloudClient) + ).execute(client) assertTrue(createOperationResult.isSuccess) @@ -140,7 +140,7 @@ class UpdateShareRemoteOperationIT : AbstractIT() { val sut = UpdateShareRemoteOperation(share.remoteId) sut.setPassword("1") - val result = sut.execute(nextcloudClient) + val result = sut.execute(client) assertFalse(result.isSuccess) val capabilityResult = GetCapabilitiesRemoteOperation().execute(nextcloudClient) @@ -181,7 +181,7 @@ class UpdateShareRemoteOperationIT : AbstractIT() { true, "", OCShare.READ_PERMISSION_FLAG - ).execute(nextcloudClient) + ).execute(client) assertTrue(createOperationResult.isSuccess) @@ -190,7 +190,7 @@ class UpdateShareRemoteOperationIT : AbstractIT() { val sut = UpdateShareRemoteOperation(share.remoteId) sut.setPassword("arnservcvcbtp234") - assertTrue(sut.execute(nextcloudClient).isSuccess) + assertTrue(sut.execute(client).isSuccess) assertTrue(RemoveFileRemoteOperation(folder).execute(client).isSuccess) } } diff --git a/library/src/main/java/com/owncloud/android/lib/common/OwnCloudAnonymousCredentials.java b/library/src/main/java/com/owncloud/android/lib/common/OwnCloudAnonymousCredentials.java index 8d04d3955..2012e697e 100644 --- a/library/src/main/java/com/owncloud/android/lib/common/OwnCloudAnonymousCredentials.java +++ b/library/src/main/java/com/owncloud/android/lib/common/OwnCloudAnonymousCredentials.java @@ -35,7 +35,7 @@ public boolean authTokenExpires() { @Override public String toOkHttpCredentials() { - return OkHttpCredentialsUtil.basic("", ""); + return OkHttpCredentialsUtil.basic(getUsername(), getAuthToken()); } @Override diff --git a/library/src/main/java/com/owncloud/android/lib/common/OwnCloudClient.java b/library/src/main/java/com/owncloud/android/lib/common/OwnCloudClient.java index 7b7c13039..3d10638ee 100644 --- a/library/src/main/java/com/owncloud/android/lib/common/OwnCloudClient.java +++ b/library/src/main/java/com/owncloud/android/lib/common/OwnCloudClient.java @@ -21,7 +21,6 @@ import android.text.TextUtils; import com.nextcloud.common.DNSCache; -import com.nextcloud.common.NextcloudClient; import com.nextcloud.common.NextcloudUriDelegate; import com.owncloud.android.lib.common.accounts.AccountUtils; import com.owncloud.android.lib.common.network.AdvancedX509KeyManager; @@ -68,8 +67,6 @@ public class OwnCloudClient extends HttpClient { private AdvancedX509KeyManager keyManager; - private Context context; - /** * Constructor */ @@ -79,7 +76,6 @@ public OwnCloudClient(Uri baseUri, HttpConnectionManager connectionMgr, Context if (baseUri == null) { throw new IllegalArgumentException("Parameter 'baseUri' cannot be NULL"); } - this.context = context; this.keyManager = new AdvancedX509KeyManager(context); nextcloudUriDelegate = new NextcloudUriDelegate(baseUri); @@ -453,18 +449,4 @@ public OwnCloudCredentials getCredentials() { public void setFollowRedirects(boolean followRedirects) { this.followRedirects = followRedirects; } - - public Context getContext() { - return context; - } - - public NextcloudClient toNextcloudClient() { - return OwnCloudClientFactory.createNextcloudClient( - getBaseUri(), - getUserId(), - getCredentials().toOkHttpCredentials(), - getContext(), - isFollowRedirects() - ); - } } diff --git a/library/src/main/java/com/owncloud/android/lib/resources/e2ee/GetMetadataRemoteOperation.java b/library/src/main/java/com/owncloud/android/lib/resources/e2ee/GetMetadataRemoteOperation.java index 13f6dd0cd..2d666baa3 100644 --- a/library/src/main/java/com/owncloud/android/lib/resources/e2ee/GetMetadataRemoteOperation.java +++ b/library/src/main/java/com/owncloud/android/lib/resources/e2ee/GetMetadataRemoteOperation.java @@ -7,13 +7,14 @@ */ package com.owncloud.android.lib.resources.e2ee; -import com.nextcloud.common.NextcloudClient; -import com.nextcloud.operations.GetMethod; +import com.owncloud.android.lib.common.OwnCloudClient; 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; +import org.apache.commons.httpclient.Header; import org.apache.commons.httpclient.HttpStatus; +import org.apache.commons.httpclient.methods.GetMethod; import org.json.JSONObject; @@ -24,6 +25,8 @@ public class GetMetadataRemoteOperation extends RemoteOperation { private static final String TAG = GetMetadataRemoteOperation.class.getSimpleName(); + private static final int SYNC_READ_TIMEOUT = 40000; + private static final int SYNC_CONNECTION_TIMEOUT = 5000; private static final String METADATA_V1_URL = "/ocs/v2.php/apps/end_to_end_encryption/api/v1/meta-data/"; private static final String METADATA_V2_URL = "/ocs/v2.php/apps/end_to_end_encryption/api/v2/meta-data/"; @@ -46,29 +49,32 @@ public GetMetadataRemoteOperation(long fileId) { * @param client Client object */ @Override - public RemoteOperationResult run(NextcloudClient client) { - com.nextcloud.operations.GetMethod getMethod = null; + protected RemoteOperationResult run(OwnCloudClient client) { + GetMethod getMethod = null; RemoteOperationResult result; try { // remote request - getMethod = new com.nextcloud.operations.GetMethod(client.getBaseUri() + METADATA_V2_URL + fileId + JSON_FORMAT, true); + getMethod = new GetMethod(client.getBaseUri() + METADATA_V2_URL + fileId + JSON_FORMAT); + getMethod.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE); - int status = client.execute(getMethod); + int status = client.executeMethod(getMethod, SYNC_READ_TIMEOUT, SYNC_CONNECTION_TIMEOUT); if (status == HttpStatus.SC_NOT_FOUND || status == HttpStatus.SC_INTERNAL_SERVER_ERROR) { // retry with v1 - getMethod = new GetMethod(client.getBaseUri() + METADATA_V1_URL + fileId + JSON_FORMAT, true); + getMethod = new GetMethod(client.getBaseUri() + METADATA_V1_URL + fileId + JSON_FORMAT); + getMethod.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE); - status = client.execute(getMethod); + status = client.executeMethod(getMethod, SYNC_READ_TIMEOUT, SYNC_CONNECTION_TIMEOUT); } if (status == HttpStatus.SC_OK) { String response = getMethod.getResponseBodyAsString(); - String signature = getMethod.getResponseHeader(HEADER_SIGNATURE); + Header signatureHeader = getMethod.getResponseHeader(HEADER_SIGNATURE); - if (signature == null) { - signature = ""; + String signature = ""; + if (signatureHeader != null) { + signature = signatureHeader.getValue(); } // Parse the response @@ -84,6 +90,7 @@ public RemoteOperationResult run(NextcloudClient client) { result.setResultData(metadataResponse); } else { result = new RemoteOperationResult<>(false, getMethod); + client.exhaustResponse(getMethod.getResponseBodyAsStream()); } } catch (Exception e) { result = new RemoteOperationResult<>(e); diff --git a/library/src/main/java/com/owncloud/android/lib/resources/e2ee/LockFileRemoteOperation.java b/library/src/main/java/com/owncloud/android/lib/resources/e2ee/LockFileRemoteOperation.java index bb4e299b7..27a265f7e 100644 --- a/library/src/main/java/com/owncloud/android/lib/resources/e2ee/LockFileRemoteOperation.java +++ b/library/src/main/java/com/owncloud/android/lib/resources/e2ee/LockFileRemoteOperation.java @@ -7,17 +7,15 @@ */ package com.owncloud.android.lib.resources.e2ee; -import com.nextcloud.common.NextcloudClient; -import com.nextcloud.operations.PostMethod; +import com.owncloud.android.lib.common.OwnCloudClient; 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; import org.apache.commons.httpclient.HttpStatus; +import org.apache.commons.httpclient.methods.Utf8PostMethod; import org.json.JSONObject; -import okhttp3.RequestBody; - /** * Lock a file @@ -25,6 +23,8 @@ public class LockFileRemoteOperation extends RemoteOperation { private static final String TAG = LockFileRemoteOperation.class.getSimpleName(); + private static final int SYNC_READ_TIMEOUT = 40000; + private static final int SYNC_CONNECTION_TIMEOUT = 5000; private static final String LOCK_FILE_URL = "/ocs/v2.php/apps/end_to_end_encryption/api/v1/lock/"; private static final String COUNTER_HEADER = "X-NC-E2EE-COUNTER"; @@ -52,22 +52,22 @@ public LockFileRemoteOperation(long localId) { * @param client Client object */ @Override - public RemoteOperationResult run(NextcloudClient client) { + protected RemoteOperationResult run(OwnCloudClient client) { RemoteOperationResult result; - PostMethod postMethod = null; + Utf8PostMethod postMethod = null; try { - RequestBody requestBody = RequestBody.create(new byte[] {}); - - postMethod = new PostMethod(client.getBaseUri() + LOCK_FILE_URL + localId + JSON_FORMAT, true, requestBody); + postMethod = new Utf8PostMethod(client.getBaseUri() + LOCK_FILE_URL + localId + JSON_FORMAT); + // remote request + postMethod.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE); postMethod.addRequestHeader(CONTENT_TYPE, FORM_URLENCODED); if (counter > 0) { postMethod.addRequestHeader(COUNTER_HEADER, String.valueOf(counter)); } - int status = client.execute(postMethod); + int status = client.executeMethod(postMethod, SYNC_READ_TIMEOUT, SYNC_CONNECTION_TIMEOUT); if (status == HttpStatus.SC_OK) { String response = postMethod.getResponseBodyAsString(); @@ -83,6 +83,7 @@ public RemoteOperationResult run(NextcloudClient client) { result.setResultData(token); } else { result = new RemoteOperationResult<>(false, postMethod); + client.exhaustResponse(postMethod.getResponseBodyAsStream()); } } catch (Exception e) { result = new RemoteOperationResult<>(e); diff --git a/library/src/main/java/com/owncloud/android/lib/resources/e2ee/UnlockFileRemoteOperation.java b/library/src/main/java/com/owncloud/android/lib/resources/e2ee/UnlockFileRemoteOperation.java index 9f83c4402..b3036c9bd 100644 --- a/library/src/main/java/com/owncloud/android/lib/resources/e2ee/UnlockFileRemoteOperation.java +++ b/library/src/main/java/com/owncloud/android/lib/resources/e2ee/UnlockFileRemoteOperation.java @@ -7,13 +7,13 @@ */ package com.owncloud.android.lib.resources.e2ee; -import com.nextcloud.common.NextcloudClient; -import com.nextcloud.operations.DeleteMethod; +import com.owncloud.android.lib.common.OwnCloudClient; 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; import org.apache.commons.httpclient.HttpStatus; +import org.apache.commons.httpclient.methods.DeleteMethod; /** @@ -22,6 +22,8 @@ public class UnlockFileRemoteOperation extends RemoteOperation { private static final String TAG = UnlockFileRemoteOperation.class.getSimpleName(); + private static final int SYNC_READ_TIMEOUT = 40000; + private static final int SYNC_CONNECTION_TIMEOUT = 5000; private static final String LOCK_FILE_URL = "/ocs/v2.php/apps/end_to_end_encryption/api/v2/lock/"; private final long localId; @@ -39,19 +41,22 @@ public UnlockFileRemoteOperation(long localId, String token) { * @param client Client object */ @Override - public RemoteOperationResult run(NextcloudClient client) { + protected RemoteOperationResult run(OwnCloudClient client) { RemoteOperationResult result; - com.nextcloud.operations.DeleteMethod deleteMethod = null; + DeleteMethod deleteMethod = null; try { // remote request - deleteMethod = new DeleteMethod(client.getBaseUri() + LOCK_FILE_URL + localId, true); + deleteMethod = new DeleteMethod(client.getBaseUri() + LOCK_FILE_URL + localId); + deleteMethod.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE); deleteMethod.addRequestHeader(CONTENT_TYPE, FORM_URLENCODED); deleteMethod.addRequestHeader(E2E_TOKEN, token); - int status = client.execute(deleteMethod); + int status = client.executeMethod(deleteMethod, SYNC_READ_TIMEOUT, SYNC_CONNECTION_TIMEOUT); result = new RemoteOperationResult<>(status == HttpStatus.SC_OK, deleteMethod); + + client.exhaustResponse(deleteMethod.getResponseBodyAsStream()); } catch (Exception e) { result = new RemoteOperationResult<>(e); Log_OC.e(TAG, "Unlock file with id " + localId + " failed: " + result.getLogMessage(), diff --git a/library/src/main/java/com/owncloud/android/lib/resources/files/ExistenceCheckRemoteOperation.java b/library/src/main/java/com/owncloud/android/lib/resources/files/ExistenceCheckRemoteOperation.java index 4a84591c1..dac141ca5 100644 --- a/library/src/main/java/com/owncloud/android/lib/resources/files/ExistenceCheckRemoteOperation.java +++ b/library/src/main/java/com/owncloud/android/lib/resources/files/ExistenceCheckRemoteOperation.java @@ -8,25 +8,32 @@ import android.content.Context; -import com.nextcloud.common.NextcloudClient; -import com.nextcloud.operations.HeadMethod; +import com.owncloud.android.lib.common.OwnCloudAnonymousCredentials; +import com.owncloud.android.lib.common.OwnCloudClient; import com.owncloud.android.lib.common.network.RedirectionPath; 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; import org.apache.commons.httpclient.HttpStatus; +import org.apache.commons.httpclient.methods.HeadMethod; /** * Operation to check the existence or absence of a path in a remote server. * * @author David A. Velasco */ -public class ExistenceCheckRemoteOperation extends RemoteOperation { - private static final String TAG = ExistenceCheckRemoteOperation.class.getSimpleName(); +public class ExistenceCheckRemoteOperation extends RemoteOperation { + + /** + * Maximum time to wait for a response from the server in MILLISECONDs. + */ + public static final int TIMEOUT = 50000; - private final String mPath; - private final boolean mSuccessIfAbsent; + private static final String TAG = ExistenceCheckRemoteOperation.class.getSimpleName(); + + private String mPath; + private boolean mSuccessIfAbsent; /** Sequence of redirections followed. Available only after executing the operation */ private RedirectionPath mRedirectionPath = null; @@ -58,21 +65,26 @@ public ExistenceCheckRemoteOperation(String remotePath, Context context, boolean } @Override - public RemoteOperationResult run(NextcloudClient client) { - RemoteOperationResult result; - com.nextcloud.operations.HeadMethod head = null; - boolean previousFollowRedirects = client.getFollowRedirects(); + protected RemoteOperationResult run(OwnCloudClient client) { + RemoteOperationResult result = null; + HeadMethod head = null; + boolean previousFollowRedirects = client.isFollowRedirects(); try { - head = new HeadMethod(client.getFilesDavUri(mPath), false); + if (client.getCredentials() instanceof OwnCloudAnonymousCredentials) { + head = new HeadMethod(client.getDavUri().toString()); + } else { + head = new HeadMethod(client.getFilesDavUri(mPath)); + } client.setFollowRedirects(false); - int status = client.execute(head); + int status = client.executeMethod(head, TIMEOUT, TIMEOUT); if (previousFollowRedirects) { mRedirectionPath = client.followRedirection(head); status = mRedirectionPath.getLastStatus(); } + client.exhaustResponse(head.getResponseBodyAsStream()); boolean success = (status == HttpStatus.SC_OK && !mSuccessIfAbsent) || (status == HttpStatus.SC_NOT_FOUND && mSuccessIfAbsent); - result = new RemoteOperationResult<>( + result = new RemoteOperationResult( success, status, head.getStatusText(), @@ -83,7 +95,7 @@ public RemoteOperationResult run(NextcloudClient client) { "finished with HTTP status " + status + (!success ? "(FAIL)" : "")); } catch (Exception e) { - result = new RemoteOperationResult<>(e); + result = new RemoteOperationResult(e); Log_OC.e(TAG, "Existence check for " + client.getFilesDavUri(mPath) + " targeting for " + (mSuccessIfAbsent ? " absence " : " existence ") + ": " + result.getLogMessage(), result.getException()); diff --git a/library/src/main/java/com/owncloud/android/lib/resources/files/RenameFileRemoteOperation.java b/library/src/main/java/com/owncloud/android/lib/resources/files/RenameFileRemoteOperation.java index fb449b760..79d92543b 100644 --- a/library/src/main/java/com/owncloud/android/lib/resources/files/RenameFileRemoteOperation.java +++ b/library/src/main/java/com/owncloud/android/lib/resources/files/RenameFileRemoteOperation.java @@ -6,7 +6,6 @@ */ package com.owncloud.android.lib.resources.files; -import com.nextcloud.common.NextcloudClient; import com.owncloud.android.lib.common.OwnCloudClient; import com.owncloud.android.lib.common.operations.RemoteOperation; import com.owncloud.android.lib.common.operations.RemoteOperationResult; @@ -76,10 +75,8 @@ protected RemoteOperationResult run(OwnCloudClient client) { } // check if a file with the new name already exists - NextcloudClient nextcloudClient = client.toNextcloudClient(); - RemoteOperationResult existenceResult = new ExistenceCheckRemoteOperation(mNewRemotePath, false) - .execute(nextcloudClient); + .execute(client); if (existenceResult.isSuccess()) { return new RemoteOperationResult(ResultCode.INVALID_OVERWRITE); } diff --git a/library/src/main/java/com/owncloud/android/lib/resources/shares/CreateShareRemoteOperation.java b/library/src/main/java/com/owncloud/android/lib/resources/shares/CreateShareRemoteOperation.java index 128f9db6f..3515c0be9 100644 --- a/library/src/main/java/com/owncloud/android/lib/resources/shares/CreateShareRemoteOperation.java +++ b/library/src/main/java/com/owncloud/android/lib/resources/shares/CreateShareRemoteOperation.java @@ -12,14 +12,13 @@ import android.text.TextUtils; -import com.nextcloud.common.JSONRequestBody; -import com.nextcloud.common.NextcloudClient; -import com.nextcloud.operations.PostMethod; +import com.owncloud.android.lib.common.OwnCloudClient; 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; import org.apache.commons.httpclient.HttpStatus; +import org.apache.commons.httpclient.methods.Utf8PostMethod; import java.io.IOException; import java.util.List; @@ -46,7 +45,7 @@ public class CreateShareRemoteOperation extends RemoteOperation> { private final String password; private final int permissions; private boolean getShareDetails; - private final String note; + private String note; /** * Constructor @@ -130,44 +129,45 @@ public void setGetShareDetails(boolean set) { } @Override - public RemoteOperationResult> run(NextcloudClient client) { + protected RemoteOperationResult> run(OwnCloudClient client) { RemoteOperationResult> result; int status; - PostMethod post = null; + Utf8PostMethod post = null; try { - // request body - JSONRequestBody jsonRequestBody = new JSONRequestBody(); + // Post Method + post = new Utf8PostMethod(client.getBaseUri() + ShareUtils.SHARING_API_PATH); - jsonRequestBody.put(PARAM_PATH, remoteFilePath); - jsonRequestBody.put(PARAM_SHARE_TYPE, Integer.toString(shareType.getValue())); - jsonRequestBody.put(PARAM_SHARE_WITH, shareWith); + post.setRequestHeader(CONTENT_TYPE, FORM_URLENCODED); + + post.addParameter(PARAM_PATH, remoteFilePath); + post.addParameter(PARAM_SHARE_TYPE, Integer.toString(shareType.getValue())); + post.addParameter(PARAM_SHARE_WITH, shareWith); if (publicUpload) { - jsonRequestBody.put(PARAM_PUBLIC_UPLOAD, Boolean.toString(true)); + post.addParameter(PARAM_PUBLIC_UPLOAD, Boolean.toString(true)); } if (password != null && password.length() > 0) { - jsonRequestBody.put(PARAM_PASSWORD, password); + post.addParameter(PARAM_PASSWORD, password); } if (OCShare.NO_PERMISSION != permissions) { - jsonRequestBody.put(PARAM_PERMISSIONS, Integer.toString(permissions)); + post.addParameter(PARAM_PERMISSIONS, Integer.toString(permissions)); } if (!TextUtils.isEmpty(note)) { - jsonRequestBody.put(PARAM_NOTE, note); + post.addParameter(PARAM_NOTE, note); } - // post method - post = new PostMethod(client.getBaseUri() + ShareUtils.SHARING_API_PATH, true, jsonRequestBody.get()); - post.addRequestHeader(CONTENT_TYPE, FORM_URLENCODED); + post.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE); - status = client.execute(post); + status = client.executeMethod(post); if (isSuccess(status)) { String response = post.getResponseBodyAsString(); - ShareToRemoteOperationResultParser parser = - new ShareToRemoteOperationResultParser(new ShareXMLParser()); + ShareToRemoteOperationResultParser parser = new ShareToRemoteOperationResultParser( + new ShareXMLParser() + ); parser.setOneOrMoreSharesRequired(true); parser.setServerBaseUri(client.getBaseUri()); result = parser.parse(response); diff --git a/library/src/main/java/com/owncloud/android/lib/resources/shares/GetShareRemoteOperation.java b/library/src/main/java/com/owncloud/android/lib/resources/shares/GetShareRemoteOperation.java index 76822138c..3ed737b86 100644 --- a/library/src/main/java/com/owncloud/android/lib/resources/shares/GetShareRemoteOperation.java +++ b/library/src/main/java/com/owncloud/android/lib/resources/shares/GetShareRemoteOperation.java @@ -9,19 +9,19 @@ */ package com.owncloud.android.lib.resources.shares; -import com.nextcloud.common.NextcloudClient; -import com.nextcloud.operations.GetMethod; +import static com.owncloud.android.lib.resources.shares.ShareUtils.INCLUDE_TAGS; +import static com.owncloud.android.lib.resources.shares.ShareUtils.SHARING_API_PATH; + +import com.owncloud.android.lib.common.OwnCloudClient; 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; import org.apache.commons.httpclient.HttpStatus; +import org.apache.commons.httpclient.methods.GetMethod; import java.util.List; -import static com.owncloud.android.lib.resources.shares.ShareUtils.INCLUDE_TAGS; -import static com.owncloud.android.lib.resources.shares.ShareUtils.SHARING_API_PATH; - /** * Get the data about a Share resource, known its remote ID. */ @@ -38,18 +38,20 @@ public GetShareRemoteOperation(long remoteId) { @Override - public RemoteOperationResult> run(NextcloudClient client) { + protected RemoteOperationResult> run(OwnCloudClient client) { RemoteOperationResult> result; int status; - // get method - com.nextcloud.operations.GetMethod get = null; + // Get Method + GetMethod get = null; + // Get the response try { - get = new GetMethod(client.getBaseUri() + SHARING_API_PATH + "/" + remoteId, true); + get = new GetMethod(client.getBaseUri() + SHARING_API_PATH + "/" + remoteId); get.setQueryString(INCLUDE_TAGS); + get.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE); - status = client.execute(get); + status = client.executeMethod(get); if (isSuccess(status)) { String response = get.getResponseBodyAsString(); diff --git a/library/src/main/java/com/owncloud/android/lib/resources/shares/GetShareesRemoteOperation.java b/library/src/main/java/com/owncloud/android/lib/resources/shares/GetShareesRemoteOperation.java index 2d7fc1d01..f7f152392 100644 --- a/library/src/main/java/com/owncloud/android/lib/resources/shares/GetShareesRemoteOperation.java +++ b/library/src/main/java/com/owncloud/android/lib/resources/shares/GetShareesRemoteOperation.java @@ -12,13 +12,13 @@ import android.net.Uri; -import com.nextcloud.common.NextcloudClient; -import com.nextcloud.operations.GetMethod; +import com.owncloud.android.lib.common.OwnCloudClient; 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; import org.apache.commons.httpclient.HttpStatus; +import org.apache.commons.httpclient.methods.GetMethod; import org.json.JSONArray; import org.json.JSONObject; @@ -103,7 +103,7 @@ public GetShareesRemoteOperation(String searchString, int page, int perPage) { } @Override - public RemoteOperationResult> run(NextcloudClient client) { + protected RemoteOperationResult> run(OwnCloudClient client) { RemoteOperationResult> result; int status; GetMethod get = null; @@ -120,9 +120,10 @@ public RemoteOperationResult> run(NextcloudClient client) uriBuilder.appendQueryParameter(PARAM_LOOKUP, VALUE_FALSE); // Get Method - get = new GetMethod(uriBuilder.build().toString(), true); + get = new GetMethod(uriBuilder.build().toString()); + get.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE); - status = client.execute(get); + status = client.executeMethod(get); if (isSuccess(status)) { String response = get.getResponseBodyAsString(); @@ -204,7 +205,12 @@ public RemoteOperationResult> run(NextcloudClient client) result = new RemoteOperationResult<>(false, get); String response = get.getResponseBodyAsString(); Log_OC.e(TAG, "Failed response while getting users/groups from the server"); - Log_OC.e(TAG, "*** status code: " + status + "; response message: " + response); + + if (response != null) { + Log_OC.e(TAG, "*** status code: " + status + "; response message: " + response); + } else { + Log_OC.e(TAG, "*** status code: " + status); + } } } catch (Exception e) { diff --git a/library/src/main/java/com/owncloud/android/lib/resources/shares/GetSharesForFileRemoteOperation.java b/library/src/main/java/com/owncloud/android/lib/resources/shares/GetSharesForFileRemoteOperation.java index 6faa01937..620a8fd1f 100644 --- a/library/src/main/java/com/owncloud/android/lib/resources/shares/GetSharesForFileRemoteOperation.java +++ b/library/src/main/java/com/owncloud/android/lib/resources/shares/GetSharesForFileRemoteOperation.java @@ -10,23 +10,21 @@ */ package com.owncloud.android.lib.resources.shares; -import com.nextcloud.common.NextcloudClient; -import com.nextcloud.operations.GetMethod; +import com.owncloud.android.lib.common.OwnCloudClient; 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; import org.apache.commons.httpclient.HttpStatus; - -import java.util.List; -import java.util.Map; +import org.apache.commons.httpclient.NameValuePair; +import org.apache.commons.httpclient.methods.GetMethod; /** * Provide a list shares for a specific file. * The input is the full path of the desired file. * The output is a list of everyone who has the file shared with them. */ -public class GetSharesForFileRemoteOperation extends RemoteOperation> { +public class GetSharesForFileRemoteOperation extends RemoteOperation { private static final String TAG = GetSharesForFileRemoteOperation.class.getSimpleName(); @@ -34,9 +32,9 @@ public class GetSharesForFileRemoteOperation extends RemoteOperation> run(NextcloudClient client) { - RemoteOperationResult> result; - int status; + protected RemoteOperationResult run(OwnCloudClient client) { + RemoteOperationResult result = null; + int status = -1; GetMethod get = null; try { - // get method - get = new com.nextcloud.operations.GetMethod(client.getBaseUri() + ShareUtils.SHARING_API_PATH, true); + // Get Method + get = new GetMethod(client.getBaseUri() + ShareUtils.SHARING_API_PATH); + + // Add Parameters to Get Method + get.setQueryString(new NameValuePair[]{ + new NameValuePair(PARAM_PATH, mRemoteFilePath), + new NameValuePair(PARAM_RESHARES, String.valueOf(mReshares)), + new NameValuePair(PARAM_SUBFILES, String.valueOf(mSubfiles)) //, + //new NameValuePair("shared_with_me", "true") + }); - // add parameters to get method - get.setQueryString(Map.of( - PARAM_PATH, mRemoteFilePath, - PARAM_RESHARES, String.valueOf(mReshares), - PARAM_SUBFILES, String.valueOf(mSubfiles) - )); + get.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE); - status = client.execute(get); + status = client.executeMethod(get); if (isSuccess(status)) { String response = get.getResponseBodyAsString(); @@ -85,15 +87,15 @@ public RemoteOperationResult> run(NextcloudClient client) { result = parser.parse(response); if (result.isSuccess()) { - Log_OC.d(TAG, "Got " + result.getResultData().size() + " shares"); + Log_OC.d(TAG, "Got " + result.getData().size() + " shares"); } } else { - result = new RemoteOperationResult<>(false, get); + result = new RemoteOperationResult(false, get); } } catch (Exception e) { - result = new RemoteOperationResult<>(e); + result = new RemoteOperationResult(e); Log_OC.e(TAG, "Exception while getting shares", e); } finally { diff --git a/library/src/main/java/com/owncloud/android/lib/resources/shares/GetSharesRemoteOperation.java b/library/src/main/java/com/owncloud/android/lib/resources/shares/GetSharesRemoteOperation.java index a9993827f..586b7fde0 100644 --- a/library/src/main/java/com/owncloud/android/lib/resources/shares/GetSharesRemoteOperation.java +++ b/library/src/main/java/com/owncloud/android/lib/resources/shares/GetSharesRemoteOperation.java @@ -10,18 +10,17 @@ */ package com.owncloud.android.lib.resources.shares; -import com.nextcloud.common.NextcloudClient; -import com.nextcloud.operations.GetMethod; +import static com.owncloud.android.lib.resources.shares.ShareUtils.INCLUDE_TAGS; + +import com.owncloud.android.lib.common.OwnCloudClient; 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; import org.apache.commons.httpclient.HttpStatus; +import org.apache.commons.httpclient.methods.GetMethod; import java.util.List; -import java.util.Map; - -import static com.owncloud.android.lib.resources.shares.ShareUtils.INCLUDE_TAGS; /** * Get the data from the server about ALL the known shares owned by the requester. @@ -29,7 +28,7 @@ public class GetSharesRemoteOperation extends RemoteOperation> { private static final String TAG = GetSharesRemoteOperation.class.getSimpleName(); - private final boolean sharedWithMe; + private boolean sharedWithMe = false; public GetSharesRemoteOperation() { this(false); @@ -40,23 +39,24 @@ public GetSharesRemoteOperation(boolean sharedWithMe) { } @Override - public RemoteOperationResult> run(NextcloudClient client) { + protected RemoteOperationResult> run(OwnCloudClient client) { RemoteOperationResult> result; int status; // Get Method - com.nextcloud.operations.GetMethod get = null; + GetMethod get = null; // Get the response try { - get = new GetMethod(client.getBaseUri() + ShareUtils.SHARING_API_PATH, true); + get = new GetMethod(client.getBaseUri() + ShareUtils.SHARING_API_PATH); get.setQueryString(INCLUDE_TAGS); + get.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE); if (sharedWithMe) { - get.setQueryString(Map.of("shared_with_me", "true")); + get.setQueryString("shared_with_me=true"); } - status = client.execute(get); + status = client.executeMethod(get); if (isSuccess(status)) { String response = get.getResponseBodyAsString(); diff --git a/library/src/main/java/com/owncloud/android/lib/resources/shares/RemoveShareRemoteOperation.java b/library/src/main/java/com/owncloud/android/lib/resources/shares/RemoveShareRemoteOperation.java index e1866e644..b0c33bc35 100644 --- a/library/src/main/java/com/owncloud/android/lib/resources/shares/RemoveShareRemoteOperation.java +++ b/library/src/main/java/com/owncloud/android/lib/resources/shares/RemoveShareRemoteOperation.java @@ -10,25 +10,23 @@ */ package com.owncloud.android.lib.resources.shares; -import com.nextcloud.common.NextcloudClient; -import com.nextcloud.operations.DeleteMethod; +import com.owncloud.android.lib.common.OwnCloudClient; 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; import org.apache.commons.httpclient.HttpStatus; - -import java.util.List; +import org.apache.jackrabbit.webdav.client.methods.DeleteMethod; /** * Remove a share */ -public class RemoveShareRemoteOperation extends RemoteOperation> { +public class RemoveShareRemoteOperation extends RemoteOperation { private static final String TAG = RemoveShareRemoteOperation.class.getSimpleName(); - private final long remoteShareId; + private long remoteShareId; /** * Constructor @@ -42,14 +40,16 @@ public RemoveShareRemoteOperation(long remoteShareId) { } @Override - public RemoteOperationResult> run(NextcloudClient client) { - RemoteOperationResult> result; - com.nextcloud.operations.DeleteMethod delete = null; + protected RemoteOperationResult run(OwnCloudClient client) { + RemoteOperationResult result; + DeleteMethod delete = null; try { - delete = new DeleteMethod(client.getBaseUri() + ShareUtils.SHARING_API_PATH + "/" + remoteShareId, true); + delete = new DeleteMethod(client.getBaseUri() + ShareUtils.SHARING_API_PATH + "/" + remoteShareId); + + delete.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE); - int status = client.execute(delete); + int status = client.executeMethod(delete); if (isSuccess(status)) { String response = delete.getResponseBodyAsString(); @@ -63,10 +63,10 @@ public RemoteOperationResult> run(NextcloudClient client) { Log_OC.d(TAG, "Unshare " + remoteShareId + ": " + result.getLogMessage()); } else { - result = new RemoteOperationResult<>(false, delete); + result = new RemoteOperationResult(false, delete); } } catch (Exception e) { - result = new RemoteOperationResult<>(e); + result = new RemoteOperationResult(e); Log_OC.e(TAG, "Unshare Link Exception " + result.getLogMessage(), e); } finally { diff --git a/library/src/main/java/com/owncloud/android/lib/resources/shares/ShareUtils.java b/library/src/main/java/com/owncloud/android/lib/resources/shares/ShareUtils.java index 0cead8dba..eb201184b 100644 --- a/library/src/main/java/com/owncloud/android/lib/resources/shares/ShareUtils.java +++ b/library/src/main/java/com/owncloud/android/lib/resources/shares/ShareUtils.java @@ -9,8 +9,6 @@ */ package com.owncloud.android.lib.resources.shares; -import java.util.Map; - /** * Contains Constants for Share Operation * @@ -23,7 +21,7 @@ public class ShareUtils { // OCS Route public static final String SHARING_API_PATH = "/ocs/v2.php/apps/files_sharing/api/v1/shares"; - public static final Map INCLUDE_TAGS = Map.of("include_tags", "true"); + public static final String INCLUDE_TAGS = "include_tags=true"; // String to build the link with the token of a share: public static final String SHARING_LINK_PATH_AFTER_VERSION_8 = "/index.php/s/"; diff --git a/library/src/main/java/com/owncloud/android/lib/resources/shares/UpdateShareRemoteOperation.java b/library/src/main/java/com/owncloud/android/lib/resources/shares/UpdateShareRemoteOperation.java index 402504bd4..5cbef9655 100644 --- a/library/src/main/java/com/owncloud/android/lib/resources/shares/UpdateShareRemoteOperation.java +++ b/library/src/main/java/com/owncloud/android/lib/resources/shares/UpdateShareRemoteOperation.java @@ -13,14 +13,14 @@ import android.net.Uri; import android.util.Pair; -import com.nextcloud.common.JSONRequestBody; -import com.nextcloud.common.NextcloudClient; -import com.nextcloud.operations.PutMethod; +import com.owncloud.android.lib.common.OwnCloudClient; 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; import org.apache.commons.httpclient.HttpStatus; +import org.apache.commons.httpclient.methods.PutMethod; +import org.apache.commons.httpclient.methods.StringRequestEntity; import java.net.URLEncoder; import java.text.DateFormat; @@ -36,7 +36,7 @@ * * Allow updating several parameters, triggering a request to the server per parameter. */ -public class UpdateShareRemoteOperation extends RemoteOperation> { +public class UpdateShareRemoteOperation extends RemoteOperation { private static final String TAG = GetShareRemoteOperation.class.getSimpleName(); @@ -47,12 +47,14 @@ public class UpdateShareRemoteOperation extends RemoteOperation> { private static final String PARAM_HIDE_DOWNLOAD = "hideDownload"; private static final String PARAM_LABEL = "label"; private static final String FORMAT_EXPIRATION_DATE = "yyyy-MM-dd"; + private static final String ENTITY_CONTENT_TYPE = "application/x-www-form-urlencoded"; + private static final String ENTITY_CHARSET = "UTF-8"; /** * Identifier of the share to update */ - private final long remoteId; + private long remoteId; /** * Password to set for the public link @@ -140,7 +142,7 @@ public void setNote(String note) { } @Override - public RemoteOperationResult> run(NextcloudClient client) { + protected RemoteOperationResult> run(OwnCloudClient client) { RemoteOperationResult> result = null; int status; @@ -180,7 +182,7 @@ public RemoteOperationResult> run(NextcloudClient client) { } /// perform required PUT requests - com.nextcloud.operations.PutMethod put = null; + PutMethod put = null; String uriString; try { @@ -194,11 +196,15 @@ public RemoteOperationResult> run(NextcloudClient client) { if (put != null) { put.releaseConnection(); } - JSONRequestBody jsonRequestBody = new JSONRequestBody(parameter.first, parameter.second); - - put = new PutMethod(uriString, true, jsonRequestBody.get()); - - status = client.execute(put); + put = new PutMethod(uriString); + put.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE); + put.setRequestEntity(new StringRequestEntity( + parameter.first + "=" + parameter.second, + ENTITY_CONTENT_TYPE, + ENTITY_CHARSET + )); + + status = client.executeMethod(put); if (status == HttpStatus.SC_OK || status == HttpStatus.SC_BAD_REQUEST) { String response = put.getResponseBodyAsString(); diff --git a/library/src/main/java/com/owncloud/android/lib/resources/status/GetStatusRemoteOperation.java b/library/src/main/java/com/owncloud/android/lib/resources/status/GetStatusRemoteOperation.java index c204dc62e..dbc828378 100644 --- a/library/src/main/java/com/owncloud/android/lib/resources/status/GetStatusRemoteOperation.java +++ b/library/src/main/java/com/owncloud/android/lib/resources/status/GetStatusRemoteOperation.java @@ -15,18 +15,21 @@ import android.net.ConnectivityManager; import android.net.Uri; -import com.nextcloud.common.NextcloudClient; -import com.nextcloud.operations.GetMethod; +import com.owncloud.android.lib.common.OwnCloudClient; +import com.owncloud.android.lib.common.OwnCloudClientManagerFactory; import com.owncloud.android.lib.common.accounts.AccountUtils; 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; import org.apache.commons.httpclient.HttpStatus; +import org.apache.commons.httpclient.methods.GetMethod; +import org.apache.commons.httpclient.params.HttpMethodParams; +import org.apache.commons.httpclient.params.HttpParams; import org.json.JSONException; import org.json.JSONObject; -import kotlin.Pair; +import java.util.ArrayList; /** * Checks if the server is valid and if the server supports the Share API @@ -34,7 +37,13 @@ * @author David A. Velasco * @author masensio */ -public class GetStatusRemoteOperation extends RemoteOperation> { +public class GetStatusRemoteOperation extends RemoteOperation { + + /** + * Maximum time to wait for a response from the server when the connection is being tested, in MILLISECONDs. + */ + private static final int TRY_CONNECTION_TIMEOUT = 50000; + private static final String TAG = GetStatusRemoteOperation.class.getSimpleName(); private static final String NODE_INSTALLED = "installed"; @@ -44,24 +53,28 @@ public class GetStatusRemoteOperation extends RemoteOperation> mLatestResult; - private final Context mContext; + private RemoteOperationResult mLatestResult; + private Context mContext; public GetStatusRemoteOperation(Context context) { mContext = context; } - private boolean tryConnection(NextcloudClient client) { + private boolean tryConnection(OwnCloudClient client) { boolean retval = false; - com.nextcloud.operations.GetMethod get = null; - String baseUrlSt = String.valueOf(client.getBaseUri()); + GetMethod get = null; + String baseUrlSt = client.getBaseUri().toString(); try { - get = new com.nextcloud.operations.GetMethod(baseUrlSt + AccountUtils.STATUS_PATH, false); + get = new GetMethod(baseUrlSt + AccountUtils.STATUS_PATH); + + HttpParams params = HttpMethodParams.getDefaultParams(); + params.setParameter(HttpMethodParams.USER_AGENT, OwnCloudClientManagerFactory.getUserAgent()); + get.getParams().setDefaults(params); client.setFollowRedirects(false); boolean isRedirectToNonSecureConnection = false; - int status = client.execute(get); - mLatestResult = new RemoteOperationResult<>((status == HttpStatus.SC_OK), get); + int status = client.executeMethod(get, TRY_CONNECTION_TIMEOUT, TRY_CONNECTION_TIMEOUT); + mLatestResult = new RemoteOperationResult((status == HttpStatus.SC_OK), get); String redirectedLocation = mLatestResult.getRedirectedLocation(); while (redirectedLocation != null && redirectedLocation.length() > 0 @@ -72,9 +85,9 @@ private boolean tryConnection(NextcloudClient client) { redirectedLocation.startsWith(PROTOCOL_HTTP) ); get.releaseConnection(); - get = new GetMethod(redirectedLocation, false); - status = client.execute(get); - mLatestResult = new RemoteOperationResult<>((status == HttpStatus.SC_OK), get); + get = new GetMethod(redirectedLocation); + status = client.executeMethod(get, TRY_CONNECTION_TIMEOUT, TRY_CONNECTION_TIMEOUT); + mLatestResult = new RemoteOperationResult((status == HttpStatus.SC_OK), get); redirectedLocation = mLatestResult.getRedirectedLocation(); } @@ -83,7 +96,7 @@ private boolean tryConnection(NextcloudClient client) { if (status == HttpStatus.SC_OK) { JSONObject json = new JSONObject(response); if (!json.getBoolean(NODE_INSTALLED)) { - mLatestResult = new RemoteOperationResult<>( + mLatestResult = new RemoteOperationResult( RemoteOperationResult.ResultCode.INSTANCE_NOT_CONFIGURED); } else { boolean extendedSupport = false; @@ -95,22 +108,24 @@ private boolean tryConnection(NextcloudClient client) { OwnCloudVersion ocVersion = new OwnCloudVersion(version); if (!ocVersion.isVersionValid()) { - mLatestResult = new RemoteOperationResult<>(RemoteOperationResult.ResultCode.BAD_OC_VERSION); + mLatestResult = new RemoteOperationResult(RemoteOperationResult.ResultCode.BAD_OC_VERSION); } else { // success if (isRedirectToNonSecureConnection) { - mLatestResult = new RemoteOperationResult<>( + mLatestResult = new RemoteOperationResult( RemoteOperationResult.ResultCode.OK_REDIRECT_TO_NON_SECURE_CONNECTION); } else { - mLatestResult = new RemoteOperationResult<>( + mLatestResult = new RemoteOperationResult( baseUrlSt.startsWith(PROTOCOL_HTTPS) ? RemoteOperationResult.ResultCode.OK_SSL : RemoteOperationResult.ResultCode.OK_NO_SSL ); } - Pair data = new Pair<>(ocVersion, extendedSupport); - mLatestResult.setResultData(data); + ArrayList data = new ArrayList<>(); + data.add(ocVersion); + data.add(extendedSupport); + mLatestResult.setData(data); retval = true; } } @@ -119,22 +134,22 @@ private boolean tryConnection(NextcloudClient client) { JSONObject json = new JSONObject(response); if (json.getInt("code") == UNTRUSTED_DOMAIN_ERROR_CODE) { - mLatestResult = new RemoteOperationResult<>(RemoteOperationResult.ResultCode.UNTRUSTED_DOMAIN); + mLatestResult = new RemoteOperationResult(RemoteOperationResult.ResultCode.UNTRUSTED_DOMAIN); } else { - mLatestResult = new RemoteOperationResult<>(false, get); + mLatestResult = new RemoteOperationResult(false, status, get.getResponseHeaders()); } } catch (JSONException e) { - mLatestResult = new RemoteOperationResult<>(false, get); + mLatestResult = new RemoteOperationResult(false, status, get.getResponseHeaders()); } } else { - mLatestResult = new RemoteOperationResult<>(false, get); + mLatestResult = new RemoteOperationResult(false, status, get.getResponseHeaders()); } } catch (JSONException e) { - mLatestResult = new RemoteOperationResult<>(RemoteOperationResult.ResultCode.INSTANCE_NOT_CONFIGURED); + mLatestResult = new RemoteOperationResult(RemoteOperationResult.ResultCode.INSTANCE_NOT_CONFIGURED); } catch (Exception e) { - mLatestResult = new RemoteOperationResult<>(e); + mLatestResult = new RemoteOperationResult(e); } finally { if (get != null) @@ -156,16 +171,18 @@ private boolean tryConnection(NextcloudClient client) { } private boolean isOnline() { - ConnectivityManager cm = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE); - return cm != null && cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isConnectedOrConnecting(); + ConnectivityManager cm = (ConnectivityManager) mContext + .getSystemService(Context.CONNECTIVITY_SERVICE); + return cm != null && cm.getActiveNetworkInfo() != null + && cm.getActiveNetworkInfo().isConnectedOrConnecting(); } @Override - public RemoteOperationResult> run(NextcloudClient client) { + protected RemoteOperationResult run(OwnCloudClient client) { if (!isOnline()) { - return new RemoteOperationResult<>(RemoteOperationResult.ResultCode.NO_NETWORK_CONNECTION); + return new RemoteOperationResult(RemoteOperationResult.ResultCode.NO_NETWORK_CONNECTION); } - String baseUriStr = String.valueOf(client.getBaseUri()); + String baseUriStr = client.getBaseUri().toString(); if (baseUriStr.startsWith(PROTOCOL_HTTP) || baseUriStr.startsWith(PROTOCOL_HTTPS)) { tryConnection(client);