From 90615fc4ab7399d1436e8d2a9df01c05b795a808 Mon Sep 17 00:00:00 2001 From: "David A. Velasco" Date: Thu, 5 Nov 2015 13:10:58 +0100 Subject: [PATCH] Added operation to update properties of exising share via link --- .../shares/CreateRemoteShareOperation.java | 2 +- .../android/lib/resources/shares/OCShare.java | 20 ++- .../lib/resources/shares/ShareXMLParser.java | 4 +- .../shares/UpdateRemoteShareOperation.java | 153 ++++++++++++++++++ .../test_project/test/RemoveShareTest.java | 2 +- 5 files changed, 169 insertions(+), 12 deletions(-) create mode 100644 src/com/owncloud/android/lib/resources/shares/UpdateRemoteShareOperation.java diff --git a/src/com/owncloud/android/lib/resources/shares/CreateRemoteShareOperation.java b/src/com/owncloud/android/lib/resources/shares/CreateRemoteShareOperation.java index 21545222..39fab768 100644 --- a/src/com/owncloud/android/lib/resources/shares/CreateRemoteShareOperation.java +++ b/src/com/owncloud/android/lib/resources/shares/CreateRemoteShareOperation.java @@ -147,7 +147,7 @@ public class CreateRemoteShareOperation extends RemoteOperation { // retrieve more info - POST only returns the index of the new share OCShare emptyShare = (OCShare) result.getData().get(0); GetRemoteShareOperation getInfo = new GetRemoteShareOperation( - emptyShare.getIdRemoteShared() + emptyShare.getRemoteId() ); result = getInfo.execute(client); } diff --git a/src/com/owncloud/android/lib/resources/shares/OCShare.java b/src/com/owncloud/android/lib/resources/shares/OCShare.java index 9135978e..347bd1ae 100644 --- a/src/com/owncloud/android/lib/resources/shares/OCShare.java +++ b/src/com/owncloud/android/lib/resources/shares/OCShare.java @@ -61,7 +61,7 @@ public class OCShare implements Parcelable, Serializable { private String mSharedWithDisplayName; private boolean mIsFolder; private long mUserId; - private long mIdRemoteShared; + private long mRemoteId; private String mShareLink; public OCShare() { @@ -95,7 +95,7 @@ public class OCShare implements Parcelable, Serializable { mSharedWithDisplayName = ""; mIsFolder = false; mUserId = -1; - mIdRemoteShared = -1; + mRemoteId = -1; mShareLink = ""; } @@ -205,12 +205,12 @@ public class OCShare implements Parcelable, Serializable { this.mUserId = userId; } - public long getIdRemoteShared() { - return mIdRemoteShared; + public long getRemoteId() { + return mRemoteId; } - public void setIdRemoteShared(long idRemoteShared) { - this.mIdRemoteShared = idRemoteShared; + public void setIdRemoteShared(long remoteId) { + this.mRemoteId = remoteId; } public String getShareLink() { @@ -220,6 +220,10 @@ public class OCShare implements Parcelable, Serializable { public void setShareLink(String shareLink) { this.mShareLink = (shareLink != null) ? shareLink : ""; } + + public boolean isPasswordProtected() { + return ShareType.PUBLIC_LINK.equals(mShareType) && mShareWith.length() > 0; + } /** * Parcelable Methods @@ -264,7 +268,7 @@ public class OCShare implements Parcelable, Serializable { mSharedWithDisplayName = source.readString(); mIsFolder = source.readInt() == 0; mUserId = source.readLong(); - mIdRemoteShared = source.readLong(); + mRemoteId = source.readLong(); mShareLink = source.readString(); } @@ -290,7 +294,7 @@ public class OCShare implements Parcelable, Serializable { dest.writeString(mSharedWithDisplayName); dest.writeInt(mIsFolder ? 1 : 0); dest.writeLong(mUserId); - dest.writeLong(mIdRemoteShared); + dest.writeLong(mRemoteId); dest.writeString(mShareLink); } diff --git a/src/com/owncloud/android/lib/resources/shares/ShareXMLParser.java b/src/com/owncloud/android/lib/resources/shares/ShareXMLParser.java index e672deeb..f8b8cb00 100644 --- a/src/com/owncloud/android/lib/resources/shares/ShareXMLParser.java +++ b/src/com/owncloud/android/lib/resources/shares/ShareXMLParser.java @@ -82,7 +82,7 @@ public class ShareXMLParser { private static final String TYPE_FOLDER = "folder"; private static final int SUCCESS = 100; - private static final int ERROR_WRONG_PARAMETER = 403; + private static final int ERROR_WRONG_PARAMETER = 400; private static final int ERROR_FORBIDDEN = 403; private static final int ERROR_NOT_FOUND = 404; @@ -368,7 +368,7 @@ public class ShareXMLParser { } private boolean isValidShare(OCShare share) { - return (share.getIdRemoteShared() > -1); + return (share.getRemoteId() > -1); } private void fixPathForFolder(OCShare share) { diff --git a/src/com/owncloud/android/lib/resources/shares/UpdateRemoteShareOperation.java b/src/com/owncloud/android/lib/resources/shares/UpdateRemoteShareOperation.java new file mode 100644 index 00000000..0d4fba1f --- /dev/null +++ b/src/com/owncloud/android/lib/resources/shares/UpdateRemoteShareOperation.java @@ -0,0 +1,153 @@ +/* ownCloud Android Library is available under MIT license + * @author David A. Velasco + * Copyright (C) 2015 ownCloud Inc. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + */ + +package com.owncloud.android.lib.resources.shares; + + import android.net.Uri; + import android.util.Pair; + + 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.methods.PutMethod; + import org.apache.commons.httpclient.methods.StringRequestEntity; + import org.apache.http.HttpStatus; + + import java.util.ArrayList; + import java.util.List; + + +/** + * Updates parameters of an existing Share resource, known its remote ID. + * + * Allow updating several parameters, triggering a request to the server per parameter. + */ + +public class UpdateRemoteShareOperation extends RemoteOperation { + + private static final String TAG = GetRemoteShareOperation.class.getSimpleName(); + + private long mRemoteId; + + private String mPassword; + + + public UpdateRemoteShareOperation(long remoteId) { + mRemoteId = remoteId; + mPassword = null; + } + + + @Override + protected RemoteOperationResult run(OwnCloudClient client) { + RemoteOperationResult result = null; + int status = -1; + + /// prepare array of parameters to update + List> parametersToUpdate = new ArrayList>(); + if (mPassword != null) { + parametersToUpdate.add(new Pair("password", mPassword)); + } + /* TODO complete rest of parameters + if (mPermissions > 0) { + parametersToUpdate.add(new Pair("permissions", Integer.toString(mPermissions))); + } + if (mPublicUpload != null) { + parametersToUpdate.add(new Pair("publicUpload", mPublicUpload.toString()); + } + + if (mExpireDate != null) { + parametersToUpdate.add(new Pair("expireData", mExpireData.toString())); + } + */ + + /// perform required PUT requests + PutMethod put = null; + + try{ + Uri requestUri = client.getBaseUri(); + Uri.Builder uriBuilder = requestUri.buildUpon(); + uriBuilder.appendEncodedPath(ShareUtils.SHARING_API_PATH.substring(1)); + uriBuilder.appendEncodedPath(Long.toString(mRemoteId)); + + for (Pair parameter : parametersToUpdate) { + if (put != null) { + put.releaseConnection(); + } + // TODO check if uriBuilder may be reused + String uriString = uriBuilder.build().toString(); + put = new PutMethod(uriString); + put.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE); + put.setRequestEntity(new StringRequestEntity( + parameter.first + "=" + parameter.second, + "application/x-www-form-urlencoded", + "UTF-8" + )); + + status = client.executeMethod(put); + + if (status == HttpStatus.SC_OK) { + String response = put.getResponseBodyAsString(); + + // Parse xml response + ShareToRemoteOperationResultParser parser = new ShareToRemoteOperationResultParser( + new ShareXMLParser() + ); + parser.setOwnCloudVersion(client.getOwnCloudVersion()); + parser.setServerBaseUri(client.getBaseUri()); + result = parser.parse(response); + + } else { + result = new RemoteOperationResult(false, status, put.getResponseHeaders()); + } + } + + } 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; + } + + /** + * Set password to update in Share resource. + * + * @param password Password to set to the target share. The empty string clears the password. + * Null results in no update applied to the password. + */ + public void setPassword(String password) { + mPassword = password; + } +} diff --git a/test_client/tests/src/com/owncloud/android/lib/test_project/test/RemoveShareTest.java b/test_client/tests/src/com/owncloud/android/lib/test_project/test/RemoveShareTest.java index 61a5ecd0..64653861 100644 --- a/test_client/tests/src/com/owncloud/android/lib/test_project/test/RemoveShareTest.java +++ b/test_client/tests/src/com/owncloud/android/lib/test_project/test/RemoveShareTest.java @@ -65,7 +65,7 @@ public class RemoveShareTest extends RemoteTest { Utils.logAndThrow(LOG_TAG, result); } else { OCShare created = (OCShare) result.getData().get(0); - mShareId = created.getIdRemoteShared(); + mShareId = created.getRemoteId(); } }