26
26
* THE SOFTWARE.
27
27
*
28
28
*/
29
-
30
29
package com.owncloud.android.lib.resources.status
31
30
32
31
import com.owncloud.android.lib.common.OwnCloudClient
@@ -36,11 +35,14 @@ import com.owncloud.android.lib.common.operations.RemoteOperation
36
35
import com.owncloud.android.lib.common.operations.RemoteOperationResult
37
36
import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode.OK
38
37
import com.owncloud.android.lib.resources.response.CapabilityResponse
38
+ import com.owncloud.android.lib.resources.response.CommonResponse
39
39
import com.owncloud.android.lib.resources.status.RemoteCapability.CapabilityBooleanType.Companion.fromBooleanValue
40
40
import com.squareup.moshi.JsonAdapter
41
41
import com.squareup.moshi.Moshi
42
- import org.json.JSONObject
42
+ import com.squareup.moshi.Types
43
+ import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory
43
44
import timber.log.Timber
45
+ import java.lang.reflect.Type
44
46
import java.net.URL
45
47
46
48
/* *
@@ -50,9 +52,6 @@ import java.net.URL
50
52
* @author masensio
51
53
* @author David González Verdugo
52
54
*/
53
- /* *
54
- * Constructor
55
- */
56
55
class GetRemoteCapabilitiesOperation : RemoteOperation <RemoteCapability >() {
57
56
58
57
override fun run (client : OwnCloudClient ): RemoteOperationResult <RemoteCapability > {
@@ -72,7 +71,7 @@ class GetRemoteCapabilitiesOperation : RemoteOperation<RemoteCapability>() {
72
71
73
72
val response = getMethod.responseBodyAsString
74
73
75
- if (! isSuccess( status) ) {
74
+ if (status != HttpConstants . HTTP_OK ) {
76
75
result = RemoteOperationResult (getMethod)
77
76
Timber .e(" Failed response while getting capabilities from the server status code: $status ; response message: $response " )
78
77
return result
@@ -81,31 +80,27 @@ class GetRemoteCapabilitiesOperation : RemoteOperation<RemoteCapability>() {
81
80
Timber .d(" Successful response: $response " )
82
81
83
82
// Parse the response
84
- val respJSON = JSONObject (response)
85
- val respOCS = respJSON.getJSONObject(NODE_OCS )
86
- val respMeta = respOCS.getJSONObject(NODE_META )
87
- val respData = respOCS.getJSONObject(NODE_DATA )
88
-
89
- // Read meta
90
- val statusProp = respMeta.getString(PROPERTY_STATUS ).equals(PROPERTY_STATUS_OK , ignoreCase = true )
91
- val statuscode = respMeta.getInt(PROPERTY_STATUSCODE )
92
- val message = respMeta.getString(PROPERTY_MESSAGE )
93
-
94
- if (statusProp) {
95
- val moshi: Moshi = Moshi .Builder ().build()
96
- val adapter: JsonAdapter <CapabilityResponse > = moshi.adapter(CapabilityResponse ::class .java)
97
- val capabilityResponse: CapabilityResponse ? = adapter.fromJson(respData.toString())
98
-
99
- val remoteCapability = capabilityResponse?.let { mapToModel(it) } ? : RemoteCapability ()
83
+ val moshi: Moshi = Moshi .Builder ().add(KotlinJsonAdapterFactory ()).build()
84
+ val type: Type = Types .newParameterizedType(CommonResponse ::class .java, CapabilityResponse ::class .java)
85
+ val adapter: JsonAdapter <CommonResponse <CapabilityResponse >> = moshi.adapter(type)
86
+ val commonResponse: CommonResponse <CapabilityResponse >? = adapter.fromJson(response)
87
+
88
+ // Read MetaData
89
+ val statusMessage = commonResponse?.ocs?.meta?.status
90
+ val statusCode = commonResponse?.ocs?.meta?.statuscode
91
+ val message = commonResponse?.ocs?.meta?.message
92
+
93
+ if (statusMessage.equals(PROPERTY_STATUS_OK , ignoreCase = true )) {
94
+ val remoteCapability = commonResponse?.ocs?.data?.let { mapToModel(it) } ? : RemoteCapability ()
100
95
// Result
101
96
result = RemoteOperationResult (OK )
102
97
result.data = remoteCapability
103
98
104
- Timber .d(" *** Get Capabilities completed $remoteCapability " )
99
+ Timber .d(" *** Get Capabilities completed and parsed to: $remoteCapability " )
105
100
} else {
106
- result = RemoteOperationResult (statuscode , message, null )
101
+ result = RemoteOperationResult (statusCode !! , message, null )
107
102
Timber .e(" Failed response while getting capabilities from the server " )
108
- Timber .e(" *** status: $statusProp ; message: $message " )
103
+ Timber .e(" *** statusCode: $ status; status : $statusMessage ; message: $message " )
109
104
}
110
105
111
106
} catch (e: Exception ) {
@@ -116,19 +111,15 @@ class GetRemoteCapabilitiesOperation : RemoteOperation<RemoteCapability>() {
116
111
return result
117
112
}
118
113
119
- private fun isSuccess (status : Int ): Boolean {
120
- return status == HttpConstants .HTTP_OK
121
- }
122
-
123
114
private fun mapToModel (capabilityResponse : CapabilityResponse ): RemoteCapability =
124
115
with (capabilityResponse) {
125
116
RemoteCapability (
126
- versionMayor = serverVersion.versionMayor ,
127
- versionMinor = serverVersion.versionMinor ,
128
- versionMicro = serverVersion.versionMicro ,
129
- versionString = serverVersion.versionString ,
130
- versionEdition = serverVersion.versionEdition ,
131
- corePollinterval = capabilities.coreCapabilities.pollInterval ,
117
+ versionMayor = serverVersion.major ,
118
+ versionMinor = serverVersion.minor ,
119
+ versionMicro = serverVersion.micro ,
120
+ versionString = serverVersion.string ,
121
+ versionEdition = serverVersion.edition ,
122
+ corePollinterval = capabilities.coreCapabilities.pollinterval ,
132
123
filesSharingApiEnabled = fromBooleanValue(capabilities.fileSharingCapabilities.fileSharingApiEnabled),
133
124
filesSharingResharing = fromBooleanValue(capabilities.fileSharingCapabilities.fileSharingReSharing),
134
125
filesSharingPublicEnabled = fromBooleanValue(capabilities.fileSharingCapabilities.fileSharingPublic.enabled),
@@ -142,9 +133,9 @@ class GetRemoteCapabilitiesOperation : RemoteOperation<RemoteCapability>() {
142
133
filesSharingPublicExpireDateEnabled = fromBooleanValue(capabilities.fileSharingCapabilities.fileSharingPublic.fileSharingPublicExpireDate.enabled),
143
134
filesSharingPublicExpireDateDays = capabilities.fileSharingCapabilities.fileSharingPublic.fileSharingPublicExpireDate.days ? : 0 ,
144
135
filesSharingPublicExpireDateEnforced = fromBooleanValue(capabilities.fileSharingCapabilities.fileSharingPublic.fileSharingPublicExpireDate.enforced? : false ),
145
- filesBigFileChunking = fromBooleanValue(capabilities.filesCapabilities.filesBigFileChunking ),
146
- filesUndelete = fromBooleanValue(capabilities.filesCapabilities.filesUnDelete ),
147
- filesVersioning = fromBooleanValue(capabilities.filesCapabilities.filesVersioning ),
136
+ filesBigFileChunking = fromBooleanValue(capabilities.filesCapabilities.bigfilechunking ),
137
+ filesUndelete = fromBooleanValue(capabilities.filesCapabilities.undelete ),
138
+ filesVersioning = fromBooleanValue(capabilities.filesCapabilities.versioning ),
148
139
filesSharingFederationIncoming = fromBooleanValue(capabilities.fileSharingCapabilities.fileSharingFederation.incoming),
149
140
filesSharingFederationOutgoing = fromBooleanValue(capabilities.fileSharingCapabilities.fileSharingFederation.outgoing)
150
141
)
@@ -161,16 +152,6 @@ class GetRemoteCapabilitiesOperation : RemoteOperation<RemoteCapability>() {
161
152
// Arguments - constant values
162
153
private const val VALUE_FORMAT = " json"
163
154
164
- // JSON Node names
165
- private const val NODE_OCS = " ocs"
166
-
167
- private const val NODE_META = " meta"
168
-
169
- private const val NODE_DATA = " data"
170
-
171
- private const val PROPERTY_STATUS = " status"
172
155
private const val PROPERTY_STATUS_OK = " ok"
173
- private const val PROPERTY_STATUSCODE = " statuscode"
174
- private const val PROPERTY_MESSAGE = " message"
175
156
}
176
157
}
0 commit comments