2
2
* @author masensio
3
3
* @author David A. Velasco
4
4
* @author David González Verdugo
5
- * Copyright (C) 2020 ownCloud GmbH
5
+ * @author Fernando Sanz Velasco
6
+ * Copyright (C) 2021 ownCloud GmbH
6
7
*
7
8
* Permission is hereby granted, free of charge, to any person obtaining a copy
8
9
* of this software and associated documentation files (the "Software"), to deal
27
28
28
29
package com.owncloud.android.lib.resources.shares
29
30
31
+ import android.net.Uri
30
32
import com.owncloud.android.lib.common.OwnCloudClient
31
33
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
32
36
import com.owncloud.android.lib.common.http.methods.nonwebdav.PostMethod
33
37
import com.owncloud.android.lib.common.operations.RemoteOperation
34
38
import com.owncloud.android.lib.common.operations.RemoteOperationResult
39
+ import com.owncloud.android.lib.resources.CommonOcsResponse
35
40
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
36
45
import okhttp3.FormBody
37
46
import timber.log.Timber
47
+ import java.lang.reflect.Type
38
48
import java.net.URL
39
49
import java.text.SimpleDateFormat
40
50
import java.util.Calendar
@@ -46,6 +56,7 @@ import java.util.Locale
46
56
* @author masensio
47
57
* @author David A. Velasco
48
58
* @author David González Verdugo
59
+ * @author Fernando Sanz Velasco
49
60
*/
50
61
51
62
/* *
@@ -70,7 +81,8 @@ class CreateRemoteShareOperation(
70
81
private val shareType : ShareType ,
71
82
private val shareWith : String ,
72
83
private val permissions : Int
73
- ) : RemoteOperation<ShareParserResult>() {
84
+ ) : RemoteOperation<ShareResponse>() {
85
+
74
86
var name = " " // Name to set for the public link
75
87
76
88
var password: String = " " // Password to set for the public link
@@ -81,90 +93,125 @@ class CreateRemoteShareOperation(
81
93
82
94
var retrieveShareDetails = false // To retrieve more info about the just created share
83
95
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
+ }
86
109
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
+ }
92
123
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 " )
96
129
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()
104
131
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
+ }
114
139
115
- val requestUri = client.baseUri
116
- val uriBuilder = requestUri.buildUpon()
117
- uriBuilder.appendEncodedPath(ShareUtils .SHARING_API_PATH )
140
+ private fun createFormBody (): FormBody {
118
141
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)
120
146
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
+ }
123
150
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
+ }
125
168
126
- val parser = ShareToRemoteOperationResultParser (
127
- ShareXMLParser ()
128
- )
169
+ return formBodyBuilder.build()
170
+ }
129
171
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
+ }
144
179
180
+ return try {
181
+ val status = client.executeHttpMethod(postMethod)
182
+ val response = postMethod.getResponseBodyAsString()
183
+
184
+ if (isSuccess(status)) {
185
+ onRequestSuccessful(response)
145
186
} else {
146
- result = parser.parse (postMethod.getResponseBodyAsString() )
187
+ onResultUnsuccessful (postMethod, response, status )
147
188
}
148
189
149
190
} 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 )
152
193
}
153
-
154
- return result
155
194
}
156
195
157
196
private fun isSuccess (status : Int ): Boolean = status == HttpConstants .HTTP_OK
158
197
159
198
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
+
160
205
private const val PARAM_NAME = " name"
161
- private const val PARAM_PASSWORD = " password"
162
206
private const val PARAM_EXPIRATION_DATE = " expireDate"
163
- private const val PARAM_PUBLIC_UPLOAD = " publicUpload"
164
207
private const val PARAM_PATH = " path"
165
208
private const val PARAM_SHARE_TYPE = " shareType"
166
209
private const val PARAM_SHARE_WITH = " shareWith"
210
+ private const val PARAM_PASSWORD = " password"
211
+ private const val PARAM_PUBLIC_UPLOAD = " publicUpload"
167
212
private const val PARAM_PERMISSIONS = " permissions"
213
+
214
+ // Arguments - constant values
168
215
private const val FORMAT_EXPIRATION_DATE = " yyyy-MM-dd"
169
216
}
170
217
}
0 commit comments