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/>.
6
21
*
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:
13
22
*
14
- * The above copyright notice and this permission notice shall be included in
15
- * all copies or substantial portions of the Software.
16
23
*
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.
25
24
*
26
25
*/
27
26
28
27
package com.owncloud.android.lib.resources.shares
29
28
29
+ import android.net.Uri
30
30
import com.owncloud.android.lib.common.OwnCloudClient
31
31
import com.owncloud.android.lib.common.http.HttpConstants
32
32
import com.owncloud.android.lib.common.http.methods.nonwebdav.DeleteMethod
33
33
import com.owncloud.android.lib.common.operations.RemoteOperation
34
34
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
35
40
import timber.log.Timber
41
+ import java.lang.reflect.Type
36
42
import java.net.URL
37
43
38
44
/* *
39
45
* Remove a share
40
- *
41
- * @author masensio
42
- * @author David A. Velasco
43
- * @author David González Verdugo
44
46
*/
45
47
46
48
/* *
@@ -50,44 +52,82 @@ import java.net.URL
50
52
*/
51
53
class RemoveRemoteShareOperation (private val remoteShareId : String ) : RemoteOperation<ShareResponse>() {
52
54
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
+ }
61
73
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
+ }
65
87
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
+ }
67
95
68
- val status = client.executeHttpMethod(deleteMethod)
96
+ override fun run ( client : OwnCloudClient ): RemoteOperationResult < ShareResponse > {
69
97
70
- if (isSuccess(status)) {
98
+ val requestUri = buildRequestUri(client.baseUri)
71
99
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
+ }
77
103
78
- Timber .d(" Unshare $remoteShareId : ${result.logMessage} " )
104
+ return try {
105
+ val status = client.executeHttpMethod(deleteMethod)
106
+ val response = deleteMethod.getResponseBodyAsString()
79
107
108
+ if (! isSuccess(status)) {
109
+ onResultUnsuccessful(deleteMethod, response, status)
80
110
} else {
81
- result = RemoteOperationResult (deleteMethod )
111
+ onRequestSuccessful(response )
82
112
}
83
-
84
113
} 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 )
87
116
}
88
-
89
- return result
90
117
}
91
118
92
119
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
+ }
93
133
}
0 commit comments