Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inject Session Time Out To The Remote Operation Constructors #1597

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -23,15 +26,23 @@

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
private static final String NODE_OCS = "ocs";
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;
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand All @@ -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();
Expand All @@ -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 {
Expand Down
13 changes: 13 additions & 0 deletions library/src/main/java/com/nextcloud/common/SessionTimeOut.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
* Nextcloud Android Library
*
* SPDX-FileCopyrightText: 2024 Alper Ozturk <alper.ozturk@nextcloud.com>
* 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)
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -25,8 +27,6 @@
public class GetMetadataRemoteOperation extends RemoteOperation<MetadataResponse> {

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/";

Expand All @@ -38,11 +38,18 @@ public class GetMetadataRemoteOperation extends RemoteOperation<MetadataResponse

private final long fileId;

private final SessionTimeOut sessionTimeOut;

/**
* Constructor
*/
public GetMetadataRemoteOperation(long fileId) {
this(fileId, SessionTimeOutKt.getDefaultSessionTimeOut());
}

public GetMetadataRemoteOperation(long fileId, SessionTimeOut sessionTimeOut) {
this.fileId = fileId;
this.sessionTimeOut = sessionTimeOut;
}

/**
Expand All @@ -58,14 +65,14 @@ protected RemoteOperationResult<MetadataResponse> 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) {
Expand Down Expand Up @@ -102,5 +109,4 @@ protected RemoteOperationResult<MetadataResponse> run(OwnCloudClient client) {
}
return result;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -23,29 +25,40 @@
public class LockFileRemoteOperation extends RemoteOperation<String> {

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;
}

/**
Expand All @@ -67,7 +80,7 @@ protected RemoteOperationResult<String> 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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";

Expand All @@ -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;
}

/**
Expand All @@ -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();
Expand All @@ -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<Object> 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 {
Expand Down
Loading
Loading