From 0fddb7abc405539c4b82517c77a225dc3d52174c Mon Sep 17 00:00:00 2001 From: Fernando Sanz Date: Mon, 11 Apr 2022 12:41:09 +0200 Subject: [PATCH] Fetch url to find file from server done --- .../lib/common/http/HttpConstants.java | 1 + .../GetPrivateLinkDiscoveredOperation.kt | 74 +++++++++++++++++++ .../resources/shares/services/ShareService.kt | 4 + .../services/implementation/OCShareService.kt | 7 ++ 4 files changed, 86 insertions(+) create mode 100644 owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/GetPrivateLinkDiscoveredOperation.kt 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 091643ab..e12ae150 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 @@ -50,6 +50,7 @@ public class HttpConstants { public static final String ACCEPT_ENCODING_HEADER = "Accept-Encoding"; public static final String ACCEPT_ENCODING_IDENTITY = "identity"; public static final String OC_FILE_REMOTE_ID = "OC-FileId"; + public static final String LOCATION_WEB_DAV_HEADER = "webdav-location"; // OAuth public static final String OAUTH_HEADER_AUTHORIZATION_CODE = "code"; diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/GetPrivateLinkDiscoveredOperation.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/GetPrivateLinkDiscoveredOperation.kt new file mode 100644 index 00000000..73f836d7 --- /dev/null +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/GetPrivateLinkDiscoveredOperation.kt @@ -0,0 +1,74 @@ +/** + * ownCloud Android client application + * + * @author Fernando Sanz Velasco + * Copyright (C) 2022 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 com.owncloud.android.lib.common.OwnCloudClient +import com.owncloud.android.lib.common.http.HttpConstants +import com.owncloud.android.lib.common.http.HttpConstants.LOCATION_WEB_DAV_HEADER +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 + +class GetPrivateLinkDiscoveredOperation(private val url: String) : RemoteOperation() { + + private fun onResultUnsuccessful( + status: Int, + method: GetMethod + ): RemoteOperationResult { + Timber.e("Failed response while while getting remote shares ") + 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 = response + return result + } + + override fun run(client: OwnCloudClient): RemoteOperationResult { + val getMethod = GetMethod(URL(url)) + + return try { + val status = client.executeHttpMethod(getMethod) + val header = getMethod.getResponseHeader(LOCATION_WEB_DAV_HEADER) + + if (!isSuccess(status)) { + onResultUnsuccessful(status, getMethod) + } else { + onRequestSuccessful(header) + } + } catch (e: Exception) { + Timber.e(e, "Exception while getting remote shares") + RemoteOperationResult(e) + } + } + + private fun isSuccess(status: Int) = status == HttpConstants.HTTP_SEE_OTHER + + 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/services/ShareService.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/services/ShareService.kt index 9774ca38..c95b8b1a 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 @@ -59,4 +59,8 @@ interface ShareService : Service { ): RemoteOperationResult fun deleteShare(remoteId: String): RemoteOperationResult + + fun checkPrivateLink( + url: 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 f8bcd1ea..bcf7aa4c 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 @@ -29,6 +29,7 @@ package com.owncloud.android.lib.resources.shares.services.implementation import com.owncloud.android.lib.common.OwnCloudClient import com.owncloud.android.lib.common.operations.RemoteOperationResult import com.owncloud.android.lib.resources.shares.CreateRemoteShareOperation +import com.owncloud.android.lib.resources.shares.GetPrivateLinkDiscoveredOperation import com.owncloud.android.lib.resources.shares.GetRemoteSharesForFileOperation import com.owncloud.android.lib.resources.shares.RemoveRemoteShareOperation import com.owncloud.android.lib.resources.shares.ShareResponse @@ -94,4 +95,10 @@ class OCShareService(override val client: OwnCloudClient) : RemoveRemoteShareOperation( remoteId ).execute(client) + + override fun checkPrivateLink(url: String): RemoteOperationResult { + return GetPrivateLinkDiscoveredOperation( + url + ).execute(client) + } }