Skip to content

Commit 78167d8

Browse files
migrate CheckRemoteWipeRemoteOperation to NextcloudClient
Signed-off-by: tobiasKaminsky <[email protected]>
1 parent 8471187 commit 78167d8

File tree

3 files changed

+66
-20
lines changed

3 files changed

+66
-20
lines changed

library/src/androidTest/java/com/owncloud/android/AbstractIT.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ public abstract class AbstractIT {
7272
protected static NextcloudClient nextcloudClient;
7373
protected static Context context;
7474
protected static Uri url;
75+
protected static String password;
7576

7677
protected String baseFolderPath = "/test_for_build/";
7778

@@ -89,7 +90,7 @@ public static void beforeAll() throws InterruptedException,
8990

9091
url = Uri.parse(arguments.getString("TEST_SERVER_URL"));
9192
String loginName = arguments.getString("TEST_SERVER_USERNAME");
92-
String password = arguments.getString("TEST_SERVER_PASSWORD");
93+
password = arguments.getString("TEST_SERVER_PASSWORD");
9394

9495
client = OwnCloudClientFactory.createOwnCloudClient(url, context, true);
9596
client.setCredentials(new OwnCloudBasicCredentials(loginName, password));
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Nextcloud Android Library
3+
*
4+
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH
5+
* SPDX-FileCopyrightText: 2024 Tobias Kaminsky <[email protected]>
6+
* SPDX-License-Identifier: MIT
7+
*/
8+
package com.owncloud.android.lib.resources.users
9+
10+
import com.owncloud.android.AbstractIT
11+
import junit.framework.TestCase.assertEquals
12+
import junit.framework.TestCase.assertFalse
13+
import junit.framework.TestCase.assertTrue
14+
import org.junit.Test
15+
import java.net.HttpURLConnection
16+
17+
class CheckRemoteWipeRemoteOperationIT : AbstractIT() {
18+
@Test
19+
fun checkRemoteWipe() {
20+
val sut = ConvertAppTokenRemoteOperation()
21+
val resultAppToken = sut.execute(nextcloudClient)
22+
assertTrue(resultAppToken.isSuccess)
23+
24+
val newPassword = resultAppToken.resultData
25+
26+
val result = CheckRemoteWipeRemoteOperation(newPassword).execute(nextcloudClient)
27+
28+
// if no remote wipe, then 404 / false
29+
assertEquals(HttpURLConnection.HTTP_NOT_FOUND, result.httpCode)
30+
assertFalse(result.isSuccess)
31+
}
32+
}

library/src/main/java/com/owncloud/android/lib/resources/users/CheckRemoteWipeRemoteOperation.java

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,65 +7,78 @@
77
*/
88
package com.owncloud.android.lib.resources.users;
99

10-
import com.owncloud.android.lib.common.OwnCloudClient;
10+
import com.google.gson.Gson;
11+
import com.nextcloud.common.NextcloudClient;
12+
import com.nextcloud.operations.PostMethod;
1113
import com.owncloud.android.lib.common.operations.RemoteOperation;
1214
import com.owncloud.android.lib.common.operations.RemoteOperationResult;
1315
import com.owncloud.android.lib.common.utils.Log_OC;
1416

1517
import org.apache.commons.httpclient.HttpStatus;
16-
import org.apache.commons.httpclient.methods.Utf8PostMethod;
1718
import org.json.JSONObject;
1819

20+
import java.util.HashMap;
21+
22+
import okhttp3.MediaType;
23+
import okhttp3.RequestBody;
24+
1925

2026
/**
2127
* Remote operation performing check if app token is scheduled for remote wipe
2228
*/
2329

24-
public class CheckRemoteWipeRemoteOperation extends RemoteOperation {
30+
public class CheckRemoteWipeRemoteOperation extends RemoteOperation<Void> {
2531

2632
private static final String TAG = CheckRemoteWipeRemoteOperation.class.getSimpleName();
27-
private static final int SYNC_READ_TIMEOUT = 40000;
28-
private static final int SYNC_CONNECTION_TIMEOUT = 5000;
2933
private static final String REMOTE_WIPE_URL = "/index.php/core/wipe/check";
3034

3135
// JSON node names
3236
private static final String WIPE = "wipe";
3337

38+
private String authToken;
39+
40+
public CheckRemoteWipeRemoteOperation(String authToken) {
41+
this.authToken = authToken;
42+
}
43+
3444
/**
3545
* @param client Client object
3646
*/
3747
@Override
38-
protected RemoteOperationResult run(OwnCloudClient client) {
39-
Utf8PostMethod postMethod = null;
40-
RemoteOperationResult result;
48+
public RemoteOperationResult<Void> run(NextcloudClient client) {
49+
PostMethod postMethod = null;
50+
RemoteOperationResult<Void> result;
4151

4252
try {
43-
postMethod = new Utf8PostMethod(client.getBaseUri() + REMOTE_WIPE_URL + JSON_FORMAT);
53+
HashMap<String, String> map = new HashMap<>();
54+
map.put(REMOTE_WIPE_TOKEN, authToken);
55+
56+
String jsonString = new Gson().toJson(map);
57+
58+
RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), jsonString);
59+
postMethod = new PostMethod(client.getBaseUri() + REMOTE_WIPE_URL + JSON_FORMAT, true, requestBody);
4460
postMethod.addRequestHeader(CONTENT_TYPE, FORM_URLENCODED);
45-
postMethod.setParameter(REMOTE_WIPE_TOKEN, client.getCredentials().getAuthToken());
4661

47-
int status = client.executeMethod(postMethod, SYNC_READ_TIMEOUT, SYNC_CONNECTION_TIMEOUT);
62+
int status = client.execute(postMethod);
4863

4964
if (HttpStatus.SC_OK == status) {
5065
String response = postMethod.getResponseBodyAsString();
5166

5267
JSONObject json = new JSONObject(response);
5368

5469
if (json.getBoolean(WIPE)) {
55-
result = new RemoteOperationResult(true, postMethod);
70+
result = new RemoteOperationResult<>(true, postMethod);
5671
} else {
57-
result = new RemoteOperationResult(false, postMethod);
72+
result = new RemoteOperationResult<>(false, postMethod);
5873
}
5974
} else {
60-
result = new RemoteOperationResult(false, postMethod);
75+
result = new RemoteOperationResult<>(false, postMethod);
6176
}
62-
63-
client.exhaustResponse(postMethod.getResponseBodyAsStream());
6477
} catch (Exception e) {
65-
result = new RemoteOperationResult(e);
78+
result = new RemoteOperationResult<>(e);
6679
Log_OC.e(TAG,
67-
"Getting remote wipe status failed: " + result.getLogMessage(),
68-
result.getException());
80+
"Getting remote wipe status failed: " + result.getLogMessage(),
81+
result.getException());
6982
} finally {
7083
if (postMethod != null) {
7184
postMethod.releaseConnection();

0 commit comments

Comments
 (0)