|
4 | 4 | * SPDX-FileCopyrightText: 2025 TSI-mc <surinder.kumar@t-systems.com> |
5 | 5 | * SPDX-License-Identifier: AGPL-3.0-or-later |
6 | 6 | */ |
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 |
28 | 23 |
|
29 | 24 | /** |
30 | 25 | * Remote operation moving a remote file or folder in the ownCloud server to a different folder |
31 | 26 | * in the same account. |
32 | | - * <p> |
| 27 | + * |
| 28 | + * |
33 | 29 | * Allows renaming the moving file/folder at the same time. |
34 | 30 | */ |
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>() { |
53 | 37 | /** |
54 | 38 | * Performs the operation. |
55 | 39 | * |
56 | 40 | * @param client Client object to communicate with the remote ownCloud server. |
57 | 41 | */ |
58 | | - @Override |
59 | | - protected RemoteOperationResult run(OwnCloudClient client) { |
| 42 | + @Deprecated("Deprecated in Java") |
| 43 | + override fun run(client: OwnCloudClient): RemoteOperationResult<Any> { |
| 44 | + /** check parameters */ |
60 | 45 |
|
61 | | - /// check parameters |
62 | | - if (mTargetRemotePath.equals(mSrcRemotePath)) { |
| 46 | + if (mTargetRemotePath == mSrcRemotePath) { |
63 | 47 | // nothing to do! |
64 | | - return new RemoteOperationResult<>(ResultCode.OK); |
| 48 | + return RemoteOperationResult(ResultCode.OK) |
65 | 49 | } |
66 | 50 |
|
67 | 51 | if (mTargetRemotePath.startsWith(mSrcRemotePath)) { |
68 | | - return new RemoteOperationResult<>(ResultCode.INVALID_COPY_INTO_DESCENDANT); |
| 52 | + return RemoteOperationResult(ResultCode.INVALID_COPY_INTO_DESCENDANT) |
69 | 53 | } |
70 | 54 |
|
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> |
74 | 58 | try { |
75 | | - copyMethod = new CopyMethod( |
| 59 | + copyMethod = CopyMethod( |
76 | 60 | 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 | + }", |
78 | 66 | 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 */ |
83 | 75 | if (status == HttpStatus.SC_MULTI_STATUS) { |
84 | | - result = processPartialError(copyMethod); |
85 | | - |
| 76 | + result = processPartialError(copyMethod) |
86 | 77 | } 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) |
91 | 80 | } 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) |
94 | 83 | } |
95 | 84 |
|
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 | + ) |
102 | 95 | } finally { |
103 | | - if (copyMethod != null) { |
104 | | - copyMethod.releaseConnection(); |
105 | | - } |
| 96 | + copyMethod?.releaseConnection() |
106 | 97 | } |
107 | 98 |
|
108 | | - return result; |
| 99 | + return result |
109 | 100 | } |
110 | 101 |
|
111 | | - |
112 | 102 | /** |
113 | 103 | * Analyzes a multistatus response from the OC server to generate an appropriate result. |
114 | | - * <p> |
| 104 | + * |
| 105 | + * |
115 | 106 | * In WebDAV, a COPY request on collections (folders) can be PARTIALLY successful: some |
116 | 107 | * children are copied, some other aren't. |
117 | | - * <p> |
| 108 | + * |
| 109 | + * |
118 | 110 | * According to the WebDAV specification, a multistatus response SHOULD NOT include partial |
119 | 111 | * successes (201, 204) nor for descendants of already failed children (424) in the response |
120 | 112 | * entity. But SHOULD NOT != MUST NOT, so take carefully. |
121 | 113 | * |
122 | 114 | * @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 |
124 | 116 | * @throws java.io.IOException If the response body could not be parsed |
125 | 117 | * @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 |
127 | 119 | */ |
128 | | - private RemoteOperationResult processPartialError(CopyMethod copyMethod) |
129 | | - throws IOException, DavException { |
| 120 | + @Throws(IOException::class, DavException::class) |
| 121 | + private fun processPartialError(copyMethod: CopyMethod): RemoteOperationResult<Any> { |
130 | 122 | // Adding a list of failed descendants to the result could be interesting; or maybe not. |
131 | 123 | // 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++ |
144 | 135 | } |
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) |
149 | 138 | } else { |
150 | | - result = new RemoteOperationResult<>(true, copyMethod); |
| 139 | + RemoteOperationResult<Any>(true, copyMethod) |
151 | 140 | } |
152 | 141 |
|
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 |
154 | 147 | } |
155 | 148 |
|
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 |
158 | 151 | } |
159 | 152 | } |
0 commit comments