1
0
mirror of https://github.com/owncloud/android-library.git synced 2025-06-07 16:06:08 +00:00

Merge pull request #254 from owncloud/new_arch/private_shares_get_create

[New Arch] Show + search + create private shares
This commit is contained in:
David González Verdugo 2019-07-05 09:55:31 +02:00 committed by GitHub
commit c17c60a0a3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 284 additions and 261 deletions

View File

@ -71,8 +71,6 @@ class CreateRemoteShareOperation(
private val shareWith: String, private val shareWith: String,
private val permissions: Int private val permissions: Int
) : RemoteOperation<ShareParserResult>() { ) : RemoteOperation<ShareParserResult>() {
var retrieveShareDetails = false // To retrieve more info about the just created share
var name = "" // Name to set for the public link var name = "" // Name to set for the public link
var password: String = "" // Password to set for the public link var password: String = "" // Password to set for the public link
@ -81,13 +79,15 @@ class CreateRemoteShareOperation(
var publicUpload: Boolean = false // Upload permissions for the public link (only folders) var publicUpload: Boolean = false // Upload permissions for the public link (only folders)
var retrieveShareDetails = false // To retrieve more info about the just created share
override fun run(client: OwnCloudClient): RemoteOperationResult<ShareParserResult> { override fun run(client: OwnCloudClient): RemoteOperationResult<ShareParserResult> {
var result: RemoteOperationResult<ShareParserResult> var result: RemoteOperationResult<ShareParserResult>
try { try {
val formBodyBuilder = FormBody.Builder() val formBodyBuilder = FormBody.Builder()
.add(PARAM_PATH, remoteFilePath) .add(PARAM_PATH, remoteFilePath)
.add(PARAM_SHARE_TYPE, Integer.toString(shareType.value)) .add(PARAM_SHARE_TYPE, shareType.value.toString())
.add(PARAM_SHARE_WITH, shareWith) .add(PARAM_SHARE_WITH, shareWith)
if (name.isNotEmpty()) { if (name.isNotEmpty()) {
@ -109,7 +109,7 @@ class CreateRemoteShareOperation(
formBodyBuilder.add(PARAM_PASSWORD, password) formBodyBuilder.add(PARAM_PASSWORD, password)
} }
if (RemoteShare.DEFAULT_PERMISSION != permissions) { if (RemoteShare.DEFAULT_PERMISSION != permissions) {
formBodyBuilder.add(PARAM_PERMISSIONS, Integer.toString(permissions)) formBodyBuilder.add(PARAM_PERMISSIONS, permissions.toString())
} }
val requestUri = client.baseUri val requestUri = client.baseUri

View File

@ -1,199 +0,0 @@
/* ownCloud Android Library is available under MIT license
*
* @author masensio
* @author David A. Velasco
* @author David González Verdugo
* Copyright (C) 2019 ownCloud GmbH.
*
* 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.http.HttpConstants;
import com.owncloud.android.lib.common.http.methods.nonwebdav.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.json.JSONArray;
import org.json.JSONObject;
import java.net.URL;
import java.util.ArrayList;
import static com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode.OK;
/**
* Created by masensio on 08/10/2015.
* <p>
* Retrieves a list of sharees (possible targets of a share) from the ownCloud server.
* <p>
* Currently only handles users and groups. Users in other OC servers (federation) should be added later.
* <p>
* Depends on SHAREE API. {@See https://github.com/owncloud/documentation/issues/1626}
* <p>
* 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
* <p>
* Status codes:
* 100 - successful
*
* @author masensio
* @author David A. Velasco
* @author David González Verdugo
*/
public class GetRemoteShareesOperation extends RemoteOperation<ArrayList<JSONObject>> {
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 = "file"; // 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";
private static final String NODE_REMOTES = "remotes";
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";
public static final String PROPERTY_SHARE_WITH_ADDITIONAL_INFO = "shareWithAdditionalInfo";
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<ArrayList<JSONObject>> run(OwnCloudClient client) {
RemoteOperationResult<ArrayList<JSONObject>> result;
try {
Uri requestUri = client.getBaseUri();
Uri.Builder uriBuilder = requestUri.buildUpon()
.appendEncodedPath(OCS_ROUTE)
.appendQueryParameter(PARAM_FORMAT, VALUE_FORMAT)
.appendQueryParameter(PARAM_ITEM_TYPE, VALUE_ITEM_TYPE)
.appendQueryParameter(PARAM_SEARCH, mSearchString)
.appendQueryParameter(PARAM_PAGE, String.valueOf(mPage))
.appendQueryParameter(PARAM_PER_PAGE, String.valueOf(mPerPage));
GetMethod getMethod = new GetMethod(new URL(uriBuilder.build().toString()));
getMethod.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE);
int status = client.executeHttpMethod(getMethod);
String response = getMethod.getResponseBodyAsString();
if (isSuccess(status)) {
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 respPartialUsers = respData.getJSONArray(NODE_USERS);
JSONArray respPartialGroups = respData.getJSONArray(NODE_GROUPS);
JSONArray respPartialRemotes = respData.getJSONArray(NODE_REMOTES);
JSONArray[] jsonResults = {
respExactUsers,
respExactGroups,
respExactRemotes,
respPartialUsers,
respPartialGroups,
respPartialRemotes
};
ArrayList<JSONObject> data = new ArrayList<>(); // For result data
for (int i = 0; i < 6; 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 = new RemoteOperationResult<>(OK);
result.setData(data);
Log_OC.d(TAG, "*** Get Users or groups completed ");
} else {
result = new RemoteOperationResult<>(getMethod);
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);
}
return result;
}
private boolean isSuccess(int status) {
return (status == HttpConstants.HTTP_OK);
}
}

View File

@ -0,0 +1,197 @@
/* ownCloud Android Library is available under MIT license
*
* @author masensio
* @author David A. Velasco
* @author David González Verdugo
* Copyright (C) 2019 ownCloud GmbH.
*
* 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.http.HttpConstants
import com.owncloud.android.lib.common.http.methods.nonwebdav.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.json.JSONObject
import java.net.URL
import java.util.ArrayList
import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode.OK
import com.owncloud.android.lib.testing.OpenForTesting
/**
* 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
*
* @author masensio
* @author David A. Velasco
* @author David González Verdugo
*/
@OpenForTesting
class GetRemoteShareesOperation
/**
* 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
*/
(private val searchString: String, private val page: Int, private val perPage: Int) :
RemoteOperation<ArrayList<JSONObject>>() {
override fun run(client: OwnCloudClient): RemoteOperationResult<ArrayList<JSONObject>> {
var result: RemoteOperationResult<ArrayList<JSONObject>>
try {
val requestUri = client.baseUri
val uriBuilder = requestUri.buildUpon()
.appendEncodedPath(OCS_ROUTE)
.appendQueryParameter(PARAM_FORMAT, VALUE_FORMAT)
.appendQueryParameter(PARAM_ITEM_TYPE, VALUE_ITEM_TYPE)
.appendQueryParameter(PARAM_SEARCH, searchString)
.appendQueryParameter(PARAM_PAGE, page.toString())
.appendQueryParameter(PARAM_PER_PAGE, perPage.toString())
val getMethod = GetMethod(URL(uriBuilder.build().toString()))
getMethod.addRequestHeader(RemoteOperation.OCS_API_HEADER, RemoteOperation.OCS_API_HEADER_VALUE)
val status = client.executeHttpMethod(getMethod)
val response = getMethod.responseBodyAsString
if (isSuccess(status)) {
Log_OC.d(TAG, "Successful response: " + response!!)
// Parse the response
val respJSON = JSONObject(response)
val respOCS = respJSON.getJSONObject(NODE_OCS)
val respData = respOCS.getJSONObject(NODE_DATA)
val respExact = respData.getJSONObject(NODE_EXACT)
val respExactUsers = respExact.getJSONArray(NODE_USERS)
val respExactGroups = respExact.getJSONArray(NODE_GROUPS)
val respExactRemotes = respExact.getJSONArray(NODE_REMOTES)
val respPartialUsers = respData.getJSONArray(NODE_USERS)
val respPartialGroups = respData.getJSONArray(NODE_GROUPS)
val respPartialRemotes = respData.getJSONArray(NODE_REMOTES)
val jsonResults = arrayOf(
respExactUsers,
respExactGroups,
respExactRemotes,
respPartialUsers,
respPartialGroups,
respPartialRemotes
)
val data = ArrayList<JSONObject>() // For result data
for (i in 0..5) {
for (j in 0 until jsonResults[i].length()) {
val jsonResult = jsonResults[i].getJSONObject(j)
data.add(jsonResult)
Log_OC.d(TAG, "*** Added item: " + jsonResult.getString(PROPERTY_LABEL))
}
}
result = RemoteOperationResult(OK)
result.data = data
Log_OC.d(TAG, "*** Get Users or groups completed ")
} else {
result = RemoteOperationResult(getMethod)
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 (e: Exception) {
result = RemoteOperationResult(e)
Log_OC.e(TAG, "Exception while getting users/groups", e)
}
return result
}
private fun isSuccess(status: Int): Boolean {
return status == HttpConstants.HTTP_OK
}
companion object {
private val TAG = GetRemoteShareesOperation::class.java.simpleName
// OCS Routes
private val OCS_ROUTE = "ocs/v2.php/apps/files_sharing/api/v1/sharees" // from OC 8.2
// Arguments - names
private val PARAM_FORMAT = "format"
private val PARAM_ITEM_TYPE = "itemType"
private val PARAM_SEARCH = "search"
private val PARAM_PAGE = "page" // default = 1
private val PARAM_PER_PAGE = "perPage" // default = 200
// Arguments - constant values
private val VALUE_FORMAT = "json"
private val VALUE_ITEM_TYPE = "file" // to get the server search for users / groups
// JSON Node names
private val NODE_OCS = "ocs"
private val NODE_DATA = "data"
private val NODE_EXACT = "exact"
private val NODE_USERS = "users"
private val NODE_GROUPS = "groups"
private val NODE_REMOTES = "remotes"
val NODE_VALUE = "value"
val PROPERTY_LABEL = "label"
val PROPERTY_SHARE_TYPE = "shareType"
val PROPERTY_SHARE_WITH = "shareWith"
val PROPERTY_SHARE_WITH_ADDITIONAL_INFO = "shareWithAdditionalInfo"
}
}

View File

@ -225,71 +225,96 @@ class ShareXMLParser {
val name = parser.name val name = parser.name
if (name.equals(NODE_ELEMENT, ignoreCase = true)) { when {
// patch to work around servers responding with extra <element> surrounding all name.equals(NODE_ELEMENT, ignoreCase = true) -> {
// the shares on the same file before // patch to work around servers responding with extra <element> surrounding all
// https://github.com/owncloud/core/issues/6992 was fixed // the shares on the same file before
readElement(parser, shares) // https://github.com/owncloud/core/issues/6992 was fixed
readElement(parser, shares)
} else if (name.equals(NODE_ID, ignoreCase = true)) {
remoteShare.id = Integer.parseInt(readNode(parser, NODE_ID)).toLong()
} else if (name.equals(NODE_ITEM_TYPE, ignoreCase = true)) {
remoteShare.isFolder = readNode(parser, NODE_ITEM_TYPE).equals(TYPE_FOLDER, ignoreCase = true)
fixPathForFolder(remoteShare)
} else if (name.equals(NODE_ITEM_SOURCE, ignoreCase = true)) {
remoteShare.itemSource = java.lang.Long.parseLong(readNode(parser, NODE_ITEM_SOURCE))
} else if (name.equals(NODE_PARENT, ignoreCase = true)) {
readNode(parser, NODE_PARENT)
} else if (name.equals(NODE_SHARE_TYPE, ignoreCase = true)) {
val value = Integer.parseInt(readNode(parser, NODE_SHARE_TYPE))
remoteShare.shareType = ShareType.fromValue(value)
} else if (name.equals(NODE_SHARE_WITH, ignoreCase = true)) {
remoteShare.shareWith = readNode(parser, NODE_SHARE_WITH)
} else if (name.equals(NODE_FILE_SOURCE, ignoreCase = true)) {
remoteShare.fileSource = java.lang.Long.parseLong(readNode(parser, NODE_FILE_SOURCE))
} else if (name.equals(NODE_PATH, ignoreCase = true)) {
remoteShare.path = readNode(parser, NODE_PATH)
fixPathForFolder(remoteShare)
} else if (name.equals(NODE_PERMISSIONS, ignoreCase = true)) {
remoteShare.permissions = Integer.parseInt(readNode(parser, NODE_PERMISSIONS))
} else if (name.equals(NODE_STIME, ignoreCase = true)) {
remoteShare.sharedDate = java.lang.Long.parseLong(readNode(parser, NODE_STIME))
} else if (name.equals(NODE_EXPIRATION, ignoreCase = true)) {
val value = readNode(parser, NODE_EXPIRATION)
if (value.isNotEmpty()) {
remoteShare.expirationDate = WebdavUtils.parseResponseDate(value)!!.time
} }
} else if (name.equals(NODE_TOKEN, ignoreCase = true)) { name.equals(NODE_ID, ignoreCase = true) -> {
remoteShare.token = readNode(parser, NODE_TOKEN) remoteShare.id = Integer.parseInt(readNode(parser, NODE_ID)).toLong()
}
} else if (name.equals(NODE_STORAGE, ignoreCase = true)) { name.equals(NODE_ITEM_TYPE, ignoreCase = true) -> {
readNode(parser, NODE_STORAGE) remoteShare.isFolder = readNode(parser, NODE_ITEM_TYPE).equals(TYPE_FOLDER, ignoreCase = true)
} else if (name.equals(NODE_MAIL_SEND, ignoreCase = true)) { fixPathForFolder(remoteShare)
readNode(parser, NODE_MAIL_SEND) }
} else if (name.equals(NODE_SHARE_WITH_DISPLAY_NAME, ignoreCase = true)) { name.equals(NODE_ITEM_SOURCE, ignoreCase = true) -> {
remoteShare.sharedWithDisplayName = readNode(parser, NODE_SHARE_WITH_DISPLAY_NAME) remoteShare.itemSource = java.lang.Long.parseLong(readNode(parser, NODE_ITEM_SOURCE))
}
} else if (name.equals(NODE_URL, ignoreCase = true)) { name.equals(NODE_PARENT, ignoreCase = true) -> {
val value = readNode(parser, NODE_URL) readNode(parser, NODE_PARENT)
remoteShare.shareLink = value }
} else if (name.equals(NODE_NAME, ignoreCase = true)) { name.equals(NODE_SHARE_TYPE, ignoreCase = true) -> {
remoteShare.name = readNode(parser, NODE_NAME) val value = Integer.parseInt(readNode(parser, NODE_SHARE_TYPE))
remoteShare.shareType = ShareType.fromValue(value)
}
} else { name.equals(NODE_SHARE_WITH, ignoreCase = true) -> {
skip(parser) remoteShare.shareWith = readNode(parser, NODE_SHARE_WITH)
}
name.equals(NODE_FILE_SOURCE, ignoreCase = true) -> {
remoteShare.fileSource = java.lang.Long.parseLong(readNode(parser, NODE_FILE_SOURCE))
}
name.equals(NODE_PATH, ignoreCase = true) -> {
remoteShare.path = readNode(parser, NODE_PATH)
fixPathForFolder(remoteShare)
}
name.equals(NODE_PERMISSIONS, ignoreCase = true) -> {
remoteShare.permissions = Integer.parseInt(readNode(parser, NODE_PERMISSIONS))
}
name.equals(NODE_STIME, ignoreCase = true) -> {
remoteShare.sharedDate = java.lang.Long.parseLong(readNode(parser, NODE_STIME))
}
name.equals(NODE_EXPIRATION, ignoreCase = true) -> {
val value = readNode(parser, NODE_EXPIRATION)
if (value.isNotEmpty()) {
remoteShare.expirationDate = WebdavUtils.parseResponseDate(value)!!.time
}
}
name.equals(NODE_TOKEN, ignoreCase = true) -> {
remoteShare.token = readNode(parser, NODE_TOKEN)
}
name.equals(NODE_STORAGE, ignoreCase = true) -> {
readNode(parser, NODE_STORAGE)
}
name.equals(NODE_MAIL_SEND, ignoreCase = true) -> {
readNode(parser, NODE_MAIL_SEND)
}
name.equals(NODE_SHARE_WITH_DISPLAY_NAME, ignoreCase = true) -> {
remoteShare.sharedWithDisplayName = readNode(parser, NODE_SHARE_WITH_DISPLAY_NAME)
}
name.equals(NODE_SHARE_WITH_ADDITIONAL_INFO, ignoreCase = true) -> {
remoteShare.sharedWithAdditionalInfo = readNode(parser, NODE_SHARE_WITH_ADDITIONAL_INFO)
}
name.equals(NODE_URL, ignoreCase = true) -> {
val value = readNode(parser, NODE_URL)
remoteShare.shareLink = value
}
name.equals(NODE_NAME, ignoreCase = true) -> {
remoteShare.name = readNode(parser, NODE_NAME)
}
else -> {
skip(parser)
}
} }
} }