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.GetMethod
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
* Provide a list shares for a specific file.
40
46
* The input is the full path of the desired file.
41
47
* The output is a list of everyone who has the file shared with them.
42
- *
43
- * @author masensio
44
- * @author David A. Velasco
45
- * @author David González Verdugo
46
48
*/
47
49
48
50
/* *
@@ -61,52 +63,87 @@ class GetRemoteSharesForFileOperation(
61
63
private val subfiles : Boolean
62
64
) : RemoteOperation<ShareResponse>() {
63
65
64
- override fun run (client : OwnCloudClient ): RemoteOperationResult <ShareResponse > {
65
- var result: RemoteOperationResult <ShareResponse >
66
+ private fun buildRequestUri (baseUri : Uri ) =
67
+ baseUri.buildUpon()
68
+ .appendEncodedPath(OCS_ROUTE )
69
+ .appendQueryParameter(PARAM_FORMAT , VALUE_FORMAT )
70
+ .appendQueryParameter(PARAM_PATH , remoteFilePath)
71
+ .appendQueryParameter(PARAM_RESHARES , reshares.toString())
72
+ .appendQueryParameter(PARAM_SUBFILES , subfiles.toString())
73
+ .build()
66
74
67
- try {
75
+ private fun parseResponse (response : String ): ShareResponse ? {
76
+ val moshi = Moshi .Builder ().build()
77
+ val listOfShareItemType: Type = Types .newParameterizedType(List ::class .java, ShareItem ::class .java)
78
+ val commonOcsType: Type = Types .newParameterizedType(CommonOcsResponse ::class .java, listOfShareItemType)
79
+ val adapter: JsonAdapter <CommonOcsResponse <List <ShareItem >>> = moshi.adapter(commonOcsType)
80
+ return adapter.fromJson(response)?.ocs?.data?.let { listOfShareItems ->
81
+ ShareResponse (listOfShareItems.map { shareItem ->
82
+ shareItem.toRemoteShare()
83
+ })
84
+ }
85
+ }
68
86
69
- val requestUri = client.baseUri
70
- val uriBuilder = requestUri.buildUpon()
71
- uriBuilder.appendEncodedPath(ShareUtils .SHARING_API_PATH )
72
- uriBuilder.appendQueryParameter(PARAM_PATH , remoteFilePath)
73
- uriBuilder.appendQueryParameter(PARAM_RESHARES , reshares.toString())
74
- uriBuilder.appendQueryParameter(PARAM_SUBFILES , subfiles.toString())
87
+ private fun onResultUnsuccessful (
88
+ method : GetMethod ,
89
+ response : String? ,
90
+ status : Int
91
+ ): RemoteOperationResult <ShareResponse > {
92
+ Timber .e(" Failed response while while getting remote shares for file operation " )
93
+ if (response != null ) {
94
+ Timber .e(" *** status code: $status ; response message: $response " )
95
+ } else {
96
+ Timber .e(" *** status code: $status " )
97
+ }
98
+ return RemoteOperationResult (method)
99
+ }
75
100
76
- val getMethod = GetMethod (URL (uriBuilder.build().toString()))
101
+ private fun onRequestSuccessful (response : String? ): RemoteOperationResult <ShareResponse > {
102
+ val result = RemoteOperationResult <ShareResponse >(RemoteOperationResult .ResultCode .OK )
103
+ Timber .d(" Successful response: $response " )
104
+ result.data = parseResponse(response!! )
105
+ Timber .d(" *** Getting remote shares for file completed " )
106
+ Timber .d(" Got ${result.data.shares.size} shares" )
107
+ return result
108
+ }
77
109
78
- getMethod.addRequestHeader(OCS_API_HEADER , OCS_API_HEADER_VALUE )
110
+ override fun run (client : OwnCloudClient ): RemoteOperationResult <ShareResponse > {
111
+ val requestUri = buildRequestUri(client.baseUri)
112
+
113
+ val getMethod = GetMethod (URL (requestUri.toString())).apply {
114
+ addRequestHeader(OCS_API_HEADER , OCS_API_HEADER_VALUE )
115
+ }
79
116
117
+ return try {
80
118
val status = client.executeHttpMethod(getMethod)
119
+ val response = getMethod.getResponseBodyAsString()
81
120
82
- if (isSuccess(status)) {
83
- // Parse xml response and obtain the list of shares
84
- val parser = ShareToRemoteOperationResultParser (
85
- ShareXMLParser ()
86
- )
87
- parser.ownCloudVersion = client.ownCloudVersion
88
- parser.serverBaseUri = client.baseUri
89
- result = parser.parse(getMethod.getResponseBodyAsString())
90
-
91
- if (result.isSuccess) {
92
- Timber .d(" Got ${result.data.shares.size} shares" )
93
- }
121
+ if (! isSuccess(status)) {
122
+ onResultUnsuccessful(getMethod, response, status)
94
123
} else {
95
- result = RemoteOperationResult (getMethod )
124
+ onRequestSuccessful(response )
96
125
}
97
126
} catch (e: Exception ) {
98
- result = RemoteOperationResult (e )
99
- Timber .e(e, " Exception while getting shares " )
127
+ Timber .e(e, " Exception while getting remote shares for file operation " )
128
+ RemoteOperationResult (e )
100
129
}
101
-
102
- return result
103
130
}
104
131
105
132
private fun isSuccess (status : Int ): Boolean = status == HttpConstants .HTTP_OK
106
133
107
134
companion object {
135
+
136
+ // OCS Route
137
+ private const val OCS_ROUTE = " ocs/v2.php/apps/files_sharing/api/v1/shares"
138
+
139
+ // Arguments - names
140
+ private const val PARAM_FORMAT = " format"
108
141
private const val PARAM_PATH = " path"
109
142
private const val PARAM_RESHARES = " reshares"
110
143
private const val PARAM_SUBFILES = " subfiles"
144
+
145
+ // Arguments - constant values
146
+ private const val VALUE_FORMAT = " json"
147
+
111
148
}
112
149
}
0 commit comments