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, +)