From 27b18c33a995c0d92d6525fc04ab9d98f3847681 Mon Sep 17 00:00:00 2001 From: Fernando Sanz Date: Fri, 27 Aug 2021 14:08:58 +0200 Subject: [PATCH 01/10] The way to retrieve list of shares has been modified from java to kotlin and its result has been parsed to ShareResponse.kt object. --- .../shares/CreateRemoteShareOperation.kt | 7 +- .../shares/GetRemoteShareOperation.java | 94 -------------- .../shares/GetRemoteShareOperation.kt | 115 ++++++++++++++++++ .../shares/UpdateRemoteShareOperation.kt | 7 +- .../shares/responses/ShareResponse.kt | 72 +++++++++++ 5 files changed, 197 insertions(+), 98 deletions(-) delete mode 100644 owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/GetRemoteShareOperation.java create mode 100644 owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/GetRemoteShareOperation.kt create mode 100644 owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/responses/ShareResponse.kt diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/CreateRemoteShareOperation.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/CreateRemoteShareOperation.kt index 896f570a..29379646 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/CreateRemoteShareOperation.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/CreateRemoteShareOperation.kt @@ -136,10 +136,13 @@ class CreateRemoteShareOperation( if (result.isSuccess && retrieveShareDetails) { // retrieve more info - POST only returns the index of the new share val emptyShare = result.data.shares[0] - val getInfo = GetRemoteShareOperation( + val getShares = GetRemoteShareOperation( emptyShare.id ) - result = getInfo.execute(client) + val remoteOperationResult = getShares.execute(client) + + result = RemoteOperationResult(remoteOperationResult) + result.data = ShareParserResult(remoteOperationResult.data.toRemoteShare()) } } else { diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/GetRemoteShareOperation.java b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/GetRemoteShareOperation.java deleted file mode 100644 index 9e71252e..00000000 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/GetRemoteShareOperation.java +++ /dev/null @@ -1,94 +0,0 @@ -/* ownCloud Android Library is available under MIT license - * @author David A. Velasco - * @author David González Verdugo - * Copyright (C) 2020 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 timber.log.Timber; - -import java.net.URL; - -/** - * Get the data about a Share resource, known its remote ID. - * - * @author David A. Velasco - * @author David González Verdugo - */ - -public class GetRemoteShareOperation extends RemoteOperation { - - private String mRemoteId; - - public GetRemoteShareOperation(String remoteId) { - mRemoteId = remoteId; - } - - @Override - protected RemoteOperationResult run(OwnCloudClient client) { - RemoteOperationResult result; - - try { - Uri requestUri = client.getBaseUri(); - Uri.Builder uriBuilder = requestUri.buildUpon(); - uriBuilder.appendEncodedPath(ShareUtils.SHARING_API_PATH); - uriBuilder.appendEncodedPath(mRemoteId); - - GetMethod getMethod = new GetMethod(new URL(uriBuilder.build().toString())); - getMethod.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE); - - int status = client.executeHttpMethod(getMethod); - - if (isSuccess(status)) { - // 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(getMethod.getResponseBodyAsString()); - - } else { - result = new RemoteOperationResult<>(getMethod); - } - - } catch (Exception e) { - result = new RemoteOperationResult<>(e); - Timber.e(e, "Exception while getting remote shares"); - } - return result; - } - - private boolean isSuccess(int status) { - return (status == HttpConstants.HTTP_OK); - } -} \ No newline at end of file diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/GetRemoteShareOperation.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/GetRemoteShareOperation.kt new file mode 100644 index 00000000..9bf4d1d8 --- /dev/null +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/GetRemoteShareOperation.kt @@ -0,0 +1,115 @@ +/* + * ownCloud Android client application + * + * @author Fernando Sanz Velasco + * Copyright (C) 2021 ownCloud GmbH. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * + * + * + */ + +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.resources.CommonOcsResponse +import com.owncloud.android.lib.resources.shares.responses.ShareResponse +import com.squareup.moshi.JsonAdapter +import com.squareup.moshi.Moshi +import com.squareup.moshi.Types +import timber.log.Timber +import java.lang.reflect.Type +import java.net.URL + +class GetRemoteShareOperation(private val remoteId: String) : RemoteOperation() { + + private fun buildRequestUri(baseUri: Uri) = + baseUri.buildUpon() + .appendEncodedPath(OCS_ROUTE) + .appendEncodedPath(remoteId) + .appendQueryParameter(PARAM_FORMAT, VALUE_FORMAT) + .build() + + private fun parseResponse(response: String): ShareResponse? { + val moshi = Moshi.Builder().build() + val type: Type = Types.newParameterizedType(CommonOcsResponse::class.java, ShareResponse::class.java) + val adapter: JsonAdapter> = moshi.adapter(type) + return adapter.fromJson(response)!!.ocs.data + } + + private fun onResultUnsuccessful( + method: GetMethod, + response: String?, + status: Int + ): RemoteOperationResult { + Timber.e("Failed response while while getting remote shares ") + if (response != null) { + Timber.e("*** status code: $status; response message: $response") + } else { + Timber.e("*** status code: $status") + } + return RemoteOperationResult(method) + } + + private fun onRequestSuccessful(response: String?): RemoteOperationResult { + val result = RemoteOperationResult(RemoteOperationResult.ResultCode.OK) + Timber.d("Successful response: $response") + result.data = parseResponse(response!!) + Timber.d("*** Get Users or groups completed ") + return result + } + + override fun run(client: OwnCloudClient): RemoteOperationResult { + val requestUri = buildRequestUri(client.baseUri) + + val getMethod = GetMethod(URL(requestUri.toString())).apply { + addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE) + } + + return try { + val status = client.executeHttpMethod(getMethod) + val response = getMethod.getResponseBodyAsString() + + if (!isSuccess(status)) { + onResultUnsuccessful(getMethod, response, status) + } else { + onRequestSuccessful(response) + } + } catch (e: Exception) { + Timber.e(e, "Exception while getting remote shares") + RemoteOperationResult(e) + } + } + + private fun isSuccess(status: Int) = status == HttpConstants.HTTP_OK + + companion object { + + //OCS Route + private const val OCS_ROUTE = "ocs/v2.php/apps/files_sharing/api/v1/shares" + + //Arguments - names + private const val PARAM_FORMAT = "format" + + //Arguments - constant values + private const val VALUE_FORMAT = "json" + + } +} diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/UpdateRemoteShareOperation.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/UpdateRemoteShareOperation.kt index 0df80bc9..c1173d66 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/UpdateRemoteShareOperation.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/UpdateRemoteShareOperation.kt @@ -166,10 +166,13 @@ class UpdateRemoteShareOperation if (result.isSuccess && retrieveShareDetails) { // retrieve more info - PUT only returns the index of the new share val emptyShare = result.data.shares.first() - val getInfo = GetRemoteShareOperation( + val getShares = GetRemoteShareOperation( emptyShare.id ) - result = getInfo.execute(client) + val remoteOperationResult = getShares.execute(client) + + result = RemoteOperationResult(remoteOperationResult) + result.data = ShareParserResult(remoteOperationResult.data.toRemoteShare()) } } catch (e: Exception) { diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/responses/ShareResponse.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/responses/ShareResponse.kt new file mode 100644 index 00000000..6c472670 --- /dev/null +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/responses/ShareResponse.kt @@ -0,0 +1,72 @@ +/* + * ownCloud Android client application + * + * @author Fernando Sanz Velasco + * Copyright (C) 2021 ownCloud GmbH. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * + * + * + */ + +package com.owncloud.android.lib.resources.shares.responses + +import com.owncloud.android.lib.resources.shares.RemoteShare +import com.owncloud.android.lib.resources.shares.RemoteShare.Companion.DEFAULT_PERMISSION +import com.owncloud.android.lib.resources.shares.RemoteShare.Companion.INIT_EXPIRATION_DATE_IN_MILLIS +import com.owncloud.android.lib.resources.shares.RemoteShare.Companion.INIT_SHARED_DATE +import com.owncloud.android.lib.resources.shares.ShareType +import com.squareup.moshi.JsonClass +import java.io.File + + +@JsonClass(generateAdapter = true) +data class ShareResponse( + val shares: List +) { + fun toRemoteShare() = this.shares.map { shareItem -> + RemoteShare( + id = shareItem.id ?: "0", + shareWith = shareItem.shareWith.orEmpty(), + path = shareItem.path.orEmpty(), + token = shareItem.token.orEmpty(), + sharedWithDisplayName = shareItem.sharedWithDisplayName.orEmpty(), + sharedWithAdditionalInfo = shareItem.sharedWithAdditionalInfo.orEmpty(), + name = shareItem.name.orEmpty(), + shareLink = shareItem.shareLink.orEmpty(), + shareType = ShareType.values().firstOrNull { it.value == shareItem.shareType } ?: ShareType.UNKNOWN, + permissions = shareItem.permissions ?: DEFAULT_PERMISSION, + sharedDate = shareItem.sharedDate ?: INIT_SHARED_DATE, + expirationDate = shareItem.expirationDate ?: INIT_EXPIRATION_DATE_IN_MILLIS, + isFolder = shareItem.path?.endsWith(File.separator) ?: false + ) + } +} + +@JsonClass(generateAdapter = true) +data class ShareItem( + val id: String? = null, + val shareWith: String? = null, + val path: String? = null, + val token: String? = null, + val sharedWithDisplayName: String? = null, + val sharedWithAdditionalInfo: String? = null, + val name: String? = null, + val shareLink: String? = null, + val shareType: Int? = null, + val permissions: Int? = null, + val sharedDate: Long? = null, + val expirationDate: Long? = null, +) From 45fb12df0e39fc70026d8a195a8807f3a4910162 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Abel=20Garci=CC=81a=20de=20Prada?= Date: Fri, 27 Aug 2021 15:38:49 +0200 Subject: [PATCH 02/10] Fix shares parsing --- .../shares/CreateRemoteShareOperation.kt | 8 ++-- .../shares/GetRemoteShareOperation.kt | 13 ++++-- .../shares/GetRemoteSharesForFileOperation.kt | 6 +-- .../shares/RemoveRemoteShareOperation.kt | 6 +-- ...{ShareParserResult.kt => ShareResponse.kt} | 2 +- .../ShareToRemoteOperationResultParser.kt | 6 +-- .../shares/UpdateRemoteShareOperation.kt | 11 ++--- .../{ShareResponse.kt => ShareItem.kt} | 42 ++++++++----------- .../resources/shares/services/ShareService.kt | 10 ++--- .../services/implementation/OCShareService.kt | 10 ++--- 10 files changed, 54 insertions(+), 60 deletions(-) rename owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/{ShareParserResult.kt => ShareResponse.kt} (95%) rename owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/responses/{ShareResponse.kt => ShareItem.kt} (62%) diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/CreateRemoteShareOperation.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/CreateRemoteShareOperation.kt index 29379646..1db73eae 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/CreateRemoteShareOperation.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/CreateRemoteShareOperation.kt @@ -70,7 +70,7 @@ class CreateRemoteShareOperation( private val shareType: ShareType, private val shareWith: String, private val permissions: Int -) : RemoteOperation() { +) : RemoteOperation() { var name = "" // Name to set for the public link var password: String = "" // Password to set for the public link @@ -81,8 +81,8 @@ class CreateRemoteShareOperation( var retrieveShareDetails = false // To retrieve more info about the just created share - override fun run(client: OwnCloudClient): RemoteOperationResult { - var result: RemoteOperationResult + override fun run(client: OwnCloudClient): RemoteOperationResult { + var result: RemoteOperationResult try { val formBodyBuilder = FormBody.Builder() @@ -142,7 +142,7 @@ class CreateRemoteShareOperation( val remoteOperationResult = getShares.execute(client) result = RemoteOperationResult(remoteOperationResult) - result.data = ShareParserResult(remoteOperationResult.data.toRemoteShare()) + result.data = remoteOperationResult.data } } else { diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/GetRemoteShareOperation.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/GetRemoteShareOperation.kt index 9bf4d1d8..9e0cfd71 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/GetRemoteShareOperation.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/GetRemoteShareOperation.kt @@ -30,7 +30,7 @@ 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.resources.CommonOcsResponse -import com.owncloud.android.lib.resources.shares.responses.ShareResponse +import com.owncloud.android.lib.resources.shares.responses.ShareItem import com.squareup.moshi.JsonAdapter import com.squareup.moshi.Moshi import com.squareup.moshi.Types @@ -49,9 +49,14 @@ class GetRemoteShareOperation(private val remoteId: String) : RemoteOperation> = moshi.adapter(type) - return adapter.fromJson(response)!!.ocs.data + val listOfShareItemType: Type = Types.newParameterizedType(List::class.java, ShareItem::class.java) + val commonOcsType: Type = Types.newParameterizedType(CommonOcsResponse::class.java, listOfShareItemType) + val adapter: JsonAdapter>> = moshi.adapter(commonOcsType) + return adapter.fromJson(response)?.ocs?.data?.let { listOfShareItems -> + ShareResponse(listOfShareItems.map { shareItem -> + shareItem.toRemoteShare() + }) + } } private fun onResultUnsuccessful( diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/GetRemoteSharesForFileOperation.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/GetRemoteSharesForFileOperation.kt index ea1d7e2a..6a866e51 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/GetRemoteSharesForFileOperation.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/GetRemoteSharesForFileOperation.kt @@ -59,10 +59,10 @@ class GetRemoteSharesForFileOperation( private val remoteFilePath: String, private val reshares: Boolean, private val subfiles: Boolean -) : RemoteOperation() { +) : RemoteOperation() { - override fun run(client: OwnCloudClient): RemoteOperationResult { - var result: RemoteOperationResult + override fun run(client: OwnCloudClient): RemoteOperationResult { + var result: RemoteOperationResult try { diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/RemoveRemoteShareOperation.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/RemoveRemoteShareOperation.kt index a7d18e5d..a9ff69c6 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/RemoveRemoteShareOperation.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/RemoveRemoteShareOperation.kt @@ -48,10 +48,10 @@ import java.net.URL * * @param remoteShareId Share ID */ -class RemoveRemoteShareOperation(private val remoteShareId: String) : RemoteOperation() { +class RemoveRemoteShareOperation(private val remoteShareId: String) : RemoteOperation() { - override fun run(client: OwnCloudClient): RemoteOperationResult { - var result: RemoteOperationResult + override fun run(client: OwnCloudClient): RemoteOperationResult { + var result: RemoteOperationResult try { val requestUri = client.baseUri diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/ShareParserResult.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/ShareResponse.kt similarity index 95% rename from owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/ShareParserResult.kt rename to owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/ShareResponse.kt index dd5f19df..0421064c 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/ShareParserResult.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/ShareResponse.kt @@ -25,4 +25,4 @@ package com.owncloud.android.lib.resources.shares -class ShareParserResult(val shares: List) +data class ShareResponse(val shares: List) diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/ShareToRemoteOperationResultParser.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/ShareToRemoteOperationResultParser.kt index 6e55fd51..389ded4e 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/ShareToRemoteOperationResultParser.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/ShareToRemoteOperationResultParser.kt @@ -41,12 +41,12 @@ class ShareToRemoteOperationResultParser(private var shareXmlParser: ShareXMLPar var ownCloudVersion: OwnCloudVersion? = null var serverBaseUri: Uri? = null - fun parse(serverResponse: String?): RemoteOperationResult { + fun parse(serverResponse: String?): RemoteOperationResult { if (serverResponse.isNullOrEmpty()) { return RemoteOperationResult(RemoteOperationResult.ResultCode.WRONG_SERVER_RESPONSE) } - var result: RemoteOperationResult + var result: RemoteOperationResult val resultData: List? try { @@ -82,7 +82,7 @@ class ShareToRemoteOperationResultParser(private var shareXmlParser: ShareXMLPar } if (resultData != null) { - result.setData(ShareParserResult(ArrayList(resultData.toMutableList()))) + result.setData(ShareResponse(ArrayList(resultData.toMutableList()))) } } else { diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/UpdateRemoteShareOperation.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/UpdateRemoteShareOperation.kt index c1173d66..0ddb69a1 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/UpdateRemoteShareOperation.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/UpdateRemoteShareOperation.kt @@ -57,7 +57,7 @@ class UpdateRemoteShareOperation */ private val remoteId: String -) : RemoteOperation() { +) : RemoteOperation() { /** * Name to update in Share resource. Ignored by servers previous to version 10.0.0 * @@ -99,8 +99,8 @@ class UpdateRemoteShareOperation var retrieveShareDetails = false // To retrieve more info about the just updated share - override fun run(client: OwnCloudClient): RemoteOperationResult { - var result: RemoteOperationResult + override fun run(client: OwnCloudClient): RemoteOperationResult { + var result: RemoteOperationResult try { val formBodyBuilder = FormBody.Builder() @@ -169,10 +169,7 @@ class UpdateRemoteShareOperation val getShares = GetRemoteShareOperation( emptyShare.id ) - val remoteOperationResult = getShares.execute(client) - - result = RemoteOperationResult(remoteOperationResult) - result.data = ShareParserResult(remoteOperationResult.data.toRemoteShare()) + result = getShares.execute(client) } } catch (e: Exception) { diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/responses/ShareResponse.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/responses/ShareItem.kt similarity index 62% rename from owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/responses/ShareResponse.kt rename to owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/responses/ShareItem.kt index 6c472670..9f1ad0c9 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/responses/ShareResponse.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/responses/ShareItem.kt @@ -31,30 +31,6 @@ import com.owncloud.android.lib.resources.shares.ShareType import com.squareup.moshi.JsonClass import java.io.File - -@JsonClass(generateAdapter = true) -data class ShareResponse( - val shares: List -) { - fun toRemoteShare() = this.shares.map { shareItem -> - RemoteShare( - id = shareItem.id ?: "0", - shareWith = shareItem.shareWith.orEmpty(), - path = shareItem.path.orEmpty(), - token = shareItem.token.orEmpty(), - sharedWithDisplayName = shareItem.sharedWithDisplayName.orEmpty(), - sharedWithAdditionalInfo = shareItem.sharedWithAdditionalInfo.orEmpty(), - name = shareItem.name.orEmpty(), - shareLink = shareItem.shareLink.orEmpty(), - shareType = ShareType.values().firstOrNull { it.value == shareItem.shareType } ?: ShareType.UNKNOWN, - permissions = shareItem.permissions ?: DEFAULT_PERMISSION, - sharedDate = shareItem.sharedDate ?: INIT_SHARED_DATE, - expirationDate = shareItem.expirationDate ?: INIT_EXPIRATION_DATE_IN_MILLIS, - isFolder = shareItem.path?.endsWith(File.separator) ?: false - ) - } -} - @JsonClass(generateAdapter = true) data class ShareItem( val id: String? = null, @@ -69,4 +45,20 @@ data class ShareItem( val permissions: Int? = null, val sharedDate: Long? = null, val expirationDate: Long? = null, -) +) { + fun toRemoteShare() = RemoteShare( + id = id ?: "0", + shareWith = shareWith.orEmpty(), + path = path.orEmpty(), + token = token.orEmpty(), + sharedWithDisplayName = sharedWithDisplayName.orEmpty(), + sharedWithAdditionalInfo = sharedWithAdditionalInfo.orEmpty(), + name = name.orEmpty(), + shareLink = shareLink.orEmpty(), + shareType = ShareType.values().firstOrNull { it.value == shareType } ?: ShareType.UNKNOWN, + permissions = permissions ?: DEFAULT_PERMISSION, + sharedDate = sharedDate ?: INIT_SHARED_DATE, + expirationDate = expirationDate ?: INIT_EXPIRATION_DATE_IN_MILLIS, + isFolder = path?.endsWith(File.separator) ?: false + ) +} diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/services/ShareService.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/services/ShareService.kt index caca3cd8..728217bb 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/services/ShareService.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/services/ShareService.kt @@ -22,7 +22,7 @@ package com.owncloud.android.lib.resources.shares.services import com.owncloud.android.lib.common.operations.RemoteOperationResult import com.owncloud.android.lib.resources.Service -import com.owncloud.android.lib.resources.shares.ShareParserResult +import com.owncloud.android.lib.resources.shares.ShareResponse import com.owncloud.android.lib.resources.shares.ShareType interface ShareService : Service { @@ -30,7 +30,7 @@ interface ShareService : Service { remoteFilePath: String, reshares: Boolean, subfiles: Boolean - ): RemoteOperationResult + ): RemoteOperationResult fun insertShare( remoteFilePath: String, @@ -41,7 +41,7 @@ interface ShareService : Service { password: String, expirationDate: Long, publicUpload: Boolean - ): RemoteOperationResult + ): RemoteOperationResult fun updateShare( remoteId: String, @@ -50,7 +50,7 @@ interface ShareService : Service { expirationDate: Long, permissions: Int, publicUpload: Boolean - ): RemoteOperationResult + ): RemoteOperationResult - fun deleteShare(remoteId: String): RemoteOperationResult + fun deleteShare(remoteId: String): RemoteOperationResult } diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/services/implementation/OCShareService.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/services/implementation/OCShareService.kt index 109c2204..0d9f36b1 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/services/implementation/OCShareService.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/services/implementation/OCShareService.kt @@ -25,7 +25,7 @@ import com.owncloud.android.lib.common.operations.RemoteOperationResult import com.owncloud.android.lib.resources.shares.CreateRemoteShareOperation import com.owncloud.android.lib.resources.shares.GetRemoteSharesForFileOperation import com.owncloud.android.lib.resources.shares.RemoveRemoteShareOperation -import com.owncloud.android.lib.resources.shares.ShareParserResult +import com.owncloud.android.lib.resources.shares.ShareResponse import com.owncloud.android.lib.resources.shares.ShareType import com.owncloud.android.lib.resources.shares.UpdateRemoteShareOperation import com.owncloud.android.lib.resources.shares.services.ShareService @@ -36,7 +36,7 @@ class OCShareService(override val client: OwnCloudClient) : remoteFilePath: String, reshares: Boolean, subfiles: Boolean - ): RemoteOperationResult = GetRemoteSharesForFileOperation( + ): RemoteOperationResult = GetRemoteSharesForFileOperation( remoteFilePath, reshares, subfiles @@ -51,7 +51,7 @@ class OCShareService(override val client: OwnCloudClient) : password: String, expirationDate: Long, publicUpload: Boolean - ): RemoteOperationResult = + ): RemoteOperationResult = CreateRemoteShareOperation( remoteFilePath, shareType, @@ -72,7 +72,7 @@ class OCShareService(override val client: OwnCloudClient) : expirationDate: Long, permissions: Int, publicUpload: Boolean - ): RemoteOperationResult = + ): RemoteOperationResult = UpdateRemoteShareOperation( remoteId ).apply { @@ -84,7 +84,7 @@ class OCShareService(override val client: OwnCloudClient) : this.retrieveShareDetails = true }.execute(client) - override fun deleteShare(remoteId: String): RemoteOperationResult = + override fun deleteShare(remoteId: String): RemoteOperationResult = RemoveRemoteShareOperation( remoteId ).execute(client) From 037a2b30df75fcfc1a573658a6e1b0595b1482d3 Mon Sep 17 00:00:00 2001 From: Fernando Sanz Date: Tue, 31 Aug 2021 08:52:55 +0200 Subject: [PATCH 03/10] Fixed some bugs on how to create shares. --- .../lib/resources/shares/RemoteShare.kt | 5 +-- .../resources/shares/responses/ShareItem.kt | 35 ++++++++++++++++--- 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/RemoteShare.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/RemoteShare.kt index 320da5fc..f34f2b6c 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/RemoteShare.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/RemoteShare.kt @@ -24,7 +24,7 @@ package com.owncloud.android.lib.resources.shares -import java.io.File +import com.owncloud.android.lib.resources.shares.responses.ItemType /** * Contains the data of a Share from the Share API @@ -38,6 +38,7 @@ data class RemoteShare( var shareWith: String = "", var path: String = "", var token: String = "", + var itemType: String = "", var sharedWithDisplayName: String = "", var sharedWithAdditionalInfo: String = "", var name: String = "", @@ -46,7 +47,7 @@ data class RemoteShare( var permissions: Int = DEFAULT_PERMISSION, var sharedDate: Long = INIT_SHARED_DATE, var expirationDate: Long = INIT_EXPIRATION_DATE_IN_MILLIS, - var isFolder: Boolean = path.endsWith(File.separator) + var isFolder: Boolean = (itemType == ItemType.FOLDER.fileValue) ) { companion object { diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/responses/ShareItem.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/responses/ShareItem.kt index 9f1ad0c9..d84c39d5 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/responses/ShareItem.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/responses/ShareItem.kt @@ -23,34 +23,57 @@ package com.owncloud.android.lib.resources.shares.responses +import com.owncloud.android.lib.common.network.WebdavUtils import com.owncloud.android.lib.resources.shares.RemoteShare import com.owncloud.android.lib.resources.shares.RemoteShare.Companion.DEFAULT_PERMISSION import com.owncloud.android.lib.resources.shares.RemoteShare.Companion.INIT_EXPIRATION_DATE_IN_MILLIS import com.owncloud.android.lib.resources.shares.RemoteShare.Companion.INIT_SHARED_DATE import com.owncloud.android.lib.resources.shares.ShareType +import com.squareup.moshi.Json import com.squareup.moshi.JsonClass import java.io.File @JsonClass(generateAdapter = true) data class ShareItem( val id: String? = null, + + @Json(name = "share_with") val shareWith: String? = null, + val path: String? = null, val token: String? = null, + + @Json(name = "item_type") + val itemType: String? = null, + + @Json(name = "share_with_displayname") val sharedWithDisplayName: String? = null, + + @Json(name = "share_with_additional_info") val sharedWithAdditionalInfo: String? = null, + val name: String? = null, + + @Json(name = "url") val shareLink: String? = null, + + @Json(name = "share_type") val shareType: Int? = null, + val permissions: Int? = null, + + @Json(name = "stime") val sharedDate: Long? = null, - val expirationDate: Long? = null, + + @Json(name = "expiration") + val expirationDate: String? = null, ) { fun toRemoteShare() = RemoteShare( id = id ?: "0", shareWith = shareWith.orEmpty(), - path = path.orEmpty(), + path = if (itemType == ItemType.FOLDER.fileValue) path.plus(File.separator) else path.orEmpty(), token = token.orEmpty(), + itemType = itemType.orEmpty(), sharedWithDisplayName = sharedWithDisplayName.orEmpty(), sharedWithAdditionalInfo = sharedWithAdditionalInfo.orEmpty(), name = name.orEmpty(), @@ -58,7 +81,11 @@ data class ShareItem( shareType = ShareType.values().firstOrNull { it.value == shareType } ?: ShareType.UNKNOWN, permissions = permissions ?: DEFAULT_PERMISSION, sharedDate = sharedDate ?: INIT_SHARED_DATE, - expirationDate = expirationDate ?: INIT_EXPIRATION_DATE_IN_MILLIS, - isFolder = path?.endsWith(File.separator) ?: false + expirationDate = expirationDate?.let { + WebdavUtils.parseResponseDate(it)?.time + } ?: INIT_EXPIRATION_DATE_IN_MILLIS, + isFolder = itemType?.equals(ItemType.FOLDER.fileValue) ?: false ) } + +enum class ItemType(val fileValue: String) { FILE("file"), FOLDER("folder") } From b40a394ab1e347187db6af36ec4ffeac145f5825 Mon Sep 17 00:00:00 2001 From: Fernando Sanz Date: Tue, 31 Aug 2021 10:43:29 +0200 Subject: [PATCH 04/10] The ShareXMLParser has been removed from CreateRemoteShareOperation.kt class. --- .../shares/CreateRemoteShareOperation.kt | 214 ++++++++++-------- 1 file changed, 120 insertions(+), 94 deletions(-) diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/CreateRemoteShareOperation.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/CreateRemoteShareOperation.kt index 1db73eae..edf283b0 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/CreateRemoteShareOperation.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/CreateRemoteShareOperation.kt @@ -1,53 +1,51 @@ -/* ownCloud Android Library is available under MIT license - * @author masensio - * @author David A. Velasco - * @author David González Verdugo - * Copyright (C) 2020 ownCloud GmbH +/* + * ownCloud Android client application + * + * @author masensio + * @author David A. Velasco + * @author David González Verdugo + * @author Fernando Sanz Velasco + * Copyright (C) 2021 ownCloud GmbH. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . * - * 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.PostMethod import com.owncloud.android.lib.common.operations.RemoteOperation import com.owncloud.android.lib.common.operations.RemoteOperationResult +import com.owncloud.android.lib.resources.CommonOcsResponse import com.owncloud.android.lib.resources.shares.RemoteShare.Companion.INIT_EXPIRATION_DATE_IN_MILLIS +import com.owncloud.android.lib.resources.shares.responses.ShareItem +import com.squareup.moshi.JsonAdapter +import com.squareup.moshi.Moshi +import com.squareup.moshi.Types import okhttp3.FormBody import timber.log.Timber +import java.lang.reflect.Type import java.net.URL import java.text.SimpleDateFormat import java.util.Calendar import java.util.Locale -/** - * Creates a new share. This allows sharing with a user or group or as a link. - * - * @author masensio - * @author David A. Velasco - * @author David González Verdugo - */ - /** * Constructor * @@ -71,6 +69,7 @@ class CreateRemoteShareOperation( private val shareWith: String, private val permissions: Int ) : RemoteOperation() { + var name = "" // Name to set for the public link var password: String = "" // Password to set for the public link @@ -81,93 +80,120 @@ class CreateRemoteShareOperation( var retrieveShareDetails = false // To retrieve more info about the just created share + private fun buildRequestUri(baseUri: Uri) = + baseUri.buildUpon() + .appendEncodedPath(OCS_ROUTE) + .appendQueryParameter(PARAM_FORMAT, VALUE_FORMAT) + .build() + + private fun parseResponse(response: String): ShareResponse? { + val moshi = Moshi.Builder().build() + val commonOcsType: Type = Types.newParameterizedType(CommonOcsResponse::class.java, ShareItem::class.java) + val adapter: JsonAdapter> = moshi.adapter(commonOcsType) + val remoteShare = adapter.fromJson(response)?.ocs?.data?.toRemoteShare() + return ShareResponse(remoteShare?.let { listOf(it) } ?: listOf()) + } + + private fun onResultUnsuccessful( + method: PostMethod, + response: String?, + status: Int + ): RemoteOperationResult { + Timber.e("Failed response while while creating new remote share operation ") + if (response != null) { + Timber.e("*** status code: $status; response message: $response") + } else { + Timber.e("*** status code: $status") + } + return RemoteOperationResult(method) + } + + private fun onRequestSuccessful(response: String?): RemoteOperationResult { + val result = RemoteOperationResult(RemoteOperationResult.ResultCode.OK) + Timber.d("Successful response: $response") + result.data = parseResponse(response!!) + Timber.d("*** Creating new remote share operation completed ") + return result + } + + private fun createFormBodyBuilder(): FormBody.Builder { + + val formBodyBuilder = FormBody.Builder() + .add(PARAM_PATH, remoteFilePath) + .add(PARAM_SHARE_TYPE, shareType.value.toString()) + .add(PARAM_SHARE_WITH, shareWith) + + if (name.isNotEmpty()) { + formBodyBuilder.add(PARAM_NAME, name) + } + + if (expirationDateInMillis > INIT_EXPIRATION_DATE_IN_MILLIS) { + val dateFormat = SimpleDateFormat(FORMAT_EXPIRATION_DATE, Locale.getDefault()) + val expirationDate = Calendar.getInstance() + expirationDate.timeInMillis = expirationDateInMillis + val formattedExpirationDate = dateFormat.format(expirationDate.time) + formBodyBuilder.add(PARAM_EXPIRATION_DATE, formattedExpirationDate) + } + + if (publicUpload) { + formBodyBuilder.add(PARAM_PUBLIC_UPLOAD, publicUpload.toString()) + } + if (password.isNotEmpty()) { + formBodyBuilder.add(PARAM_PASSWORD, password) + } + if (RemoteShare.DEFAULT_PERMISSION != permissions) { + formBodyBuilder.add(PARAM_PERMISSIONS, permissions.toString()) + } + + return formBodyBuilder + } + override fun run(client: OwnCloudClient): RemoteOperationResult { - var result: RemoteOperationResult + val requestUri = buildRequestUri(client.baseUri) - try { - val formBodyBuilder = FormBody.Builder() - .add(PARAM_PATH, remoteFilePath) - .add(PARAM_SHARE_TYPE, shareType.value.toString()) - .add(PARAM_SHARE_WITH, shareWith) + val formBodyBuilder = createFormBodyBuilder() - if (name.isNotEmpty()) { - formBodyBuilder.add(PARAM_NAME, name) - } - - if (expirationDateInMillis > INIT_EXPIRATION_DATE_IN_MILLIS) { - val dateFormat = SimpleDateFormat(FORMAT_EXPIRATION_DATE, Locale.getDefault()) - val expirationDate = Calendar.getInstance() - expirationDate.timeInMillis = expirationDateInMillis - val formattedExpirationDate = dateFormat.format(expirationDate.time) - formBodyBuilder.add(PARAM_EXPIRATION_DATE, formattedExpirationDate) - } - - if (publicUpload) { - formBodyBuilder.add(PARAM_PUBLIC_UPLOAD, publicUpload.toString()) - } - if (password.isNotEmpty()) { - formBodyBuilder.add(PARAM_PASSWORD, password) - } - if (RemoteShare.DEFAULT_PERMISSION != permissions) { - formBodyBuilder.add(PARAM_PERMISSIONS, permissions.toString()) - } - - val requestUri = client.baseUri - val uriBuilder = requestUri.buildUpon() - uriBuilder.appendEncodedPath(ShareUtils.SHARING_API_PATH) - - val postMethod = PostMethod(URL(uriBuilder.build().toString()), formBodyBuilder.build()) - - postMethod.setRequestHeader(HttpConstants.CONTENT_TYPE_HEADER, HttpConstants.CONTENT_TYPE_URLENCODED_UTF8) - postMethod.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE) + val postMethod = PostMethod(URL(requestUri.toString()), formBodyBuilder.build()).apply { + setRequestHeader(HttpConstants.CONTENT_TYPE_HEADER, HttpConstants.CONTENT_TYPE_URLENCODED_UTF8) + addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE) + } + return try { val status = client.executeHttpMethod(postMethod) + val response = postMethod.getResponseBodyAsString() - val parser = ShareToRemoteOperationResultParser( - ShareXMLParser() - ) - - if (isSuccess(status)) { - parser.oneOrMoreSharesRequired = true - parser.ownCloudVersion = client.ownCloudVersion - parser.serverBaseUri = client.baseUri - result = parser.parse(postMethod.getResponseBodyAsString()) - - if (result.isSuccess && retrieveShareDetails) { - // retrieve more info - POST only returns the index of the new share - val emptyShare = result.data.shares[0] - val getShares = GetRemoteShareOperation( - emptyShare.id - ) - val remoteOperationResult = getShares.execute(client) - - result = RemoteOperationResult(remoteOperationResult) - result.data = remoteOperationResult.data - } - + if (!isSuccess(status)) { + onResultUnsuccessful(postMethod, response, status) } else { - result = parser.parse(postMethod.getResponseBodyAsString()) + onRequestSuccessful(response) } } catch (e: Exception) { - result = RemoteOperationResult(e) - Timber.e(e, "Exception while Creating New Share") + Timber.e(e, "Exception while creating new remote share operation ") + RemoteOperationResult(e) } - - return result } private fun isSuccess(status: Int): Boolean = status == HttpConstants.HTTP_OK companion object { + + //OCS Route + private const val OCS_ROUTE = "ocs/v2.php/apps/files_sharing/api/v1/shares" + + //Arguments - names + private const val PARAM_FORMAT = "format" private const val PARAM_NAME = "name" - private const val PARAM_PASSWORD = "password" private const val PARAM_EXPIRATION_DATE = "expireDate" - private const val PARAM_PUBLIC_UPLOAD = "publicUpload" private const val PARAM_PATH = "path" private const val PARAM_SHARE_TYPE = "shareType" private const val PARAM_SHARE_WITH = "shareWith" + private const val PARAM_PASSWORD = "password" + private const val PARAM_PUBLIC_UPLOAD = "publicUpload" private const val PARAM_PERMISSIONS = "permissions" + + //Arguments - constant values + private const val VALUE_FORMAT = "json" private const val FORMAT_EXPIRATION_DATE = "yyyy-MM-dd" } } From 6285c6c70a71786aaaa9f9132add8f7a99f919d5 Mon Sep 17 00:00:00 2001 From: Fernando Sanz Date: Tue, 31 Aug 2021 11:59:41 +0200 Subject: [PATCH 05/10] The ShareXMLParser has been removed from GetRemoteSharesForFileOperation.kt class. --- .../shares/GetRemoteSharesForFileOperation.kt | 157 +++++++++++------- 1 file changed, 97 insertions(+), 60 deletions(-) diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/GetRemoteSharesForFileOperation.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/GetRemoteSharesForFileOperation.kt index 6a866e51..0df6632b 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/GetRemoteSharesForFileOperation.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/GetRemoteSharesForFileOperation.kt @@ -1,48 +1,50 @@ -/* ownCloud Android Library is available under MIT license - * @author masensio - * @author David A. Velasco - * @author David González Verdugo - * Copyright (C) 2020 ownCloud GmbH. +/* + * ownCloud Android client application + * + * @author masensio + * @author David A. Velasco + * @author David González Verdugo + * @author Fernando Sanz Velasco + * Copyright (C) 2021 ownCloud GmbH. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . * - * 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.resources.CommonOcsResponse +import com.owncloud.android.lib.resources.shares.responses.ShareItem +import com.squareup.moshi.JsonAdapter +import com.squareup.moshi.Moshi +import com.squareup.moshi.Types import timber.log.Timber +import java.lang.reflect.Type import java.net.URL /** * 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. - * - * @author masensio - * @author David A. Velasco - * @author David González Verdugo */ /** @@ -61,52 +63,87 @@ class GetRemoteSharesForFileOperation( private val subfiles: Boolean ) : RemoteOperation() { + private fun buildRequestUri(baseUri: Uri) = + baseUri.buildUpon() + .appendEncodedPath(OCS_ROUTE) + .appendQueryParameter(PARAM_FORMAT, VALUE_FORMAT) + .appendQueryParameter(PARAM_PATH, remoteFilePath) + .appendQueryParameter(PARAM_RESHARES, reshares.toString()) + .appendQueryParameter(PARAM_SUBFILES, subfiles.toString()) + .build() + + private fun parseResponse(response: String): ShareResponse? { + val moshi = Moshi.Builder().build() + val listOfShareItemType: Type = Types.newParameterizedType(List::class.java, ShareItem::class.java) + val commonOcsType: Type = Types.newParameterizedType(CommonOcsResponse::class.java, listOfShareItemType) + val adapter: JsonAdapter>> = moshi.adapter(commonOcsType) + return adapter.fromJson(response)?.ocs?.data?.let { listOfShareItems -> + ShareResponse(listOfShareItems.map { shareItem -> + shareItem.toRemoteShare() + }) + } + } + + private fun onResultUnsuccessful( + method: GetMethod, + response: String?, + status: Int + ): RemoteOperationResult { + Timber.e("Failed response while while getting remote shares for file operation ") + if (response != null) { + Timber.e("*** status code: $status; response message: $response") + } else { + Timber.e("*** status code: $status") + } + return RemoteOperationResult(method) + } + + private fun onRequestSuccessful(response: String?): RemoteOperationResult { + val result = RemoteOperationResult(RemoteOperationResult.ResultCode.OK) + Timber.d("Successful response: $response") + result.data = parseResponse(response!!) + Timber.d("*** Getting remote shares for file completed ") + Timber.d("Got ${result.data.shares.size} shares") + return result + } + override fun run(client: OwnCloudClient): RemoteOperationResult { - var result: RemoteOperationResult + val requestUri = buildRequestUri(client.baseUri) - try { - - val requestUri = client.baseUri - val uriBuilder = requestUri.buildUpon() - uriBuilder.appendEncodedPath(ShareUtils.SHARING_API_PATH) - uriBuilder.appendQueryParameter(PARAM_PATH, remoteFilePath) - uriBuilder.appendQueryParameter(PARAM_RESHARES, reshares.toString()) - uriBuilder.appendQueryParameter(PARAM_SUBFILES, subfiles.toString()) - - val getMethod = GetMethod(URL(uriBuilder.build().toString())) - - getMethod.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE) - - val status = client.executeHttpMethod(getMethod) - - if (isSuccess(status)) { - // Parse xml response and obtain the list of shares - val parser = ShareToRemoteOperationResultParser( - ShareXMLParser() - ) - parser.ownCloudVersion = client.ownCloudVersion - parser.serverBaseUri = client.baseUri - result = parser.parse(getMethod.getResponseBodyAsString()) - - if (result.isSuccess) { - Timber.d("Got ${result.data.shares.size} shares") - } - } else { - result = RemoteOperationResult(getMethod) - } - } catch (e: Exception) { - result = RemoteOperationResult(e) - Timber.e(e, "Exception while getting shares") + val getMethod = GetMethod(URL(requestUri.toString())).apply { + addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE) } - return result + return try { + val status = client.executeHttpMethod(getMethod) + val response = getMethod.getResponseBodyAsString() + + if (!isSuccess(status)) { + onResultUnsuccessful(getMethod, response, status) + } else { + onRequestSuccessful(response) + } + } catch (e: Exception) { + Timber.e(e, "Exception while getting remote shares for file operation") + RemoteOperationResult(e) + } } private fun isSuccess(status: Int): Boolean = status == HttpConstants.HTTP_OK companion object { + + //OCS Route + private const val OCS_ROUTE = "ocs/v2.php/apps/files_sharing/api/v1/shares" + + //Arguments - names + private const val PARAM_FORMAT = "format" private const val PARAM_PATH = "path" private const val PARAM_RESHARES = "reshares" private const val PARAM_SUBFILES = "subfiles" + + //Arguments - constant values + private const val VALUE_FORMAT = "json" + } } From 7baf3e43a5954fa155a61192472ab2c61efa7a18 Mon Sep 17 00:00:00 2001 From: Fernando Sanz Date: Tue, 31 Aug 2021 12:37:36 +0200 Subject: [PATCH 06/10] The ShareXMLParser has been removed from RemoveRemoteShareOperation.kt class. --- .../shares/RemoveRemoteShareOperation.kt | 156 +++++++++++------- 1 file changed, 98 insertions(+), 58 deletions(-) diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/RemoveRemoteShareOperation.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/RemoveRemoteShareOperation.kt index a9ff69c6..ec5eedda 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/RemoveRemoteShareOperation.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/RemoveRemoteShareOperation.kt @@ -1,46 +1,48 @@ -/* ownCloud Android Library is available under MIT license - * @author masensio - * @author David A. Velasco - * @author David González Verdugo - * Copyright (C) 2020 ownCloud GmbH. +/* + * ownCloud Android client application + * + * @author masensio + * @author David A. Velasco + * @author David González Verdugo + * @author Fernando Sanz Velasco + * Copyright (C) 2021 ownCloud GmbH. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . * - * 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.DeleteMethod import com.owncloud.android.lib.common.operations.RemoteOperation import com.owncloud.android.lib.common.operations.RemoteOperationResult +import com.owncloud.android.lib.resources.CommonOcsResponse +import com.owncloud.android.lib.resources.shares.responses.ShareItem +import com.squareup.moshi.JsonAdapter +import com.squareup.moshi.Moshi +import com.squareup.moshi.Types import timber.log.Timber +import java.lang.reflect.Type import java.net.URL /** * Remove a share - * - * @author masensio - * @author David A. Velasco - * @author David González Verdugo */ /** @@ -50,44 +52,82 @@ import java.net.URL */ class RemoveRemoteShareOperation(private val remoteShareId: String) : RemoteOperation() { - override fun run(client: OwnCloudClient): RemoteOperationResult { - var result: RemoteOperationResult + private fun buildRequestUri(baseUri: Uri) = + baseUri.buildUpon() + .appendEncodedPath(OCS_ROUTE) + .appendEncodedPath(remoteShareId) + .appendQueryParameter(PARAM_FORMAT, VALUE_FORMAT) + .build() - try { - val requestUri = client.baseUri - val uriBuilder = requestUri.buildUpon() - uriBuilder.appendEncodedPath(ShareUtils.SHARING_API_PATH) - uriBuilder.appendEncodedPath(remoteShareId) - - val deleteMethod = DeleteMethod( - URL(uriBuilder.build().toString()) - ) - - deleteMethod.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE) - - val status = client.executeHttpMethod(deleteMethod) - - if (isSuccess(status)) { - - // Parse xml response and obtain the list of shares - val parser = ShareToRemoteOperationResultParser( - ShareXMLParser() - ) - result = parser.parse(deleteMethod.getResponseBodyAsString()) - - Timber.d("Unshare $remoteShareId: ${result.logMessage}") - - } else { - result = RemoteOperationResult(deleteMethod) - } - - } catch (e: Exception) { - result = RemoteOperationResult(e) - Timber.e(e, "Unshare Link Exception ${result.logMessage}") + private fun parseResponse(response: String): ShareResponse? { + val moshi = Moshi.Builder().build() + val listOfShareItemType: Type = Types.newParameterizedType(List::class.java, ShareItem::class.java) + val commonOcsType: Type = Types.newParameterizedType(CommonOcsResponse::class.java, listOfShareItemType) + val adapter: JsonAdapter>> = moshi.adapter(commonOcsType) + return adapter.fromJson(response)?.ocs?.data?.let { listOfShareItems -> + ShareResponse(listOfShareItems.map { shareItem -> + shareItem.toRemoteShare() + }) } + } + private fun onResultUnsuccessful( + method: DeleteMethod, + response: String?, + status: Int + ): RemoteOperationResult { + Timber.e("Failed response while unshare link ") + if (response != null) { + Timber.e("*** status code: $status; response message: $response") + } else { + Timber.e("*** status code: $status") + } + return RemoteOperationResult(method) + } + + private fun onRequestSuccessful(response: String?): RemoteOperationResult { + val result = RemoteOperationResult(RemoteOperationResult.ResultCode.OK) + Timber.d("Successful response: $response") + result.data = parseResponse(response!!) + Timber.d("*** Unshare link completed ") return result } + override fun run(client: OwnCloudClient): RemoteOperationResult { + + val requestUri = buildRequestUri(client.baseUri) + + val deleteMethod = DeleteMethod(URL(requestUri.toString())).apply { + addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE) + } + + return try { + val status = client.executeHttpMethod(deleteMethod) + val response = deleteMethod.getResponseBodyAsString() + + if (!isSuccess(status)) { + onResultUnsuccessful(deleteMethod, response, status) + } else { + onRequestSuccessful(response) + } + } catch (e: Exception) { + Timber.e(e, "Exception while unshare link") + RemoteOperationResult(e) + } + } + private fun isSuccess(status: Int): Boolean = status == HttpConstants.HTTP_OK + + companion object { + + //OCS Route + private const val OCS_ROUTE = "ocs/v2.php/apps/files_sharing/api/v1/shares" + + //Arguments - names + private const val PARAM_FORMAT = "format" + + //Arguments - constant values + private const val VALUE_FORMAT = "json" + + } } From 91bd322b68c45db0cdba82e7e956826a3bb735fd Mon Sep 17 00:00:00 2001 From: Fernando Sanz Date: Tue, 31 Aug 2021 14:47:48 +0200 Subject: [PATCH 07/10] The ShareXMLParser has been removed from UpdateRemoteShareOperation.kt class. --- .../ShareToRemoteOperationResultParser.kt | 120 ----- .../lib/resources/shares/ShareUtils.java | 41 -- .../lib/resources/shares/ShareXMLParser.kt | 420 ------------------ .../shares/UpdateRemoteShareOperation.kt | 229 ++++++---- 4 files changed, 132 insertions(+), 678 deletions(-) delete mode 100644 owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/ShareToRemoteOperationResultParser.kt delete mode 100644 owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/ShareUtils.java delete mode 100644 owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/ShareXMLParser.kt diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/ShareToRemoteOperationResultParser.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/ShareToRemoteOperationResultParser.kt deleted file mode 100644 index 389ded4e..00000000 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/ShareToRemoteOperationResultParser.kt +++ /dev/null @@ -1,120 +0,0 @@ -/* ownCloud Android Library is available under MIT license - * @author David A. Velasco - * @author David González Verdugo - * @author Christian Schabesberger - * Copyright (C) 2020 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.operations.RemoteOperationResult -import com.owncloud.android.lib.resources.status.OwnCloudVersion -import org.xmlpull.v1.XmlPullParserException -import timber.log.Timber -import java.io.ByteArrayInputStream -import java.io.IOException -import java.util.ArrayList - -class ShareToRemoteOperationResultParser(private var shareXmlParser: ShareXMLParser?) { - var oneOrMoreSharesRequired = false - var ownCloudVersion: OwnCloudVersion? = null - var serverBaseUri: Uri? = null - - fun parse(serverResponse: String?): RemoteOperationResult { - if (serverResponse.isNullOrEmpty()) { - return RemoteOperationResult(RemoteOperationResult.ResultCode.WRONG_SERVER_RESPONSE) - } - - var result: RemoteOperationResult - val resultData: List? - - try { - // Parse xml response and obtain the list of shares - val byteArrayServerResponse = ByteArrayInputStream(serverResponse.toByteArray()) - if (shareXmlParser == null) { - Timber.w("No ShareXmlParser provided, creating new instance") - shareXmlParser = ShareXMLParser() - } - val shares = shareXmlParser?.parseXMLResponse(byteArrayServerResponse) - - when { - shareXmlParser?.isSuccess!! -> { - if (!shares.isNullOrEmpty() || !oneOrMoreSharesRequired) { - result = RemoteOperationResult(RemoteOperationResult.ResultCode.OK) - - resultData = shares?.map { share -> - if (share.shareType != ShareType.PUBLIC_LINK || - share.shareLink.isNotEmpty() || - share.token.isEmpty() - ) { - return@map share - } - - if (serverBaseUri != null) { - val sharingLinkPath = ShareUtils.SHARING_LINK_PATH - share.shareLink = serverBaseUri.toString() + sharingLinkPath + share.token - } else { - Timber.e("Couldn't build link for public share :(") - } - - share - } - - if (resultData != null) { - result.setData(ShareResponse(ArrayList(resultData.toMutableList()))) - } - - } else { - result = RemoteOperationResult(RemoteOperationResult.ResultCode.WRONG_SERVER_RESPONSE) - Timber.e("Successful status with no share in the response") - } - } - shareXmlParser?.isWrongParameter!! -> { - result = RemoteOperationResult(RemoteOperationResult.ResultCode.SHARE_WRONG_PARAMETER) - result.httpPhrase = shareXmlParser?.message - } - shareXmlParser?.isNotFound!! -> { - result = RemoteOperationResult(RemoteOperationResult.ResultCode.SHARE_NOT_FOUND) - result.httpPhrase = shareXmlParser?.message - } - shareXmlParser?.isForbidden!! -> { - result = RemoteOperationResult(RemoteOperationResult.ResultCode.SHARE_FORBIDDEN) - result.httpPhrase = shareXmlParser?.message - } - else -> { - result = RemoteOperationResult(RemoteOperationResult.ResultCode.WRONG_SERVER_RESPONSE) - } - } - } catch (e: XmlPullParserException) { - Timber.e(e, "Error parsing response from server") - result = RemoteOperationResult(RemoteOperationResult.ResultCode.WRONG_SERVER_RESPONSE) - - } catch (e: IOException) { - Timber.e(e, "Error reading response from server") - result = RemoteOperationResult(RemoteOperationResult.ResultCode.WRONG_SERVER_RESPONSE) - } - - return result - } -} diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/ShareUtils.java b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/ShareUtils.java deleted file mode 100644 index 518666a1..00000000 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/ShareUtils.java +++ /dev/null @@ -1,41 +0,0 @@ -/* ownCloud Android Library is available under MIT license - * Copyright (C) 2020 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; - -/** - * Contains Constants for Share Operation - * - * @author masensio - * @author David González Verdugo - */ - -public class ShareUtils { - - // OCS Route - public static final String SHARING_API_PATH = "ocs/v2.php/apps/files_sharing/api/v1/shares"; - - // String to build the link with the token of a share: - public static final String SHARING_LINK_PATH = "/index.php/s/"; -} diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/ShareXMLParser.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/ShareXMLParser.kt deleted file mode 100644 index a0f0e0b9..00000000 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/ShareXMLParser.kt +++ /dev/null @@ -1,420 +0,0 @@ -/* ownCloud Android Library is available under MIT license - * Copyright (C) 2020 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.util.Xml -import com.owncloud.android.lib.common.network.WebdavUtils -import org.xmlpull.v1.XmlPullParser -import org.xmlpull.v1.XmlPullParserException -import org.xmlpull.v1.XmlPullParserFactory -import java.io.File -import java.io.IOException -import java.io.InputStream -import java.util.ArrayList - -/** - * Parser for Share API Response - * - * @author masensio - * @author David González Verdugo - */ - -class ShareXMLParser { - // Getters and Setters - var status: String? = null - var statusCode: Int = 0 - var message: String? = null - - val isSuccess: Boolean - get() = statusCode == SUCCESS - - val isForbidden: Boolean - get() = statusCode == ERROR_FORBIDDEN - - val isNotFound: Boolean - get() = statusCode == ERROR_NOT_FOUND - - val isWrongParameter: Boolean - get() = statusCode == ERROR_WRONG_PARAMETER - - // Constructor - init { - statusCode = INIT - } - - /** - * Parse is as response of Share API - * @param inputStream - * @return List of ShareRemoteFiles - * @throws XmlPullParserException - * @throws IOException - */ - @Throws(XmlPullParserException::class, IOException::class) - fun parseXMLResponse(inputStream: InputStream): ArrayList { - - try { - // XMLPullParser - val factory = XmlPullParserFactory.newInstance() - factory.isNamespaceAware = true - - val parser = Xml.newPullParser() - parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false) - parser.setInput(inputStream, null) - parser.nextTag() - return readOCS(parser) - - } finally { - inputStream.close() - } - } - - /** - * Parse OCS node - * @param parser - * @return List of ShareRemoteFiles - * @throws XmlPullParserException - * @throws IOException - */ - @Throws(XmlPullParserException::class, IOException::class) - private fun readOCS(parser: XmlPullParser): ArrayList { - var shares = ArrayList() - parser.require(XmlPullParser.START_TAG, ns, NODE_OCS) - while (parser.next() != XmlPullParser.END_TAG) { - if (parser.eventType != XmlPullParser.START_TAG) { - continue - } - val name = parser.name - // read NODE_META and NODE_DATA - when { - name.equals(NODE_META, ignoreCase = true) -> { - readMeta(parser) - } - name.equals(NODE_DATA, ignoreCase = true) -> { - shares = readData(parser) - } - else -> { - skip(parser) - } - } - } - return shares - } - - /** - * Parse Meta node - * @param parser - * @throws XmlPullParserException - * @throws IOException - */ - @Throws(XmlPullParserException::class, IOException::class) - private fun readMeta(parser: XmlPullParser) { - parser.require(XmlPullParser.START_TAG, ns, NODE_META) - while (parser.next() != XmlPullParser.END_TAG) { - if (parser.eventType != XmlPullParser.START_TAG) { - continue - } - val name = parser.name - - when { - name.equals(NODE_STATUS, ignoreCase = true) -> { - status = readNode(parser, NODE_STATUS) - } - name.equals(NODE_STATUS_CODE, ignoreCase = true) -> { - statusCode = Integer.parseInt(readNode(parser, NODE_STATUS_CODE)) - } - name.equals(NODE_MESSAGE, ignoreCase = true) -> { - message = readNode(parser, NODE_MESSAGE) - } - else -> { - skip(parser) - } - } - } - } - - /** - * Parse Data node - * @param parser - * @return - * @throws XmlPullParserException - * @throws IOException - */ - @Throws(XmlPullParserException::class, IOException::class) - private fun readData(parser: XmlPullParser): ArrayList { - val shares = ArrayList() - var share: RemoteShare? = null - - parser.require(XmlPullParser.START_TAG, ns, NODE_DATA) - while (parser.next() != XmlPullParser.END_TAG) { - if (parser.eventType != XmlPullParser.START_TAG) { - continue - } - val name = parser.name - when { - name.equals(NODE_ELEMENT, ignoreCase = true) -> { - readElement(parser, shares) - } - name.equals(NODE_ID, ignoreCase = true) -> { // Parse Create XML Response - share = RemoteShare() - val value = readNode(parser, NODE_ID) - share.id = value - } - name.equals(NODE_URL, ignoreCase = true) -> { - // NOTE: this field is received in all the public shares from OC 9.0.0 - // in previous versions, it's received in the result of POST requests, but not - // in GET requests - share!!.shareType = ShareType.PUBLIC_LINK - val value = readNode(parser, NODE_URL) - share.shareLink = value - } - name.equals(NODE_TOKEN, ignoreCase = true) -> { - share!!.token = readNode(parser, NODE_TOKEN) - } - else -> { - skip(parser) - } - } - } - - if (share != null) { - // this is the response of a request for creation; don't pass to isValidShare() - shares.add(share) - } - - return shares - - } - - /** - * Parse Element node - * @param parser - * @return - * @throws XmlPullParserException - * @throws IOException - */ - @Throws(XmlPullParserException::class, IOException::class) - private fun readElement(parser: XmlPullParser, shares: ArrayList) { - parser.require(XmlPullParser.START_TAG, ns, NODE_ELEMENT) - - val remoteShare = RemoteShare() - - while (parser.next() != XmlPullParser.END_TAG) { - if (parser.eventType != XmlPullParser.START_TAG) { - continue - } - - val name = parser.name - - when { - name.equals(NODE_ELEMENT, ignoreCase = true) -> { - // patch to work around servers responding with extra surrounding all - // the shares on the same file before - // https://github.com/owncloud/core/issues/6992 was fixed - readElement(parser, shares) - } - - name.equals(NODE_ID, ignoreCase = true) -> { - remoteShare.id = readNode(parser, NODE_ID) - } - - name.equals(NODE_ITEM_TYPE, ignoreCase = true) -> { - remoteShare.isFolder = readNode(parser, NODE_ITEM_TYPE).equals(TYPE_FOLDER, ignoreCase = true) - fixPathForFolder(remoteShare) - } - - name.equals(NODE_PARENT, ignoreCase = true) -> { - readNode(parser, NODE_PARENT) - } - - name.equals(NODE_SHARE_TYPE, ignoreCase = true) -> { - val value = Integer.parseInt(readNode(parser, NODE_SHARE_TYPE)) - remoteShare.shareType = ShareType.fromValue(value) - } - - name.equals(NODE_SHARE_WITH, ignoreCase = true) -> { - remoteShare.shareWith = readNode(parser, NODE_SHARE_WITH) - } - - 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) - } - } - } - - shares.add(remoteShare) - } - - private fun fixPathForFolder(share: RemoteShare) { - if (share.isFolder && share.path.isNotEmpty() && - !share.path.endsWith(File.separator) - ) { - share.path = share.path + File.separator - } - } - - /** - * Parse a node, to obtain its text. Needs readText method - * @param parser - * @param node - * @return Text of the node - * @throws XmlPullParserException - * @throws IOException - */ - @Throws(XmlPullParserException::class, IOException::class) - private fun readNode(parser: XmlPullParser, node: String): String { - parser.require(XmlPullParser.START_TAG, ns, node) - val value = readText(parser) - parser.require(XmlPullParser.END_TAG, ns, node) - return value - } - - /** - * Read the text from a node - * @param parser - * @return Text of the node - * @throws IOException - * @throws XmlPullParserException - */ - @Throws(IOException::class, XmlPullParserException::class) - private fun readText(parser: XmlPullParser): String { - var result = "" - if (parser.next() == XmlPullParser.TEXT) { - result = parser.text - parser.nextTag() - } - return result - } - - /** - * Skip tags in parser procedure - * @param parser - * @throws XmlPullParserException - * @throws IOException - */ - @Throws(XmlPullParserException::class, IOException::class) - private fun skip(parser: XmlPullParser) { - if (parser.eventType != XmlPullParser.START_TAG) { - throw IllegalStateException() - } - var depth = 1 - while (depth != 0) { - when (parser.next()) { - XmlPullParser.END_TAG -> depth-- - XmlPullParser.START_TAG -> depth++ - } - } - } - - companion object { - - // No namespaces - private val ns: String? = null - - // NODES for XML Parser - private const val NODE_OCS = "ocs" - - private const val NODE_META = "meta" - private const val NODE_STATUS = "status" - private const val NODE_STATUS_CODE = "statuscode" - private const val NODE_MESSAGE = "message" - - private const val NODE_DATA = "data" - private const val NODE_ELEMENT = "element" - private const val NODE_ID = "id" - private const val NODE_ITEM_TYPE = "item_type" - private const val NODE_PARENT = "parent" - private const val NODE_SHARE_TYPE = "share_type" - private const val NODE_SHARE_WITH = "share_with" - private const val NODE_PATH = "path" - private const val NODE_PERMISSIONS = "permissions" - private const val NODE_STIME = "stime" - private const val NODE_EXPIRATION = "expiration" - private const val NODE_TOKEN = "token" - private const val NODE_STORAGE = "storage" - private const val NODE_MAIL_SEND = "mail_send" - private const val NODE_SHARE_WITH_DISPLAY_NAME = "share_with_displayname" - private const val NODE_SHARE_WITH_ADDITIONAL_INFO = "share_with_additional_info" - private const val NODE_NAME = "name" - - private const val NODE_URL = "url" - - private const val TYPE_FOLDER = "folder" - - private const val SUCCESS = 200 - private const val ERROR_WRONG_PARAMETER = 400 - private const val ERROR_FORBIDDEN = 403 - private const val ERROR_NOT_FOUND = 404 - private const val INIT = -1 - } -} diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/UpdateRemoteShareOperation.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/UpdateRemoteShareOperation.kt index 0ddb69a1..dcec411a 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/UpdateRemoteShareOperation.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/UpdateRemoteShareOperation.kt @@ -1,38 +1,45 @@ -/* ownCloud Android Library is available under MIT license +/* + * ownCloud Android client application * - * Copyright (C) 2020 ownCloud GmbH. + * @author David A. Velasco + * @author David González Verdugo + * @author Fernando Sanz Velasco + * Copyright (C) 2021 ownCloud GmbH. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . * - * 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.PutMethod import com.owncloud.android.lib.common.operations.RemoteOperation import com.owncloud.android.lib.common.operations.RemoteOperationResult +import com.owncloud.android.lib.resources.CommonOcsResponse import com.owncloud.android.lib.resources.shares.RemoteShare.Companion.DEFAULT_PERMISSION +import com.owncloud.android.lib.resources.shares.responses.ShareItem +import com.squareup.moshi.JsonAdapter +import com.squareup.moshi.Moshi +import com.squareup.moshi.Types import okhttp3.FormBody import timber.log.Timber +import java.lang.reflect.Type import java.net.URL import java.text.SimpleDateFormat import java.util.Calendar @@ -40,12 +47,7 @@ import java.util.Locale /** * Updates parameters of an existing Share resource, known its remote ID. - * - * * Allow updating several parameters, triggering a request to the server per parameter. - * - * @author David A. Velasco - * @author David González Verdugo */ class UpdateRemoteShareOperation /** @@ -99,100 +101,133 @@ class UpdateRemoteShareOperation var retrieveShareDetails = false // To retrieve more info about the just updated share - override fun run(client: OwnCloudClient): RemoteOperationResult { - var result: RemoteOperationResult + private fun buildRequestUri(baseUri: Uri) = + baseUri.buildUpon() + .appendEncodedPath(OCS_ROUTE) + .appendEncodedPath(remoteId) + .appendQueryParameter(PARAM_FORMAT, VALUE_FORMAT) + .build() - try { - val formBodyBuilder = FormBody.Builder() + private fun parseResponse(response: String): ShareResponse? { + val moshi = Moshi.Builder().build() + val commonOcsType: Type = Types.newParameterizedType(CommonOcsResponse::class.java, ShareItem::class.java) + val adapter: JsonAdapter> = moshi.adapter(commonOcsType) + val remoteShare = adapter.fromJson(response)?.ocs?.data?.toRemoteShare() + return ShareResponse(remoteShare?.let { listOf(it) } ?: listOf()) + } - // Parameters to update - if (name != null) { - formBodyBuilder.add(PARAM_NAME, name!!) - } + private fun onResultUnsuccessful( + method: PutMethod, + response: String?, + status: Int + ): RemoteOperationResult { + Timber.e("Failed response while while updating remote shares ") + if (response != null) { + Timber.e("*** status code: $status; response message: $response") + } else { + Timber.e("*** status code: $status") + } + return RemoteOperationResult(method) + } - if (password != null) { - formBodyBuilder.add(PARAM_PASSWORD, password!!) - } + private fun onRequestSuccessful(response: String?): RemoteOperationResult { + val result = RemoteOperationResult(RemoteOperationResult.ResultCode.OK) + Timber.d("Successful response: $response") + result.data = parseResponse(response!!) + Timber.d("*** Retrieve the index of the new share completed ") + val emptyShare = result.data.shares.first() - if (expirationDateInMillis < INITIAL_EXPIRATION_DATE_IN_MILLIS) { - // clear expiration date - formBodyBuilder.add(PARAM_EXPIRATION_DATE, "") + return if (retrieveShareDetails) { + // retrieve more info - PUT only returns the index of the new share + GetRemoteShareOperation(emptyShare.id).execute(client) + } else { + result + } + } - } else if (expirationDateInMillis > INITIAL_EXPIRATION_DATE_IN_MILLIS) { - // set expiration date - val dateFormat = SimpleDateFormat(FORMAT_EXPIRATION_DATE, Locale.getDefault()) - val expirationDate = Calendar.getInstance() - expirationDate.timeInMillis = expirationDateInMillis - val formattedExpirationDate = dateFormat.format(expirationDate.time) - formBodyBuilder.add(PARAM_EXPIRATION_DATE, formattedExpirationDate) - } // else, ignore - no update + private fun createFormBodyBuilder(): FormBody.Builder { + val formBodyBuilder = FormBody.Builder() - if (publicUpload != null) { - formBodyBuilder.add(PARAM_PUBLIC_UPLOAD, publicUpload.toString()) - } - - // IMPORTANT: permissions parameter needs to be updated after mPublicUpload parameter, - // otherwise they would be set always as 1 (READ) in the server when mPublicUpload was updated - if (permissions > DEFAULT_PERMISSION) { - // set permissions - formBodyBuilder.add(PARAM_PERMISSIONS, permissions.toString()) - } - - val requestUri = client.baseUri - val uriBuilder = requestUri.buildUpon() - uriBuilder.appendEncodedPath(ShareUtils.SHARING_API_PATH) - uriBuilder.appendEncodedPath(remoteId.toString()) - - val putMethod = PutMethod(URL(uriBuilder.build().toString()), formBodyBuilder.build()) - - putMethod.setRequestHeader(HttpConstants.CONTENT_TYPE_HEADER, HttpConstants.CONTENT_TYPE_URLENCODED_UTF8) - putMethod.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE) - - val status = client.executeHttpMethod(putMethod) - - // Parse xml response - val parser = ShareToRemoteOperationResultParser( - ShareXMLParser() - ) - - if (!isSuccess(status)) { - return parser.parse(putMethod.getResponseBodyAsString()) - } - - parser.ownCloudVersion = client.ownCloudVersion - parser.serverBaseUri = client.baseUri - result = parser.parse(putMethod.getResponseBodyAsString()) - - if (result.isSuccess && retrieveShareDetails) { - // retrieve more info - PUT only returns the index of the new share - val emptyShare = result.data.shares.first() - val getShares = GetRemoteShareOperation( - emptyShare.id - ) - result = getShares.execute(client) - } - - } catch (e: Exception) { - result = RemoteOperationResult(e) - Timber.e(e, "Exception while Creating New Share") + // Parameters to update + if (name != null) { + formBodyBuilder.add(PARAM_NAME, name!!) } - return result + if (password != null) { + formBodyBuilder.add(PARAM_PASSWORD, password!!) + } + + if (expirationDateInMillis < INITIAL_EXPIRATION_DATE_IN_MILLIS) { + // clear expiration date + formBodyBuilder.add(PARAM_EXPIRATION_DATE, "") + + } else if (expirationDateInMillis > INITIAL_EXPIRATION_DATE_IN_MILLIS) { + // set expiration date + val dateFormat = SimpleDateFormat(FORMAT_EXPIRATION_DATE, Locale.getDefault()) + val expirationDate = Calendar.getInstance() + expirationDate.timeInMillis = expirationDateInMillis + val formattedExpirationDate = dateFormat.format(expirationDate.time) + formBodyBuilder.add(PARAM_EXPIRATION_DATE, formattedExpirationDate) + } // else, ignore - no update + + if (publicUpload != null) { + formBodyBuilder.add(PARAM_PUBLIC_UPLOAD, publicUpload.toString()) + } + + // IMPORTANT: permissions parameter needs to be updated after mPublicUpload parameter, + // otherwise they would be set always as 1 (READ) in the server when mPublicUpload was updated + if (permissions > DEFAULT_PERMISSION) { + // set permissions + formBodyBuilder.add(PARAM_PERMISSIONS, permissions.toString()) + } + + return formBodyBuilder + } + + override fun run(client: OwnCloudClient): RemoteOperationResult { + val requestUri = buildRequestUri(client.baseUri) + + val formBodyBuilder = createFormBodyBuilder() + + val putMethod = PutMethod(URL(requestUri.toString()), formBodyBuilder.build()).apply { + setRequestHeader(HttpConstants.CONTENT_TYPE_HEADER, HttpConstants.CONTENT_TYPE_URLENCODED_UTF8) + addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE) + } + + + return try { + val status = client.executeHttpMethod(putMethod) + val response = putMethod.getResponseBodyAsString() + + if (!isSuccess(status)) { + onResultUnsuccessful(putMethod, response, status) + } else { + onRequestSuccessful(response) + } + } catch (e: Exception) { + Timber.e(e, "Exception while updating remote share") + RemoteOperationResult(e) + } } private fun isSuccess(status: Int): Boolean = status == HttpConstants.HTTP_OK companion object { + //OCS Route + private const val OCS_ROUTE = "ocs/v2.php/apps/files_sharing/api/v1/shares" + + //Arguments - names + private const val PARAM_FORMAT = "format" private const val PARAM_NAME = "name" private const val PARAM_PASSWORD = "password" private const val PARAM_EXPIRATION_DATE = "expireDate" private const val PARAM_PERMISSIONS = "permissions" private const val PARAM_PUBLIC_UPLOAD = "publicUpload" - private const val FORMAT_EXPIRATION_DATE = "yyyy-MM-dd" - private const val ENTITY_CONTENT_TYPE = "application/x-www-form-urlencoded" - private const val ENTITY_CHARSET = "UTF-8" + //Arguments - constant values + private const val VALUE_FORMAT = "json" + private const val FORMAT_EXPIRATION_DATE = "yyyy-MM-dd" private const val INITIAL_EXPIRATION_DATE_IN_MILLIS: Long = 0 } } From 0895732ed4abcd0925ba0f7cbb0ada25b1fbddaa Mon Sep 17 00:00:00 2001 From: Fernando Sanz Date: Wed, 1 Sep 2021 13:23:49 +0200 Subject: [PATCH 08/10] Library's copyright has been modified and restored. --- .../shares/CreateRemoteShareOperation.kt | 51 +++++++++++-------- .../shares/GetRemoteShareOperation.kt | 36 ++++++------- .../shares/GetRemoteSharesForFileOperation.kt | 47 +++++++++-------- .../shares/RemoveRemoteShareOperation.kt | 47 +++++++++-------- .../shares/UpdateRemoteShareOperation.kt | 47 ++++++++++------- .../resources/shares/responses/ShareItem.kt | 36 ++++++------- 6 files changed, 151 insertions(+), 113 deletions(-) diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/CreateRemoteShareOperation.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/CreateRemoteShareOperation.kt index edf283b0..5ab4732c 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/CreateRemoteShareOperation.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/CreateRemoteShareOperation.kt @@ -1,26 +1,28 @@ -/* - * ownCloud Android client application - * - * @author masensio - * @author David A. Velasco - * @author David González Verdugo - * @author Fernando Sanz Velasco - * Copyright (C) 2021 ownCloud GmbH. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License version 2, - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . +/* ownCloud Android Library is available under MIT license + * @author masensio + * @author David A. Velasco + * @author David González Verdugo + * @author Fernando Sanz Velasco + * Copyright (C) 2021 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. * */ @@ -46,6 +48,15 @@ import java.text.SimpleDateFormat import java.util.Calendar import java.util.Locale +/** + * Creates a new share. This allows sharing with a user or group or as a link. + * + * @author masensio + * @author David A. Velasco + * @author David González Verdugo + * @author Fernando Sanz Velasco + */ + /** * Constructor * diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/GetRemoteShareOperation.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/GetRemoteShareOperation.kt index 9e0cfd71..cd95e6db 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/GetRemoteShareOperation.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/GetRemoteShareOperation.kt @@ -1,23 +1,25 @@ -/* - * ownCloud Android client application - * - * @author Fernando Sanz Velasco - * Copyright (C) 2021 ownCloud GmbH. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License version 2, - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . +/* ownCloud Android Library is available under MIT license + * @author Fernando Sanz Velasco + * Copyright (C) 2021 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. * */ diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/GetRemoteSharesForFileOperation.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/GetRemoteSharesForFileOperation.kt index 0df6632b..6301e8f7 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/GetRemoteSharesForFileOperation.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/GetRemoteSharesForFileOperation.kt @@ -1,26 +1,28 @@ -/* - * ownCloud Android client application - * - * @author masensio - * @author David A. Velasco - * @author David González Verdugo - * @author Fernando Sanz Velasco - * Copyright (C) 2021 ownCloud GmbH. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License version 2, - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . +/* ownCloud Android Library is available under MIT license + * @author masensio + * @author David A. Velasco + * @author David González Verdugo + * @author Fernando Sanz Velasco + * Copyright (C) 2021 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. * */ @@ -45,6 +47,11 @@ import java.net.URL * 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. + * + * @author masensio + * @author David A. Velasco + * @author David González Verdugo + * @author Fernando Sanz Velasco */ /** diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/RemoveRemoteShareOperation.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/RemoveRemoteShareOperation.kt index ec5eedda..15e5ce58 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/RemoveRemoteShareOperation.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/RemoveRemoteShareOperation.kt @@ -1,26 +1,28 @@ -/* - * ownCloud Android client application - * - * @author masensio - * @author David A. Velasco - * @author David González Verdugo - * @author Fernando Sanz Velasco - * Copyright (C) 2021 ownCloud GmbH. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License version 2, - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . +/* ownCloud Android Library is available under MIT license + * @author masensio + * @author David A. Velasco + * @author David González Verdugo + * @author Fernando Sanz Velasco + * Copyright (C) 2021 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. * */ @@ -43,6 +45,11 @@ import java.net.URL /** * Remove a share + * + * @author masensio + * @author David A. Velasco + * @author David González Verdugo + * @author Fernando Sanz Velasco */ /** diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/UpdateRemoteShareOperation.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/UpdateRemoteShareOperation.kt index dcec411a..12143227 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/UpdateRemoteShareOperation.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/UpdateRemoteShareOperation.kt @@ -1,25 +1,28 @@ -/* - * ownCloud Android client application - * - * @author David A. Velasco - * @author David González Verdugo - * @author Fernando Sanz Velasco - * Copyright (C) 2021 ownCloud GmbH. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License version 2, - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . +/* ownCloud Android Library is available under MIT license + * @author masensio + * @author David A. Velasco + * @author David González Verdugo + * @author Fernando Sanz Velasco + * Copyright (C) 2021 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. * */ @@ -47,7 +50,13 @@ import java.util.Locale /** * Updates parameters of an existing Share resource, known its remote ID. + * + * * Allow updating several parameters, triggering a request to the server per parameter. + * + * @author David A. Velasco + * @author David González Verdugo + * @author Fernando Sanz Velasco */ class UpdateRemoteShareOperation /** diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/responses/ShareItem.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/responses/ShareItem.kt index d84c39d5..18e9b897 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/responses/ShareItem.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/responses/ShareItem.kt @@ -1,23 +1,25 @@ -/* - * ownCloud Android client application - * - * @author Fernando Sanz Velasco - * Copyright (C) 2021 ownCloud GmbH. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License version 2, - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . +/* ownCloud Android Library is available under MIT license + * @author Fernando Sanz Velasco + * Copyright (C) 2021 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. * */ From f14abb0bcde1bb99274f4dbfd115f56681bbf7df Mon Sep 17 00:00:00 2001 From: Fernando Sanz Date: Thu, 2 Sep 2021 10:55:02 +0200 Subject: [PATCH 09/10] Code revision suggestions have been implemented. --- .../lib/common/http/HttpConstants.java | 14 +++++++++++++- .../shares/CreateRemoteShareOperation.kt | 19 +++++++++---------- .../shares/GetRemoteShareOperation.kt | 10 ++-------- .../shares/GetRemoteShareesOperation.kt | 10 +++++----- .../shares/GetRemoteSharesForFileOperation.kt | 13 +++++-------- .../shares/RemoveRemoteShareOperation.kt | 16 +++++----------- .../shares/UpdateRemoteShareOperation.kt | 10 +++++----- 7 files changed, 44 insertions(+), 48 deletions(-) diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/HttpConstants.java b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/HttpConstants.java index 12a2c3f6..091643ab 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/HttpConstants.java +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/HttpConstants.java @@ -66,6 +66,18 @@ public class HttpConstants { public static final String CONTENT_TYPE_JSON = "application/json"; public static final String CONTENT_TYPE_WWW_FORM = "application/x-www-form-urlencoded"; + /*********************************************************************************************************** + ************************************************ ARGUMENTS NAMES ******************************************** + ***********************************************************************************************************/ + + public static final String PARAM_FORMAT = "format"; + + /*********************************************************************************************************** + ************************************************ ARGUMENTS VALUES ******************************************** + ***********************************************************************************************************/ + + public static final String VALUE_FORMAT = "json"; + /*********************************************************************************************************** ************************************************ STATUS CODES ********************************************* ***********************************************************************************************************/ @@ -205,4 +217,4 @@ public class HttpConstants { * Default timeout for establishing a connection */ public static final int DEFAULT_CONNECTION_TIMEOUT = 60000; -} \ No newline at end of file +} diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/CreateRemoteShareOperation.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/CreateRemoteShareOperation.kt index 5ab4732c..0f946bcd 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/CreateRemoteShareOperation.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/CreateRemoteShareOperation.kt @@ -31,6 +31,8 @@ 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.HttpConstants.PARAM_FORMAT +import com.owncloud.android.lib.common.http.HttpConstants.VALUE_FORMAT import com.owncloud.android.lib.common.http.methods.nonwebdav.PostMethod import com.owncloud.android.lib.common.operations.RemoteOperation import com.owncloud.android.lib.common.operations.RemoteOperationResult @@ -127,7 +129,7 @@ class CreateRemoteShareOperation( return result } - private fun createFormBodyBuilder(): FormBody.Builder { + private fun createFormBodyBuilder(): FormBody { val formBodyBuilder = FormBody.Builder() .add(PARAM_PATH, remoteFilePath) @@ -156,15 +158,13 @@ class CreateRemoteShareOperation( formBodyBuilder.add(PARAM_PERMISSIONS, permissions.toString()) } - return formBodyBuilder + return formBodyBuilder.build() } override fun run(client: OwnCloudClient): RemoteOperationResult { val requestUri = buildRequestUri(client.baseUri) - val formBodyBuilder = createFormBodyBuilder() - - val postMethod = PostMethod(URL(requestUri.toString()), formBodyBuilder.build()).apply { + val postMethod = PostMethod(URL(requestUri.toString()), createFormBodyBuilder()).apply { setRequestHeader(HttpConstants.CONTENT_TYPE_HEADER, HttpConstants.CONTENT_TYPE_URLENCODED_UTF8) addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE) } @@ -173,10 +173,10 @@ class CreateRemoteShareOperation( val status = client.executeHttpMethod(postMethod) val response = postMethod.getResponseBodyAsString() - if (!isSuccess(status)) { - onResultUnsuccessful(postMethod, response, status) - } else { + if (isSuccess(status)) { onRequestSuccessful(response) + } else { + onResultUnsuccessful(postMethod, response, status) } } catch (e: Exception) { @@ -193,7 +193,7 @@ class CreateRemoteShareOperation( private const val OCS_ROUTE = "ocs/v2.php/apps/files_sharing/api/v1/shares" //Arguments - names - private const val PARAM_FORMAT = "format" + private const val PARAM_NAME = "name" private const val PARAM_EXPIRATION_DATE = "expireDate" private const val PARAM_PATH = "path" @@ -204,7 +204,6 @@ class CreateRemoteShareOperation( private const val PARAM_PERMISSIONS = "permissions" //Arguments - constant values - private const val VALUE_FORMAT = "json" private const val FORMAT_EXPIRATION_DATE = "yyyy-MM-dd" } } diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/GetRemoteShareOperation.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/GetRemoteShareOperation.kt index cd95e6db..0f14168e 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/GetRemoteShareOperation.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/GetRemoteShareOperation.kt @@ -28,6 +28,8 @@ 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.HttpConstants.PARAM_FORMAT +import com.owncloud.android.lib.common.http.HttpConstants.VALUE_FORMAT 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 @@ -108,15 +110,7 @@ class GetRemoteShareOperation(private val remoteId: String) : RemoteOperation Date: Thu, 2 Sep 2021 12:33:48 +0200 Subject: [PATCH 10/10] Code revision suggestions have been implemented. --- .../resources/shares/CreateRemoteShareOperation.kt | 14 +++++++++++--- .../resources/shares/UpdateRemoteShareOperation.kt | 4 ++-- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/CreateRemoteShareOperation.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/CreateRemoteShareOperation.kt index 0f946bcd..1f043cd1 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/CreateRemoteShareOperation.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/CreateRemoteShareOperation.kt @@ -126,10 +126,18 @@ class CreateRemoteShareOperation( Timber.d("Successful response: $response") result.data = parseResponse(response!!) Timber.d("*** Creating new remote share operation completed ") - return result + + val emptyShare = result.data.shares.first() + + return if (retrieveShareDetails) { + // retrieve more info - PUT only returns the index of the new share + GetRemoteShareOperation(emptyShare.id).execute(client) + } else { + result + } } - private fun createFormBodyBuilder(): FormBody { + private fun createFormBody(): FormBody { val formBodyBuilder = FormBody.Builder() .add(PARAM_PATH, remoteFilePath) @@ -164,7 +172,7 @@ class CreateRemoteShareOperation( override fun run(client: OwnCloudClient): RemoteOperationResult { val requestUri = buildRequestUri(client.baseUri) - val postMethod = PostMethod(URL(requestUri.toString()), createFormBodyBuilder()).apply { + val postMethod = PostMethod(URL(requestUri.toString()), createFormBody()).apply { setRequestHeader(HttpConstants.CONTENT_TYPE_HEADER, HttpConstants.CONTENT_TYPE_URLENCODED_UTF8) addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE) } diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/UpdateRemoteShareOperation.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/UpdateRemoteShareOperation.kt index f18a3be3..73c331ad 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/UpdateRemoteShareOperation.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/UpdateRemoteShareOperation.kt @@ -161,11 +161,11 @@ class UpdateRemoteShareOperation // Parameters to update if (name != null) { - formBodyBuilder.add(PARAM_NAME, name!!) + formBodyBuilder.add(PARAM_NAME, name.orEmpty()) } if (password != null) { - formBodyBuilder.add(PARAM_PASSWORD, password!!) + formBodyBuilder.add(PARAM_PASSWORD, password.orEmpty()) } if (expirationDateInMillis < INITIAL_EXPIRATION_DATE_IN_MILLIS) {