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

Commit 8cee0a3

Browse files
authored
Merge pull request #553 from owncloud/feature/app_registry
[Feature] Open in specific web provider
2 parents 602ab41 + 393ec6e commit 8cee0a3

File tree

7 files changed

+230
-13
lines changed

7 files changed

+230
-13
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/* ownCloud Android Library is available under MIT license
2+
* @author Abel García de Prada
3+
*
4+
* Copyright (C) 2023 ownCloud GmbH.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20+
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21+
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
* THE SOFTWARE.
24+
*
25+
*/
26+
package com.owncloud.android.lib.resources.appregistry
27+
28+
import com.owncloud.android.lib.common.OwnCloudClient
29+
import com.owncloud.android.lib.common.http.HttpConstants
30+
import com.owncloud.android.lib.common.http.methods.nonwebdav.GetMethod
31+
import com.owncloud.android.lib.common.operations.RemoteOperation
32+
import com.owncloud.android.lib.common.operations.RemoteOperationResult
33+
import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode.OK
34+
import com.owncloud.android.lib.resources.appregistry.responses.AppRegistryResponse
35+
import com.squareup.moshi.JsonAdapter
36+
import com.squareup.moshi.Moshi
37+
import timber.log.Timber
38+
import java.net.URL
39+
40+
class GetRemoteAppRegistryOperation : RemoteOperation<AppRegistryResponse>() {
41+
42+
override fun run(client: OwnCloudClient): RemoteOperationResult<AppRegistryResponse> {
43+
var result: RemoteOperationResult<AppRegistryResponse>
44+
45+
try {
46+
val uriBuilder = client.baseUri.buildUpon().apply {
47+
appendEncodedPath(APP_REGISTRY_ENDPOINT)
48+
}
49+
val getMethod = GetMethod(URL(uriBuilder.build().toString()))
50+
val status = client.executeHttpMethod(getMethod)
51+
52+
val response = getMethod.getResponseBodyAsString()
53+
54+
if (status == HttpConstants.HTTP_OK) {
55+
Timber.d("Successful response $response")
56+
57+
// Parse the response
58+
val moshi: Moshi = Moshi.Builder().build()
59+
val adapter: JsonAdapter<AppRegistryResponse> = moshi.adapter(AppRegistryResponse::class.java)
60+
val appRegistryResponse: AppRegistryResponse = response?.let { adapter.fromJson(it) } ?: AppRegistryResponse(value = emptyList())
61+
62+
result = RemoteOperationResult(OK)
63+
result.data = appRegistryResponse
64+
65+
Timber.d("Get AppRegistry completed and parsed to ${result.data}")
66+
} else {
67+
result = RemoteOperationResult(getMethod)
68+
Timber.e("Failed response while getting app registry from the server status code: $status; response message: $response")
69+
}
70+
71+
} catch (e: Exception) {
72+
result = RemoteOperationResult(e)
73+
Timber.e(e, "Exception while getting app registry")
74+
}
75+
76+
return result
77+
}
78+
79+
companion object {
80+
private const val APP_REGISTRY_ENDPOINT = "app/list"
81+
}
82+
}
Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
* THE SOFTWARE.
2222
*
2323
*/
24-
package com.owncloud.android.lib.resources.files
24+
package com.owncloud.android.lib.resources.appregistry
2525

2626
import com.owncloud.android.lib.common.OwnCloudClient
2727
import com.owncloud.android.lib.common.http.HttpConstants
@@ -30,7 +30,6 @@ import com.owncloud.android.lib.common.network.WebdavUtils
3030
import com.owncloud.android.lib.common.operations.RemoteOperation
3131
import com.owncloud.android.lib.common.operations.RemoteOperationResult
3232
import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode
33-
import com.owncloud.android.lib.resources.files.GetUrlToOpenInWebRemoteOperation.OpenInWebParams.Companion.PARAM_FILE_ID
3433
import com.squareup.moshi.JsonAdapter
3534
import com.squareup.moshi.JsonClass
3635
import com.squareup.moshi.Moshi
@@ -43,14 +42,16 @@ import java.util.concurrent.TimeUnit
4342
class GetUrlToOpenInWebRemoteOperation(
4443
val openWithWebEndpoint: String,
4544
val fileId: String,
45+
val appName: String,
4646
) : RemoteOperation<String>() {
4747

4848
override fun run(client: OwnCloudClient): RemoteOperationResult<String> {
4949
return try {
5050

51-
val openInWebRequestBody = OpenInWebParams(fileId).toRequestBody()
51+
val openInWebRequestBody = OpenInWebParams(fileId, appName).toRequestBody()
5252

53-
val stringUrl = client.baseUri.toString() + WebdavUtils.encodePath(openWithWebEndpoint) + "?$PARAM_FILE_ID=$fileId"
53+
val stringUrl =
54+
client.baseUri.toString() + WebdavUtils.encodePath(openWithWebEndpoint)
5455

5556
val postMethod = PostMethod(URL(stringUrl), openInWebRequestBody).apply {
5657
setReadTimeout(TIMEOUT, TimeUnit.MILLISECONDS)
@@ -77,12 +78,19 @@ class GetUrlToOpenInWebRemoteOperation(
7778

7879
private fun isSuccess(status: Int) = status == HttpConstants.HTTP_OK || status == HttpConstants.HTTP_MULTI_STATUS
7980

80-
data class OpenInWebParams(val fileId: String) {
81+
data class OpenInWebParams(
82+
val fileId: String,
83+
val appName: String,
84+
) {
8185
fun toRequestBody(): RequestBody =
82-
FormBody.Builder().build()
86+
FormBody.Builder()
87+
.add(PARAM_FILE_ID, fileId)
88+
.add(PARAM_APP_NAME, appName)
89+
.build()
8390

8491
companion object {
8592
const val PARAM_FILE_ID = "file_id"
93+
const val PARAM_APP_NAME = "app_name"
8694
}
8795
}
8896

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/* ownCloud Android Library is available under MIT license
2+
* @author Abel García de Prada
3+
*
4+
* Copyright (C) 2023 ownCloud GmbH.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20+
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21+
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
* THE SOFTWARE.
24+
*
25+
*/
26+
package com.owncloud.android.lib.resources.appregistry.responses
27+
28+
import com.squareup.moshi.Json
29+
import com.squareup.moshi.JsonClass
30+
31+
@JsonClass(generateAdapter = true)
32+
data class AppRegistryResponse(
33+
@Json(name = "mime-types")
34+
val value: List<AppRegistryMimeTypeResponse>
35+
)
36+
37+
@JsonClass(generateAdapter = true)
38+
data class AppRegistryMimeTypeResponse(
39+
@Json(name = "mime_type") val mimeType: String,
40+
val ext: String? = null,
41+
@Json(name = "app_providers")
42+
val appProviders: List<AppRegistryProviderResponse>,
43+
val name: String? = null,
44+
val icon: String? = null,
45+
val description: String? = null,
46+
@Json(name = "allow_creation")
47+
val allowCreation: Boolean? = null,
48+
@Json(name = "default_application")
49+
val defaultApplication: String? = null
50+
)
51+
52+
@JsonClass(generateAdapter = true)
53+
data class AppRegistryProviderResponse(
54+
val name: String,
55+
val icon: String,
56+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/* ownCloud Android Library is available under MIT license
2+
* Copyright (C) 2023 ownCloud GmbH.
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in
12+
* all copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
18+
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
19+
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
package com.owncloud.android.lib.resources.appregistry.services
24+
25+
import com.owncloud.android.lib.common.operations.RemoteOperationResult
26+
import com.owncloud.android.lib.resources.Service
27+
import com.owncloud.android.lib.resources.appregistry.responses.AppRegistryResponse
28+
29+
interface AppRegistryService : Service {
30+
fun getAppRegistry(): RemoteOperationResult<AppRegistryResponse>
31+
32+
fun getUrlToOpenInWeb(
33+
openWebEndpoint: String,
34+
fileId: String,
35+
appName: String,
36+
): RemoteOperationResult<String>
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/* ownCloud Android Library is available under MIT license
2+
* Copyright (C) 2023 ownCloud GmbH.
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in
12+
* all copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
18+
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
19+
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
package com.owncloud.android.lib.resources.appregistry.services
24+
25+
import com.owncloud.android.lib.common.OwnCloudClient
26+
import com.owncloud.android.lib.common.operations.RemoteOperationResult
27+
import com.owncloud.android.lib.resources.appregistry.GetRemoteAppRegistryOperation
28+
import com.owncloud.android.lib.resources.appregistry.GetUrlToOpenInWebRemoteOperation
29+
import com.owncloud.android.lib.resources.appregistry.responses.AppRegistryResponse
30+
31+
class OCAppRegistryService(override val client: OwnCloudClient) : AppRegistryService {
32+
override fun getAppRegistry(): RemoteOperationResult<AppRegistryResponse> =
33+
GetRemoteAppRegistryOperation().execute(client)
34+
35+
override fun getUrlToOpenInWeb(openWebEndpoint: String, fileId: String, appName: String): RemoteOperationResult<String> =
36+
GetUrlToOpenInWebRemoteOperation(
37+
openWithWebEndpoint = openWebEndpoint,
38+
fileId = fileId,
39+
appName = appName
40+
).execute(client)
41+
}

owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/services/FileService.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ import com.owncloud.android.lib.resources.Service
2828
import com.owncloud.android.lib.resources.files.RemoteFile
2929

3030
interface FileService : Service {
31-
fun getUrlToOpenInWeb(openWebEndpoint: String, fileId: String): RemoteOperationResult<String>
32-
3331
fun checkPathExistence(
3432
path: String,
3533
isUserLogged: Boolean,

owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/services/implementation/OCFileService.kt

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ import com.owncloud.android.lib.resources.files.CheckPathExistenceRemoteOperatio
2929
import com.owncloud.android.lib.resources.files.CopyRemoteFileOperation
3030
import com.owncloud.android.lib.resources.files.CreateRemoteFolderOperation
3131
import com.owncloud.android.lib.resources.files.DownloadRemoteFileOperation
32-
import com.owncloud.android.lib.resources.files.GetUrlToOpenInWebRemoteOperation
3332
import com.owncloud.android.lib.resources.files.MoveRemoteFileOperation
3433
import com.owncloud.android.lib.resources.files.ReadRemoteFileOperation
3534
import com.owncloud.android.lib.resources.files.ReadRemoteFolderOperation
@@ -39,7 +38,6 @@ import com.owncloud.android.lib.resources.files.RenameRemoteFileOperation
3938
import com.owncloud.android.lib.resources.files.services.FileService
4039

4140
class OCFileService(override val client: OwnCloudClient) : FileService {
42-
4341
override fun checkPathExistence(
4442
path: String,
4543
isUserLogged: Boolean,
@@ -51,9 +49,6 @@ class OCFileService(override val client: OwnCloudClient) : FileService {
5149
spaceWebDavUrl = spaceWebDavUrl,
5250
).execute(client)
5351

54-
override fun getUrlToOpenInWeb(openWebEndpoint: String, fileId: String): RemoteOperationResult<String> =
55-
GetUrlToOpenInWebRemoteOperation(openWithWebEndpoint = openWebEndpoint, fileId = fileId).execute(client)
56-
5752
override fun copyFile(
5853
sourceRemotePath: String,
5954
targetRemotePath: String,

0 commit comments

Comments
 (0)