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

Commit 32ab89fc authored by Maria Asensio's avatar Maria Asensio
Browse files

Merge pull request #90 from owncloud/share_with_users

Added operations to allow apps share with OC users, including GetRemoteShareesOperation
parents f02dffb1 fe62a9fd
Loading
Loading
Loading
Loading
+4 −3
Original line number Diff line number Diff line
sudo: false
language: android
android:
  components:
@@ -20,8 +21,8 @@ script:
- ./gradlew clean build
env:
  global:
  - secure: HxHoqnC8mauCKi87zlo7pQcSsSw0W5MtW+iUcB8T11quwTBgUPWIOmycXv2FcmwpST0E43Ct+dhE+mttm+6P+5PSB33HQNLq00hfTVIJ4ttcb/5eWW8MnP7L+kPK8d0EtfDG6GQto7QktaybeG4+sNKKD336ZlFfM7xgPtPv+tg=
  - secure: WQMw0ciloe8i2ApGhePhuTmmH8UgAV1Ri10C1qhUH9hVOJAr+/1X5A93VPYGrgJ2EH5MdiL6f2XMDCYAgb9efuvZIUKNE0J92xh8m/yRa8nAVWNBE0PBdS4+OycoHpIQfMcUghooERXjP4GUYd/ZwICvWA+sXdOYWDdKjODUgl4=
  - secure: QPxKT8vC7sm1b/hYJcfkQkLgpwNRBvVKk8S8S0t43mmqPJfs94FJTQHH4kZaGSwOeuDkRQbGuKzYtXOnGOKX2hhUBqKJd1idpJnUID8id8Kqo6VutjG017+XxZQp0hPHmfmDxYkDvlaLeoZpP2NkpwZ1p4TL2MSCr2Ibl6uTWvc=
  - secure: a21NrzTCQnkTKtRGo7B1lyF2WWbpBT2N64V2Tf+oUcRLOUx3XM/mDi60mAdGd0WXV9tHD8GGmu0tQ7LGZ2VsIxixVxjzEYwX4HRTodewYez6WxBMjVEHHy+3jmc1zU4k3AAqr+uW7L4BKa5r9tH+nq9ecJMDMgW8o9MKXuP7Vso=
  - secure: UVnaC5Qzat2C8WlMMb8aycz1ChZKjP8Kz89qBbVcqYK+PLAGKpUNxFa39/2oA5jkMyyOcXYC9bX1ZYzHLH7nJ8LbQgaxXMT4gvgvN0l6KezjDavIW60idD9BbugkwzNj/cjoU/DdxBykPsTn4vSRaESVNTdEbM27YU4FBPzTANY=
  - secure: XEQY8s7p65lWWOuKbVDdTh6ZJtRTJ74Zw3H/+2Ms6vjZhFZsdUJjGo66LW6YvlhmYDXgiPB9piYQGcrGgT3hXJwXoge6n1pdixnV250J0T0dIZMbXYTWTARxDsyq48+LgUuF3pgqvqDWhBcemrePWv48q7fs/mwzuFSOyc8QwLk=
  matrix:
  - ANDROID_TARGET=android-19 ANDROID_ABI=armeabi-v7a
+2 −1
Original line number Diff line number Diff line
@@ -107,7 +107,8 @@ public class RemoteOperationResult implements Serializable {
        INVALID_COPY_INTO_DESCENDANT,
		PARTIAL_MOVE_DONE,
        PARTIAL_COPY_DONE,
        INVALID_CHARACTER_DETECT_IN_SERVER
        SHARE_WRONG_PARAMETER,
        WRONG_SERVER_RESPONSE, INVALID_CHARACTER_DETECT_IN_SERVER
    }

    private boolean mSuccess = false;
+41 −41
Original line number Diff line number Diff line
/* ownCloud Android Library is available under MIT license
 *   @author masensio
 *   @author David A. Velasco
 *   Copyright (C) 2015 ownCloud Inc.
 *   
 *   Permission is hereby granted, free of charge, to any person obtaining a copy
@@ -24,24 +26,16 @@

package com.owncloud.android.lib.resources.shares;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.ArrayList;

import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.http.HttpStatus;

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.operations.RemoteOperationResult.ResultCode;
import com.owncloud.android.lib.common.utils.Log_OC;

/**
 * Creates a new share.  This allows sharing with a user or group or as a link.
 * 
 * @author masensio
 *
 */
public class CreateRemoteShareOperation extends RemoteOperation {

@@ -54,20 +48,20 @@ public class CreateRemoteShareOperation extends RemoteOperation {
	private static final String PARAM_PASSWORD = "password";
	private static final String PARAM_PERMISSIONS = "permissions";

	private ArrayList<OCShare> mShares;  // List of shares for result, one share in this case
	
	private String mRemoteFilePath;
	private ShareType mShareType;
	private String mShareWith;
	private boolean mPublicUpload;
	private String mPassword;
	private int mPermissions;
	private boolean mGetShareDetails;

	/**
	 * Constructor
	 * @param remoteFilePath	Full path of the file/folder being shared. Mandatory argument
	 * @param shareType			0 = user, 1 = group, 3 = Public link. Mandatory argument
	 * @param shareWith			User/group ID with who the file should be shared.  This is mandatory for shareType of 0 or 1
	 * @param shareWith			User/group ID with who the file should be shared.  This is mandatory for shareType
	 *                          of 0 or 1
	 * @param publicUpload		If false (default) public cannot upload to a public shared folder.
	 * 							If true public can upload to a shared folder. Only available for public link shares
	 * @param password			Password to protect a public link share. Only available for public link shares
@@ -81,8 +75,14 @@ public class CreateRemoteShareOperation extends RemoteOperation {
	 * 							To obtain combinations, add the desired values together.  
	 * 							For instance, for Re-Share, delete, read, update, add 16+8+2+1 = 27.
	 */
	public CreateRemoteShareOperation(String remoteFilePath, ShareType shareType, String shareWith, boolean publicUpload, 
			String password, int permissions) {
	public CreateRemoteShareOperation(
			String remoteFilePath,
			ShareType shareType,
			String shareWith,
			boolean publicUpload,
			String password,
			int permissions
	) {

		mRemoteFilePath = remoteFilePath;
		mShareType = shareType;
@@ -90,6 +90,15 @@ public class CreateRemoteShareOperation extends RemoteOperation {
		mPublicUpload = publicUpload;
		mPassword = password;
		mPermissions = permissions;
		mGetShareDetails = false; 		// defaults to false for backwards compatibility
	}

	public boolean isGettingShareDetails () {
		return mGetShareDetails;
	}

	public void setGetShareDetails(boolean set) {
		mGetShareDetails = set;
	}

	@Override
@@ -102,7 +111,6 @@ public class CreateRemoteShareOperation extends RemoteOperation {
		try {
			// Post Method
			post = new PostMethod(client.getBaseUri() + ShareUtils.SHARING_API_PATH);
			//Log_OC.d(TAG, "URL ------> " + client.getBaseUri() + ShareUtils.SHARING_API_PATH);

			post.setRequestHeader( "Content-Type",
                    "application/x-www-form-urlencoded; charset=utf-8"); // necessary for special characters
@@ -111,12 +119,14 @@ public class CreateRemoteShareOperation extends RemoteOperation {
			post.addParameter(PARAM_SHARE_TYPE, Integer.toString(mShareType.getValue()));
			post.addParameter(PARAM_SHARE_WITH, mShareWith);
			if (mPublicUpload) {
				post.addParameter(PARAM_PUBLIC_UPLOAD, Boolean.toString(mPublicUpload));
				post.addParameter(PARAM_PUBLIC_UPLOAD, Boolean.toString(true));
			}
			if (mPassword != null && mPassword.length() > 0) {
				post.addParameter(PARAM_PASSWORD, mPassword);
			}
			if (OCShare.DEFAULT_PERMISSION != mPermissions) {
				post.addParameter(PARAM_PERMISSIONS, Integer.toString(mPermissions));
			}

			post.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE);
            
@@ -125,31 +135,21 @@ public class CreateRemoteShareOperation extends RemoteOperation {
			if(isSuccess(status)) {
				String response = post.getResponseBodyAsString();

				result = new RemoteOperationResult(ResultCode.OK);
				
				// Parse xml response --> obtain the response in ShareFiles ArrayList
				// convert String into InputStream
				InputStream is = new ByteArrayInputStream(response.getBytes());
				ShareXMLParser xmlParser = new ShareXMLParser();
				mShares = xmlParser.parseXMLResponse(is);
				if (xmlParser.isSuccess()) {
					if (mShares != null) {
						Log_OC.d(TAG, "Created " + mShares.size() + " share(s)");
						result = new RemoteOperationResult(ResultCode.OK);
						ArrayList<Object> sharesObjects = new ArrayList<Object>();
						for (OCShare share: mShares) {
							sharesObjects.add(share);
						}
						result.setData(sharesObjects);
					}
				} else if (xmlParser.isFileNotFound()){
					result = new RemoteOperationResult(ResultCode.SHARE_NOT_FOUND);
					
				} else if (xmlParser.isFailure()) {
					result = new RemoteOperationResult(ResultCode.SHARE_FORBIDDEN);

				} else {
					result = new RemoteOperationResult(false, status, post.getResponseHeaders());	
				ShareToRemoteOperationResultParser parser = new ShareToRemoteOperationResultParser(
						new ShareXMLParser()
				);
				parser.setOneOrMoreSharesRequired(true);
				parser.setOwnCloudVersion(client.getOwnCloudVersion());
				parser.setServerBaseUri(client.getBaseUri());
				result = parser.parse(response);

				if (result.isSuccess() && mGetShareDetails) {
					// 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()
					);
					result = getInfo.execute(client);
				}

			} else {
+101 −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 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.GetMethod;
import org.apache.http.HttpStatus;


/** 
 * Get the data about a Share resource, known its remote ID.
 */

public class GetRemoteShareOperation extends RemoteOperation {

	private static final String TAG = GetRemoteShareOperation.class.getSimpleName();

	private long mRemoteId;


	public GetRemoteShareOperation(long remoteId) {
		mRemoteId = remoteId;
	}


	@Override
	protected RemoteOperationResult run(OwnCloudClient client) {
		RemoteOperationResult result = null;
		int status = -1;

		// Get Method        
		GetMethod get = null;

		// Get the response
		try{
			get = new GetMethod(client.getBaseUri() + ShareUtils.SHARING_API_PATH + "/" + Long.toString(mRemoteId));
			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.setOwnCloudVersion(client.getOwnCloudVersion());
				parser.setServerBaseUri(client.getBaseUri());
				result = parser.parse(response);

			} else {
				result = new RemoteOperationResult(false, status, get.getResponseHeaders());
			}
			
		} 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;
	}

	private boolean isSuccess(int status) {
		return (status == HttpStatus.SC_OK);
	}


}
+199 −0
Original line number Diff line number Diff line
/* ownCloud Android Library is available under MIT license
 *
 *   @author masensio
 *   @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 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.GetMethod;
import org.apache.http.HttpStatus;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * Created by masensio on 08/10/2015.
 *
 * Retrieves a list of sharees (possible targets of a share) from the ownCloud server.
 *
 * Currently only handles users and groups. Users in other OC servers (federation) should be added later.
 *
 * Depends on SHAREE API. {@See https://github.com/owncloud/documentation/issues/1626}
 *
 * Syntax:
 *    Entry point: ocs/v2.php/apps/files_sharing/api/v1/sharees
 *    HTTP method: GET
 *    url argument: itemType - string, required
 *    url argument: format - string, optional
 *    url argument: search - string, optional
 *    url arguments: perPage - int, optional
 *    url arguments: page - int, optional
 *
 * Status codes:
 *    100 - successful
 */
public class GetRemoteShareesOperation extends RemoteOperation{

    private static final String TAG = GetRemoteShareesOperation.class.getSimpleName();

    // OCS Routes
    private static final String OCS_ROUTE = "ocs/v2.php/apps/files_sharing/api/v1/sharees";    // from OC 8.2

    // Arguments - names
    private static final String PARAM_FORMAT = "format";
    private static final String PARAM_ITEM_TYPE = "itemType";
    private static final String PARAM_SEARCH = "search";
    private static final String PARAM_PAGE = "page";                //  default = 1
    private static final String PARAM_PER_PAGE = "perPage";         //  default = 200

    // Arguments - constant values
    private static final String VALUE_FORMAT = "json";
    private static final String VALUE_ITEM_TYPE = "search";         //  to get the server search for users / groups


    // JSON Node names
    private static final String NODE_OCS = "ocs";
    private static final String NODE_DATA = "data";
    private static final String NODE_EXACT = "exact";
    private static final String NODE_USERS = "users";
    private static final String NODE_GROUPS = "groups";
    public static final String NODE_VALUE = "value";
    public static final String PROPERTY_LABEL = "label";
    public static final String PROPERTY_SHARE_TYPE = "shareType";
    public static final String PROPERTY_SHARE_WITH = "shareWith";

    // Result types
    public static final Byte USER_TYPE = 0;
    public static final Byte GROUP_TYPE = 1;

    private String mSearchString;
    private int mPage;
    private int mPerPage;

    /**
     * Constructor
     *
     * @param searchString  	string for searching users, optional
     * @param page			    page index in the list of results; beginning in 1
     * @param perPage           maximum number of results in a single page
     */
    public GetRemoteShareesOperation(String searchString, int page, int perPage) {
        mSearchString = searchString;
        mPage = page;
        mPerPage = perPage;
    }

    @Override
    protected RemoteOperationResult run(OwnCloudClient client) {
        RemoteOperationResult result = null;
        int status;
        GetMethod get = null;

        try{
            Uri requestUri = client.getBaseUri();
            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, Uri.encode(mSearchString, "@"));
            uriBuilder.appendQueryParameter(PARAM_PAGE, String.valueOf(mPage));
            uriBuilder.appendQueryParameter(PARAM_PER_PAGE, String.valueOf(mPerPage));

            // Get Method
            get = new 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 respPartialUsers = respData.getJSONArray(NODE_USERS);
                JSONArray respPartialGroups = respData.getJSONArray(NODE_GROUPS);
                JSONArray[] jsonResults = {
                        respExactUsers,
                        respExactGroups,
                        respPartialUsers,
                        respPartialGroups
                };

                ArrayList<Object> data = new ArrayList<Object>(); // For result data
                for (int i=0; i<4; i++) {
                    for(int j=0; j< jsonResults[i].length(); j++){
                        JSONObject jsonResult = jsonResults[i].getJSONObject(j);
                        data.add(jsonResult);
                        Log_OC.d(TAG, "*** Added item: " + jsonResult.getString(PROPERTY_LABEL));
                    }
                }

                // Result
                result = new RemoteOperationResult(true, status, get.getResponseHeaders());
                result.setData(data);

                Log_OC.d(TAG, "*** Get Users or groups completed " );

            } else {
                result = new RemoteOperationResult(false, status, get.getResponseHeaders());
                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 (Exception e) {
            result = new RemoteOperationResult(e);
            Log_OC.e(TAG, "Exception while getting users/groups", e);

        } finally {
            if (get != null) {
                get.releaseConnection();
            }
        }
        return result;
    }

    private boolean isSuccess(int status) {
        return (status == HttpStatus.SC_OK);
    }
}
Loading