1
1
/* ownCloud Android Library is available under MIT license
2
2
*
3
+ * @author Christian Schabesberger
3
4
* @author masensio
4
5
* @author David A. Velasco
5
6
* @author David González Verdugo
28
29
29
30
package com.owncloud.android.lib.resources.shares
30
31
32
+ import android.net.Uri
31
33
import com.owncloud.android.lib.common.OwnCloudClient
32
34
import com.owncloud.android.lib.common.http.HttpConstants
33
35
import com.owncloud.android.lib.common.http.methods.nonwebdav.GetMethod
34
36
import com.owncloud.android.lib.common.operations.RemoteOperation
35
37
import com.owncloud.android.lib.common.operations.RemoteOperationResult
36
38
import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode.OK
37
- import org.json.JSONObject
39
+ import com.owncloud.android.lib.resources.CommonOcsResponse
40
+ import com.owncloud.android.lib.resources.shares.responses.ShareeOcsResponse
41
+ import com.squareup.moshi.JsonAdapter
42
+ import com.squareup.moshi.Moshi
43
+ import com.squareup.moshi.Types
38
44
import timber.log.Timber
45
+ import java.lang.reflect.Type
39
46
import java.net.URL
40
- import java.util.ArrayList
41
47
42
48
/* *
43
49
* Created by masensio on 08/10/2015.
@@ -65,6 +71,7 @@ import java.util.ArrayList
65
71
* Status codes:
66
72
* 100 - successful
67
73
*
74
+ * @author Christian Schabesberger
68
75
* @author masensio
69
76
* @author David A. Velasco
70
77
* @author David González Verdugo
@@ -78,80 +85,66 @@ class GetRemoteShareesOperation
78
85
* @param perPage maximum number of results in a single page
79
86
*/
80
87
(private val searchString: String , private val page: Int , private val perPage: Int ) :
81
- RemoteOperation <ArrayList <JSONObject >>() {
88
+ RemoteOperation <ShareeOcsResponse >() {
89
+
90
+ private fun buildRequestUri (baseUri : Uri ) =
91
+ baseUri.buildUpon()
92
+ .appendEncodedPath(OCS_ROUTE )
93
+ .appendQueryParameter(PARAM_FORMAT , VALUE_FORMAT )
94
+ .appendQueryParameter(PARAM_ITEM_TYPE , VALUE_ITEM_TYPE )
95
+ .appendQueryParameter(PARAM_SEARCH , searchString)
96
+ .appendQueryParameter(PARAM_PAGE , page.toString())
97
+ .appendQueryParameter(PARAM_PER_PAGE , perPage.toString())
98
+ .build()
99
+
100
+ private fun parseResponse (response : String ): ShareeOcsResponse ? {
101
+ val moshi = Moshi .Builder ().build()
102
+ val type: Type = Types .newParameterizedType(CommonOcsResponse ::class .java, ShareeOcsResponse ::class .java)
103
+ val adapter: JsonAdapter <CommonOcsResponse <ShareeOcsResponse >> = moshi.adapter(type)
104
+ return adapter.fromJson(response)!! .ocs.data
105
+ }
82
106
83
- override fun run (client : OwnCloudClient ): RemoteOperationResult <ArrayList <JSONObject >> {
84
- var result: RemoteOperationResult <ArrayList <JSONObject >>
107
+ private fun onResultUnsuccessful (
108
+ method : GetMethod ,
109
+ response : String? ,
110
+ status : Int
111
+ ): RemoteOperationResult <ShareeOcsResponse > {
112
+ Timber .e(" Failed response while getting users/groups from the server " )
113
+ if (response != null ) {
114
+ Timber .e(" *** status code: $status ; response message: $response " )
115
+ } else {
116
+ Timber .e(" *** status code: $status " )
117
+ }
118
+ return RemoteOperationResult (method)
119
+ }
85
120
86
- try {
87
- val requestUri = client.baseUri
88
- val uriBuilder = requestUri.buildUpon()
89
- .appendEncodedPath(OCS_ROUTE )
90
- .appendQueryParameter(PARAM_FORMAT , VALUE_FORMAT )
91
- .appendQueryParameter(PARAM_ITEM_TYPE , VALUE_ITEM_TYPE )
92
- .appendQueryParameter(PARAM_SEARCH , searchString)
93
- .appendQueryParameter(PARAM_PAGE , page.toString())
94
- .appendQueryParameter(PARAM_PER_PAGE , perPage.toString())
121
+ private fun onRequestSuccessful (response : String? ): RemoteOperationResult <ShareeOcsResponse > {
122
+ val result = RemoteOperationResult <ShareeOcsResponse >(OK )
123
+ Timber .d(" Successful response: $response " )
124
+ result.data = parseResponse(response!! )
125
+ Timber .d(" *** Get Users or groups completed " )
126
+ return result
127
+ }
95
128
96
- val getMethod = GetMethod (URL (uriBuilder.build().toString()))
129
+ override fun run (client : OwnCloudClient ): RemoteOperationResult <ShareeOcsResponse > {
130
+ val requestUri = buildRequestUri(client.baseUri)
97
131
98
- getMethod.addRequestHeader(OCS_API_HEADER , OCS_API_HEADER_VALUE )
132
+ val getMethod = GetMethod (URL (requestUri.toString()))
133
+ getMethod.addRequestHeader(OCS_API_HEADER , OCS_API_HEADER_VALUE )
99
134
135
+ return try {
100
136
val status = client.executeHttpMethod(getMethod)
101
137
val response = getMethod.getResponseBodyAsString()
102
138
103
- if (isSuccess(status)) {
104
- Timber .d(" Successful response: $response " )
105
-
106
- // Parse the response
107
- val respJSON = JSONObject (response)
108
- val respOCS = respJSON.getJSONObject(NODE_OCS )
109
- val respData = respOCS.getJSONObject(NODE_DATA )
110
- val respExact = respData.getJSONObject(NODE_EXACT )
111
- val respExactUsers = respExact.getJSONArray(NODE_USERS )
112
- val respExactGroups = respExact.getJSONArray(NODE_GROUPS )
113
- val respExactRemotes = respExact.getJSONArray(NODE_REMOTES )
114
- val respPartialUsers = respData.getJSONArray(NODE_USERS )
115
- val respPartialGroups = respData.getJSONArray(NODE_GROUPS )
116
- val respPartialRemotes = respData.getJSONArray(NODE_REMOTES )
117
- val jsonResults = arrayOf(
118
- respExactUsers,
119
- respExactGroups,
120
- respExactRemotes,
121
- respPartialUsers,
122
- respPartialGroups,
123
- respPartialRemotes
124
- )
125
-
126
- val data = ArrayList <JSONObject >() // For result data
127
- for (i in 0 .. 5 ) {
128
- for (j in 0 until jsonResults[i].length()) {
129
- val jsonResult = jsonResults[i].getJSONObject(j)
130
- data.add(jsonResult)
131
- Timber .d(" *** Added item: ${jsonResult.getString(PROPERTY_LABEL )} " )
132
- }
133
- }
134
-
135
- result = RemoteOperationResult (OK )
136
- result.data = data
137
-
138
- Timber .d(" *** Get Users or groups completed " )
139
-
139
+ if (! isSuccess(status)) {
140
+ onResultUnsuccessful(getMethod, response, status)
140
141
} else {
141
- result = RemoteOperationResult (getMethod)
142
- Timber .e(" Failed response while getting users/groups from the server " )
143
- if (response != null ) {
144
- Timber .e(" *** status code: $status ; response message: $response " )
145
- } else {
146
- Timber .e(" *** status code: $status " )
147
- }
142
+ onRequestSuccessful(response)
148
143
}
149
144
} catch (e: Exception ) {
150
- result = RemoteOperationResult (e)
151
145
Timber .e(e, " Exception while getting users/groups" )
146
+ RemoteOperationResult (e)
152
147
}
153
-
154
- return result
155
148
}
156
149
157
150
private fun isSuccess (status : Int ) = status == HttpConstants .HTTP_OK
@@ -171,18 +164,5 @@ class GetRemoteShareesOperation
171
164
// Arguments - constant values
172
165
private const val VALUE_FORMAT = " json"
173
166
private const val VALUE_ITEM_TYPE = " file" // to get the server search for users / groups
174
-
175
- // JSON Node names
176
- private const val NODE_OCS = " ocs"
177
- private const val NODE_DATA = " data"
178
- private const val NODE_EXACT = " exact"
179
- private const val NODE_USERS = " users"
180
- private const val NODE_GROUPS = " groups"
181
- private const val NODE_REMOTES = " remotes"
182
- const val NODE_VALUE = " value"
183
- const val PROPERTY_LABEL = " label"
184
- const val PROPERTY_SHARE_TYPE = " shareType"
185
- const val PROPERTY_SHARE_WITH = " shareWith"
186
- const val PROPERTY_SHARE_WITH_ADDITIONAL_INFO = " shareWithAdditionalInfo"
187
167
}
188
168
}
0 commit comments