From 6a13f5457a8b27d4166b05415a97bff7ec3bf25d Mon Sep 17 00:00:00 2001 From: ZetaTom <70907959+ZetaTom@users.noreply.github.com> Date: Tue, 12 Dec 2023 14:42:17 +0100 Subject: [PATCH] Deprecate OwncloudClient - Comments Signed-off-by: ZetaTom <70907959+ZetaTom@users.noreply.github.com> --- .../comments/CommentFileRemoteOperationIT.kt | 2 +- .../comments/CommentFileRemoteOperation.java | 51 +++++++------------ 2 files changed, 19 insertions(+), 34 deletions(-) diff --git a/library/src/androidTest/java/com/owncloud/android/lib/resources/comments/CommentFileRemoteOperationIT.kt b/library/src/androidTest/java/com/owncloud/android/lib/resources/comments/CommentFileRemoteOperationIT.kt index bd53d4688..2caa8fc49 100644 --- a/library/src/androidTest/java/com/owncloud/android/lib/resources/comments/CommentFileRemoteOperationIT.kt +++ b/library/src/androidTest/java/com/owncloud/android/lib/resources/comments/CommentFileRemoteOperationIT.kt @@ -48,7 +48,7 @@ class CommentFileRemoteOperationIT : AbstractIT() { assertTrue( CommentFileRemoteOperation("test", remoteFile.localId) - .execute(client) + .execute(nextcloudClient) .isSuccess ) diff --git a/library/src/main/java/com/owncloud/android/lib/resources/comments/CommentFileRemoteOperation.java b/library/src/main/java/com/owncloud/android/lib/resources/comments/CommentFileRemoteOperation.java index 99d72d377..809aa0cd0 100644 --- a/library/src/main/java/com/owncloud/android/lib/resources/comments/CommentFileRemoteOperation.java +++ b/library/src/main/java/com/owncloud/android/lib/resources/comments/CommentFileRemoteOperation.java @@ -29,28 +29,22 @@ import android.util.Log; -import com.google.gson.GsonBuilder; -import com.owncloud.android.lib.common.OwnCloudClient; +import com.nextcloud.common.JSONRequestBody; +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 org.apache.commons.httpclient.HttpStatus; -import org.apache.commons.httpclient.methods.StringRequestEntity; -import org.apache.commons.httpclient.methods.Utf8PostMethod; import java.io.IOException; -import java.util.HashMap; -import java.util.Map; /** * Comment file */ -public class CommentFileRemoteOperation extends RemoteOperation { +public class CommentFileRemoteOperation extends RemoteOperation { private static final String TAG = CommentFileRemoteOperation.class.getSimpleName(); - private static final int POST_READ_TIMEOUT = 30000; - private static final int POST_CONNECTION_TIMEOUT = 5000; - private static final String ACTOR_ID = "actorId"; private static final String ACTOR_TYPE = "actorType"; private static final String ACTOR_TYPE_VALUE = "users"; @@ -77,32 +71,27 @@ public CommentFileRemoteOperation(String message, long fileId) { * @param client Client object to communicate with the remote ownCloud server. */ @Override - protected RemoteOperationResult run(OwnCloudClient client) { + public RemoteOperationResult run(NextcloudClient client) { - Utf8PostMethod postMethod = null; - RemoteOperationResult result; + PostMethod postMethod = null; + RemoteOperationResult result; try { - String url = client.getCommentsUri(fileId); - postMethod = new Utf8PostMethod(url); - postMethod.addRequestHeader("Content-type", "application/json"); - - Map values = new HashMap<>(); - values.put(ACTOR_ID, client.getUserId()); - values.put(ACTOR_TYPE, ACTOR_TYPE_VALUE); - values.put(VERB, VERB_VALUE); - values.put(MESSAGE, message); - - String json = new GsonBuilder().create().toJson(values, Map.class); + // request body + JSONRequestBody jsonRequestBody = new JSONRequestBody(ACTOR_ID, client.getUserId()); + jsonRequestBody.put(ACTOR_TYPE, ACTOR_TYPE_VALUE); + jsonRequestBody.put(VERB, VERB_VALUE); + jsonRequestBody.put(MESSAGE, message); - postMethod.setRequestEntity(new StringRequestEntity(json)); + // post method + String url = client.getCommentsUri(fileId); + postMethod = new PostMethod(url, false, jsonRequestBody.get()); - int status = client.executeMethod(postMethod, POST_READ_TIMEOUT, POST_CONNECTION_TIMEOUT); + int status = client.execute(postMethod); - result = new RemoteOperationResult(isSuccess(status), postMethod); + result = new RemoteOperationResult<>(status == HttpStatus.SC_CREATED, postMethod); - client.exhaustResponse(postMethod.getResponseBodyAsStream()); } catch (IOException e) { - result = new RemoteOperationResult(e); + result = new RemoteOperationResult<>(e); Log.e(TAG, "Post comment to file with id " + fileId + " failed: " + result.getLogMessage(), e); } finally { if (postMethod != null) { @@ -112,8 +101,4 @@ protected RemoteOperationResult run(OwnCloudClient client) { return result; } - - private boolean isSuccess(int status) { - return status == HttpStatus.SC_CREATED; - } }