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

Commit af579a3

Browse files
authored
Merge pull request #352 from owncloud/new_arch/moshi_parse_sharees
[New arch] Use moshi to parse sharees
2 parents 6193782 + 68b8877 commit af579a3

File tree

15 files changed

+326
-105
lines changed

15 files changed

+326
-105
lines changed

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
android.enableJetifier=true
22
android.useAndroidX=true
3-
org.gradle.jvmargs=-Xmx1536M
3+
org.gradle.jvmargs=-Xmx1536M

owncloudComLibrary/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ dependencies {
1414
exclude module: "kotlin-reflect"
1515
}
1616
kapt "com.squareup.moshi:moshi-kotlin-codegen:$moshiVersion"
17+
18+
testImplementation 'junit:junit:4.13'
1719
}
1820

1921
android {
Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@
2121
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2222
* THE SOFTWARE.
2323
*/
24-
package com.owncloud.android.lib.resources.response
24+
package com.owncloud.android.lib.resources
2525

26+
import com.squareup.moshi.Json
2627
import com.squareup.moshi.JsonClass
2728

2829
// Response retrieved by OCS Rest API, used to obtain capabilities, shares and user info among others.
@@ -41,6 +42,11 @@ data class OCSResponse<T>(
4142
@JsonClass(generateAdapter = true)
4243
data class MetaData(
4344
val status: String,
44-
val statuscode: Int,
45-
val message: String?
45+
@Json(name = "statuscode")
46+
val statusCode: Int,
47+
val message: String?,
48+
@Json(name = "itemsperpage")
49+
val itemsPerPage: String?,
50+
@Json(name = "totalitems")
51+
val totalItems: String?
4652
)
Lines changed: 56 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/* ownCloud Android Library is available under MIT license
22
*
3+
* @author Christian Schabesberger
34
* @author masensio
45
* @author David A. Velasco
56
* @author David González Verdugo
@@ -28,16 +29,21 @@
2829

2930
package com.owncloud.android.lib.resources.shares
3031

32+
import android.net.Uri
3133
import com.owncloud.android.lib.common.OwnCloudClient
3234
import com.owncloud.android.lib.common.http.HttpConstants
3335
import com.owncloud.android.lib.common.http.methods.nonwebdav.GetMethod
3436
import com.owncloud.android.lib.common.operations.RemoteOperation
3537
import com.owncloud.android.lib.common.operations.RemoteOperationResult
3638
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
3844
import timber.log.Timber
45+
import java.lang.reflect.Type
3946
import java.net.URL
40-
import java.util.ArrayList
4147

4248
/**
4349
* Created by masensio on 08/10/2015.
@@ -65,6 +71,7 @@ import java.util.ArrayList
6571
* Status codes:
6672
* 100 - successful
6773
*
74+
* @author Christian Schabesberger
6875
* @author masensio
6976
* @author David A. Velasco
7077
* @author David González Verdugo
@@ -78,80 +85,66 @@ class GetRemoteShareesOperation
7885
* @param perPage maximum number of results in a single page
7986
*/
8087
(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+
}
82106

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+
}
85120

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+
}
95128

96-
val getMethod = GetMethod(URL(uriBuilder.build().toString()))
129+
override fun run(client: OwnCloudClient): RemoteOperationResult<ShareeOcsResponse> {
130+
val requestUri = buildRequestUri(client.baseUri)
97131

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)
99134

135+
return try {
100136
val status = client.executeHttpMethod(getMethod)
101137
val response = getMethod.getResponseBodyAsString()
102138

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)
140141
} 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)
148143
}
149144
} catch (e: Exception) {
150-
result = RemoteOperationResult(e)
151145
Timber.e(e, "Exception while getting users/groups")
146+
RemoteOperationResult(e)
152147
}
153-
154-
return result
155148
}
156149

157150
private fun isSuccess(status: Int) = status == HttpConstants.HTTP_OK
@@ -171,18 +164,5 @@ class GetRemoteShareesOperation
171164
// Arguments - constant values
172165
private const val VALUE_FORMAT = "json"
173166
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"
187167
}
188168
}

owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/RemoteShare.kt

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424

2525
package com.owncloud.android.lib.resources.shares
2626

27-
import com.owncloud.android.lib.resources.files.FileUtils
2827
import java.io.File
2928

3029
/**
@@ -106,17 +105,6 @@ enum class ShareType constructor(val value: Int) {
106105
FEDERATED(6);
107106

108107
companion object {
109-
fun fromValue(value: Int): ShareType? {
110-
return when (value) {
111-
-1 -> UNKNOWN
112-
0 -> USER
113-
1 -> GROUP
114-
3 -> PUBLIC_LINK
115-
4 -> EMAIL
116-
5 -> CONTACT
117-
6 -> FEDERATED
118-
else -> null
119-
}
120-
}
108+
fun fromValue(value: Int) = values().firstOrNull { it.value == value }
121109
}
122110
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/* ownCloud Android Library is available under MIT license
2+
*
3+
* Copyright (C) 2020 ownCloud GmbH.
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19+
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20+
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
24+
package com.owncloud.android.lib.resources.shares.responses
25+
26+
import com.squareup.moshi.Json
27+
import com.squareup.moshi.JsonClass
28+
29+
/**
30+
* This was modeled according to the documentation:
31+
* https://doc.owncloud.com/server/developer_manual/core/apis/ocs-recipient-api.html#get-shares-recipients
32+
*/
33+
@JsonClass(generateAdapter = true)
34+
data class ShareeOcsResponse(
35+
val exact: ExactSharees?,
36+
val groups: List<ShareeItem>,
37+
val remotes: List<ShareeItem>,
38+
val users: List<ShareeItem>
39+
) {
40+
fun getFlatRepresentationWithoutExact() = ArrayList<ShareeItem>().apply {
41+
addAll(users)
42+
addAll(remotes)
43+
addAll(groups)
44+
}
45+
}
46+
47+
@JsonClass(generateAdapter = true)
48+
data class ExactSharees(
49+
val groups: List<ShareeItem>,
50+
val remotes: List<ShareeItem>,
51+
val users: List<ShareeItem>
52+
) {
53+
fun getFlatRepresentation() = ArrayList<ShareeItem>().apply {
54+
addAll(users)
55+
addAll(remotes)
56+
addAll(groups)
57+
}
58+
}
59+
60+
@JsonClass(generateAdapter = true)
61+
data class ShareeItem(
62+
val label: String,
63+
val value: ShareeValue
64+
)
65+
66+
@JsonClass(generateAdapter = true)
67+
data class ShareeValue(
68+
val shareType: Int,
69+
val shareWith: String,
70+
@Json(name = "shareWithAdditionalInfo")
71+
val additionalInfo: String?
72+
)

owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/services/ShareeService.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/**
22
* ownCloud Android client application
33
*
4+
* @author Christian Schabesberger
45
* @author David González Verdugo
56
*
67
* Copyright (C) 2020 ownCloud GmbH.
@@ -22,13 +23,12 @@ package com.owncloud.android.lib.resources.shares.services
2223

2324
import com.owncloud.android.lib.common.operations.RemoteOperationResult
2425
import com.owncloud.android.lib.resources.Service
25-
import org.json.JSONObject
26-
import java.util.ArrayList
26+
import com.owncloud.android.lib.resources.shares.responses.ShareeOcsResponse
2727

2828
interface ShareeService : Service {
2929
fun getSharees(
3030
searchString: String,
3131
page: Int,
3232
perPage: Int
33-
): RemoteOperationResult<ArrayList<JSONObject>>
33+
): RemoteOperationResult<ShareeOcsResponse>
3434
}

owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/shares/services/implementation/OCShareeService.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,16 @@ package com.owncloud.android.lib.resources.shares.services.implementation
2323
import com.owncloud.android.lib.common.OwnCloudClient
2424
import com.owncloud.android.lib.common.operations.RemoteOperationResult
2525
import com.owncloud.android.lib.resources.shares.GetRemoteShareesOperation
26+
import com.owncloud.android.lib.resources.shares.responses.ShareeOcsResponse
2627
import com.owncloud.android.lib.resources.shares.services.ShareeService
27-
import org.json.JSONObject
28-
import java.util.ArrayList
2928

3029
class OCShareeService(override val client: OwnCloudClient) :
3130
ShareeService {
3231
override fun getSharees(
3332
searchString: String,
3433
page: Int,
3534
perPage: Int
36-
): RemoteOperationResult<ArrayList<JSONObject>> =
35+
): RemoteOperationResult<ShareeOcsResponse> =
3736
GetRemoteShareesOperation(
3837
searchString,
3938
page,

owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/status/GetRemoteCapabilitiesOperation.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ import com.owncloud.android.lib.common.http.methods.nonwebdav.GetMethod
3434
import com.owncloud.android.lib.common.operations.RemoteOperation
3535
import com.owncloud.android.lib.common.operations.RemoteOperationResult
3636
import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode.OK
37-
import com.owncloud.android.lib.resources.response.CapabilityResponse
38-
import com.owncloud.android.lib.resources.response.CommonOcsResponse
37+
import com.owncloud.android.lib.resources.status.responses.CapabilityResponse
38+
import com.owncloud.android.lib.resources.CommonOcsResponse
3939
import com.squareup.moshi.JsonAdapter
4040
import com.squareup.moshi.Moshi
4141
import com.squareup.moshi.Types

owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/response/CapabilityResponse.kt renamed to owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/status/responses/CapabilityResponse.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
* THE SOFTWARE.
2323
*
2424
*/
25-
package com.owncloud.android.lib.resources.response
25+
package com.owncloud.android.lib.resources.status.responses
2626

2727
import com.owncloud.android.lib.resources.status.RemoteCapability
2828
import com.owncloud.android.lib.resources.status.RemoteCapability.CapabilityBooleanType

0 commit comments

Comments
 (0)