diff --git a/library/src/main/java/com/nextcloud/android/lib/resources/users/GenerateAppPasswordRemoteOperation.java b/library/src/main/java/com/nextcloud/android/lib/resources/users/GenerateAppPasswordRemoteOperation.java index 39648d8d2..957108f46 100644 --- a/library/src/main/java/com/nextcloud/android/lib/resources/users/GenerateAppPasswordRemoteOperation.java +++ b/library/src/main/java/com/nextcloud/android/lib/resources/users/GenerateAppPasswordRemoteOperation.java @@ -7,6 +7,9 @@ */ package com.nextcloud.android.lib.resources.users; + +import com.nextcloud.common.SessionTimeOut; +import com.nextcloud.common.SessionTimeOutKt; import com.owncloud.android.lib.common.OwnCloudClient; import com.owncloud.android.lib.common.operations.RemoteOperationResult; import com.owncloud.android.lib.common.utils.Log_OC; @@ -23,8 +26,6 @@ public class GenerateAppPasswordRemoteOperation extends OCSRemoteOperation { private static final String TAG = GenerateAppPasswordRemoteOperation.class.getSimpleName(); - private static final int SYNC_READ_TIMEOUT = 40000; - private static final int SYNC_CONNECTION_TIMEOUT = 5000; private static final String DIRECT_ENDPOINT = "/ocs/v2.php/core/getapppassword"; // JSON node names @@ -32,6 +33,16 @@ public class GenerateAppPasswordRemoteOperation extends OCSRemoteOperation { private static final String NODE_DATA = "data"; private static final String NODE_APPPASSWORD = "apppassword"; + private final SessionTimeOut sessionTimeOut; + + public GenerateAppPasswordRemoteOperation() { + this(SessionTimeOutKt.getDefaultSessionTimeOut()); + } + + public GenerateAppPasswordRemoteOperation(SessionTimeOut sessionTimeOut) { + this.sessionTimeOut = sessionTimeOut; + } + protected RemoteOperationResult run(OwnCloudClient client) { RemoteOperationResult result; GetMethod getMethod = null; @@ -42,7 +53,7 @@ protected RemoteOperationResult run(OwnCloudClient client) { // remote request getMethod.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE); - int status = client.executeMethod(getMethod, SYNC_READ_TIMEOUT, SYNC_CONNECTION_TIMEOUT); + int status = client.executeMethod(getMethod, sessionTimeOut.getReadTimeOut(), sessionTimeOut.getConnectionTimeOut()); if (status == HttpStatus.SC_OK) { String response = getMethod.getResponseBodyAsString(); diff --git a/library/src/main/java/com/nextcloud/android/lib/richWorkspace/RichWorkspaceDirectEditingRemoteOperation.java b/library/src/main/java/com/nextcloud/android/lib/richWorkspace/RichWorkspaceDirectEditingRemoteOperation.java index d798d0f22..8644643b3 100644 --- a/library/src/main/java/com/nextcloud/android/lib/richWorkspace/RichWorkspaceDirectEditingRemoteOperation.java +++ b/library/src/main/java/com/nextcloud/android/lib/richWorkspace/RichWorkspaceDirectEditingRemoteOperation.java @@ -8,6 +8,8 @@ package com.nextcloud.android.lib.richWorkspace; import com.google.gson.GsonBuilder; +import com.nextcloud.common.SessionTimeOut; +import com.nextcloud.common.SessionTimeOutKt; import com.owncloud.android.lib.common.OwnCloudClient; import com.owncloud.android.lib.common.operations.RemoteOperation; import com.owncloud.android.lib.common.operations.RemoteOperationResult; @@ -27,15 +29,19 @@ public class RichWorkspaceDirectEditingRemoteOperation extends RemoteOperation { private static final String TAG = RichWorkspaceDirectEditingRemoteOperation.class.getSimpleName(); - private static final int SYNC_READ_TIMEOUT = 40000; - private static final int SYNC_CONNECTION_TIMEOUT = 5000; private static final String DIRECT_ENDPOINT = "/ocs/v2.php/apps/text/workspace/direct"; private static final String PATH = "path"; - private String path; + private final SessionTimeOut sessionTimeOut; + private final String path; public RichWorkspaceDirectEditingRemoteOperation(String path) { + this(path, SessionTimeOutKt.getDefaultSessionTimeOut()); + } + + public RichWorkspaceDirectEditingRemoteOperation(String path, SessionTimeOut sessionTimeOut) { this.path = path; + this.sessionTimeOut = sessionTimeOut; } protected RemoteOperationResult run(OwnCloudClient client) { @@ -54,7 +60,7 @@ protected RemoteOperationResult run(OwnCloudClient client) { postMethod.setRequestEntity(new StringRequestEntity(json)); - int status = client.executeMethod(postMethod, SYNC_READ_TIMEOUT, SYNC_CONNECTION_TIMEOUT); + int status = client.executeMethod(postMethod, sessionTimeOut.getReadTimeOut(), sessionTimeOut.getConnectionTimeOut()); if (status == HttpStatus.SC_OK) { String response = postMethod.getResponseBodyAsString(); @@ -63,14 +69,14 @@ protected RemoteOperationResult run(OwnCloudClient client) { JSONObject respJSON = new JSONObject(response); String url = (String) respJSON.getJSONObject("ocs").getJSONObject("data").get("url"); - result = new RemoteOperationResult(true, postMethod); + result = new RemoteOperationResult<>(true, postMethod); result.setSingleData(url); } else { - result = new RemoteOperationResult(false, postMethod); + result = new RemoteOperationResult<>(false, postMethod); client.exhaustResponse(postMethod.getResponseBodyAsStream()); } } catch (Exception e) { - result = new RemoteOperationResult(e); + result = new RemoteOperationResult<>(e); Log_OC.e(TAG, "Get edit url for rich workspace failed: " + result.getLogMessage(), result.getException()); } finally { diff --git a/library/src/main/java/com/nextcloud/common/SessionTimeOut.kt b/library/src/main/java/com/nextcloud/common/SessionTimeOut.kt new file mode 100644 index 000000000..4e2fe838a --- /dev/null +++ b/library/src/main/java/com/nextcloud/common/SessionTimeOut.kt @@ -0,0 +1,13 @@ +/* + * Nextcloud Android Library + * + * SPDX-FileCopyrightText: 2024 Alper Ozturk + * SPDX-License-Identifier: MIT + */ + +package com.nextcloud.common + +data class SessionTimeOut(val readTimeOut: Int, val connectionTimeOut: Int) + +@Suppress("Detekt.MagicNumber") +val defaultSessionTimeOut = SessionTimeOut(60000, 15000) diff --git a/library/src/main/java/com/owncloud/android/lib/resources/e2ee/GetMetadataRemoteOperation.java b/library/src/main/java/com/owncloud/android/lib/resources/e2ee/GetMetadataRemoteOperation.java index 2d666baa3..ec21af37e 100644 --- a/library/src/main/java/com/owncloud/android/lib/resources/e2ee/GetMetadataRemoteOperation.java +++ b/library/src/main/java/com/owncloud/android/lib/resources/e2ee/GetMetadataRemoteOperation.java @@ -7,6 +7,8 @@ */ package com.owncloud.android.lib.resources.e2ee; +import com.nextcloud.common.SessionTimeOut; +import com.nextcloud.common.SessionTimeOutKt; import com.owncloud.android.lib.common.OwnCloudClient; import com.owncloud.android.lib.common.operations.RemoteOperation; import com.owncloud.android.lib.common.operations.RemoteOperationResult; @@ -25,8 +27,6 @@ public class GetMetadataRemoteOperation extends RemoteOperation { private static final String TAG = GetMetadataRemoteOperation.class.getSimpleName(); - private static final int SYNC_READ_TIMEOUT = 40000; - private static final int SYNC_CONNECTION_TIMEOUT = 5000; private static final String METADATA_V1_URL = "/ocs/v2.php/apps/end_to_end_encryption/api/v1/meta-data/"; private static final String METADATA_V2_URL = "/ocs/v2.php/apps/end_to_end_encryption/api/v2/meta-data/"; @@ -38,11 +38,18 @@ public class GetMetadataRemoteOperation extends RemoteOperation run(OwnCloudClient client) { getMethod = new GetMethod(client.getBaseUri() + METADATA_V2_URL + fileId + JSON_FORMAT); getMethod.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE); - int status = client.executeMethod(getMethod, SYNC_READ_TIMEOUT, SYNC_CONNECTION_TIMEOUT); + int status = client.executeMethod(getMethod, sessionTimeOut.getReadTimeOut(), sessionTimeOut.getConnectionTimeOut()); if (status == HttpStatus.SC_NOT_FOUND || status == HttpStatus.SC_INTERNAL_SERVER_ERROR) { // retry with v1 getMethod = new GetMethod(client.getBaseUri() + METADATA_V1_URL + fileId + JSON_FORMAT); getMethod.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE); - status = client.executeMethod(getMethod, SYNC_READ_TIMEOUT, SYNC_CONNECTION_TIMEOUT); + status = client.executeMethod(getMethod, sessionTimeOut.getReadTimeOut(), sessionTimeOut.getConnectionTimeOut()); } if (status == HttpStatus.SC_OK) { @@ -102,5 +109,4 @@ protected RemoteOperationResult run(OwnCloudClient client) { } return result; } - } diff --git a/library/src/main/java/com/owncloud/android/lib/resources/e2ee/LockFileRemoteOperation.java b/library/src/main/java/com/owncloud/android/lib/resources/e2ee/LockFileRemoteOperation.java index 27a265f7e..7b519f89a 100644 --- a/library/src/main/java/com/owncloud/android/lib/resources/e2ee/LockFileRemoteOperation.java +++ b/library/src/main/java/com/owncloud/android/lib/resources/e2ee/LockFileRemoteOperation.java @@ -7,6 +7,8 @@ */ package com.owncloud.android.lib.resources.e2ee; +import com.nextcloud.common.SessionTimeOut; +import com.nextcloud.common.SessionTimeOutKt; import com.owncloud.android.lib.common.OwnCloudClient; import com.owncloud.android.lib.common.operations.RemoteOperation; import com.owncloud.android.lib.common.operations.RemoteOperationResult; @@ -23,29 +25,40 @@ public class LockFileRemoteOperation extends RemoteOperation { private static final String TAG = LockFileRemoteOperation.class.getSimpleName(); - private static final int SYNC_READ_TIMEOUT = 40000; - private static final int SYNC_CONNECTION_TIMEOUT = 5000; private static final String LOCK_FILE_URL = "/ocs/v2.php/apps/end_to_end_encryption/api/v1/lock/"; private static final String COUNTER_HEADER = "X-NC-E2EE-COUNTER"; private final long localId; - private long counter = -1; + private final long counter; + + private static final long defaultCounter = -1; // JSON node names private static final String NODE_OCS = "ocs"; private static final String NODE_DATA = "data"; + private final SessionTimeOut sessionTimeOut; + /** * Constructor */ public LockFileRemoteOperation(long localId, long counter) { - this.localId = localId; - this.counter = counter; + this(localId, counter, SessionTimeOutKt.getDefaultSessionTimeOut()); } public LockFileRemoteOperation(long localId) { + this(localId, SessionTimeOutKt.getDefaultSessionTimeOut()); + } + + public LockFileRemoteOperation(long localId, SessionTimeOut sessionTimeOut) { + this(localId, defaultCounter, sessionTimeOut); + } + + public LockFileRemoteOperation(long localId, long counter, SessionTimeOut sessionTimeOut) { this.localId = localId; + this.counter = counter; + this.sessionTimeOut = sessionTimeOut; } /** @@ -67,7 +80,7 @@ protected RemoteOperationResult run(OwnCloudClient client) { postMethod.addRequestHeader(COUNTER_HEADER, String.valueOf(counter)); } - int status = client.executeMethod(postMethod, SYNC_READ_TIMEOUT, SYNC_CONNECTION_TIMEOUT); + int status = client.executeMethod(postMethod, sessionTimeOut.getReadTimeOut(), sessionTimeOut.getConnectionTimeOut()); if (status == HttpStatus.SC_OK) { String response = postMethod.getResponseBodyAsString(); diff --git a/library/src/main/java/com/owncloud/android/lib/resources/e2ee/StoreMetadataRemoteOperation.java b/library/src/main/java/com/owncloud/android/lib/resources/e2ee/StoreMetadataRemoteOperation.java index 1c95b95d4..a7345cf3d 100644 --- a/library/src/main/java/com/owncloud/android/lib/resources/e2ee/StoreMetadataRemoteOperation.java +++ b/library/src/main/java/com/owncloud/android/lib/resources/e2ee/StoreMetadataRemoteOperation.java @@ -7,6 +7,8 @@ */ package com.owncloud.android.lib.resources.e2ee; +import com.nextcloud.common.SessionTimeOut; +import com.nextcloud.common.SessionTimeOutKt; import com.owncloud.android.lib.common.OwnCloudClient; import com.owncloud.android.lib.common.operations.RemoteOperation; import com.owncloud.android.lib.common.operations.RemoteOperationResult; @@ -26,8 +28,6 @@ public class StoreMetadataRemoteOperation extends RemoteOperation { private static final String TAG = StoreMetadataRemoteOperation.class.getSimpleName(); - private static final int SYNC_READ_TIMEOUT = 40000; - private static final int SYNC_CONNECTION_TIMEOUT = 5000; private static final String METADATA_URL = "/ocs/v2.php/apps/end_to_end_encryption/api/v1/meta-data/"; private static final String METADATA = "metaData"; @@ -39,12 +39,19 @@ public class StoreMetadataRemoteOperation extends RemoteOperation { private final long fileId; private final String encryptedMetadataJson; + private final SessionTimeOut sessionTimeOut; + /** * Constructor */ public StoreMetadataRemoteOperation(long fileId, String encryptedMetadataJson) { + this(fileId, encryptedMetadataJson, SessionTimeOutKt.getDefaultSessionTimeOut()); + } + + public StoreMetadataRemoteOperation(long fileId, String encryptedMetadataJson, SessionTimeOut sessionTimeOut) { this.fileId = fileId; this.encryptedMetadataJson = encryptedMetadataJson; + this.sessionTimeOut = sessionTimeOut; } /** @@ -61,7 +68,7 @@ protected RemoteOperationResult run(OwnCloudClient client) { postMethod.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE); postMethod.setParameter(METADATA, encryptedMetadataJson); - int status = client.executeMethod(postMethod, SYNC_READ_TIMEOUT, SYNC_CONNECTION_TIMEOUT); + int status = client.executeMethod(postMethod, sessionTimeOut.getReadTimeOut(), sessionTimeOut.getConnectionTimeOut()); if (status == HttpStatus.SC_OK) { String response = postMethod.getResponseBodyAsString(); @@ -71,16 +78,16 @@ protected RemoteOperationResult run(OwnCloudClient client) { String metadata = (String) respJSON.getJSONObject(NODE_OCS).getJSONObject(NODE_DATA) .get(NODE_META_DATA); - result = new RemoteOperationResult(true, postMethod); + result = new RemoteOperationResult<>(true, postMethod); ArrayList keys = new ArrayList<>(); keys.add(metadata); result.setData(keys); } else { - result = new RemoteOperationResult(false, postMethod); + result = new RemoteOperationResult<>(false, postMethod); client.exhaustResponse(postMethod.getResponseBodyAsStream()); } } catch (Exception e) { - result = new RemoteOperationResult(e); + result = new RemoteOperationResult<>(e); Log_OC.e(TAG, "Storing of metadata for folder " + fileId + " failed: " + result.getLogMessage(), result.getException()); } finally { diff --git a/library/src/main/java/com/owncloud/android/lib/resources/e2ee/StoreMetadataV2RemoteOperation.kt b/library/src/main/java/com/owncloud/android/lib/resources/e2ee/StoreMetadataV2RemoteOperation.kt index 85be51a80..a2a49c215 100644 --- a/library/src/main/java/com/owncloud/android/lib/resources/e2ee/StoreMetadataV2RemoteOperation.kt +++ b/library/src/main/java/com/owncloud/android/lib/resources/e2ee/StoreMetadataV2RemoteOperation.kt @@ -8,6 +8,8 @@ package com.owncloud.android.lib.resources.e2ee import android.util.Log +import com.nextcloud.common.SessionTimeOut +import com.nextcloud.common.defaultSessionTimeOut import com.owncloud.android.lib.common.OwnCloudClient import com.owncloud.android.lib.common.operations.RemoteOperation import com.owncloud.android.lib.common.operations.RemoteOperationResult @@ -19,73 +21,76 @@ import org.json.JSONObject /** * Remote operation to store the folder metadata */ -class StoreMetadataV2RemoteOperation( - private val remoteId: String, - private val encryptedMetadataJson: String, - private val token: String, - private val signature: String -) : RemoteOperation() { - /** - * @param client Client object - */ - @Deprecated("Deprecated in Java") - @Suppress("Detekt.TooGenericExceptionCaught") - override fun run(client: OwnCloudClient): RemoteOperationResult { - var postMethod: Utf8PostMethod? = null - var result: RemoteOperationResult - try { - // remote request - postMethod = Utf8PostMethod(client.baseUri.toString() + METADATA_URL + remoteId + JSON_FORMAT) - postMethod.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE) - postMethod.addRequestHeader(E2E_TOKEN, token) - postMethod.addRequestHeader(HEADER_SIGNATURE, signature) - postMethod.setParameter(METADATA, encryptedMetadataJson) - val status = client.executeMethod(postMethod, SYNC_READ_TIMEOUT, SYNC_CONNECTION_TIMEOUT) - if (status == HttpStatus.SC_OK) { - val response = postMethod.responseBodyAsString +class StoreMetadataV2RemoteOperation + @JvmOverloads + constructor( + private val remoteId: String, + private val encryptedMetadataJson: String, + private val token: String, + private val signature: String, + private val sessionTimeOut: SessionTimeOut = defaultSessionTimeOut + ) : RemoteOperation() { + /** + * @param client Client object + */ + @Deprecated("Deprecated in Java") + @Suppress("Detekt.TooGenericExceptionCaught") + override fun run(client: OwnCloudClient): RemoteOperationResult { + var postMethod: Utf8PostMethod? = null + var result: RemoteOperationResult + try { + // remote request + postMethod = Utf8PostMethod(client.baseUri.toString() + METADATA_URL + remoteId + JSON_FORMAT) + postMethod.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE) + postMethod.addRequestHeader(E2E_TOKEN, token) + postMethod.addRequestHeader(HEADER_SIGNATURE, signature) + postMethod.setParameter(METADATA, encryptedMetadataJson) + val status = + client + .executeMethod(postMethod, sessionTimeOut.readTimeOut, sessionTimeOut.connectionTimeOut) + if (status == HttpStatus.SC_OK) { + val response = postMethod.responseBodyAsString - // Parse the response - val respJSON = JSONObject(response) - val metadata = - respJSON - .getJSONObject(NODE_OCS) - .getJSONObject(NODE_DATA) - .getString(NODE_META_DATA) - result = RemoteOperationResult(true, postMethod) - result.setResultData(metadata) - } else { - result = RemoteOperationResult(false, postMethod) - Log.e( + // Parse the response + val respJSON = JSONObject(response) + val metadata = + respJSON + .getJSONObject(NODE_OCS) + .getJSONObject(NODE_DATA) + .getString(NODE_META_DATA) + result = RemoteOperationResult(true, postMethod) + result.setResultData(metadata) + } else { + result = RemoteOperationResult(false, postMethod) + Log.e( + TAG, + "Storing of metadata for folder " + remoteId + " failed: " + postMethod.responseBodyAsString, + result.exception + ) + client.exhaustResponse(postMethod.responseBodyAsStream) + } + } catch (e: Exception) { + result = RemoteOperationResult(e) + Log_OC.e( TAG, - "Storing of metadata for folder " + remoteId + " failed: " + postMethod.responseBodyAsString, + "Storing of metadata for folder " + remoteId + " failed: " + result.logMessage, result.exception ) - client.exhaustResponse(postMethod.responseBodyAsStream) + } finally { + postMethod?.releaseConnection() } - } catch (e: Exception) { - result = RemoteOperationResult(e) - Log_OC.e( - TAG, - "Storing of metadata for folder " + remoteId + " failed: " + result.logMessage, - result.exception - ) - } finally { - postMethod?.releaseConnection() + return result } - return result - } - companion object { - private val TAG = StoreMetadataV2RemoteOperation::class.java.simpleName - private const val SYNC_READ_TIMEOUT = 40000 - private const val SYNC_CONNECTION_TIMEOUT = 5000 - private const val METADATA_URL = "/ocs/v2.php/apps/end_to_end_encryption/api/v2/meta-data/" - private const val METADATA = "metaData" - private const val HEADER_SIGNATURE = "X-NC-E2EE-SIGNATURE" + companion object { + private val TAG = StoreMetadataV2RemoteOperation::class.java.simpleName + private const val METADATA_URL = "/ocs/v2.php/apps/end_to_end_encryption/api/v2/meta-data/" + private const val METADATA = "metaData" + private const val HEADER_SIGNATURE = "X-NC-E2EE-SIGNATURE" - // JSON node names - private const val NODE_OCS = "ocs" - private const val NODE_DATA = "data" - private const val NODE_META_DATA = "meta-data" + // JSON node names + private const val NODE_OCS = "ocs" + private const val NODE_DATA = "data" + private const val NODE_META_DATA = "meta-data" + } } -} diff --git a/library/src/main/java/com/owncloud/android/lib/resources/e2ee/ToggleEncryptionRemoteOperation.java b/library/src/main/java/com/owncloud/android/lib/resources/e2ee/ToggleEncryptionRemoteOperation.java index d6720a75f..3651b826d 100644 --- a/library/src/main/java/com/owncloud/android/lib/resources/e2ee/ToggleEncryptionRemoteOperation.java +++ b/library/src/main/java/com/owncloud/android/lib/resources/e2ee/ToggleEncryptionRemoteOperation.java @@ -7,6 +7,8 @@ */ package com.owncloud.android.lib.resources.e2ee; +import com.nextcloud.common.SessionTimeOut; +import com.nextcloud.common.SessionTimeOutKt; import com.owncloud.android.lib.common.OwnCloudClient; import com.owncloud.android.lib.common.operations.RemoteOperation; import com.owncloud.android.lib.common.operations.RemoteOperationResult; @@ -26,21 +28,26 @@ public class ToggleEncryptionRemoteOperation extends RemoteOperation { private static final String TAG = ToggleEncryptionRemoteOperation.class.getSimpleName(); - private static final int SYNC_READ_TIMEOUT = 40000; - private static final int SYNC_CONNECTION_TIMEOUT = 5000; private static final String ENCRYPTED_URL = "/ocs/v2.php/apps/end_to_end_encryption/api/v1/encrypted/"; private final long localId; private final String remotePath; private final boolean encryption; + private final SessionTimeOut sessionTimeOut; + /** * Constructor */ public ToggleEncryptionRemoteOperation(long localId, String remotePath, boolean encryption) { + this(localId, remotePath, encryption, SessionTimeOutKt.getDefaultSessionTimeOut()); + } + + public ToggleEncryptionRemoteOperation(long localId, String remotePath, boolean encryption, SessionTimeOut sessionTimeOut) { this.localId = localId; this.remotePath = remotePath; this.encryption = encryption; + this.sessionTimeOut = sessionTimeOut; } /** @@ -72,7 +79,7 @@ protected RemoteOperationResult run(OwnCloudClient client) { method.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE); method.addRequestHeader(CONTENT_TYPE, FORM_URLENCODED); - int status = client.executeMethod(method, SYNC_READ_TIMEOUT, SYNC_CONNECTION_TIMEOUT); + int status = client.executeMethod(method, sessionTimeOut.getReadTimeOut(), sessionTimeOut.getConnectionTimeOut()); if (status == HttpStatus.SC_OK) { result = new RemoteOperationResult<>(true, method); diff --git a/library/src/main/java/com/owncloud/android/lib/resources/e2ee/UnlockFileRemoteOperation.java b/library/src/main/java/com/owncloud/android/lib/resources/e2ee/UnlockFileRemoteOperation.java index db110881e..ae5e34b21 100644 --- a/library/src/main/java/com/owncloud/android/lib/resources/e2ee/UnlockFileRemoteOperation.java +++ b/library/src/main/java/com/owncloud/android/lib/resources/e2ee/UnlockFileRemoteOperation.java @@ -7,6 +7,8 @@ */ package com.owncloud.android.lib.resources.e2ee; +import com.nextcloud.common.SessionTimeOut; +import com.nextcloud.common.SessionTimeOutKt; import com.owncloud.android.lib.common.OwnCloudClient; import com.owncloud.android.lib.common.operations.RemoteOperation; import com.owncloud.android.lib.common.operations.RemoteOperationResult; @@ -22,19 +24,24 @@ public class UnlockFileRemoteOperation extends RemoteOperation { private static final String TAG = UnlockFileRemoteOperation.class.getSimpleName(); - private static final int SYNC_READ_TIMEOUT = 40000; - private static final int SYNC_CONNECTION_TIMEOUT = 5000; private static final String LOCK_FILE_URL = "/ocs/v2.php/apps/end_to_end_encryption/api/v2/lock/"; private final long localId; private final String token; + private final SessionTimeOut sessionTimeOut; + /** * Constructor */ public UnlockFileRemoteOperation(long localId, String token) { + this(localId, token, SessionTimeOutKt.getDefaultSessionTimeOut()); + } + + public UnlockFileRemoteOperation(long localId, String token, SessionTimeOut sessionTimeOut) { this.localId = localId; this.token = token; + this.sessionTimeOut = sessionTimeOut; } /** @@ -52,7 +59,7 @@ protected RemoteOperationResult run(OwnCloudClient client) { deleteMethod.addRequestHeader(CONTENT_TYPE, FORM_URLENCODED); deleteMethod.addRequestHeader(E2E_TOKEN, token); - int status = client.executeMethod(deleteMethod, SYNC_READ_TIMEOUT, SYNC_CONNECTION_TIMEOUT); + int status = client.executeMethod(deleteMethod, sessionTimeOut.getReadTimeOut(), sessionTimeOut.getConnectionTimeOut()); result = new RemoteOperationResult<>(status == HttpStatus.SC_OK, deleteMethod); diff --git a/library/src/main/java/com/owncloud/android/lib/resources/e2ee/UnlockFileV1RemoteOperation.kt b/library/src/main/java/com/owncloud/android/lib/resources/e2ee/UnlockFileV1RemoteOperation.kt index c13e24694..dc7b5bc73 100644 --- a/library/src/main/java/com/owncloud/android/lib/resources/e2ee/UnlockFileV1RemoteOperation.kt +++ b/library/src/main/java/com/owncloud/android/lib/resources/e2ee/UnlockFileV1RemoteOperation.kt @@ -7,6 +7,8 @@ */ package com.owncloud.android.lib.resources.e2ee +import com.nextcloud.common.SessionTimeOut +import com.nextcloud.common.defaultSessionTimeOut import com.owncloud.android.lib.common.OwnCloudClient import com.owncloud.android.lib.common.operations.RemoteOperation import com.owncloud.android.lib.common.operations.RemoteOperationResult @@ -17,45 +19,43 @@ import org.apache.commons.httpclient.methods.DeleteMethod /** * Unlock a file */ -class UnlockFileV1RemoteOperation( - private val localId: Long, - private val token: String -) : RemoteOperation() { - /** - * @param client Client object - */ - @Deprecated("Deprecated in Java") - @Suppress("Detekt.TooGenericExceptionCaught") - override fun run(client: OwnCloudClient): RemoteOperationResult { - var result: RemoteOperationResult - var deleteMethod: DeleteMethod? = null - try { - // remote request - deleteMethod = DeleteMethod(client.baseUri.toString() + LOCK_FILE_URL + localId) - deleteMethod.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE) - deleteMethod.addRequestHeader(CONTENT_TYPE, FORM_URLENCODED) - deleteMethod.addRequestHeader(E2E_TOKEN, token) - val status = - client.executeMethod(deleteMethod, SYNC_READ_TIMEOUT, SYNC_CONNECTION_TIMEOUT) - result = RemoteOperationResult(status == HttpStatus.SC_OK, deleteMethod) - client.exhaustResponse(deleteMethod.responseBodyAsStream) - } catch (e: Exception) { - result = RemoteOperationResult(e) - Log_OC.e( - TAG, - "Unlock file with id " + localId + " failed: " + result.logMessage, - result.exception - ) - } finally { - deleteMethod?.releaseConnection() +class UnlockFileV1RemoteOperation + @JvmOverloads + constructor( + private val localId: Long, + private val token: String, + private val sessionTimeOut: SessionTimeOut = defaultSessionTimeOut + ) : RemoteOperation() { + @Deprecated("Deprecated in Java") + @Suppress("Detekt.TooGenericExceptionCaught") + override fun run(client: OwnCloudClient): RemoteOperationResult { + var result: RemoteOperationResult + var deleteMethod: DeleteMethod? = null + try { + // remote request + deleteMethod = DeleteMethod(client.baseUri.toString() + LOCK_FILE_URL + localId) + deleteMethod.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE) + deleteMethod.addRequestHeader(CONTENT_TYPE, FORM_URLENCODED) + deleteMethod.addRequestHeader(E2E_TOKEN, token) + val status = + client.executeMethod(deleteMethod, sessionTimeOut.readTimeOut, sessionTimeOut.connectionTimeOut) + result = RemoteOperationResult(status == HttpStatus.SC_OK, deleteMethod) + client.exhaustResponse(deleteMethod.responseBodyAsStream) + } catch (e: Exception) { + result = RemoteOperationResult(e) + Log_OC.e( + TAG, + "Unlock file with id " + localId + " failed: " + result.logMessage, + result.exception + ) + } finally { + deleteMethod?.releaseConnection() + } + return result } - return result - } - companion object { - private val TAG = UnlockFileV1RemoteOperation::class.java.simpleName - private const val SYNC_READ_TIMEOUT = 40000 - private const val SYNC_CONNECTION_TIMEOUT = 5000 - private const val LOCK_FILE_URL = "/ocs/v2.php/apps/end_to_end_encryption/api/v1/lock/" + companion object { + private val TAG = UnlockFileV1RemoteOperation::class.java.simpleName + private const val LOCK_FILE_URL = "/ocs/v2.php/apps/end_to_end_encryption/api/v1/lock/" + } } -} diff --git a/library/src/main/java/com/owncloud/android/lib/resources/e2ee/UpdateMetadataRemoteOperation.java b/library/src/main/java/com/owncloud/android/lib/resources/e2ee/UpdateMetadataRemoteOperation.java index 0ebe60f01..1033d0e95 100644 --- a/library/src/main/java/com/owncloud/android/lib/resources/e2ee/UpdateMetadataRemoteOperation.java +++ b/library/src/main/java/com/owncloud/android/lib/resources/e2ee/UpdateMetadataRemoteOperation.java @@ -7,6 +7,8 @@ */ package com.owncloud.android.lib.resources.e2ee; +import com.nextcloud.common.SessionTimeOut; +import com.nextcloud.common.SessionTimeOutKt; import com.owncloud.android.lib.common.OwnCloudClient; import com.owncloud.android.lib.common.operations.RemoteOperation; import com.owncloud.android.lib.common.operations.RemoteOperationResult; @@ -29,8 +31,6 @@ public class UpdateMetadataRemoteOperation extends RemoteOperation { private static final String TAG = UpdateMetadataRemoteOperation.class.getSimpleName(); - private static final int SYNC_READ_TIMEOUT = 40000; - private static final int SYNC_CONNECTION_TIMEOUT = 5000; private static final String METADATA_URL = "/ocs/v2.php/apps/end_to_end_encryption/api/v1/meta-data/"; private static final String FORMAT = "format"; @@ -43,13 +43,20 @@ public class UpdateMetadataRemoteOperation extends RemoteOperation { private final String encryptedMetadataJson; private final String token; + private final SessionTimeOut sessionTimeOut; + /** * Constructor */ public UpdateMetadataRemoteOperation(long fileId, String encryptedMetadataJson, String token) { + this(fileId, encryptedMetadataJson, token, SessionTimeOutKt.getDefaultSessionTimeOut()); + } + + public UpdateMetadataRemoteOperation(long fileId, String encryptedMetadataJson, String token, SessionTimeOut sessionTimeOut) { this.fileId = fileId; this.encryptedMetadataJson = URLEncoder.encode(encryptedMetadataJson); this.token = token; + this.sessionTimeOut = sessionTimeOut; } /** @@ -75,7 +82,7 @@ protected RemoteOperationResult run(OwnCloudClient client) { "application/json", "UTF-8"); putMethod.setRequestEntity(data); - int status = client.executeMethod(putMethod, SYNC_READ_TIMEOUT, SYNC_CONNECTION_TIMEOUT); + int status = client.executeMethod(putMethod, sessionTimeOut.getReadTimeOut(), sessionTimeOut.getConnectionTimeOut()); if (status == HttpStatus.SC_OK) { String response = putMethod.getResponseBodyAsString(); @@ -85,16 +92,16 @@ protected RemoteOperationResult run(OwnCloudClient client) { String metadata = (String) respJSON.getJSONObject(NODE_OCS).getJSONObject(NODE_DATA) .get(NODE_META_DATA); - result = new RemoteOperationResult(true, putMethod); + result = new RemoteOperationResult<>(true, putMethod); ArrayList keys = new ArrayList<>(); keys.add(metadata); result.setData(keys); } else { - result = new RemoteOperationResult(false, putMethod); + result = new RemoteOperationResult<>(false, putMethod); client.exhaustResponse(putMethod.getResponseBodyAsStream()); } } catch (Exception e) { - result = new RemoteOperationResult(e); + result = new RemoteOperationResult<>(e); Log_OC.e(TAG, "Storing of metadata for folder " + fileId + " failed: " + result.getLogMessage(), result.getException()); } finally { diff --git a/library/src/main/java/com/owncloud/android/lib/resources/e2ee/UpdateMetadataV2RemoteOperation.kt b/library/src/main/java/com/owncloud/android/lib/resources/e2ee/UpdateMetadataV2RemoteOperation.kt index c8d6e5b14..986361a29 100644 --- a/library/src/main/java/com/owncloud/android/lib/resources/e2ee/UpdateMetadataV2RemoteOperation.kt +++ b/library/src/main/java/com/owncloud/android/lib/resources/e2ee/UpdateMetadataV2RemoteOperation.kt @@ -8,6 +8,8 @@ package com.owncloud.android.lib.resources.e2ee import android.util.Log +import com.nextcloud.common.SessionTimeOut +import com.nextcloud.common.defaultSessionTimeOut import com.owncloud.android.lib.common.OwnCloudClient import com.owncloud.android.lib.common.operations.RemoteOperation import com.owncloud.android.lib.common.operations.RemoteOperationResult @@ -21,93 +23,87 @@ import java.net.URLEncoder /** * Remote operation to update the folder metadata */ -class UpdateMetadataV2RemoteOperation( - private val remoteId: String, - encryptedMetadataJson: String, - private val token: String, - private val signature: String -) : RemoteOperation() { - private val encryptedMetadataJson: String +class UpdateMetadataV2RemoteOperation + @JvmOverloads + constructor( + private val remoteId: String, + encryptedMetadataJson: String, + private val token: String, + private val signature: String, + private val sessionTimeOut: SessionTimeOut = defaultSessionTimeOut + ) : RemoteOperation() { + private val encryptedMetadataJson: String = URLEncoder.encode(encryptedMetadataJson) - /** - * Constructor - */ - init { - this.encryptedMetadataJson = URLEncoder.encode(encryptedMetadataJson) - // this.encryptedMetadataJson = encryptedMetadataJson; - } - - /** - * @param client Client object - */ - @Deprecated("Deprecated in Java") - @Suppress("Detekt.TooGenericExceptionCaught") - override fun run(client: OwnCloudClient): RemoteOperationResult { - var putMethod: PutMethod? = null - var result: RemoteOperationResult - try { - // remote request - putMethod = PutMethod(client.baseUri.toString() + METADATA_URL + remoteId) - putMethod.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE) - putMethod.addRequestHeader(E2E_TOKEN, token) - putMethod.addRequestHeader(HEADER_SIGNATURE, signature) - putMethod.addRequestHeader(CONTENT_TYPE, FORM_URLENCODED) - val putParams = arrayOfNulls(1) - putParams[0] = NameValuePair(FORMAT, "json") - putMethod.setQueryString(putParams) - val data = - StringRequestEntity( - "metaData=$encryptedMetadataJson", - "application/json", - "UTF-8" - ) - putMethod.requestEntity = data - val status = client.executeMethod(putMethod, SYNC_READ_TIMEOUT, SYNC_CONNECTION_TIMEOUT) - if (status == HttpStatus.SC_OK) { - val response = putMethod.responseBodyAsString + /** + * @param client Client object + */ + @Deprecated("Deprecated in Java") + @Suppress("Detekt.TooGenericExceptionCaught") + override fun run(client: OwnCloudClient): RemoteOperationResult { + var putMethod: PutMethod? = null + var result: RemoteOperationResult + try { + // remote request + putMethod = PutMethod(client.baseUri.toString() + METADATA_URL + remoteId) + putMethod.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE) + putMethod.addRequestHeader(E2E_TOKEN, token) + putMethod.addRequestHeader(HEADER_SIGNATURE, signature) + putMethod.addRequestHeader(CONTENT_TYPE, FORM_URLENCODED) + val putParams = arrayOfNulls(1) + putParams[0] = NameValuePair(FORMAT, "json") + putMethod.setQueryString(putParams) + val data = + StringRequestEntity( + "metaData=$encryptedMetadataJson", + "application/json", + "UTF-8" + ) + putMethod.requestEntity = data + val status = + client + .executeMethod(putMethod, sessionTimeOut.readTimeOut, sessionTimeOut.connectionTimeOut) + if (status == HttpStatus.SC_OK) { + val response = putMethod.responseBodyAsString - // Parse the response - val respJSON = JSONObject(response) - val metadata = - respJSON - .getJSONObject(NODE_OCS) - .getJSONObject(NODE_DATA) - .getString(NODE_META_DATA) - result = RemoteOperationResult(true, putMethod) - result.setResultData(metadata) - } else { - result = RemoteOperationResult(false, putMethod) + val respJSON = JSONObject(response) + val metadata = + respJSON + .getJSONObject(NODE_OCS) + .getJSONObject(NODE_DATA) + .getString(NODE_META_DATA) + result = RemoteOperationResult(true, putMethod) + result.setResultData(metadata) + } else { + result = RemoteOperationResult(false, putMethod) + Log.e( + TAG, + "Updating metadata for folder " + remoteId + " failed: " + putMethod.responseBodyAsString, + result.exception + ) + client.exhaustResponse(putMethod.responseBodyAsStream) + } + } catch (e: Exception) { + result = RemoteOperationResult(e) Log.e( TAG, - "Updating metadata for folder " + remoteId + " failed: " + putMethod.responseBodyAsString, + "Updating metadata for folder " + remoteId + " failed: " + result.logMessage, result.exception ) - client.exhaustResponse(putMethod.responseBodyAsStream) + } finally { + putMethod?.releaseConnection() } - } catch (e: Exception) { - result = RemoteOperationResult(e) - Log.e( - TAG, - "Updating metadata for folder " + remoteId + " failed: " + result.logMessage, - result.exception - ) - } finally { - putMethod?.releaseConnection() + return result } - return result - } - companion object { - private val TAG = UpdateMetadataV2RemoteOperation::class.java.simpleName - private const val SYNC_READ_TIMEOUT = 40000 - private const val SYNC_CONNECTION_TIMEOUT = 5000 - private const val METADATA_URL = "/ocs/v2.php/apps/end_to_end_encryption/api/v2/meta-data/" - private const val FORMAT = "format" + companion object { + private val TAG = UpdateMetadataV2RemoteOperation::class.java.simpleName + private const val METADATA_URL = "/ocs/v2.php/apps/end_to_end_encryption/api/v2/meta-data/" + private const val FORMAT = "format" - // JSON node names - private const val NODE_OCS = "ocs" - private const val NODE_DATA = "data" - private const val NODE_META_DATA = "meta-data" - private const val HEADER_SIGNATURE = "X-NC-E2EE-SIGNATURE" + // JSON node names + private const val NODE_OCS = "ocs" + private const val NODE_DATA = "data" + private const val NODE_META_DATA = "meta-data" + private const val HEADER_SIGNATURE = "X-NC-E2EE-SIGNATURE" + } } -} diff --git a/library/src/main/java/com/owncloud/android/lib/resources/files/CheckEtagRemoteOperation.java b/library/src/main/java/com/owncloud/android/lib/resources/files/CheckEtagRemoteOperation.java index 51f9dd33d..32d87b8c8 100644 --- a/library/src/main/java/com/owncloud/android/lib/resources/files/CheckEtagRemoteOperation.java +++ b/library/src/main/java/com/owncloud/android/lib/resources/files/CheckEtagRemoteOperation.java @@ -7,6 +7,8 @@ */ package com.owncloud.android.lib.resources.files; +import com.nextcloud.common.SessionTimeOut; +import com.nextcloud.common.SessionTimeOutKt; import com.owncloud.android.lib.common.OwnCloudClient; import com.owncloud.android.lib.common.network.WebdavUtils; import com.owncloud.android.lib.common.operations.RemoteOperation; @@ -29,19 +31,23 @@ */ public class CheckEtagRemoteOperation extends RemoteOperation { - private static final int SYNC_READ_TIMEOUT = 40000; - private static final int SYNC_CONNECTION_TIMEOUT = 5000; private static final String TAG = CheckEtagRemoteOperation.class.getSimpleName(); - private String path; - private String expectedEtag; + private final String path; + private final String expectedEtag; + + private final SessionTimeOut sessionTimeOut; public CheckEtagRemoteOperation(String path, String expectedEtag) { + this(path, expectedEtag, SessionTimeOutKt.getDefaultSessionTimeOut()); + } + + public CheckEtagRemoteOperation(String path, String expectedEtag, SessionTimeOut sessionTimeOut) { this.path = path; this.expectedEtag = expectedEtag; + this.sessionTimeOut = sessionTimeOut; } - @Override protected RemoteOperationResult run(OwnCloudClient client) { PropFindMethod propfind = null; @@ -53,7 +59,7 @@ protected RemoteOperationResult run(OwnCloudClient client) { propfind = new PropFindMethod(client.getFilesDavUri(path), propSet, 0); - int status = client.executeMethod(propfind, SYNC_READ_TIMEOUT, SYNC_CONNECTION_TIMEOUT); + int status = client.executeMethod(propfind, sessionTimeOut.getReadTimeOut(), sessionTimeOut.getConnectionTimeOut()); if (status == HttpStatus.SC_MULTI_STATUS || status == HttpStatus.SC_OK) { MultiStatusResponse resp = propfind.getResponseBodyAsMultiStatus().getResponses()[0]; @@ -62,9 +68,9 @@ protected RemoteOperationResult run(OwnCloudClient client) { .get(DavPropertyName.GETETAG).getValue()); if (etag.equals(expectedEtag)) { - return new RemoteOperationResult(ResultCode.ETAG_UNCHANGED); + return new RemoteOperationResult<>(ResultCode.ETAG_UNCHANGED); } else { - RemoteOperationResult result = new RemoteOperationResult(ResultCode.ETAG_CHANGED); + final var result = new RemoteOperationResult<>(ResultCode.ETAG_CHANGED); ArrayList list = new ArrayList<>(); list.add(etag); @@ -75,7 +81,7 @@ protected RemoteOperationResult run(OwnCloudClient client) { } if (status == HttpStatus.SC_NOT_FOUND) { - return new RemoteOperationResult(ResultCode.FILE_NOT_FOUND); + return new RemoteOperationResult<>(ResultCode.FILE_NOT_FOUND); } } catch (DavException | IOException e) { Log_OC.e(TAG, "Error while retrieving eTag"); @@ -85,6 +91,6 @@ protected RemoteOperationResult run(OwnCloudClient client) { } } - return new RemoteOperationResult(ResultCode.ETAG_CHANGED); + return new RemoteOperationResult<>(ResultCode.ETAG_CHANGED); } } diff --git a/library/src/main/java/com/owncloud/android/lib/resources/files/CopyFileRemoteOperation.java b/library/src/main/java/com/owncloud/android/lib/resources/files/CopyFileRemoteOperation.java index 3bec3c96a..0ca980caf 100644 --- a/library/src/main/java/com/owncloud/android/lib/resources/files/CopyFileRemoteOperation.java +++ b/library/src/main/java/com/owncloud/android/lib/resources/files/CopyFileRemoteOperation.java @@ -8,6 +8,8 @@ import android.util.Log; +import com.nextcloud.common.SessionTimeOut; +import com.nextcloud.common.SessionTimeOutKt; import com.owncloud.android.lib.common.OwnCloudClient; import com.owncloud.android.lib.common.operations.RemoteOperation; import com.owncloud.android.lib.common.operations.RemoteOperationResult; @@ -34,13 +36,13 @@ public class CopyFileRemoteOperation extends RemoteOperation { private static final String TAG = CopyFileRemoteOperation.class.getSimpleName(); - private static final int COPY_READ_TIMEOUT = 600000; - private static final int COPY_CONNECTION_TIMEOUT = 5000; - private String mSrcRemotePath; - private String mTargetRemotePath; + private final String mSrcRemotePath; + private final String mTargetRemotePath; - private boolean mOverwrite; + private final boolean mOverwrite; + + private final SessionTimeOut sessionTimeOut; /** @@ -51,14 +53,18 @@ public class CopyFileRemoteOperation extends RemoteOperation { * @param srcRemotePath Remote path of the file/folder to move. * @param targetRemotePath Remove path desired for the file/folder after moving it. */ - public CopyFileRemoteOperation(String srcRemotePath, String targetRemotePath, boolean overwrite - ) { + public CopyFileRemoteOperation(String srcRemotePath, String targetRemotePath, boolean overwrite) { + this(srcRemotePath, targetRemotePath, overwrite, SessionTimeOutKt.getDefaultSessionTimeOut()); + } + + + public CopyFileRemoteOperation(String srcRemotePath, String targetRemotePath, boolean overwrite, SessionTimeOut sessionTimeOut) { mSrcRemotePath = srcRemotePath; mTargetRemotePath = targetRemotePath; mOverwrite = overwrite; + this.sessionTimeOut = sessionTimeOut; } - /** * Performs the rename operation. * @@ -70,11 +76,11 @@ protected RemoteOperationResult run(OwnCloudClient client) { /// check parameters if (mTargetRemotePath.equals(mSrcRemotePath)) { // nothing to do! - return new RemoteOperationResult(ResultCode.OK); + return new RemoteOperationResult<>(ResultCode.OK); } if (mTargetRemotePath.startsWith(mSrcRemotePath)) { - return new RemoteOperationResult(ResultCode.INVALID_COPY_INTO_DESCENDANT); + return new RemoteOperationResult<>(ResultCode.INVALID_COPY_INTO_DESCENDANT); } /// perform remote operation @@ -86,7 +92,7 @@ protected RemoteOperationResult run(OwnCloudClient client) { client.getFilesDavUri(mTargetRemotePath), mOverwrite ); - int status = client.executeMethod(copyMethod, COPY_READ_TIMEOUT, COPY_CONNECTION_TIMEOUT); + int status = client.executeMethod(copyMethod, sessionTimeOut.getReadTimeOut(), sessionTimeOut.getConnectionTimeOut()); /// process response if (status == HttpStatus.SC_MULTI_STATUS) { @@ -94,7 +100,7 @@ protected RemoteOperationResult run(OwnCloudClient client) { } else if (status == HttpStatus.SC_PRECONDITION_FAILED && !mOverwrite) { - result = new RemoteOperationResult(ResultCode.INVALID_OVERWRITE); + result = new RemoteOperationResult<>(ResultCode.INVALID_OVERWRITE); client.exhaustResponse(copyMethod.getResponseBodyAsStream()); @@ -102,14 +108,14 @@ protected RemoteOperationResult run(OwnCloudClient client) { /// http://www.webdav.org/specs/rfc4918.html#rfc.section.9.9.4 } else { - result = new RemoteOperationResult(isSuccess(status), copyMethod); + result = new RemoteOperationResult<>(isSuccess(status), copyMethod); client.exhaustResponse(copyMethod.getResponseBodyAsStream()); } Log.i(TAG, "Copy " + mSrcRemotePath + " to " + mTargetRemotePath + ": " + result.getLogMessage()); } catch (Exception e) { - result = new RemoteOperationResult(e); + result = new RemoteOperationResult<>(e); Log.e(TAG, "Copy " + mSrcRemotePath + " to " + mTargetRemotePath + ": " + result.getLogMessage(), e); } finally { @@ -158,9 +164,9 @@ private RemoteOperationResult processPartialError(CopyMethod copyMethod) RemoteOperationResult result; if (failFound) { - result = new RemoteOperationResult(ResultCode.PARTIAL_COPY_DONE); + result = new RemoteOperationResult<>(ResultCode.PARTIAL_COPY_DONE); } else { - result = new RemoteOperationResult(true, copyMethod); + result = new RemoteOperationResult<>(true, copyMethod); } return result; diff --git a/library/src/main/java/com/owncloud/android/lib/resources/files/CreateFolderRemoteOperation.java b/library/src/main/java/com/owncloud/android/lib/resources/files/CreateFolderRemoteOperation.java index 328378d88..820c09931 100644 --- a/library/src/main/java/com/owncloud/android/lib/resources/files/CreateFolderRemoteOperation.java +++ b/library/src/main/java/com/owncloud/android/lib/resources/files/CreateFolderRemoteOperation.java @@ -8,6 +8,8 @@ import android.text.TextUtils; +import com.nextcloud.common.SessionTimeOut; +import com.nextcloud.common.SessionTimeOutKt; import com.owncloud.android.lib.common.OwnCloudClient; import com.owncloud.android.lib.common.operations.RemoteOperation; import com.owncloud.android.lib.common.operations.RemoteOperationResult; @@ -28,13 +30,11 @@ public class CreateFolderRemoteOperation extends RemoteOperation { private static final String TAG = CreateFolderRemoteOperation.class.getSimpleName(); - private static final int READ_TIMEOUT = 30000; - private static final int CONNECTION_TIMEOUT = 5000; - - private boolean createFullPath; - private String remotePath; - private String token; + private final boolean createFullPath; + private final String remotePath; + private final String token; + private final SessionTimeOut sessionTimeOut; /** * Constructor @@ -44,13 +44,22 @@ public class CreateFolderRemoteOperation extends RemoteOperation { * if don't exist yet. */ public CreateFolderRemoteOperation(String remotePath, boolean createFullPath) { - this.remotePath = remotePath; - this.createFullPath = createFullPath; + this(remotePath, createFullPath, SessionTimeOutKt.getDefaultSessionTimeOut()); } public CreateFolderRemoteOperation(String remotePath, boolean createFullPath, String token) { - this(remotePath, createFullPath); + this(remotePath, createFullPath, token, SessionTimeOutKt.getDefaultSessionTimeOut()); + } + + public CreateFolderRemoteOperation(String remotePath, boolean createFullPath, SessionTimeOut sessionTimeOut) { + this(remotePath, createFullPath, "", sessionTimeOut); + } + + public CreateFolderRemoteOperation(String remotePath, boolean createFullPath, String token, SessionTimeOut sessionTimeOut) { + this.remotePath = remotePath; + this.createFullPath = createFullPath; this.token = token; + this.sessionTimeOut = sessionTimeOut; } /** @@ -86,7 +95,7 @@ private RemoteOperationResult createFolder(OwnCloudClient client) { mkCol.addRequestHeader(E2E_TOKEN, token); } - client.executeMethod(mkCol, READ_TIMEOUT, CONNECTION_TIMEOUT); + client.executeMethod(mkCol, sessionTimeOut.getReadTimeOut(), sessionTimeOut.getConnectionTimeOut()); if (HttpStatus.SC_METHOD_NOT_ALLOWED == mkCol.getStatusCode()) { result = new RemoteOperationResult<>(RemoteOperationResult.ResultCode.FOLDER_ALREADY_EXISTS); @@ -120,6 +129,4 @@ private RemoteOperationResult createParentFolder(String parentPath, OwnC RemoteOperation operation = new CreateFolderRemoteOperation(parentPath, createFullPath); return operation.execute(client); } - - } diff --git a/library/src/main/java/com/owncloud/android/lib/resources/files/MoveFileRemoteOperation.java b/library/src/main/java/com/owncloud/android/lib/resources/files/MoveFileRemoteOperation.java index 4ca495a4a..31f836658 100644 --- a/library/src/main/java/com/owncloud/android/lib/resources/files/MoveFileRemoteOperation.java +++ b/library/src/main/java/com/owncloud/android/lib/resources/files/MoveFileRemoteOperation.java @@ -8,6 +8,8 @@ import android.util.Log; +import com.nextcloud.common.SessionTimeOut; +import com.nextcloud.common.SessionTimeOutKt; import com.owncloud.android.lib.common.OwnCloudClient; import com.owncloud.android.lib.common.operations.RemoteOperation; import com.owncloud.android.lib.common.operations.RemoteOperationResult; @@ -33,13 +35,11 @@ public class MoveFileRemoteOperation extends RemoteOperation { private static final String TAG = MoveFileRemoteOperation.class.getSimpleName(); - private static final int MOVE_READ_TIMEOUT = 600000; - private static final int MOVE_CONNECTION_TIMEOUT = 5000; + private final String mSrcRemotePath; + private final String mTargetRemotePath; + private final boolean mOverwrite; - private String mSrcRemotePath; - private String mTargetRemotePath; - - private boolean mOverwrite; + private final SessionTimeOut sessionTimeOut; /** @@ -50,13 +50,15 @@ public class MoveFileRemoteOperation extends RemoteOperation { * @param srcRemotePath Remote path of the file/folder to move. * @param targetRemotePath Remove path desired for the file/folder after moving it. */ - public MoveFileRemoteOperation( - String srcRemotePath, String targetRemotePath, boolean overwrite - ) { + public MoveFileRemoteOperation(String srcRemotePath, String targetRemotePath, boolean overwrite) { + this(srcRemotePath, targetRemotePath, overwrite, SessionTimeOutKt.getDefaultSessionTimeOut()); + } + public MoveFileRemoteOperation(String srcRemotePath, String targetRemotePath, boolean overwrite, SessionTimeOut sessionTimeOut) { mSrcRemotePath = srcRemotePath; mTargetRemotePath = targetRemotePath; mOverwrite = overwrite; + this.sessionTimeOut = sessionTimeOut; } @@ -70,11 +72,11 @@ protected RemoteOperationResult run(OwnCloudClient client) { // check parameters if (mTargetRemotePath.equals(mSrcRemotePath)) { // nothing to do! - return new RemoteOperationResult(ResultCode.OK); + return new RemoteOperationResult<>(ResultCode.OK); } if (mTargetRemotePath.startsWith(mSrcRemotePath)) { - return new RemoteOperationResult(ResultCode.INVALID_MOVE_INTO_DESCENDANT); + return new RemoteOperationResult<>(ResultCode.INVALID_MOVE_INTO_DESCENDANT); } @@ -87,7 +89,7 @@ protected RemoteOperationResult run(OwnCloudClient client) { client.getFilesDavUri(mTargetRemotePath), mOverwrite ); - int status = client.executeMethod(move, MOVE_READ_TIMEOUT, MOVE_CONNECTION_TIMEOUT); + int status = client.executeMethod(move, sessionTimeOut.getReadTimeOut(), sessionTimeOut.getConnectionTimeOut()); /// process response if (status == HttpStatus.SC_MULTI_STATUS) { @@ -103,7 +105,7 @@ protected RemoteOperationResult run(OwnCloudClient client) { /// http://www.webdav.org/specs/rfc4918.html#rfc.section.9.9.4 } else { - result = new RemoteOperationResult(isSuccess(status), move); + result = new RemoteOperationResult<>(isSuccess(status), move); client.exhaustResponse(move.getResponseBodyAsStream()); } @@ -111,7 +113,7 @@ protected RemoteOperationResult run(OwnCloudClient client) { result.getLogMessage()); } catch (Exception e) { - result = new RemoteOperationResult(e); + result = new RemoteOperationResult<>(e); Log.e(TAG, "Move " + mSrcRemotePath + " to " + mTargetRemotePath + ": " + result.getLogMessage(), e); @@ -160,18 +162,15 @@ private RemoteOperationResult processPartialError(MoveMethod move) RemoteOperationResult result; if (failFound) { - result = new RemoteOperationResult(ResultCode.PARTIAL_MOVE_DONE); + result = new RemoteOperationResult<>(ResultCode.PARTIAL_MOVE_DONE); } else { - result = new RemoteOperationResult(true, move); + result = new RemoteOperationResult<>(true, move); } return result; - } - protected boolean isSuccess(int status) { return status == HttpStatus.SC_CREATED || status == HttpStatus.SC_NO_CONTENT; } - } diff --git a/library/src/main/java/com/owncloud/android/lib/resources/files/ReadFileRemoteOperation.java b/library/src/main/java/com/owncloud/android/lib/resources/files/ReadFileRemoteOperation.java index bf350ac93..52a97a839 100644 --- a/library/src/main/java/com/owncloud/android/lib/resources/files/ReadFileRemoteOperation.java +++ b/library/src/main/java/com/owncloud/android/lib/resources/files/ReadFileRemoteOperation.java @@ -6,6 +6,8 @@ */ package com.owncloud.android.lib.resources.files; +import com.nextcloud.common.SessionTimeOut; +import com.nextcloud.common.SessionTimeOutKt; import com.owncloud.android.lib.common.OwnCloudClient; import com.owncloud.android.lib.common.network.WebdavEntry; import com.owncloud.android.lib.common.network.WebdavUtils; @@ -32,10 +34,8 @@ public class ReadFileRemoteOperation extends RemoteOperation { private static final String TAG = ReadFileRemoteOperation.class.getSimpleName(); - private static final int SYNC_READ_TIMEOUT = 40000; - private static final int SYNC_CONNECTION_TIMEOUT = 5000; - - private String mRemotePath; + private final String mRemotePath; + private final SessionTimeOut sessionTimeOut; /** @@ -44,7 +44,12 @@ public class ReadFileRemoteOperation extends RemoteOperation { * @param remotePath Remote path of the file. */ public ReadFileRemoteOperation(String remotePath) { + this(remotePath, SessionTimeOutKt.getDefaultSessionTimeOut()); + } + + public ReadFileRemoteOperation(String remotePath, SessionTimeOut sessionTimeOut) { mRemotePath = remotePath; + this.sessionTimeOut = sessionTimeOut; } /** @@ -64,7 +69,7 @@ protected RemoteOperationResult run(OwnCloudClient client) { WebdavUtils.getFilePropSet(), // PropFind Properties DavConstants.DEPTH_0); int status; - status = client.executeMethod(propfind, SYNC_READ_TIMEOUT, SYNC_CONNECTION_TIMEOUT); + status = client.executeMethod(propfind, sessionTimeOut.getReadTimeOut(), sessionTimeOut.getConnectionTimeOut()); boolean isSuccess = ( status == HttpStatus.SC_MULTI_STATUS || @@ -80,16 +85,16 @@ protected RemoteOperationResult run(OwnCloudClient client) { files.add(remoteFile); // Result of the operation - result = new RemoteOperationResult(true, propfind); + result = new RemoteOperationResult<>(true, propfind); result.setData(files); } else { - result = new RemoteOperationResult(false, propfind); + result = new RemoteOperationResult<>(false, propfind); client.exhaustResponse(propfind.getResponseBodyAsStream()); } } catch (Exception e) { - result = new RemoteOperationResult(e); + result = new RemoteOperationResult<>(e); Log_OC.e(TAG, "Read file " + mRemotePath + " failed: " + result.getLogMessage(), result.getException()); } finally { @@ -98,5 +103,4 @@ protected RemoteOperationResult run(OwnCloudClient client) { } return result; } - } diff --git a/library/src/main/java/com/owncloud/android/lib/resources/files/RemoveFileRemoteOperation.java b/library/src/main/java/com/owncloud/android/lib/resources/files/RemoveFileRemoteOperation.java index 42aaab3e7..0c856ddbf 100644 --- a/library/src/main/java/com/owncloud/android/lib/resources/files/RemoveFileRemoteOperation.java +++ b/library/src/main/java/com/owncloud/android/lib/resources/files/RemoveFileRemoteOperation.java @@ -6,6 +6,8 @@ */ package com.owncloud.android.lib.resources.files; +import com.nextcloud.common.SessionTimeOut; +import com.nextcloud.common.SessionTimeOutKt; import com.owncloud.android.lib.common.OwnCloudClient; import com.owncloud.android.lib.common.operations.RemoteOperation; import com.owncloud.android.lib.common.operations.RemoteOperationResult; @@ -23,10 +25,8 @@ public class RemoveFileRemoteOperation extends RemoteOperation { private static final String TAG = RemoveFileRemoteOperation.class.getSimpleName(); - private static final int REMOVE_READ_TIMEOUT = 30000; - private static final int REMOVE_CONNECTION_TIMEOUT = 5000; - - private String mRemotePath; + private final String mRemotePath; + private final SessionTimeOut sessionTimeOut; /** * Constructor @@ -34,7 +34,12 @@ public class RemoveFileRemoteOperation extends RemoteOperation { * @param remotePath RemotePath of the remote file or folder to remove from the server */ public RemoveFileRemoteOperation(String remotePath) { + this(remotePath, SessionTimeOutKt.getDefaultSessionTimeOut()); + } + + public RemoveFileRemoteOperation(String remotePath, SessionTimeOut sessionTimeOut) { mRemotePath = remotePath; + this.sessionTimeOut = sessionTimeOut; } /** @@ -49,17 +54,17 @@ protected RemoteOperationResult run(OwnCloudClient client) { try { delete = new DeleteMethod(client.getFilesDavUri(mRemotePath)); - int status = client.executeMethod(delete, REMOVE_READ_TIMEOUT, REMOVE_CONNECTION_TIMEOUT); + int status = client.executeMethod(delete, sessionTimeOut.getReadTimeOut(), sessionTimeOut.getConnectionTimeOut()); delete.getResponseBodyAsString(); // exhaust the response, although not interesting - result = new RemoteOperationResult( + result = new RemoteOperationResult<>( (delete.succeeded() || status == HttpStatus.SC_NOT_FOUND), delete ); Log_OC.i(TAG, "Remove " + mRemotePath + ": " + result.getLogMessage()); } catch (Exception e) { - result = new RemoteOperationResult(e); + result = new RemoteOperationResult<>(e); Log_OC.e(TAG, "Remove " + mRemotePath + ": " + result.getLogMessage(), e); } finally { @@ -69,5 +74,4 @@ protected RemoteOperationResult run(OwnCloudClient client) { return result; } - } diff --git a/library/src/main/java/com/owncloud/android/lib/resources/files/RenameFileRemoteOperation.java b/library/src/main/java/com/owncloud/android/lib/resources/files/RenameFileRemoteOperation.java index ec3300f0e..a2a85a010 100644 --- a/library/src/main/java/com/owncloud/android/lib/resources/files/RenameFileRemoteOperation.java +++ b/library/src/main/java/com/owncloud/android/lib/resources/files/RenameFileRemoteOperation.java @@ -6,6 +6,8 @@ */ package com.owncloud.android.lib.resources.files; +import com.nextcloud.common.SessionTimeOut; +import com.nextcloud.common.SessionTimeOutKt; import com.owncloud.android.lib.common.OwnCloudClient; import com.owncloud.android.lib.common.operations.RemoteOperation; import com.owncloud.android.lib.common.operations.RemoteOperationResult; @@ -27,14 +29,13 @@ public class RenameFileRemoteOperation extends RemoteOperation { private static final String TAG = RenameFileRemoteOperation.class.getSimpleName(); - private static final int RENAME_READ_TIMEOUT = 600000; - private static final int RENAME_CONNECTION_TIMEOUT = 5000; - private String mOldName; private String mOldRemotePath; private String mNewName; private String mNewRemotePath; + private final SessionTimeOut sessionTimeOut; + /** * Constructor @@ -44,19 +45,23 @@ public class RenameFileRemoteOperation extends RemoteOperation { * @param newName New name to set as the name of file. * @param isFolder 'true' for folder and 'false' for files */ - public RenameFileRemoteOperation(String oldName, String oldRemotePath, String newName, - boolean isFolder) { + public RenameFileRemoteOperation(String oldName, String oldRemotePath, String newName, boolean isFolder) { + this(oldName, oldRemotePath, newName, isFolder, SessionTimeOutKt.getDefaultSessionTimeOut()); + } + + public RenameFileRemoteOperation(String oldName, String oldRemotePath, String newName, boolean isFolder, SessionTimeOut sessionTimeOut) { mOldName = oldName; mOldRemotePath = oldRemotePath; mNewName = newName; String parent = (new File(mOldRemotePath)).getParent(); - parent = (parent.endsWith(FileUtils.PATH_SEPARATOR)) ? parent : parent + - FileUtils.PATH_SEPARATOR; + parent = (parent.endsWith(FileUtils.PATH_SEPARATOR)) ? parent : parent + FileUtils.PATH_SEPARATOR; mNewRemotePath = parent + mNewName; if (isFolder) { mNewRemotePath += FileUtils.PATH_SEPARATOR; } + + this.sessionTimeOut = sessionTimeOut; } /** @@ -71,27 +76,27 @@ protected RemoteOperationResult run(OwnCloudClient client) { MoveMethod move = null; try { if (mNewName.equals(mOldName)) { - return new RemoteOperationResult(ResultCode.OK); + return new RemoteOperationResult<>(ResultCode.OK); } // check if a file with the new name already exists - RemoteOperationResult existenceResult = new ExistenceCheckRemoteOperation(mNewRemotePath, false) + final var existenceResult = new ExistenceCheckRemoteOperation(mNewRemotePath, false) .execute(client); if (existenceResult.isSuccess()) { - return new RemoteOperationResult(ResultCode.INVALID_OVERWRITE); + return new RemoteOperationResult<>(ResultCode.INVALID_OVERWRITE); } move = new MoveMethod(client.getFilesDavUri(mOldRemotePath), client.getFilesDavUri(mNewRemotePath), true); - client.executeMethod(move, RENAME_READ_TIMEOUT, RENAME_CONNECTION_TIMEOUT); - result = new RemoteOperationResult(move.succeeded(), move); + client.executeMethod(move, sessionTimeOut.getReadTimeOut(), sessionTimeOut.getConnectionTimeOut()); + result = new RemoteOperationResult<>(move.succeeded(), move); Log_OC.i(TAG, "Rename " + mOldRemotePath + " to " + mNewRemotePath + ": " + result.getLogMessage() ); client.exhaustResponse(move.getResponseBodyAsStream()); } catch (Exception e) { - result = new RemoteOperationResult(e); + result = new RemoteOperationResult<>(e); Log_OC.e(TAG, "Rename " + mOldRemotePath + " to " + ((mNewRemotePath == null) ? mNewName : mNewRemotePath) + ": " + result.getLogMessage(), e); diff --git a/library/src/main/java/com/owncloud/android/lib/resources/files/RestoreFileVersionRemoteOperation.java b/library/src/main/java/com/owncloud/android/lib/resources/files/RestoreFileVersionRemoteOperation.java index 1c283b956..c4f63ccff 100644 --- a/library/src/main/java/com/owncloud/android/lib/resources/files/RestoreFileVersionRemoteOperation.java +++ b/library/src/main/java/com/owncloud/android/lib/resources/files/RestoreFileVersionRemoteOperation.java @@ -10,6 +10,8 @@ import android.net.Uri; import android.util.Log; +import com.nextcloud.common.SessionTimeOut; +import com.nextcloud.common.SessionTimeOutKt; import com.owncloud.android.lib.common.OwnCloudClient; import com.owncloud.android.lib.common.operations.RemoteOperation; import com.owncloud.android.lib.common.operations.RemoteOperationResult; @@ -27,11 +29,10 @@ public class RestoreFileVersionRemoteOperation extends RemoteOperation { private static final String TAG = RestoreFileVersionRemoteOperation.class.getSimpleName(); - private static final int RESTORE_READ_TIMEOUT = 30000; - private static final int RESTORE_CONNECTION_TIMEOUT = 5000; private final long fileId; private final String fileName; + private final SessionTimeOut sessionTimeOut; /** * Constructor @@ -40,8 +41,13 @@ public class RestoreFileVersionRemoteOperation extends RemoteOperation { * @param fileName version date in unixtime */ public RestoreFileVersionRemoteOperation(long fileId, String fileName) { + this(fileId, fileName, SessionTimeOutKt.getDefaultSessionTimeOut()); + } + + public RestoreFileVersionRemoteOperation(long fileId, String fileName, SessionTimeOut sessionTimeOut) { this.fileId = fileId; this.fileName = fileName; + this.sessionTimeOut = sessionTimeOut; } /** @@ -60,13 +66,13 @@ protected RemoteOperationResult run(OwnCloudClient client) { String target = client.getDavUri() + "/versions/" + client.getUserId() + "/restore/" + fileId; move = new MoveMethod(source, target, true); - int status = client.executeMethod(move, RESTORE_READ_TIMEOUT, RESTORE_CONNECTION_TIMEOUT); + int status = client.executeMethod(move, sessionTimeOut.getReadTimeOut(), sessionTimeOut.getConnectionTimeOut()); - result = new RemoteOperationResult(isSuccess(status), move); + result = new RemoteOperationResult<>(isSuccess(status), move); client.exhaustResponse(move.getResponseBodyAsStream()); } catch (IOException e) { - result = new RemoteOperationResult(e); + result = new RemoteOperationResult<>(e); Log.e(TAG, "Restore file version with id " + fileId + " failed: " + result.getLogMessage(), e); } finally { if (move != null) { diff --git a/library/src/main/java/com/owncloud/android/lib/resources/trashbin/RemoveTrashbinFileRemoteOperation.java b/library/src/main/java/com/owncloud/android/lib/resources/trashbin/RemoveTrashbinFileRemoteOperation.java index d6b325998..2417e1ea9 100644 --- a/library/src/main/java/com/owncloud/android/lib/resources/trashbin/RemoveTrashbinFileRemoteOperation.java +++ b/library/src/main/java/com/owncloud/android/lib/resources/trashbin/RemoveTrashbinFileRemoteOperation.java @@ -7,6 +7,8 @@ */ package com.owncloud.android.lib.resources.trashbin; +import com.nextcloud.common.SessionTimeOut; +import com.nextcloud.common.SessionTimeOutKt; import com.owncloud.android.lib.common.OwnCloudClient; import com.owncloud.android.lib.common.network.WebdavUtils; import com.owncloud.android.lib.common.operations.RemoteOperation; @@ -22,10 +24,8 @@ public class RemoveTrashbinFileRemoteOperation extends RemoteOperation { private static final String TAG = RemoveTrashbinFileRemoteOperation.class.getSimpleName(); - private static final int REMOVE_READ_TIMEOUT = 30000; - private static final int REMOVE_CONNECTION_TIMEOUT = 5000; - - private String remotePath; + private final String remotePath; + private final SessionTimeOut sessionTimeOut; /** * Constructor @@ -33,7 +33,12 @@ public class RemoveTrashbinFileRemoteOperation extends RemoteOperation { * @param remotePath RemotePath of the remote file or folder to remove from the server */ public RemoveTrashbinFileRemoteOperation(String remotePath) { + this(remotePath, SessionTimeOutKt.getDefaultSessionTimeOut()); + } + + public RemoveTrashbinFileRemoteOperation(String remotePath, SessionTimeOut sessionTimeOut) { this.remotePath = remotePath; + this.sessionTimeOut = sessionTimeOut; } /** @@ -48,14 +53,14 @@ protected RemoteOperationResult run(OwnCloudClient client) { try { delete = new DeleteMethod(client.getDavUri() + WebdavUtils.encodePath(remotePath)); - int status = client.executeMethod(delete, REMOVE_READ_TIMEOUT, REMOVE_CONNECTION_TIMEOUT); + int status = client.executeMethod(delete, sessionTimeOut.getReadTimeOut(), sessionTimeOut.getConnectionTimeOut()); delete.getResponseBodyAsString(); // exhaust the response, although not interesting - result = new RemoteOperationResult((delete.succeeded() || status == HttpStatus.SC_NOT_FOUND), delete); + result = new RemoteOperationResult<>((delete.succeeded() || status == HttpStatus.SC_NOT_FOUND), delete); Log_OC.i(TAG, "Remove " + remotePath + ": " + result.getLogMessage()); } catch (Exception e) { - result = new RemoteOperationResult(e); + result = new RemoteOperationResult<>(e); Log_OC.e(TAG, "Remove " + remotePath + ": " + result.getLogMessage(), e); } finally { diff --git a/library/src/main/java/com/owncloud/android/lib/resources/trashbin/RestoreTrashbinFileRemoteOperation.java b/library/src/main/java/com/owncloud/android/lib/resources/trashbin/RestoreTrashbinFileRemoteOperation.java index a1455f5a6..331ea4e2c 100644 --- a/library/src/main/java/com/owncloud/android/lib/resources/trashbin/RestoreTrashbinFileRemoteOperation.java +++ b/library/src/main/java/com/owncloud/android/lib/resources/trashbin/RestoreTrashbinFileRemoteOperation.java @@ -10,6 +10,8 @@ import android.net.Uri; import android.util.Log; +import com.nextcloud.common.SessionTimeOut; +import com.nextcloud.common.SessionTimeOutKt; import com.owncloud.android.lib.common.OwnCloudClient; import com.owncloud.android.lib.common.network.WebdavUtils; import com.owncloud.android.lib.common.operations.RemoteOperation; @@ -27,11 +29,10 @@ public class RestoreTrashbinFileRemoteOperation extends RemoteOperation { private static final String TAG = RestoreTrashbinFileRemoteOperation.class.getSimpleName(); - private static final int RESTORE_READ_TIMEOUT = 30000; - private static final int RESTORE_CONNECTION_TIMEOUT = 5000; - private String sourcePath; - private String fileName; + private final String sourcePath; + private final String fileName; + private final SessionTimeOut sessionTimeOut; /** * Constructor @@ -40,8 +41,13 @@ public class RestoreTrashbinFileRemoteOperation extends RemoteOperation { * @param fileName original filename */ public RestoreTrashbinFileRemoteOperation(String sourcePath, String fileName) { + this(sourcePath, fileName, SessionTimeOutKt.getDefaultSessionTimeOut()); + } + + public RestoreTrashbinFileRemoteOperation(String sourcePath, String fileName, SessionTimeOut sessionTimeOut) { this.sourcePath = sourcePath; this.fileName = fileName; + this.sessionTimeOut = sessionTimeOut; } /** @@ -60,13 +66,13 @@ protected RemoteOperationResult run(OwnCloudClient client) { Uri.encode(fileName); move = new MoveMethod(source, target, true); - int status = client.executeMethod(move, RESTORE_READ_TIMEOUT, RESTORE_CONNECTION_TIMEOUT); + int status = client.executeMethod(move, sessionTimeOut.getReadTimeOut(), sessionTimeOut.getConnectionTimeOut()); - result = new RemoteOperationResult(isSuccess(status), move); + result = new RemoteOperationResult<>(isSuccess(status), move); client.exhaustResponse(move.getResponseBodyAsStream()); } catch (IOException e) { - result = new RemoteOperationResult(e); + result = new RemoteOperationResult<>(e); Log.e(TAG, "Restore trashbin file " + sourcePath + " failed: " + result.getLogMessage(), e); } finally { if (move != null) { diff --git a/library/src/main/java/com/owncloud/android/lib/resources/users/CheckRemoteWipeRemoteOperation.java b/library/src/main/java/com/owncloud/android/lib/resources/users/CheckRemoteWipeRemoteOperation.java index 3a1e4ae55..527a127cb 100644 --- a/library/src/main/java/com/owncloud/android/lib/resources/users/CheckRemoteWipeRemoteOperation.java +++ b/library/src/main/java/com/owncloud/android/lib/resources/users/CheckRemoteWipeRemoteOperation.java @@ -7,6 +7,8 @@ */ package com.owncloud.android.lib.resources.users; +import com.nextcloud.common.SessionTimeOut; +import com.nextcloud.common.SessionTimeOutKt; import com.owncloud.android.lib.common.OwnCloudClient; import com.owncloud.android.lib.common.operations.RemoteOperation; import com.owncloud.android.lib.common.operations.RemoteOperationResult; @@ -24,12 +26,19 @@ public class CheckRemoteWipeRemoteOperation extends RemoteOperation { private static final String TAG = CheckRemoteWipeRemoteOperation.class.getSimpleName(); - private static final int SYNC_READ_TIMEOUT = 40000; - private static final int SYNC_CONNECTION_TIMEOUT = 5000; private static final String REMOTE_WIPE_URL = "/index.php/core/wipe/check"; // JSON node names private static final String WIPE = "wipe"; + private final SessionTimeOut sessionTimeOut; + + public CheckRemoteWipeRemoteOperation() { + this(SessionTimeOutKt.getDefaultSessionTimeOut()); + } + + public CheckRemoteWipeRemoteOperation(SessionTimeOut sessionTimeOut) { + this.sessionTimeOut = sessionTimeOut; + } /** * @param client Client object @@ -44,7 +53,7 @@ protected RemoteOperationResult run(OwnCloudClient client) { postMethod.addRequestHeader(CONTENT_TYPE, FORM_URLENCODED); postMethod.setParameter(REMOTE_WIPE_TOKEN, client.getCredentials().getAuthToken()); - int status = client.executeMethod(postMethod, SYNC_READ_TIMEOUT, SYNC_CONNECTION_TIMEOUT); + int status = client.executeMethod(postMethod, sessionTimeOut.getReadTimeOut(), sessionTimeOut.getConnectionTimeOut()); if (HttpStatus.SC_OK == status) { String response = postMethod.getResponseBodyAsString(); @@ -52,17 +61,17 @@ protected RemoteOperationResult run(OwnCloudClient client) { JSONObject json = new JSONObject(response); if (json.getBoolean(WIPE)) { - result = new RemoteOperationResult(true, postMethod); + result = new RemoteOperationResult<>(true, postMethod); } else { - result = new RemoteOperationResult(false, postMethod); + result = new RemoteOperationResult<>(false, postMethod); } } else { - result = new RemoteOperationResult(false, postMethod); + result = new RemoteOperationResult<>(false, postMethod); } client.exhaustResponse(postMethod.getResponseBodyAsStream()); } catch (Exception e) { - result = new RemoteOperationResult(e); + result = new RemoteOperationResult<>(e); Log_OC.e(TAG, "Getting remote wipe status failed: " + result.getLogMessage(), result.getException()); diff --git a/library/src/main/java/com/owncloud/android/lib/resources/users/RemoteWipeSuccessRemoteOperation.java b/library/src/main/java/com/owncloud/android/lib/resources/users/RemoteWipeSuccessRemoteOperation.java index 6a0a56c13..a7dd57675 100644 --- a/library/src/main/java/com/owncloud/android/lib/resources/users/RemoteWipeSuccessRemoteOperation.java +++ b/library/src/main/java/com/owncloud/android/lib/resources/users/RemoteWipeSuccessRemoteOperation.java @@ -7,6 +7,8 @@ */ package com.owncloud.android.lib.resources.users; +import com.nextcloud.common.SessionTimeOut; +import com.nextcloud.common.SessionTimeOutKt; import com.owncloud.android.lib.common.OwnCloudClient; import com.owncloud.android.lib.common.operations.RemoteOperation; import com.owncloud.android.lib.common.operations.RemoteOperationResult; @@ -23,14 +25,18 @@ public class RemoteWipeSuccessRemoteOperation extends RemoteOperation { private static final String TAG = RemoteWipeSuccessRemoteOperation.class.getSimpleName(); - private static final int SYNC_READ_TIMEOUT = 40000; - private static final int SYNC_CONNECTION_TIMEOUT = 5000; private static final String REMOTE_WIPE_URL = "/index.php/core/wipe/success"; - private String appToken; + private final String appToken; + private final SessionTimeOut sessionTimeOut; public RemoteWipeSuccessRemoteOperation(String appToken) { + this(appToken, SessionTimeOutKt.getDefaultSessionTimeOut()); + } + + public RemoteWipeSuccessRemoteOperation(String appToken, SessionTimeOut sessionTimeOut) { this.appToken = appToken; + this.sessionTimeOut = sessionTimeOut; } /** @@ -46,17 +52,17 @@ protected RemoteOperationResult run(OwnCloudClient client) { postMethod.addRequestHeader(CONTENT_TYPE, FORM_URLENCODED); postMethod.setParameter(REMOTE_WIPE_TOKEN, appToken); - int status = client.executeMethod(postMethod, SYNC_READ_TIMEOUT, SYNC_CONNECTION_TIMEOUT); + int status = client.executeMethod(postMethod, sessionTimeOut.getReadTimeOut(), sessionTimeOut.getConnectionTimeOut()); client.exhaustResponse(postMethod.getResponseBodyAsStream()); if (HttpStatus.SC_OK == status) { - result = new RemoteOperationResult(RemoteOperationResult.ResultCode.OK); + result = new RemoteOperationResult<>(RemoteOperationResult.ResultCode.OK); } else { - result = new RemoteOperationResult( + result = new RemoteOperationResult<>( RemoteOperationResult.ResultCode.valueOf(String.valueOf(status))); } } catch (Exception e) { - result = new RemoteOperationResult(e); + result = new RemoteOperationResult<>(e); Log_OC.e(TAG, "Setting status of remote wipe status failed: " + result.getLogMessage(), result.getException());