Skip to content
This repository was archived by the owner on Mar 19, 2024. It is now read-only.

Commit 7baf3e4

Browse files
committed
The ShareXMLParser has been removed from RemoveRemoteShareOperation.kt class.
1 parent 6285c6c commit 7baf3e4

File tree

1 file changed

+91
-51
lines changed

1 file changed

+91
-51
lines changed
Lines changed: 91 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,48 @@
1-
/* ownCloud Android Library is available under MIT license
2-
* @author masensio
3-
* @author David A. Velasco
4-
* @author David González Verdugo
5-
* Copyright (C) 2020 ownCloud GmbH.
1+
/*
2+
* ownCloud Android client application
3+
*
4+
* @author masensio
5+
* @author David A. Velasco
6+
* @author David González Verdugo
7+
* @author Fernando Sanz Velasco
8+
* Copyright (C) 2021 ownCloud GmbH.
9+
*
10+
* This program is free software: you can redistribute it and/or modify
11+
* it under the terms of the GNU General Public License version 2,
12+
* as published by the Free Software Foundation.
13+
*
14+
* This program is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* GNU General Public License for more details.
18+
*
19+
* You should have received a copy of the GNU General Public License
20+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
621
*
7-
* Permission is hereby granted, free of charge, to any person obtaining a copy
8-
* of this software and associated documentation files (the "Software"), to deal
9-
* in the Software without restriction, including without limitation the rights
10-
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11-
* copies of the Software, and to permit persons to whom the Software is
12-
* furnished to do so, subject to the following conditions:
1322
*
14-
* The above copyright notice and this permission notice shall be included in
15-
* all copies or substantial portions of the Software.
1623
*
17-
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18-
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19-
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20-
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
21-
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22-
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23-
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24-
* THE SOFTWARE.
2524
*
2625
*/
2726

2827
package com.owncloud.android.lib.resources.shares
2928

29+
import android.net.Uri
3030
import com.owncloud.android.lib.common.OwnCloudClient
3131
import com.owncloud.android.lib.common.http.HttpConstants
3232
import com.owncloud.android.lib.common.http.methods.nonwebdav.DeleteMethod
3333
import com.owncloud.android.lib.common.operations.RemoteOperation
3434
import com.owncloud.android.lib.common.operations.RemoteOperationResult
35+
import com.owncloud.android.lib.resources.CommonOcsResponse
36+
import com.owncloud.android.lib.resources.shares.responses.ShareItem
37+
import com.squareup.moshi.JsonAdapter
38+
import com.squareup.moshi.Moshi
39+
import com.squareup.moshi.Types
3540
import timber.log.Timber
41+
import java.lang.reflect.Type
3642
import java.net.URL
3743

3844
/**
3945
* Remove a share
40-
*
41-
* @author masensio
42-
* @author David A. Velasco
43-
* @author David González Verdugo
4446
*/
4547

4648
/**
@@ -50,44 +52,82 @@ import java.net.URL
5052
*/
5153
class RemoveRemoteShareOperation(private val remoteShareId: String) : RemoteOperation<ShareResponse>() {
5254

53-
override fun run(client: OwnCloudClient): RemoteOperationResult<ShareResponse> {
54-
var result: RemoteOperationResult<ShareResponse>
55-
56-
try {
57-
val requestUri = client.baseUri
58-
val uriBuilder = requestUri.buildUpon()
59-
uriBuilder.appendEncodedPath(ShareUtils.SHARING_API_PATH)
60-
uriBuilder.appendEncodedPath(remoteShareId)
55+
private fun buildRequestUri(baseUri: Uri) =
56+
baseUri.buildUpon()
57+
.appendEncodedPath(OCS_ROUTE)
58+
.appendEncodedPath(remoteShareId)
59+
.appendQueryParameter(PARAM_FORMAT, VALUE_FORMAT)
60+
.build()
61+
62+
private fun parseResponse(response: String): ShareResponse? {
63+
val moshi = Moshi.Builder().build()
64+
val listOfShareItemType: Type = Types.newParameterizedType(List::class.java, ShareItem::class.java)
65+
val commonOcsType: Type = Types.newParameterizedType(CommonOcsResponse::class.java, listOfShareItemType)
66+
val adapter: JsonAdapter<CommonOcsResponse<List<ShareItem>>> = moshi.adapter(commonOcsType)
67+
return adapter.fromJson(response)?.ocs?.data?.let { listOfShareItems ->
68+
ShareResponse(listOfShareItems.map { shareItem ->
69+
shareItem.toRemoteShare()
70+
})
71+
}
72+
}
6173

62-
val deleteMethod = DeleteMethod(
63-
URL(uriBuilder.build().toString())
64-
)
74+
private fun onResultUnsuccessful(
75+
method: DeleteMethod,
76+
response: String?,
77+
status: Int
78+
): RemoteOperationResult<ShareResponse> {
79+
Timber.e("Failed response while unshare link ")
80+
if (response != null) {
81+
Timber.e("*** status code: $status; response message: $response")
82+
} else {
83+
Timber.e("*** status code: $status")
84+
}
85+
return RemoteOperationResult(method)
86+
}
6587

66-
deleteMethod.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE)
88+
private fun onRequestSuccessful(response: String?): RemoteOperationResult<ShareResponse> {
89+
val result = RemoteOperationResult<ShareResponse>(RemoteOperationResult.ResultCode.OK)
90+
Timber.d("Successful response: $response")
91+
result.data = parseResponse(response!!)
92+
Timber.d("*** Unshare link completed ")
93+
return result
94+
}
6795

68-
val status = client.executeHttpMethod(deleteMethod)
96+
override fun run(client: OwnCloudClient): RemoteOperationResult<ShareResponse> {
6997

70-
if (isSuccess(status)) {
98+
val requestUri = buildRequestUri(client.baseUri)
7199

72-
// Parse xml response and obtain the list of shares
73-
val parser = ShareToRemoteOperationResultParser(
74-
ShareXMLParser()
75-
)
76-
result = parser.parse(deleteMethod.getResponseBodyAsString())
100+
val deleteMethod = DeleteMethod(URL(requestUri.toString())).apply {
101+
addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE)
102+
}
77103

78-
Timber.d("Unshare $remoteShareId: ${result.logMessage}")
104+
return try {
105+
val status = client.executeHttpMethod(deleteMethod)
106+
val response = deleteMethod.getResponseBodyAsString()
79107

108+
if (!isSuccess(status)) {
109+
onResultUnsuccessful(deleteMethod, response, status)
80110
} else {
81-
result = RemoteOperationResult(deleteMethod)
111+
onRequestSuccessful(response)
82112
}
83-
84113
} catch (e: Exception) {
85-
result = RemoteOperationResult(e)
86-
Timber.e(e, "Unshare Link Exception ${result.logMessage}")
114+
Timber.e(e, "Exception while unshare link")
115+
RemoteOperationResult(e)
87116
}
88-
89-
return result
90117
}
91118

92119
private fun isSuccess(status: Int): Boolean = status == HttpConstants.HTTP_OK
120+
121+
companion object {
122+
123+
//OCS Route
124+
private const val OCS_ROUTE = "ocs/v2.php/apps/files_sharing/api/v1/shares"
125+
126+
//Arguments - names
127+
private const val PARAM_FORMAT = "format"
128+
129+
//Arguments - constant values
130+
private const val VALUE_FORMAT = "json"
131+
132+
}
93133
}

0 commit comments

Comments
 (0)