1
+ /* ownCloud Android Library is available under MIT license
2
+ * Copyright (C) 2022 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
+ */
24
+ package com.owncloud.android.lib.resources.files
25
+
26
+ import com.owncloud.android.lib.common.OwnCloudClient
27
+ import com.owncloud.android.lib.common.http.HttpConstants
28
+ import com.owncloud.android.lib.common.http.methods.webdav.DavUtils
29
+ import com.owncloud.android.lib.common.http.methods.webdav.PropfindMethod
30
+ import com.owncloud.android.lib.common.network.WebdavUtils
31
+ import com.owncloud.android.lib.common.operations.RemoteOperation
32
+ import com.owncloud.android.lib.common.operations.RemoteOperationResult
33
+ import timber.log.Timber
34
+ import java.net.URL
35
+ import java.util.concurrent.TimeUnit
36
+
37
+ /* *
38
+ * Operation to get the base url, which might differ in case of a redirect.
39
+ *
40
+ * @author Christian Schabesberger
41
+ */
42
+
43
+ class GetBaseUrlRemoteOperation : RemoteOperation <String ?>() {
44
+ override fun run (client : OwnCloudClient ): RemoteOperationResult <String ?> {
45
+ return try {
46
+ val stringUrl = client.baseFilesWebDavUri.toString()
47
+
48
+ val propFindMethod = PropfindMethod (URL (stringUrl), 0 , DavUtils .allPropset).apply {
49
+ setReadTimeout(TIMEOUT .toLong(), TimeUnit .SECONDS )
50
+ setConnectionTimeout(TIMEOUT .toLong(), TimeUnit .SECONDS )
51
+ }
52
+
53
+ val status = client.executeHttpMethod(propFindMethod)
54
+
55
+ if (isSuccess(status)) RemoteOperationResult <String ?>(RemoteOperationResult .ResultCode .OK ).apply {
56
+ data = propFindMethod.getFinalUrl().toString()
57
+ }
58
+ else RemoteOperationResult <String ?>(propFindMethod).apply { data = null }
59
+ } catch (e: Exception ) {
60
+ Timber .e(e, " Could not get actuall (or redirected) base URL from base url (/)." )
61
+ RemoteOperationResult <String ?>(e);
62
+ }
63
+ }
64
+
65
+ private fun isSuccess (status : Int ) = status == HttpConstants .HTTP_OK || status == HttpConstants .HTTP_MULTI_STATUS
66
+
67
+ companion object {
68
+ /* *
69
+ * Maximum time to wait for a response from the server in milliseconds.
70
+ */
71
+ private const val TIMEOUT = 10000
72
+ }
73
+ }
0 commit comments