-
Notifications
You must be signed in to change notification settings - Fork 91
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
migrate CheckRemoteWipeRemoteOperation to NextcloudClient #1472
Open
tobiasKaminsky
wants to merge
1
commit into
master
Choose a base branch
from
depocc/remoteWipe
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
...oidTest/java/com/owncloud/android/lib/resources/users/CheckRemoteWipeRemoteOperationIT.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* Nextcloud Android Library | ||
* | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH | ||
* SPDX-FileCopyrightText: 2024 Tobias Kaminsky <[email protected]> | ||
* SPDX-License-Identifier: MIT | ||
*/ | ||
package com.owncloud.android.lib.resources.users | ||
|
||
import com.owncloud.android.AbstractIT | ||
import junit.framework.TestCase.assertEquals | ||
import junit.framework.TestCase.assertFalse | ||
import junit.framework.TestCase.assertTrue | ||
import org.junit.Test | ||
import java.net.HttpURLConnection | ||
|
||
class CheckRemoteWipeRemoteOperationIT : AbstractIT() { | ||
@Test | ||
fun checkRemoteWipe() { | ||
val sut = ConvertAppTokenRemoteOperation() | ||
val resultAppToken = sut.execute(nextcloudClient) | ||
assertTrue(resultAppToken.isSuccess) | ||
|
||
val newPassword = resultAppToken.resultData | ||
|
||
val result = CheckRemoteWipeRemoteOperation(newPassword).execute(nextcloudClient) | ||
|
||
// if no remote wipe, then 404 / false | ||
assertEquals(HttpURLConnection.HTTP_NOT_FOUND, result.httpCode) | ||
assertFalse(result.isSuccess) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,65 +7,78 @@ | |
*/ | ||
package com.owncloud.android.lib.resources.users; | ||
|
||
import com.owncloud.android.lib.common.OwnCloudClient; | ||
import com.google.gson.Gson; | ||
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 java.util.HashMap; | ||
|
||
import okhttp3.MediaType; | ||
import okhttp3.RequestBody; | ||
|
||
|
||
/** | ||
* Remote operation performing check if app token is scheduled for remote wipe | ||
*/ | ||
|
||
public class CheckRemoteWipeRemoteOperation extends RemoteOperation { | ||
public class CheckRemoteWipeRemoteOperation extends RemoteOperation<Void> { | ||
|
||
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 String authToken; | ||
|
||
public CheckRemoteWipeRemoteOperation(String authToken) { | ||
this.authToken = authToken; | ||
} | ||
|
||
/** | ||
* @param client Client object | ||
*/ | ||
@Override | ||
protected RemoteOperationResult run(OwnCloudClient client) { | ||
Utf8PostMethod postMethod = null; | ||
RemoteOperationResult result; | ||
public RemoteOperationResult<Void> run(NextcloudClient client) { | ||
PostMethod postMethod = null; | ||
RemoteOperationResult<Void> result; | ||
|
||
try { | ||
postMethod = new Utf8PostMethod(client.getBaseUri() + REMOTE_WIPE_URL + JSON_FORMAT); | ||
HashMap<String, String> map = new HashMap<>(); | ||
map.put(REMOTE_WIPE_TOKEN, authToken); | ||
|
||
String jsonString = new Gson().toJson(map); | ||
|
||
RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), jsonString); | ||
postMethod = new PostMethod(client.getBaseUri() + REMOTE_WIPE_URL + JSON_FORMAT, true, requestBody); | ||
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.execute(postMethod); | ||
|
||
if (HttpStatus.SC_OK == status) { | ||
String response = postMethod.getResponseBodyAsString(); | ||
|
||
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); | ||
Comment on lines
69
to
+72
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If I'm not mistaken, this if statement can be simplified to the following expression: result = new RemoteOperationResult<>(json.getBoolean(WIPE), 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()); | ||
"Getting remote wipe status failed: " + result.getLogMessage(), | ||
result.getException()); | ||
} finally { | ||
if (postMethod != null) { | ||
postMethod.releaseConnection(); | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure why this variable was made static.