Skip to content

Commit 8195e8a

Browse files
committed
Operations migrated to kotlin.
1 parent 5e26654 commit 8195e8a

8 files changed

Lines changed: 456 additions & 489 deletions

library/src/main/java/com/owncloud/android/lib/resources/albums/CopyFileToAlbumRemoteOperation.kt

Lines changed: 93 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -4,156 +4,149 @@
44
* SPDX-FileCopyrightText: 2025 TSI-mc <surinder.kumar@t-systems.com>
55
* SPDX-License-Identifier: AGPL-3.0-or-later
66
*/
7-
8-
package com.owncloud.android.lib.resources.albums;
9-
10-
import android.util.Log;
11-
12-
import com.nextcloud.common.SessionTimeOut;
13-
import com.nextcloud.common.SessionTimeOutKt;
14-
import com.owncloud.android.lib.common.OwnCloudClient;
15-
import com.owncloud.android.lib.common.network.WebdavUtils;
16-
import com.owncloud.android.lib.common.operations.RemoteOperation;
17-
import com.owncloud.android.lib.common.operations.RemoteOperationResult;
18-
import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode;
19-
20-
import org.apache.commons.httpclient.HttpStatus;
21-
import org.apache.jackrabbit.webdav.DavException;
22-
import org.apache.jackrabbit.webdav.MultiStatusResponse;
23-
import org.apache.jackrabbit.webdav.Status;
24-
import org.apache.jackrabbit.webdav.client.methods.CopyMethod;
25-
26-
import java.io.IOException;
27-
7+
package com.owncloud.android.lib.resources.albums
8+
9+
import android.util.Log
10+
import com.nextcloud.common.SessionTimeOut
11+
import com.nextcloud.common.defaultSessionTimeOut
12+
import com.owncloud.android.lib.common.OwnCloudClient
13+
import com.owncloud.android.lib.common.network.WebdavUtils
14+
import com.owncloud.android.lib.common.operations.RemoteOperation
15+
import com.owncloud.android.lib.common.operations.RemoteOperationResult
16+
import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode
17+
import com.owncloud.android.lib.resources.albums.CopyFileToAlbumRemoteOperation
18+
import org.apache.commons.httpclient.HttpStatus
19+
import org.apache.jackrabbit.webdav.DavException
20+
import org.apache.jackrabbit.webdav.Status
21+
import org.apache.jackrabbit.webdav.client.methods.CopyMethod
22+
import java.io.IOException
2823

2924
/**
3025
* Remote operation moving a remote file or folder in the ownCloud server to a different folder
3126
* in the same account.
32-
* <p>
27+
*
28+
*
3329
* Allows renaming the moving file/folder at the same time.
3430
*/
35-
public class CopyFileToAlbumRemoteOperation extends RemoteOperation {
36-
private static final String TAG = CopyFileToAlbumRemoteOperation.class.getSimpleName();
37-
38-
private final String mSrcRemotePath;
39-
private final String mTargetRemotePath;
40-
41-
private final SessionTimeOut sessionTimeOut;
42-
43-
public CopyFileToAlbumRemoteOperation(String srcRemotePath, String targetRemotePath) {
44-
this(srcRemotePath, targetRemotePath, SessionTimeOutKt.getDefaultSessionTimeOut());
45-
}
46-
47-
public CopyFileToAlbumRemoteOperation(String srcRemotePath, String targetRemotePath, SessionTimeOut sessionTimeOut) {
48-
mSrcRemotePath = srcRemotePath;
49-
mTargetRemotePath = targetRemotePath;
50-
this.sessionTimeOut = sessionTimeOut;
51-
}
52-
31+
class CopyFileToAlbumRemoteOperation @JvmOverloads constructor(
32+
private val mSrcRemotePath: String,
33+
private val mTargetRemotePath: String,
34+
private val sessionTimeOut: SessionTimeOut = defaultSessionTimeOut
35+
) :
36+
RemoteOperation<Any>() {
5337
/**
5438
* Performs the operation.
5539
*
5640
* @param client Client object to communicate with the remote ownCloud server.
5741
*/
58-
@Override
59-
protected RemoteOperationResult run(OwnCloudClient client) {
42+
@Deprecated("Deprecated in Java")
43+
override fun run(client: OwnCloudClient): RemoteOperationResult<Any> {
44+
/** check parameters */
6045

61-
/// check parameters
62-
if (mTargetRemotePath.equals(mSrcRemotePath)) {
46+
if (mTargetRemotePath == mSrcRemotePath) {
6347
// nothing to do!
64-
return new RemoteOperationResult<>(ResultCode.OK);
48+
return RemoteOperationResult(ResultCode.OK)
6549
}
6650

6751
if (mTargetRemotePath.startsWith(mSrcRemotePath)) {
68-
return new RemoteOperationResult<>(ResultCode.INVALID_COPY_INTO_DESCENDANT);
52+
return RemoteOperationResult(ResultCode.INVALID_COPY_INTO_DESCENDANT)
6953
}
7054

71-
/// perform remote operation
72-
CopyMethod copyMethod = null;
73-
RemoteOperationResult result;
55+
/** perform remote operation */
56+
var copyMethod: CopyMethod? = null
57+
var result: RemoteOperationResult<Any>
7458
try {
75-
copyMethod = new CopyMethod(
59+
copyMethod = CopyMethod(
7660
client.getFilesDavUri(this.mSrcRemotePath),
77-
client.getBaseUri() + "/remote.php/dav/photos/" + client.getUserId() + "/albums" + WebdavUtils.encodePath(mTargetRemotePath),
61+
"${client.baseUri}/remote.php/dav/photos/${client.userId}/albums${
62+
WebdavUtils.encodePath(
63+
mTargetRemotePath
64+
)
65+
}",
7866
false
79-
);
80-
int status = client.executeMethod(copyMethod, sessionTimeOut.getReadTimeOut(), sessionTimeOut.getConnectionTimeOut());
81-
82-
/// process response
67+
)
68+
val status = client.executeMethod(
69+
copyMethod,
70+
sessionTimeOut.readTimeOut,
71+
sessionTimeOut.connectionTimeOut
72+
)
73+
74+
/** process response */
8375
if (status == HttpStatus.SC_MULTI_STATUS) {
84-
result = processPartialError(copyMethod);
85-
76+
result = processPartialError(copyMethod)
8677
} else if (status == HttpStatus.SC_PRECONDITION_FAILED) {
87-
88-
result = new RemoteOperationResult<>(ResultCode.INVALID_OVERWRITE);
89-
client.exhaustResponse(copyMethod.getResponseBodyAsStream());
90-
78+
result = RemoteOperationResult<Any>(ResultCode.INVALID_OVERWRITE)
79+
client.exhaustResponse(copyMethod.responseBodyAsStream)
9180
} else {
92-
result = new RemoteOperationResult<>(isSuccess(status), copyMethod);
93-
client.exhaustResponse(copyMethod.getResponseBodyAsStream());
81+
result = RemoteOperationResult<Any>(isSuccess(status), copyMethod)
82+
client.exhaustResponse(copyMethod.responseBodyAsStream)
9483
}
9584

96-
Log.i(TAG, "Copy " + mSrcRemotePath + " to " + mTargetRemotePath + ": " + result.getLogMessage());
97-
98-
} catch (Exception e) {
99-
result = new RemoteOperationResult<>(e);
100-
Log.e(TAG, "Copy " + mSrcRemotePath + " to " + mTargetRemotePath + ": " + result.getLogMessage(), e);
101-
85+
Log.i(
86+
TAG,
87+
"Copy $mSrcRemotePath to $mTargetRemotePath : ${result.logMessage}"
88+
)
89+
} catch (e: Exception) {
90+
result = RemoteOperationResult<Any>(e)
91+
Log.e(
92+
TAG,
93+
"Copy $mSrcRemotePath to $mTargetRemotePath : ${result.logMessage}", e
94+
)
10295
} finally {
103-
if (copyMethod != null) {
104-
copyMethod.releaseConnection();
105-
}
96+
copyMethod?.releaseConnection()
10697
}
10798

108-
return result;
99+
return result
109100
}
110101

111-
112102
/**
113103
* Analyzes a multistatus response from the OC server to generate an appropriate result.
114-
* <p>
104+
*
105+
*
115106
* In WebDAV, a COPY request on collections (folders) can be PARTIALLY successful: some
116107
* children are copied, some other aren't.
117-
* <p>
108+
*
109+
*
118110
* According to the WebDAV specification, a multistatus response SHOULD NOT include partial
119111
* successes (201, 204) nor for descendants of already failed children (424) in the response
120112
* entity. But SHOULD NOT != MUST NOT, so take carefully.
121113
*
122114
* @param copyMethod Copy operation just finished with a multistatus response
123-
* @return A result for the {@link CopyFileToAlbumRemoteOperation} caller
115+
* @return A result for the [CopyFileToAlbumRemoteOperation] caller
124116
* @throws java.io.IOException If the response body could not be parsed
125117
* @throws org.apache.jackrabbit.webdav.DavException If the status code is other than MultiStatus or if obtaining
126-
* the response XML document fails
118+
* the response XML document fails
127119
*/
128-
private RemoteOperationResult processPartialError(CopyMethod copyMethod)
129-
throws IOException, DavException {
120+
@Throws(IOException::class, DavException::class)
121+
private fun processPartialError(copyMethod: CopyMethod): RemoteOperationResult<Any> {
130122
// Adding a list of failed descendants to the result could be interesting; or maybe not.
131123
// For the moment, let's take the easy way.
132-
133-
/// check that some error really occurred
134-
MultiStatusResponse[] responses = copyMethod.getResponseBodyAsMultiStatus().getResponses();
135-
Status[] status;
136-
boolean failFound = false;
137-
for (int i = 0; i < responses.length && !failFound; i++) {
138-
status = responses[i].getStatus();
139-
failFound = (
140-
status != null &&
141-
status.length > 0 &&
142-
status[0].getStatusCode() > 299
143-
);
124+
/** check that some error really occurred */
125+
126+
val responses = copyMethod.responseBodyAsMultiStatus.responses
127+
var status: Array<Status>?
128+
var failFound = false
129+
var i = 0
130+
while (i < responses.size && !failFound) {
131+
status = responses[i].status
132+
failFound = (!status.isNullOrEmpty() && status[0].statusCode > 299
133+
)
134+
i++
144135
}
145-
146-
RemoteOperationResult result;
147-
if (failFound) {
148-
result = new RemoteOperationResult<>(ResultCode.PARTIAL_COPY_DONE);
136+
val result: RemoteOperationResult<Any> = if (failFound) {
137+
RemoteOperationResult<Any>(ResultCode.PARTIAL_COPY_DONE)
149138
} else {
150-
result = new RemoteOperationResult<>(true, copyMethod);
139+
RemoteOperationResult<Any>(true, copyMethod)
151140
}
152141

153-
return result;
142+
return result
143+
}
144+
145+
private fun isSuccess(status: Int): Boolean {
146+
return status == HttpStatus.SC_CREATED || status == HttpStatus.SC_NO_CONTENT
154147
}
155148

156-
protected boolean isSuccess(int status) {
157-
return status == HttpStatus.SC_CREATED || status == HttpStatus.SC_NO_CONTENT;
149+
companion object {
150+
private val TAG: String = CopyFileToAlbumRemoteOperation::class.java.simpleName
158151
}
159152
}

library/src/main/java/com/owncloud/android/lib/resources/albums/CreateNewAlbumRemoteOperation.kt

Lines changed: 47 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -4,69 +4,67 @@
44
* SPDX-FileCopyrightText: 2025 TSI-mc <surinder.kumar@t-systems.com>
55
* SPDX-License-Identifier: AGPL-3.0-or-later
66
*/
7+
package com.owncloud.android.lib.resources.albums
78

8-
package com.owncloud.android.lib.resources.albums;
9-
10-
import com.nextcloud.common.SessionTimeOut;
11-
import com.nextcloud.common.SessionTimeOutKt;
12-
import com.owncloud.android.lib.common.OwnCloudClient;
13-
import com.owncloud.android.lib.common.network.WebdavUtils;
14-
import com.owncloud.android.lib.common.operations.RemoteOperation;
15-
import com.owncloud.android.lib.common.operations.RemoteOperationResult;
16-
import com.owncloud.android.lib.common.utils.Log_OC;
17-
18-
import org.apache.jackrabbit.webdav.client.methods.MkColMethod;
19-
20-
public class CreateNewAlbumRemoteOperation extends RemoteOperation<Void> {
21-
private static final String TAG = CreateNewAlbumRemoteOperation.class.getSimpleName();
22-
private final String newAlbumName;
23-
private final SessionTimeOut sessionTimeOut;
24-
25-
public CreateNewAlbumRemoteOperation(String newAlbumName) {
26-
this(newAlbumName, SessionTimeOutKt.getDefaultSessionTimeOut());
27-
}
28-
29-
public CreateNewAlbumRemoteOperation(String newAlbumName, SessionTimeOut sessionTimeOut) {
30-
this.newAlbumName = newAlbumName;
31-
this.sessionTimeOut = sessionTimeOut;
32-
}
33-
34-
public String getNewAlbumName() {
35-
return newAlbumName;
36-
}
9+
import com.nextcloud.common.SessionTimeOut
10+
import com.nextcloud.common.defaultSessionTimeOut
11+
import com.owncloud.android.lib.common.OwnCloudClient
12+
import com.owncloud.android.lib.common.network.WebdavUtils
13+
import com.owncloud.android.lib.common.operations.RemoteOperation
14+
import com.owncloud.android.lib.common.operations.RemoteOperationResult
15+
import com.owncloud.android.lib.common.utils.Log_OC
16+
import com.owncloud.android.lib.resources.albums.CreateNewAlbumRemoteOperation
17+
import org.apache.commons.httpclient.HttpStatus
18+
import org.apache.jackrabbit.webdav.client.methods.MkColMethod
3719

20+
class CreateNewAlbumRemoteOperation @JvmOverloads constructor(
21+
val newAlbumName: String,
22+
private val sessionTimeOut: SessionTimeOut = defaultSessionTimeOut
23+
) :
24+
RemoteOperation<Void>() {
3825
/**
3926
* Performs the operation.
4027
*
4128
* @param client Client object to communicate with the remote ownCloud server.
4229
*/
43-
@Override
44-
protected RemoteOperationResult<Void> run(OwnCloudClient client) {
45-
MkColMethod mkCol = null;
46-
RemoteOperationResult<Void> result;
30+
@Deprecated("Deprecated in Java")
31+
override fun run(client: OwnCloudClient): RemoteOperationResult<Void> {
32+
var mkCol: MkColMethod? = null
33+
var result: RemoteOperationResult<Void>
4734
try {
48-
mkCol = new MkColMethod(client.getBaseUri() + "/remote.php/dav/photos/" + client.getUserId() + "/albums" + WebdavUtils.encodePath(newAlbumName));
49-
client.executeMethod(mkCol, sessionTimeOut.getReadTimeOut(), sessionTimeOut.getConnectionTimeOut());
50-
if (405 == mkCol.getStatusCode()) {
51-
result = new RemoteOperationResult<>(RemoteOperationResult.ResultCode.FOLDER_ALREADY_EXISTS);
35+
mkCol = MkColMethod(
36+
"${client.baseUri}/remote.php/dav/photos/${client.userId}/albums${
37+
WebdavUtils.encodePath(
38+
newAlbumName
39+
)
40+
}"
41+
)
42+
client.executeMethod(
43+
mkCol,
44+
sessionTimeOut.readTimeOut,
45+
sessionTimeOut.connectionTimeOut
46+
)
47+
if (HttpStatus.SC_METHOD_NOT_ALLOWED == mkCol.statusCode) {
48+
result =
49+
RemoteOperationResult(RemoteOperationResult.ResultCode.FOLDER_ALREADY_EXISTS)
5250
} else {
53-
result = new RemoteOperationResult<>(mkCol.succeeded(), mkCol);
54-
result.setResultData(null);
51+
result = RemoteOperationResult(mkCol.succeeded(), mkCol)
52+
result.setResultData(null)
5553
}
5654

57-
Log_OC.d(TAG, "Create album " + newAlbumName + ": " + result.getLogMessage());
58-
client.exhaustResponse(mkCol.getResponseBodyAsStream());
59-
} catch (Exception e) {
60-
result = new RemoteOperationResult<>(e);
61-
Log_OC.e(TAG, "Create album " + newAlbumName + ": " + result.getLogMessage(), e);
55+
Log_OC.d(TAG, "Create album $newAlbumName : ${result.logMessage}")
56+
client.exhaustResponse(mkCol.responseBodyAsStream)
57+
} catch (e: Exception) {
58+
result = RemoteOperationResult(e)
59+
Log_OC.e(TAG, "Create album $newAlbumName : ${result.logMessage}", e)
6260
} finally {
63-
if (mkCol != null) {
64-
mkCol.releaseConnection();
65-
}
66-
61+
mkCol?.releaseConnection()
6762
}
6863

69-
return result;
64+
return result
7065
}
7166

67+
companion object {
68+
private val TAG: String = CreateNewAlbumRemoteOperation::class.java.simpleName
69+
}
7270
}

0 commit comments

Comments
 (0)