Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 90615fc4 authored by David A. Velasco's avatar David A. Velasco
Browse files

Added operation to update properties of exising share via link

parent 0238e519
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -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);
				}
+12 −8
Original line number Diff line number Diff line
@@ -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() {
@@ -221,6 +221,10 @@ public class OCShare implements Parcelable, Serializable {
        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);
    }

+2 −2
Original line number Diff line number Diff line
@@ -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) {
+153 −0
Original line number Diff line number Diff line
/* 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<Pair<String, String>> parametersToUpdate = new ArrayList<Pair<String, String>>();
        if (mPassword != null) {
            parametersToUpdate.add(new Pair<String, String>("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<String, String> 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;
    }
}
+1 −1
Original line number Diff line number Diff line
@@ -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();
		}
		
	}