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

Commit 17821e5

Browse files
committed
Migrate ReadRemoteFileOperation to kotlin. Second step to keep git history
1 parent 2b79175 commit 17821e5

File tree

2 files changed

+48
-60
lines changed

2 files changed

+48
-60
lines changed

owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/PropfindMethod.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class PropfindMethod(
5353
depth = depth,
5454
reqProp = propertiesToRequest,
5555
listOfHeaders = super.getRequestHeadersAsHashMap(),
56-
callback = { response: Response, hrefRelation: HrefRelation? ->
56+
callback = { response: Response, hrefRelation: HrefRelation ->
5757
when (hrefRelation) {
5858
HrefRelation.MEMBER -> members.add(response)
5959
HrefRelation.SELF -> this.root = response

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

Lines changed: 47 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,20 @@
2323
*/
2424
package com.owncloud.android.lib.resources.files;
2525

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
4140

4241
/**
4342
* 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
4746
* @author David González Verdugo
4847
*/
4948

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>() {
6550

6651
/**
6752
* Performs the read operation.
6853
*
6954
* @param client Client object to communicate with the remote ownCloud server.
7055
*/
7156
@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> {
7758
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+
}
9067

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}")
9370

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+
)
9677

78+
RemoteOperationResult<RemoteFile>(RemoteOperationResult.ResultCode.OK).apply {
79+
data = remoteFile
80+
}
9781
} else {
98-
result = new RemoteOperationResult<>(propfind);
99-
client.exhaustResponse(propfind.getResponseBodyAsStream());
82+
RemoteOperationResult<RemoteFile>(propFind).also {
83+
client.exhaustResponse(propFind.getResponseBodyAsStream())
84+
}
10085
}
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)
10588
}
89+
}
90+
91+
private fun isSuccess(status: Int) = status.isOneOf(HTTP_MULTI_STATUS, HTTP_OK)
10692

107-
return result;
93+
companion object {
94+
private const val SYNC_READ_TIMEOUT = 40_000L
95+
private const val SYNC_CONNECTION_TIMEOUT = 5_000L
10896
}
109-
}
97+
}

0 commit comments

Comments
 (0)