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

Commit 1baeebe

Browse files
authored
Merge pull request #430 from owncloud/feature/parse_shares
[FEATURE REQUEST] Use Moshi to parse shares
2 parents fa7d83e + 3c46c95 commit 1baeebe

16 files changed

+600
-880
lines changed

owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/HttpConstants.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,18 @@ public class HttpConstants {
6666
public static final String CONTENT_TYPE_JSON = "application/json";
6767
public static final String CONTENT_TYPE_WWW_FORM = "application/x-www-form-urlencoded";
6868

69+
/***********************************************************************************************************
70+
************************************************ ARGUMENTS NAMES ********************************************
71+
***********************************************************************************************************/
72+
73+
public static final String PARAM_FORMAT = "format";
74+
75+
/***********************************************************************************************************
76+
************************************************ ARGUMENTS VALUES ********************************************
77+
***********************************************************************************************************/
78+
79+
public static final String VALUE_FORMAT = "json";
80+
6981
/***********************************************************************************************************
7082
************************************************ STATUS CODES *********************************************
7183
***********************************************************************************************************/
@@ -205,4 +217,4 @@ public class HttpConstants {
205217
* Default timeout for establishing a connection
206218
*/
207219
public static final int DEFAULT_CONNECTION_TIMEOUT = 60000;
208-
}
220+
}

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

Lines changed: 106 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
* @author masensio
33
* @author David A. Velasco
44
* @author David González Verdugo
5-
* Copyright (C) 2020 ownCloud GmbH
5+
* @author Fernando Sanz Velasco
6+
* Copyright (C) 2021 ownCloud GmbH
67
*
78
* Permission is hereby granted, free of charge, to any person obtaining a copy
89
* of this software and associated documentation files (the "Software"), to deal
@@ -27,14 +28,23 @@
2728

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

31+
import android.net.Uri
3032
import com.owncloud.android.lib.common.OwnCloudClient
3133
import com.owncloud.android.lib.common.http.HttpConstants
34+
import com.owncloud.android.lib.common.http.HttpConstants.PARAM_FORMAT
35+
import com.owncloud.android.lib.common.http.HttpConstants.VALUE_FORMAT
3236
import com.owncloud.android.lib.common.http.methods.nonwebdav.PostMethod
3337
import com.owncloud.android.lib.common.operations.RemoteOperation
3438
import com.owncloud.android.lib.common.operations.RemoteOperationResult
39+
import com.owncloud.android.lib.resources.CommonOcsResponse
3540
import com.owncloud.android.lib.resources.shares.RemoteShare.Companion.INIT_EXPIRATION_DATE_IN_MILLIS
41+
import com.owncloud.android.lib.resources.shares.responses.ShareItem
42+
import com.squareup.moshi.JsonAdapter
43+
import com.squareup.moshi.Moshi
44+
import com.squareup.moshi.Types
3645
import okhttp3.FormBody
3746
import timber.log.Timber
47+
import java.lang.reflect.Type
3848
import java.net.URL
3949
import java.text.SimpleDateFormat
4050
import java.util.Calendar
@@ -46,6 +56,7 @@ import java.util.Locale
4656
* @author masensio
4757
* @author David A. Velasco
4858
* @author David González Verdugo
59+
* @author Fernando Sanz Velasco
4960
*/
5061

5162
/**
@@ -70,7 +81,8 @@ class CreateRemoteShareOperation(
7081
private val shareType: ShareType,
7182
private val shareWith: String,
7283
private val permissions: Int
73-
) : RemoteOperation<ShareParserResult>() {
84+
) : RemoteOperation<ShareResponse>() {
85+
7486
var name = "" // Name to set for the public link
7587

7688
var password: String = "" // Password to set for the public link
@@ -81,90 +93,125 @@ class CreateRemoteShareOperation(
8193

8294
var retrieveShareDetails = false // To retrieve more info about the just created share
8395

84-
override fun run(client: OwnCloudClient): RemoteOperationResult<ShareParserResult> {
85-
var result: RemoteOperationResult<ShareParserResult>
96+
private fun buildRequestUri(baseUri: Uri) =
97+
baseUri.buildUpon()
98+
.appendEncodedPath(OCS_ROUTE)
99+
.appendQueryParameter(PARAM_FORMAT, VALUE_FORMAT)
100+
.build()
101+
102+
private fun parseResponse(response: String): ShareResponse? {
103+
val moshi = Moshi.Builder().build()
104+
val commonOcsType: Type = Types.newParameterizedType(CommonOcsResponse::class.java, ShareItem::class.java)
105+
val adapter: JsonAdapter<CommonOcsResponse<ShareItem>> = moshi.adapter(commonOcsType)
106+
val remoteShare = adapter.fromJson(response)?.ocs?.data?.toRemoteShare()
107+
return ShareResponse(remoteShare?.let { listOf(it) } ?: listOf())
108+
}
86109

87-
try {
88-
val formBodyBuilder = FormBody.Builder()
89-
.add(PARAM_PATH, remoteFilePath)
90-
.add(PARAM_SHARE_TYPE, shareType.value.toString())
91-
.add(PARAM_SHARE_WITH, shareWith)
110+
private fun onResultUnsuccessful(
111+
method: PostMethod,
112+
response: String?,
113+
status: Int
114+
): RemoteOperationResult<ShareResponse> {
115+
Timber.e("Failed response while while creating new remote share operation ")
116+
if (response != null) {
117+
Timber.e("*** status code: $status; response message: $response")
118+
} else {
119+
Timber.e("*** status code: $status")
120+
}
121+
return RemoteOperationResult(method)
122+
}
92123

93-
if (name.isNotEmpty()) {
94-
formBodyBuilder.add(PARAM_NAME, name)
95-
}
124+
private fun onRequestSuccessful(response: String?): RemoteOperationResult<ShareResponse> {
125+
val result = RemoteOperationResult<ShareResponse>(RemoteOperationResult.ResultCode.OK)
126+
Timber.d("Successful response: $response")
127+
result.data = parseResponse(response!!)
128+
Timber.d("*** Creating new remote share operation completed ")
96129

97-
if (expirationDateInMillis > INIT_EXPIRATION_DATE_IN_MILLIS) {
98-
val dateFormat = SimpleDateFormat(FORMAT_EXPIRATION_DATE, Locale.getDefault())
99-
val expirationDate = Calendar.getInstance()
100-
expirationDate.timeInMillis = expirationDateInMillis
101-
val formattedExpirationDate = dateFormat.format(expirationDate.time)
102-
formBodyBuilder.add(PARAM_EXPIRATION_DATE, formattedExpirationDate)
103-
}
130+
val emptyShare = result.data.shares.first()
104131

105-
if (publicUpload) {
106-
formBodyBuilder.add(PARAM_PUBLIC_UPLOAD, publicUpload.toString())
107-
}
108-
if (password.isNotEmpty()) {
109-
formBodyBuilder.add(PARAM_PASSWORD, password)
110-
}
111-
if (RemoteShare.DEFAULT_PERMISSION != permissions) {
112-
formBodyBuilder.add(PARAM_PERMISSIONS, permissions.toString())
113-
}
132+
return if (retrieveShareDetails) {
133+
// retrieve more info - PUT only returns the index of the new share
134+
GetRemoteShareOperation(emptyShare.id).execute(client)
135+
} else {
136+
result
137+
}
138+
}
114139

115-
val requestUri = client.baseUri
116-
val uriBuilder = requestUri.buildUpon()
117-
uriBuilder.appendEncodedPath(ShareUtils.SHARING_API_PATH)
140+
private fun createFormBody(): FormBody {
118141

119-
val postMethod = PostMethod(URL(uriBuilder.build().toString()), formBodyBuilder.build())
142+
val formBodyBuilder = FormBody.Builder()
143+
.add(PARAM_PATH, remoteFilePath)
144+
.add(PARAM_SHARE_TYPE, shareType.value.toString())
145+
.add(PARAM_SHARE_WITH, shareWith)
120146

121-
postMethod.setRequestHeader(HttpConstants.CONTENT_TYPE_HEADER, HttpConstants.CONTENT_TYPE_URLENCODED_UTF8)
122-
postMethod.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE)
147+
if (name.isNotEmpty()) {
148+
formBodyBuilder.add(PARAM_NAME, name)
149+
}
123150

124-
val status = client.executeHttpMethod(postMethod)
151+
if (expirationDateInMillis > INIT_EXPIRATION_DATE_IN_MILLIS) {
152+
val dateFormat = SimpleDateFormat(FORMAT_EXPIRATION_DATE, Locale.getDefault())
153+
val expirationDate = Calendar.getInstance()
154+
expirationDate.timeInMillis = expirationDateInMillis
155+
val formattedExpirationDate = dateFormat.format(expirationDate.time)
156+
formBodyBuilder.add(PARAM_EXPIRATION_DATE, formattedExpirationDate)
157+
}
158+
159+
if (publicUpload) {
160+
formBodyBuilder.add(PARAM_PUBLIC_UPLOAD, publicUpload.toString())
161+
}
162+
if (password.isNotEmpty()) {
163+
formBodyBuilder.add(PARAM_PASSWORD, password)
164+
}
165+
if (RemoteShare.DEFAULT_PERMISSION != permissions) {
166+
formBodyBuilder.add(PARAM_PERMISSIONS, permissions.toString())
167+
}
125168

126-
val parser = ShareToRemoteOperationResultParser(
127-
ShareXMLParser()
128-
)
169+
return formBodyBuilder.build()
170+
}
129171

130-
if (isSuccess(status)) {
131-
parser.oneOrMoreSharesRequired = true
132-
parser.ownCloudVersion = client.ownCloudVersion
133-
parser.serverBaseUri = client.baseUri
134-
result = parser.parse(postMethod.getResponseBodyAsString())
135-
136-
if (result.isSuccess && retrieveShareDetails) {
137-
// retrieve more info - POST only returns the index of the new share
138-
val emptyShare = result.data.shares[0]
139-
val getInfo = GetRemoteShareOperation(
140-
emptyShare.id
141-
)
142-
result = getInfo.execute(client)
143-
}
172+
override fun run(client: OwnCloudClient): RemoteOperationResult<ShareResponse> {
173+
val requestUri = buildRequestUri(client.baseUri)
174+
175+
val postMethod = PostMethod(URL(requestUri.toString()), createFormBody()).apply {
176+
setRequestHeader(HttpConstants.CONTENT_TYPE_HEADER, HttpConstants.CONTENT_TYPE_URLENCODED_UTF8)
177+
addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE)
178+
}
144179

180+
return try {
181+
val status = client.executeHttpMethod(postMethod)
182+
val response = postMethod.getResponseBodyAsString()
183+
184+
if (isSuccess(status)) {
185+
onRequestSuccessful(response)
145186
} else {
146-
result = parser.parse(postMethod.getResponseBodyAsString())
187+
onResultUnsuccessful(postMethod, response, status)
147188
}
148189

149190
} catch (e: Exception) {
150-
result = RemoteOperationResult(e)
151-
Timber.e(e, "Exception while Creating New Share")
191+
Timber.e(e, "Exception while creating new remote share operation ")
192+
RemoteOperationResult(e)
152193
}
153-
154-
return result
155194
}
156195

157196
private fun isSuccess(status: Int): Boolean = status == HttpConstants.HTTP_OK
158197

159198
companion object {
199+
200+
//OCS Route
201+
private const val OCS_ROUTE = "ocs/v2.php/apps/files_sharing/api/v1/shares"
202+
203+
//Arguments - names
204+
160205
private const val PARAM_NAME = "name"
161-
private const val PARAM_PASSWORD = "password"
162206
private const val PARAM_EXPIRATION_DATE = "expireDate"
163-
private const val PARAM_PUBLIC_UPLOAD = "publicUpload"
164207
private const val PARAM_PATH = "path"
165208
private const val PARAM_SHARE_TYPE = "shareType"
166209
private const val PARAM_SHARE_WITH = "shareWith"
210+
private const val PARAM_PASSWORD = "password"
211+
private const val PARAM_PUBLIC_UPLOAD = "publicUpload"
167212
private const val PARAM_PERMISSIONS = "permissions"
213+
214+
//Arguments - constant values
168215
private const val FORMAT_EXPIRATION_DATE = "yyyy-MM-dd"
169216
}
170217
}

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

Lines changed: 0 additions & 94 deletions
This file was deleted.

0 commit comments

Comments
 (0)