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 896f570a..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 @@ -2,7 +2,8 @@ * @author masensio * @author David A. Velasco * @author David González Verdugo - * Copyright (C) 2020 ownCloud GmbH + * @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 @@ -27,14 +28,23 @@ 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 +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 @@ -46,6 +56,7 @@ import java.util.Locale * @author masensio * @author David A. Velasco * @author David González Verdugo + * @author Fernando Sanz Velasco */ /** @@ -70,7 +81,8 @@ 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,90 +93,125 @@ class CreateRemoteShareOperation( var retrieveShareDetails = false // To retrieve more info about the just created share - override fun run(client: OwnCloudClient): RemoteOperationResult { - var result: RemoteOperationResult + private fun buildRequestUri(baseUri: Uri) = + baseUri.buildUpon() + .appendEncodedPath(OCS_ROUTE) + .appendQueryParameter(PARAM_FORMAT, VALUE_FORMAT) + .build() - try { - val formBodyBuilder = FormBody.Builder() - .add(PARAM_PATH, remoteFilePath) - .add(PARAM_SHARE_TYPE, shareType.value.toString()) - .add(PARAM_SHARE_WITH, shareWith) + 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()) + } - if (name.isNotEmpty()) { - formBodyBuilder.add(PARAM_NAME, name) - } + 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) + } - 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) - } + 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 ") - 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 emptyShare = result.data.shares.first() - val requestUri = client.baseUri - val uriBuilder = requestUri.buildUpon() - uriBuilder.appendEncodedPath(ShareUtils.SHARING_API_PATH) + return if (retrieveShareDetails) { + // retrieve more info - PUT only returns the index of the new share + GetRemoteShareOperation(emptyShare.id).execute(client) + } else { + result + } + } - val postMethod = PostMethod(URL(uriBuilder.build().toString()), formBodyBuilder.build()) + private fun createFormBody(): FormBody { - postMethod.setRequestHeader(HttpConstants.CONTENT_TYPE_HEADER, HttpConstants.CONTENT_TYPE_URLENCODED_UTF8) - postMethod.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE) + 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.build() + } + + override fun run(client: OwnCloudClient): RemoteOperationResult { + val requestUri = buildRequestUri(client.baseUri) + + 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) + } + + return try { val status = client.executeHttpMethod(postMethod) - - val parser = ShareToRemoteOperationResultParser( - ShareXMLParser() - ) + val response = postMethod.getResponseBodyAsString() 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 getInfo = GetRemoteShareOperation( - emptyShare.id - ) - result = getInfo.execute(client) - } - + onRequestSuccessful(response) } else { - result = parser.parse(postMethod.getResponseBodyAsString()) + onResultUnsuccessful(postMethod, response, status) } } 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_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 FORMAT_EXPIRATION_DATE = "yyyy-MM-dd" } } 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..0f14168e --- /dev/null +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/GetRemoteShareOperation.kt @@ -0,0 +1,116 @@ +/* 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. + * + */ + +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 +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 + +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 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 ") + 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" + } +} diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/GetRemoteShareesOperation.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/GetRemoteShareesOperation.kt index 546bf05b..10154820 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/GetRemoteShareesOperation.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/GetRemoteShareesOperation.kt @@ -32,6 +32,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 @@ -136,10 +138,10 @@ class GetRemoteShareesOperation val status = client.executeHttpMethod(getMethod) val response = getMethod.getResponseBodyAsString() - if (!isSuccess(status)) { - onResultUnsuccessful(getMethod, response, status) - } else { + if (isSuccess(status)) { onRequestSuccessful(response) + } else { + onResultUnsuccessful(getMethod, response, status) } } catch (e: Exception) { Timber.e(e, "Exception while getting users/groups") @@ -155,14 +157,12 @@ class GetRemoteShareesOperation private const val OCS_ROUTE = "ocs/v2.php/apps/files_sharing/api/v1/sharees" // from OC 8.2 // Arguments - names - private const val PARAM_FORMAT = "format" private const val PARAM_ITEM_TYPE = "itemType" private const val PARAM_SEARCH = "search" private const val PARAM_PAGE = "page" // default = 1 private const val PARAM_PER_PAGE = "perPage" // default = 200 // Arguments - constant values - private const val VALUE_FORMAT = "json" private const val VALUE_ITEM_TYPE = "file" // to get the server search for users / groups } } 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..adb4374e 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 @@ -2,7 +2,8 @@ * @author masensio * @author David A. Velasco * @author David González Verdugo - * Copyright (C) 2020 ownCloud GmbH. + * @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 @@ -27,12 +28,21 @@ 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 +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 /** @@ -43,6 +53,7 @@ import java.net.URL * @author masensio * @author David A. Velasco * @author David González Verdugo + * @author Fernando Sanz Velasco */ /** @@ -59,52 +70,82 @@ 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 + 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() - try { + 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() + }) + } + } - 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()) + 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) + } - val getMethod = GetMethod(URL(uriBuilder.build().toString())) + 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 + } - getMethod.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE) + override fun run(client: OwnCloudClient): RemoteOperationResult { + val requestUri = buildRequestUri(client.baseUri) - 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)) { + onRequestSuccessful(response) + } else { + onResultUnsuccessful(getMethod, response, status) + } + } 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_PATH = "path" private const val PARAM_RESHARES = "reshares" private const val PARAM_SUBFILES = "subfiles" 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/RemoveRemoteShareOperation.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/RemoveRemoteShareOperation.kt index a7d18e5d..2ce20a2c 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 @@ -2,7 +2,8 @@ * @author masensio * @author David A. Velasco * @author David González Verdugo - * Copyright (C) 2020 ownCloud GmbH. + * @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 @@ -27,12 +28,21 @@ 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.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 /** @@ -41,6 +51,7 @@ import java.net.URL * @author masensio * @author David A. Velasco * @author David González Verdugo + * @author Fernando Sanz Velasco */ /** @@ -48,46 +59,76 @@ 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 + 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)) { + onRequestSuccessful(response) + } else { + onResultUnsuccessful(deleteMethod, response, status) + } + } 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" + } } 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 deleted file mode 100644 index 6e55fd51..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(ShareParserResult(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 0df80bc9..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 @@ -1,6 +1,9 @@ /* ownCloud Android Library is available under MIT license - * - * Copyright (C) 2020 ownCloud GmbH. + * @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 @@ -25,14 +28,23 @@ 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.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 @@ -46,6 +58,7 @@ import java.util.Locale * * @author David A. Velasco * @author David González Verdugo + * @author Fernando Sanz Velasco */ class UpdateRemoteShareOperation /** @@ -57,7 +70,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,100 +112,131 @@ 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 getInfo = GetRemoteShareOperation( - emptyShare.id - ) - result = getInfo.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.orEmpty()) } - return result + if (password != null) { + formBodyBuilder.add(PARAM_PASSWORD, password.orEmpty()) + } + + 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)) { + onRequestSuccessful(response) + } else { + onResultUnsuccessful(putMethod, response, status) + } + } 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_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 FORMAT_EXPIRATION_DATE = "yyyy-MM-dd" private const val INITIAL_EXPIRATION_DATE_IN_MILLIS: Long = 0 } } 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 new file mode 100644 index 00000000..18e9b897 --- /dev/null +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/responses/ShareItem.kt @@ -0,0 +1,93 @@ +/* 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. + * + */ + +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, + + @Json(name = "expiration") + val expirationDate: String? = null, +) { + fun toRemoteShare() = RemoteShare( + id = id ?: "0", + shareWith = shareWith.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(), + shareLink = shareLink.orEmpty(), + shareType = ShareType.values().firstOrNull { it.value == shareType } ?: ShareType.UNKNOWN, + permissions = permissions ?: DEFAULT_PERMISSION, + sharedDate = sharedDate ?: INIT_SHARED_DATE, + 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") } 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)