diff --git a/gradle/verification-metadata.xml b/gradle/verification-metadata.xml index 0147ebad8..09b3cca24 100644 --- a/gradle/verification-metadata.xml +++ b/gradle/verification-metadata.xml @@ -3639,7 +3639,10 @@ - + + + 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/common/OwnCloudClient.java b/library/src/main/java/com/owncloud/android/lib/common/OwnCloudClient.java index 3d10638ee..af3e9d100 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,15 +68,19 @@ public class OwnCloudClient extends HttpClient { private AdvancedX509KeyManager keyManager; + private Context context; + /** * Constructor */ + @SuppressFBWarnings("EI2") public OwnCloudClient(Uri baseUri, HttpConnectionManager connectionMgr, Context context) { super(connectionMgr); 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 +454,19 @@ public OwnCloudCredentials getCredentials() { public void setFollowRedirects(boolean followRedirects) { this.followRedirects = followRedirects; } + + @SuppressFBWarnings("EI") + 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/common/accounts/AccountUtils.java b/library/src/main/java/com/owncloud/android/lib/common/accounts/AccountUtils.java index f67e7fc58..b6d3b0331 100644 --- a/library/src/main/java/com/owncloud/android/lib/common/accounts/AccountUtils.java +++ b/library/src/main/java/com/owncloud/android/lib/common/accounts/AccountUtils.java @@ -30,6 +30,9 @@ import java.io.IOException; +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; + +@SuppressFBWarnings("FCCD") public class AccountUtils { private static final String TAG = AccountUtils.class.getSimpleName(); 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..0e3200d7b 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,6 +12,9 @@ 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; @@ -45,7 +48,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 @@ -166,7 +169,7 @@ protected RemoteOperationResult> run(OwnCloudClient client) { String response = post.getResponseBodyAsString(); ShareToRemoteOperationResultParser parser = new ShareToRemoteOperationResultParser( - new ShareXMLParser() + new ShareXMLParser() ); parser.setOneOrMoreSharesRequired(true); parser.setServerBaseUri(client.getBaseUri()); @@ -194,6 +197,72 @@ protected RemoteOperationResult> run(OwnCloudClient client) { } return result; } + + @Override + public RemoteOperationResult> run(NextcloudClient client) { + RemoteOperationResult> result; + int status; + + PostMethod post = null; + + try { + // request body + JSONRequestBody jsonRequestBody = new JSONRequestBody(); + + jsonRequestBody.put(PARAM_PATH, remoteFilePath); + jsonRequestBody.put(PARAM_SHARE_TYPE, Integer.toString(shareType.getValue())); + jsonRequestBody.put(PARAM_SHARE_WITH, shareWith); + if (publicUpload) { + jsonRequestBody.put(PARAM_PUBLIC_UPLOAD, Boolean.toString(true)); + } + if (password != null && password.length() > 0) { + jsonRequestBody.put(PARAM_PASSWORD, password); + } + if (OCShare.NO_PERMISSION != permissions) { + jsonRequestBody.put(PARAM_PERMISSIONS, Integer.toString(permissions)); + } + + if (!TextUtils.isEmpty(note)) { + jsonRequestBody.put(PARAM_NOTE, note); + } + + // post method + post = new PostMethod(client.getBaseUri() + ShareUtils.SHARING_API_PATH, true, jsonRequestBody.get()); + post.addRequestHeader(CONTENT_TYPE, FORM_URLENCODED); + + status = client.execute(post); + + if (isSuccess(status)) { + String response = post.getResponseBodyAsString(); + + ShareToRemoteOperationResultParser parser = + new ShareToRemoteOperationResultParser(new ShareXMLParser()); + parser.setOneOrMoreSharesRequired(true); + parser.setServerBaseUri(client.getBaseUri()); + result = parser.parse(response); + + if (result.isSuccess() && getShareDetails) { + // retrieve more info - POST only returns the index of the new share + OCShare emptyShare = result.getResultData().get(0); + GetShareRemoteOperation getInfo = new GetShareRemoteOperation(emptyShare.getRemoteId()); + result = getInfo.execute(client); + } + + } else { + result = new RemoteOperationResult<>(false, post); + } + + } catch (IOException e) { + result = new RemoteOperationResult<>(e); + Log_OC.e(TAG, "Exception while Creating New Share", e); + + } finally { + if (post != null) { + post.releaseConnection(); + } + } + return result; + } private boolean isSuccess(int status) { return status == HttpStatus.SC_OK || status == HttpStatus.SC_FORBIDDEN; 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..7fa2c8ec8 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 @@ -10,15 +10,17 @@ 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.INCLUDE_TAGS_OC; import static com.owncloud.android.lib.resources.shares.ShareUtils.SHARING_API_PATH; +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 java.util.List; @@ -36,23 +38,63 @@ public GetShareRemoteOperation(long remoteId) { this.remoteId = remoteId; } - @Override protected RemoteOperationResult> run(OwnCloudClient client) { RemoteOperationResult> result; int status; // Get Method - GetMethod get = null; + org.apache.commons.httpclient.methods.GetMethod get = null; // Get the response try { - get = new GetMethod(client.getBaseUri() + SHARING_API_PATH + "/" + remoteId); - get.setQueryString(INCLUDE_TAGS); + get = new org.apache.commons.httpclient.methods.GetMethod(client.getBaseUri() + SHARING_API_PATH + "/" + remoteId); + get.setQueryString(INCLUDE_TAGS_OC); get.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE); status = client.executeMethod(get); + if (isSuccess(status)) { + String response = get.getResponseBodyAsString(); + + // Parse xml response and obtain the list of shares + ShareToRemoteOperationResultParser parser = new ShareToRemoteOperationResultParser( + new ShareXMLParser() + ); + parser.setOneOrMoreSharesRequired(true); + parser.setServerBaseUri(client.getBaseUri()); + result = parser.parse(response); + + } else { + result = new RemoteOperationResult<>(false, get); + } + + } catch (Exception e) { + result = new RemoteOperationResult<>(e); + Log_OC.e(TAG, "Exception while getting remote shares ", e); + + } finally { + if (get != null) { + get.releaseConnection(); + } + } + return result; + } + + @Override + public RemoteOperationResult> run(NextcloudClient client) { + RemoteOperationResult> result; + int status; + + // get method + com.nextcloud.operations.GetMethod get = null; + + try { + get = new GetMethod(client.getBaseUri() + SHARING_API_PATH + "/" + remoteId, true); + get.setQueryString(INCLUDE_TAGS); + + 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..4e5c40745 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,18 +12,21 @@ 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; import java.util.ArrayList; +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; + /** * Created by masensio on 08/10/2015. * @@ -103,10 +106,11 @@ public GetShareesRemoteOperation(String searchString, int page, int perPage) { } @Override + @SuppressFBWarnings("EXS") protected RemoteOperationResult> run(OwnCloudClient client) { RemoteOperationResult> result; int status; - GetMethod get = null; + org.apache.commons.httpclient.methods.GetMethod get = null; try { Uri requestUri = client.getBaseUri(); @@ -120,11 +124,139 @@ protected RemoteOperationResult> run(OwnCloudClient client uriBuilder.appendQueryParameter(PARAM_LOOKUP, VALUE_FALSE); // Get Method - get = new GetMethod(uriBuilder.build().toString()); + get = new org.apache.commons.httpclient.methods.GetMethod(uriBuilder.build().toString()); get.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE); status = client.executeMethod(get); + if (isSuccess(status)) { + String response = get.getResponseBodyAsString(); + Log_OC.d(TAG, "Successful response: " + response); + + // Parse the response + JSONObject respJSON = new JSONObject(response); + JSONObject respOCS = respJSON.getJSONObject(NODE_OCS); + JSONObject respData = respOCS.getJSONObject(NODE_DATA); + JSONObject respExact = respData.getJSONObject(NODE_EXACT); + + JSONArray respExactUsers = respExact.getJSONArray(NODE_USERS); + JSONArray respExactGroups = respExact.getJSONArray(NODE_GROUPS); + JSONArray respExactRemotes = respExact.getJSONArray(NODE_REMOTES); + JSONArray respExactCircles; + if (respExact.has(NODE_CIRCLES)) { + respExactCircles = respExact.getJSONArray(NODE_CIRCLES); + } else { + respExactCircles = new JSONArray(); + } + + JSONArray respExactRooms; + if (respExact.has(NODE_ROOMS)) { + respExactRooms = respExact.getJSONArray(NODE_ROOMS); + } else { + respExactRooms = new JSONArray(); + } + + JSONArray respExactEmails = respExact.getJSONArray(NODE_EMAILS); + JSONArray respPartialUsers = respData.getJSONArray(NODE_USERS); + JSONArray respPartialGroups = respData.getJSONArray(NODE_GROUPS); + JSONArray respPartialRemotes = respData.getJSONArray(NODE_REMOTES); + JSONArray respPartialCircles; + + if (respData.has(NODE_CIRCLES)) { + respPartialCircles = respData.getJSONArray(NODE_CIRCLES); + } else { + respPartialCircles = new JSONArray(); + } + + JSONArray respPartialRooms; + + if (respData.has(NODE_ROOMS)) { + respPartialRooms = respData.getJSONArray(NODE_ROOMS); + } else { + respPartialRooms = new JSONArray(); + } + + JSONArray[] jsonResults = { + respExactUsers, + respExactGroups, + respExactRemotes, + respExactRooms, + respExactEmails, + respExactCircles, + respPartialUsers, + respPartialGroups, + respPartialRemotes, + respPartialRooms, + respPartialCircles + }; + + ArrayList data = new ArrayList<>(); + for (JSONArray jsonResult : jsonResults) { + for (int j = 0; j < jsonResult.length(); j++) { + JSONObject jsonObject = jsonResult.getJSONObject(j); + data.add(jsonObject); + Log_OC.d(TAG, "*** Added item: " + jsonObject.getString(PROPERTY_LABEL)); + } + } + + // Result + result = new RemoteOperationResult<>(true, get); + result.setResultData(data); + + Log_OC.d(TAG, "*** Get Users or groups completed"); + + } else { + 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); + } + } + + } catch (RuntimeException e) { + throw e; + } catch (Exception e) { + result = new RemoteOperationResult<>(e); + Log_OC.e(TAG, "Exception while getting users/groups", e); + + } finally { + if (get != null) { + get.releaseConnection(); + } + } + return result; + } + + @Override + public RemoteOperationResult> run(NextcloudClient client) { + RemoteOperationResult> result; + int status; + GetMethod get = null; + + try { + Uri requestUri = client.getBaseUri(); + if (requestUri == null) { + throw new IllegalArgumentException("requestUri may not be null!"); + } + + Uri.Builder uriBuilder = requestUri.buildUpon(); + uriBuilder.appendEncodedPath(OCS_ROUTE); + uriBuilder.appendQueryParameter(PARAM_FORMAT, VALUE_FORMAT); + uriBuilder.appendQueryParameter(PARAM_ITEM_TYPE, VALUE_ITEM_TYPE); + uriBuilder.appendQueryParameter(PARAM_SEARCH, searchString); + uriBuilder.appendQueryParameter(PARAM_PAGE, String.valueOf(page)); + uriBuilder.appendQueryParameter(PARAM_PER_PAGE, String.valueOf(perPage)); + uriBuilder.appendQueryParameter(PARAM_LOOKUP, VALUE_FALSE); + + // Get Method + get = new GetMethod(uriBuilder.build().toString(), true); + + status = client.execute(get); + if (isSuccess(status)) { String response = get.getResponseBodyAsString(); Log_OC.d(TAG, "Successful response: " + response); @@ -205,12 +337,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..e1cbbaab8 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,6 +10,8 @@ */ 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; @@ -17,14 +19,16 @@ 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 +36,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,8 +50,7 @@ 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; @@ -58,18 +61,18 @@ protected RemoteOperationResult run(OwnCloudClient client) { RemoteOperationResult result = null; int status = -1; - GetMethod get = null; + org.apache.commons.httpclient.methods.GetMethod get = null; try { // Get Method - get = new GetMethod(client.getBaseUri() + ShareUtils.SHARING_API_PATH); + get = new org.apache.commons.httpclient.methods.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") + new NameValuePair(PARAM_SUBFILES, String.valueOf(mSubfiles)) //, + //new NameValuePair("shared_with_me", "true") }); get.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE); @@ -106,6 +109,56 @@ protected RemoteOperationResult run(OwnCloudClient client) { return result; } + @Override + public RemoteOperationResult> run(NextcloudClient client) { + RemoteOperationResult> result; + int status; + + GetMethod get = null; + + try { + // get method + get = new com.nextcloud.operations.GetMethod(client.getBaseUri() + ShareUtils.SHARING_API_PATH, true); + + // add parameters to get method + get.setQueryString(Map.of( + PARAM_PATH, mRemoteFilePath, + PARAM_RESHARES, String.valueOf(mReshares), + PARAM_SUBFILES, String.valueOf(mSubfiles) + )); + + status = client.execute(get); + + if (isSuccess(status)) { + String response = get.getResponseBodyAsString(); + + // Parse xml response and obtain the list of shares + ShareToRemoteOperationResultParser parser = new ShareToRemoteOperationResultParser( + new ShareXMLParser() + ); + parser.setServerBaseUri(client.getBaseUri()); + result = parser.parse(response); + + if (result.isSuccess()) { + Log_OC.d(TAG, "Got " + result.getResultData().size() + " shares"); + } + + } else { + result = new RemoteOperationResult<>(false, get); + } + + } catch (Exception e) { + result = new RemoteOperationResult<>(e); + Log_OC.e(TAG, "Exception while getting shares", e); + + } finally { + if (get != null) { + get.releaseConnection(); + } + } + return result; + } + private boolean isSuccess(int status) { return (status == HttpStatus.SC_OK); } 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..acbf2b01e 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,17 +10,20 @@ */ 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.INCLUDE_TAGS_OC; + +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 java.util.List; - -import static com.owncloud.android.lib.resources.shares.ShareUtils.INCLUDE_TAGS; +import java.util.Map; /** * Get the data from the server about ALL the known shares owned by the requester. @@ -28,7 +31,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); @@ -44,12 +47,12 @@ protected RemoteOperationResult> run(OwnCloudClient client) { int status; // Get Method - GetMethod get = null; + org.apache.commons.httpclient.methods.GetMethod get = null; // Get the response try { - get = new GetMethod(client.getBaseUri() + ShareUtils.SHARING_API_PATH); - get.setQueryString(INCLUDE_TAGS); + get = new org.apache.commons.httpclient.methods.GetMethod(client.getBaseUri() + ShareUtils.SHARING_API_PATH); + get.setQueryString(INCLUDE_TAGS_OC); get.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE); if (sharedWithMe) { @@ -58,6 +61,50 @@ protected RemoteOperationResult> run(OwnCloudClient client) { status = client.executeMethod(get); + if (isSuccess(status)) { + String response = get.getResponseBodyAsString(); + + // Parse xml response and obtain the list of shares + ShareToRemoteOperationResultParser parser = new ShareToRemoteOperationResultParser( + new ShareXMLParser() + ); + parser.setServerBaseUri(client.getBaseUri()); + result = parser.parse(response); + } else { + result = new RemoteOperationResult<>(false, get); + } + + } catch (Exception e) { + result = new RemoteOperationResult<>(e); + Log_OC.e(TAG, "Exception while getting remote shares ", e); + + } finally { + if (get != null) { + get.releaseConnection(); + } + } + return result; + } + + @Override + public RemoteOperationResult> run(NextcloudClient client) { + RemoteOperationResult> result; + int status; + + // Get Method + com.nextcloud.operations.GetMethod get = null; + + // Get the response + try { + get = new GetMethod(client.getBaseUri() + ShareUtils.SHARING_API_PATH, true); + get.setQueryString(INCLUDE_TAGS); + + if (sharedWithMe) { + get.setQueryString(Map.of("shared_with_me", "true")); + } + + 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..e562e7884 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,26 @@ */ 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 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 @@ -42,10 +45,10 @@ public RemoveShareRemoteOperation(long remoteShareId) { @Override protected RemoteOperationResult run(OwnCloudClient client) { RemoteOperationResult result; - DeleteMethod delete = null; + org.apache.jackrabbit.webdav.client.methods.DeleteMethod delete = null; try { - delete = new DeleteMethod(client.getBaseUri() + ShareUtils.SHARING_API_PATH + "/" + remoteShareId); + delete = new org.apache.jackrabbit.webdav.client.methods.DeleteMethod(client.getBaseUri() + ShareUtils.SHARING_API_PATH + "/" + remoteShareId); delete.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE); @@ -76,6 +79,41 @@ protected RemoteOperationResult run(OwnCloudClient client) { return result; } + @Override + public RemoteOperationResult> run(NextcloudClient client) { + RemoteOperationResult> result; + com.nextcloud.operations.DeleteMethod delete = null; + + try { + delete = new DeleteMethod(client.getBaseUri() + ShareUtils.SHARING_API_PATH + "/" + remoteShareId, true); + + int status = client.execute(delete); + + if (isSuccess(status)) { + String response = delete.getResponseBodyAsString(); + + // Parse xml response and obtain the list of shares + ShareToRemoteOperationResultParser parser = new ShareToRemoteOperationResultParser( + new ShareXMLParser() + ); + result = parser.parse(response); + + Log_OC.d(TAG, "Unshare " + remoteShareId + ": " + result.getLogMessage()); + + } else { + result = new RemoteOperationResult<>(false, delete); + } + } catch (Exception e) { + result = new RemoteOperationResult<>(e); + Log_OC.e(TAG, "Unshare Link Exception " + result.getLogMessage(), e); + + } finally { + if (delete != null) + delete.releaseConnection(); + } + return result; + } + private boolean isSuccess(int status) { return (status == HttpStatus.SC_OK); 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..a23fc2269 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,8 @@ 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 String INCLUDE_TAGS_OC = "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..c66bb5bf4 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,13 +13,15 @@ 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; @@ -36,7 +38,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(); @@ -54,7 +56,7 @@ public class UpdateShareRemoteOperation extends RemoteOperation { /** * Identifier of the share to update */ - private long remoteId; + private final long remoteId; /** * Password to set for the public link @@ -163,7 +165,7 @@ protected RemoteOperationResult> run(OwnCloudClient client) { String formattedExpirationDate = dateFormat.format(expirationDate.getTime()); parametersToUpdate.add(new Pair<>(PARAM_EXPIRATION_DATE, formattedExpirationDate)); } - + if (permissions > 0) { // set permissions parametersToUpdate.add(new Pair<>(PARAM_PERMISSIONS, Integer.toString(permissions))); @@ -182,7 +184,7 @@ protected RemoteOperationResult> run(OwnCloudClient client) { } /// perform required PUT requests - PutMethod put = null; + org.apache.commons.httpclient.methods.PutMethod put = null; String uriString; try { @@ -196,16 +198,114 @@ protected RemoteOperationResult> run(OwnCloudClient client) { if (put != null) { put.releaseConnection(); } - put = new PutMethod(uriString); + put = new org.apache.commons.httpclient.methods.PutMethod(uriString); put.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE); put.setRequestEntity(new StringRequestEntity( - parameter.first + "=" + parameter.second, - ENTITY_CONTENT_TYPE, - ENTITY_CHARSET + 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(); + + // Parse xml response + ShareToRemoteOperationResultParser parser = new ShareToRemoteOperationResultParser( + new ShareXMLParser() + ); + parser.setServerBaseUri(client.getBaseUri()); + result = parser.parse(response); + + } else { + result = new RemoteOperationResult<>(false, put); + } + if (!result.isSuccess()) { + break; + } + } + + } catch (Exception e) { + result = new RemoteOperationResult<>(e); + Log_OC.e(TAG, "Exception while updating remote share ", e); + if (put != null) { + put.releaseConnection(); + } + + } finally { + if (put != null) { + put.releaseConnection(); + } + } + return result; + } + + @Override + public RemoteOperationResult> run(NextcloudClient client) { + RemoteOperationResult> result = null; + int status; + + /// prepare array of parameters to update + List> parametersToUpdate = new ArrayList<>(); + if (password != null) { + parametersToUpdate.add(new Pair<>(PARAM_PASSWORD, password)); + } + + if (expirationDateInMillis < 0) { + // clear expiration date + parametersToUpdate.add(new Pair<>(PARAM_EXPIRATION_DATE, "")); + } else if (expirationDateInMillis > 0) { + // set expiration date + DateFormat dateFormat = new SimpleDateFormat(FORMAT_EXPIRATION_DATE, Locale.US); + Calendar expirationDate = Calendar.getInstance(); + expirationDate.setTimeInMillis(expirationDateInMillis); + String formattedExpirationDate = dateFormat.format(expirationDate.getTime()); + parametersToUpdate.add(new Pair<>(PARAM_EXPIRATION_DATE, formattedExpirationDate)); + } + + if (permissions > 0) { + // set permissions + parametersToUpdate.add(new Pair<>(PARAM_PERMISSIONS, Integer.toString(permissions))); + } + + if (hideFileDownload != null) { + parametersToUpdate.add(new Pair<>(PARAM_HIDE_DOWNLOAD, Boolean.toString(hideFileDownload))); + } + + if (note != null) { + parametersToUpdate.add(new Pair<>(PARAM_NOTE, note)); + } + + if (label != null) { + parametersToUpdate.add(new Pair<>(PARAM_LABEL, label)); + } + + /// perform required PUT requests + com.nextcloud.operations.PutMethod put = null; + String uriString; + + try { + Uri requestUri = client.getBaseUri(); + if (requestUri == null) { + throw new IllegalArgumentException("requestUri may not be null!"); + } + + Uri.Builder uriBuilder = requestUri.buildUpon(); + uriBuilder.appendEncodedPath(ShareUtils.SHARING_API_PATH.substring(1)); + uriBuilder.appendEncodedPath(Long.toString(remoteId)); + uriString = uriBuilder.build().toString(); + + for (Pair parameter : parametersToUpdate) { + if (put != null) { + put.releaseConnection(); + } + 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(); @@ -224,6 +324,8 @@ protected RemoteOperationResult> run(OwnCloudClient client) { } } + } catch (RuntimeException e) { + throw e; } catch (Exception e) { result = new RemoteOperationResult<>(e); Log_OC.e(TAG, "Exception while updating remote share ", e);