mirror of
				https://github.com/owncloud/android-library.git
				synced 2025-10-31 02:17:41 +00:00 
			
		
		
		
	The way to retrieve list of shares has been modified from java to kotlin and its result has been parsed to ShareResponse.kt object.
This commit is contained in:
		
							parent
							
								
									c24ffcfaa4
								
							
						
					
					
						commit
						27b18c33a9
					
				| @ -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 { | ||||
|  | ||||
| @ -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<ShareParserResult> { | ||||
| 
 | ||||
|     private String mRemoteId; | ||||
| 
 | ||||
|     public GetRemoteShareOperation(String remoteId) { | ||||
|         mRemoteId = remoteId; | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|     protected RemoteOperationResult run(OwnCloudClient client) { | ||||
|         RemoteOperationResult<ShareParserResult> 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); | ||||
|     } | ||||
| } | ||||
| @ -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 <http://www.gnu.org/licenses/>. | ||||
|  * | ||||
|  * | ||||
|  * | ||||
|  * | ||||
|  */ | ||||
| 
 | ||||
| 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<ShareResponse>() { | ||||
| 
 | ||||
|     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<CommonOcsResponse<ShareResponse>> = moshi.adapter(type) | ||||
|         return adapter.fromJson(response)!!.ocs.data | ||||
|     } | ||||
| 
 | ||||
|     private fun onResultUnsuccessful( | ||||
|         method: GetMethod, | ||||
|         response: String?, | ||||
|         status: Int | ||||
|     ): RemoteOperationResult<ShareResponse> { | ||||
|         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<ShareResponse> { | ||||
|         val result = RemoteOperationResult<ShareResponse>(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<ShareResponse> { | ||||
|         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" | ||||
| 
 | ||||
|     } | ||||
| } | ||||
| @ -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) { | ||||
|  | ||||
| @ -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 <http://www.gnu.org/licenses/>. | ||||
|  * | ||||
|  * | ||||
|  * | ||||
|  * | ||||
|  */ | ||||
| 
 | ||||
| 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<ShareItem> | ||||
| ) { | ||||
|     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, | ||||
| ) | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user