Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
Signed-off-by: tobiasKaminsky <[email protected]>
  • Loading branch information
tobiasKaminsky committed Jun 17, 2024
1 parent faa485d commit d021d74
Show file tree
Hide file tree
Showing 16 changed files with 604 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class DashboardGetWidgetItemsRemoteOperationIT : AbstractIT() {
testOnlyOnServer(NextcloudVersion.nextcloud_25)

// create folder to have some content
assertTrue(CreateFolderRemoteOperation("/testFolder", false).execute(nextcloudClient2).isSuccess)
assertTrue(CreateFolderRemoteOperation("/testFolder", false).execute(client2).isSuccess)
assertTrue(
CreateShareRemoteOperation(
"/testFolder",
Expand All @@ -32,7 +32,7 @@ class DashboardGetWidgetItemsRemoteOperationIT : AbstractIT() {
false,
"",
OCShare.MAXIMUM_PERMISSIONS_FOR_FOLDER
).execute(nextcloudClient2)
).execute(client2)
.isSuccess
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ private static void testConnection() throws KeyStoreException,
InterruptedException {
GetStatusRemoteOperation getStatus = new GetStatusRemoteOperation(context);

RemoteOperationResult result = getStatus.execute(nextcloudClient);
RemoteOperationResult result = getStatus.execute(client);

if (result.isSuccess()) {
Log_OC.d("AbstractIT", "Connection to server successful");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@
*/
package com.owncloud.android.lib.resources.e2ee;

import com.nextcloud.common.NextcloudClient;
import com.nextcloud.operations.GetMethod;
import com.owncloud.android.lib.common.OwnCloudClient;
import com.owncloud.android.lib.common.operations.RemoteOperation;
import com.owncloud.android.lib.common.operations.RemoteOperationResult;
import com.owncloud.android.lib.common.utils.Log_OC;

import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.json.JSONObject;


Expand All @@ -24,6 +25,8 @@
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 @@ -46,29 +49,32 @@ public GetMetadataRemoteOperation(long fileId) {
* @param client Client object
*/
@Override
public RemoteOperationResult<MetadataResponse> run(NextcloudClient client) {
com.nextcloud.operations.GetMethod getMethod = null;
protected RemoteOperationResult<MetadataResponse> run(OwnCloudClient client) {
GetMethod getMethod = null;
RemoteOperationResult<MetadataResponse> result;

try {
// remote request
getMethod = new com.nextcloud.operations.GetMethod(client.getBaseUri() + METADATA_V2_URL + fileId + JSON_FORMAT, true);
getMethod = new GetMethod(client.getBaseUri() + METADATA_V2_URL + fileId + JSON_FORMAT);
getMethod.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE);

int status = client.execute(getMethod);
int status = client.executeMethod(getMethod, SYNC_READ_TIMEOUT, SYNC_CONNECTION_TIMEOUT);

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, true);
getMethod = new GetMethod(client.getBaseUri() + METADATA_V1_URL + fileId + JSON_FORMAT);
getMethod.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE);

status = client.execute(getMethod);
status = client.executeMethod(getMethod, SYNC_READ_TIMEOUT, SYNC_CONNECTION_TIMEOUT);
}

if (status == HttpStatus.SC_OK) {
String response = getMethod.getResponseBodyAsString();
String signature = getMethod.getResponseHeader(HEADER_SIGNATURE);
Header signatureHeader = getMethod.getResponseHeader(HEADER_SIGNATURE);

if (signature == null) {
signature = "";
String signature = "";
if (signatureHeader != null) {
signature = signatureHeader.getValue();
}

// Parse the response
Expand All @@ -84,6 +90,7 @@ public RemoteOperationResult<MetadataResponse> run(NextcloudClient client) {
result.setResultData(metadataResponse);
} else {
result = new RemoteOperationResult<>(false, getMethod);
client.exhaustResponse(getMethod.getResponseBodyAsStream());
}
} catch (Exception e) {
result = new RemoteOperationResult<>(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,24 @@
*/
package com.owncloud.android.lib.resources.e2ee;

import com.nextcloud.common.NextcloudClient;
import com.nextcloud.operations.PostMethod;
import com.owncloud.android.lib.common.OwnCloudClient;
import com.owncloud.android.lib.common.operations.RemoteOperation;
import com.owncloud.android.lib.common.operations.RemoteOperationResult;
import com.owncloud.android.lib.common.utils.Log_OC;

import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.Utf8PostMethod;
import org.json.JSONObject;

import okhttp3.RequestBody;


/**
* Lock a file
*/
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";
Expand Down Expand Up @@ -52,22 +52,22 @@ public LockFileRemoteOperation(long localId) {
* @param client Client object
*/
@Override
public RemoteOperationResult<String> run(NextcloudClient client) {
protected RemoteOperationResult<String> run(OwnCloudClient client) {
RemoteOperationResult<String> result;
PostMethod postMethod = null;
Utf8PostMethod postMethod = null;

try {
RequestBody requestBody = RequestBody.create(new byte[] {});

postMethod = new PostMethod(client.getBaseUri() + LOCK_FILE_URL + localId + JSON_FORMAT, true, requestBody);
postMethod = new Utf8PostMethod(client.getBaseUri() + LOCK_FILE_URL + localId + JSON_FORMAT);

// remote request
postMethod.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE);
postMethod.addRequestHeader(CONTENT_TYPE, FORM_URLENCODED);

if (counter > 0) {
postMethod.addRequestHeader(COUNTER_HEADER, String.valueOf(counter));
}

int status = client.execute(postMethod);
int status = client.executeMethod(postMethod, SYNC_READ_TIMEOUT, SYNC_CONNECTION_TIMEOUT);

if (status == HttpStatus.SC_OK) {
String response = postMethod.getResponseBodyAsString();
Expand All @@ -83,6 +83,7 @@ public RemoteOperationResult<String> run(NextcloudClient client) {
result.setResultData(token);
} else {
result = new RemoteOperationResult<>(false, postMethod);
client.exhaustResponse(postMethod.getResponseBodyAsStream());
}
} catch (Exception e) {
result = new RemoteOperationResult<>(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
*/
package com.owncloud.android.lib.resources.e2ee;

import com.nextcloud.common.NextcloudClient;
import com.nextcloud.operations.DeleteMethod;
import com.owncloud.android.lib.common.OwnCloudClient;
import com.owncloud.android.lib.common.operations.RemoteOperation;
import com.owncloud.android.lib.common.operations.RemoteOperationResult;
import com.owncloud.android.lib.common.utils.Log_OC;

import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.DeleteMethod;


/**
Expand All @@ -22,6 +22,8 @@
public class UnlockFileRemoteOperation extends RemoteOperation<Void> {

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;
Expand All @@ -39,19 +41,22 @@ public UnlockFileRemoteOperation(long localId, String token) {
* @param client Client object
*/
@Override
public RemoteOperationResult<Void> run(NextcloudClient client) {
protected RemoteOperationResult<Void> run(OwnCloudClient client) {
RemoteOperationResult<Void> result;
com.nextcloud.operations.DeleteMethod deleteMethod = null;
DeleteMethod deleteMethod = null;

try {
// remote request
deleteMethod = new DeleteMethod(client.getBaseUri() + LOCK_FILE_URL + localId, true);
deleteMethod = new DeleteMethod(client.getBaseUri() + LOCK_FILE_URL + localId);
deleteMethod.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE);
deleteMethod.addRequestHeader(CONTENT_TYPE, FORM_URLENCODED);
deleteMethod.addRequestHeader(E2E_TOKEN, token);

int status = client.execute(deleteMethod);
int status = client.executeMethod(deleteMethod, SYNC_READ_TIMEOUT, SYNC_CONNECTION_TIMEOUT);

result = new RemoteOperationResult<>(status == HttpStatus.SC_OK, deleteMethod);

client.exhaustResponse(deleteMethod.getResponseBodyAsStream());
} catch (Exception e) {
result = new RemoteOperationResult<>(e);
Log_OC.e(TAG, "Unlock file with id " + localId + " failed: " + result.getLogMessage(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,32 @@

import android.content.Context;

import com.nextcloud.common.NextcloudClient;
import com.nextcloud.operations.HeadMethod;
import com.owncloud.android.lib.common.OwnCloudAnonymousCredentials;
import com.owncloud.android.lib.common.OwnCloudClient;
import com.owncloud.android.lib.common.network.RedirectionPath;
import com.owncloud.android.lib.common.operations.RemoteOperation;
import com.owncloud.android.lib.common.operations.RemoteOperationResult;
import com.owncloud.android.lib.common.utils.Log_OC;

import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.HeadMethod;

/**
* Operation to check the existence or absence of a path in a remote server.
*
* @author David A. Velasco
*/
public class ExistenceCheckRemoteOperation extends RemoteOperation<Void> {
private static final String TAG = ExistenceCheckRemoteOperation.class.getSimpleName();
public class ExistenceCheckRemoteOperation extends RemoteOperation {

/**
* Maximum time to wait for a response from the server in MILLISECONDs.
*/
public static final int TIMEOUT = 50000;

private final String mPath;
private final boolean mSuccessIfAbsent;
private static final String TAG = ExistenceCheckRemoteOperation.class.getSimpleName();

private String mPath;
private boolean mSuccessIfAbsent;

/** Sequence of redirections followed. Available only after executing the operation */
private RedirectionPath mRedirectionPath = null;
Expand Down Expand Up @@ -58,21 +65,26 @@ public ExistenceCheckRemoteOperation(String remotePath, Context context, boolean
}

@Override
public RemoteOperationResult<Void> run(NextcloudClient client) {
RemoteOperationResult<Void> result;
com.nextcloud.operations.HeadMethod head = null;
boolean previousFollowRedirects = client.getFollowRedirects();
protected RemoteOperationResult run(OwnCloudClient client) {
RemoteOperationResult result = null;
HeadMethod head = null;
boolean previousFollowRedirects = client.isFollowRedirects();
try {
head = new HeadMethod(client.getFilesDavUri(mPath), false);
if (client.getCredentials() instanceof OwnCloudAnonymousCredentials) {
head = new HeadMethod(client.getDavUri().toString());
} else {
head = new HeadMethod(client.getFilesDavUri(mPath));
}
client.setFollowRedirects(false);
int status = client.execute(head);
int status = client.executeMethod(head, TIMEOUT, TIMEOUT);
if (previousFollowRedirects) {
mRedirectionPath = client.followRedirection(head);
status = mRedirectionPath.getLastStatus();
}
client.exhaustResponse(head.getResponseBodyAsStream());
boolean success = (status == HttpStatus.SC_OK && !mSuccessIfAbsent) ||
(status == HttpStatus.SC_NOT_FOUND && mSuccessIfAbsent);
result = new RemoteOperationResult<>(
result = new RemoteOperationResult(
success,
status,
head.getStatusText(),
Expand All @@ -83,7 +95,7 @@ public RemoteOperationResult<Void> run(NextcloudClient client) {
"finished with HTTP status " + status + (!success ? "(FAIL)" : ""));

} catch (Exception e) {
result = new RemoteOperationResult<>(e);
result = new RemoteOperationResult(e);
Log_OC.e(TAG, "Existence check for " + client.getFilesDavUri(mPath) + " targeting for " +
(mSuccessIfAbsent ? " absence " : " existence ") + ": " +
result.getLogMessage(), result.getException());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
*/
package com.owncloud.android.lib.resources.files;

import com.nextcloud.common.NextcloudClient;
import com.owncloud.android.lib.common.OwnCloudClient;
import com.owncloud.android.lib.common.operations.RemoteOperation;
import com.owncloud.android.lib.common.operations.RemoteOperationResult;
Expand Down Expand Up @@ -76,10 +75,8 @@ protected RemoteOperationResult run(OwnCloudClient client) {
}

// check if a file with the new name already exists
NextcloudClient nextcloudClient = client.toNextcloudClient();

RemoteOperationResult existenceResult = new ExistenceCheckRemoteOperation(mNewRemotePath, false)
.execute(nextcloudClient);
.execute(client);
if (existenceResult.isSuccess()) {
return new RemoteOperationResult(ResultCode.INVALID_OVERWRITE);
}
Expand Down
Loading

0 comments on commit d021d74

Please sign in to comment.