Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
Signed-off-by: ZetaTom <[email protected]>
  • Loading branch information
ZetaTom authored and tobiasKaminsky committed Jun 10, 2024
1 parent 434642f commit 2601614
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@
*/
package com.owncloud.android.lib.resources.e2ee;

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

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

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

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

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

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

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

// Parse the response
Expand All @@ -90,7 +84,6 @@ protected RemoteOperationResult<MetadataResponse> run(OwnCloudClient 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.owncloud.android.lib.common.OwnCloudClient;
import com.nextcloud.common.NextcloudClient;
import com.nextcloud.operations.PostMethod;
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
protected RemoteOperationResult<String> run(OwnCloudClient client) {
public RemoteOperationResult<String> run(NextcloudClient client) {
RemoteOperationResult<String> result;
Utf8PostMethod postMethod = null;
PostMethod postMethod = null;

try {
postMethod = new Utf8PostMethod(client.getBaseUri() + LOCK_FILE_URL + localId + JSON_FORMAT);
RequestBody requestBody = RequestBody.create(new byte[] {});

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

// 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.executeMethod(postMethod, SYNC_READ_TIMEOUT, SYNC_CONNECTION_TIMEOUT);
int status = client.execute(postMethod);

if (status == HttpStatus.SC_OK) {
String response = postMethod.getResponseBodyAsString();
Expand All @@ -83,7 +83,6 @@ protected RemoteOperationResult<String> run(OwnCloudClient 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.owncloud.android.lib.common.OwnCloudClient;
import com.nextcloud.common.NextcloudClient;
import com.nextcloud.operations.DeleteMethod;
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,8 +22,6 @@
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 @@ -41,22 +39,19 @@ public UnlockFileRemoteOperation(long localId, String token) {
* @param client Client object
*/
@Override
protected RemoteOperationResult<Void> run(OwnCloudClient client) {
public RemoteOperationResult<Void> run(NextcloudClient client) {
RemoteOperationResult<Void> result;
DeleteMethod deleteMethod = null;
com.nextcloud.operations.DeleteMethod deleteMethod = null;

try {
// remote request
deleteMethod = new DeleteMethod(client.getBaseUri() + LOCK_FILE_URL + localId);
deleteMethod.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE);
deleteMethod = new DeleteMethod(client.getBaseUri() + LOCK_FILE_URL + localId, true);
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.execute(deleteMethod);

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

0 comments on commit 2601614

Please sign in to comment.