23
23
*/
24
24
package com.owncloud.android.lib.resources.files ;
25
25
26
- import com.owncloud.android.lib.common.OwnCloudClient;
27
- import com.owncloud.android.lib.common.accounts.AccountUtils;
28
- import com.owncloud.android.lib.common.http.HttpConstants;
29
- import com.owncloud.android.lib.common.http.methods.webdav.DavUtils;
30
- import com.owncloud.android.lib.common.http.methods.webdav.PropfindMethod;
31
- import com.owncloud.android.lib.common.network.WebdavUtils;
32
- import com.owncloud.android.lib.common.operations.RemoteOperation;
33
- import com.owncloud.android.lib.common.operations.RemoteOperationResult;
34
- import timber.log.Timber;
35
-
36
- import java.net.URL;
37
- import java.util.concurrent.TimeUnit;
38
-
39
- import static com.owncloud.android.lib.common.http.methods.webdav.DavConstants .DEPTH_0 ;
40
- import static com.owncloud.android.lib.common.operations.RemoteOperationResult .ResultCode .OK ;
26
+ import com.owncloud.android.lib.common.OwnCloudClient
27
+ import com.owncloud.android.lib.common.accounts.AccountUtils
28
+ import com.owncloud.android.lib.common.http.HttpConstants.HTTP_MULTI_STATUS
29
+ import com.owncloud.android.lib.common.http.HttpConstants.HTTP_OK
30
+ import com.owncloud.android.lib.common.http.methods.webdav.DavConstants.DEPTH_0
31
+ import com.owncloud.android.lib.common.http.methods.webdav.DavUtils
32
+ import com.owncloud.android.lib.common.http.methods.webdav.PropfindMethod
33
+ import com.owncloud.android.lib.common.network.WebdavUtils
34
+ import com.owncloud.android.lib.common.operations.RemoteOperation
35
+ import com.owncloud.android.lib.common.operations.RemoteOperationResult
36
+ import com.owncloud.android.lib.common.utils.isOneOf
37
+ import timber.log.Timber
38
+ import java.net.URL
39
+ import java.util.concurrent.TimeUnit
41
40
42
41
/* *
43
42
* Remote operation performing the read a file from the ownCloud server.
@@ -47,63 +46,52 @@ import static com.owncloud.android.lib.common.operations.RemoteOperationResult.R
47
46
* @author David González Verdugo
48
47
*/
49
48
50
- public class ReadRemoteFileOperation extends RemoteOperation <RemoteFile > {
51
-
52
- private static final int SYNC_READ_TIMEOUT = 40000 ;
53
- private static final int SYNC_CONNECTION_TIMEOUT = 5000 ;
54
-
55
- private String mRemotePath;
56
-
57
- /* *
58
- * Constructor
59
- *
60
- * @param remotePath Remote path of the file.
61
- */
62
- public ReadRemoteFileOperation (String remotePath) {
63
- mRemotePath = remotePath;
64
- }
49
+ class ReadRemoteFileOperation (val remotePath : String ) : RemoteOperation<RemoteFile>() {
65
50
66
51
/* *
67
52
* Performs the read operation.
68
53
*
69
54
* @param client Client object to communicate with the remote ownCloud server.
70
55
*/
71
56
@Override
72
- protected RemoteOperationResult <RemoteFile > run (OwnCloudClient client) {
73
- PropfindMethod propfind;
74
- RemoteOperationResult <RemoteFile > result;
75
-
76
- // / take the duty of check the server for the current state of the file there
57
+ override fun run (client : OwnCloudClient ): RemoteOperationResult <RemoteFile > {
77
58
try {
78
- // remote request
79
- propfind = new PropfindMethod (
80
- new URL (client.getUserFilesWebDavUri() + WebdavUtils .encodePath(mRemotePath)),
81
- DEPTH_0 ,
82
- DavUtils .getAllPropset());
83
-
84
- propfind.setReadTimeout(SYNC_READ_TIMEOUT , TimeUnit .SECONDS );
85
- propfind.setConnectionTimeout(SYNC_CONNECTION_TIMEOUT , TimeUnit .SECONDS );
86
- final int status = client.executeHttpMethod(propfind);
87
-
88
- if (status == HttpConstants .HTTP_MULTI_STATUS
89
- || status == HttpConstants .HTTP_OK ) {
59
+ val propFind = PropfindMethod (
60
+ url = URL (" ${client.userFilesWebDavUri}${WebdavUtils .encodePath(remotePath)} " ),
61
+ depth = DEPTH_0 ,
62
+ propertiesToRequest = DavUtils .allPropset
63
+ ).apply {
64
+ setReadTimeout(SYNC_READ_TIMEOUT , TimeUnit .SECONDS )
65
+ setConnectionTimeout(SYNC_CONNECTION_TIMEOUT , TimeUnit .SECONDS )
66
+ }
90
67
91
- final RemoteFile file = RemoteFile . Companion .getRemoteFileFromDav(propfind.getRoot(),
92
- AccountUtils .getUserId(mAccount, mContext), mAccount.name);
68
+ val status = client.executeHttpMethod(propFind)
69
+ Timber .i( " Read remote file $remotePath with status ${propFind.statusCode} " )
93
70
94
- result = new RemoteOperationResult <> (OK );
95
- result.setData(file);
71
+ return if (isSuccess(status)) {
72
+ // TODO: Remove that !!
73
+ val remoteFile = RemoteFile .getRemoteFileFromDav(
74
+ propFind.root!! ,
75
+ AccountUtils .getUserId(mAccount, mContext), mAccount.name
76
+ )
96
77
78
+ RemoteOperationResult <RemoteFile >(RemoteOperationResult .ResultCode .OK ).apply {
79
+ data = remoteFile
80
+ }
97
81
} else {
98
- result = new RemoteOperationResult <> (propfind);
99
- client.exhaustResponse(propfind.getResponseBodyAsStream());
82
+ RemoteOperationResult <RemoteFile >(propFind).also {
83
+ client.exhaustResponse(propFind.getResponseBodyAsStream())
84
+ }
100
85
}
101
-
102
- } catch (Exception e) {
103
- result = new RemoteOperationResult <> (e);
104
- Timber .e(e, " Synchronizing file %s" , mRemotePath);
86
+ } catch (exception: Exception ) {
87
+ return RemoteOperationResult (exception)
105
88
}
89
+ }
90
+
91
+ private fun isSuccess (status : Int ) = status.isOneOf(HTTP_MULTI_STATUS , HTTP_OK )
106
92
107
- return result;
93
+ companion object {
94
+ private const val SYNC_READ_TIMEOUT = 40_000L
95
+ private const val SYNC_CONNECTION_TIMEOUT = 5_000L
108
96
}
109
- }
97
+ }
0 commit comments