Skip to content

Commit 8471187

Browse files
Merge pull request #1295 from nextcloud/depocc/comments
Deprecate OwncloudClient - Comments
2 parents 354911a + 3b672c1 commit 8471187

File tree

2 files changed

+19
-34
lines changed

2 files changed

+19
-34
lines changed

library/src/androidTest/java/com/owncloud/android/lib/resources/comments/CommentFileRemoteOperationIT.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class CommentFileRemoteOperationIT : AbstractIT() {
2929

3030
assertTrue(
3131
CommentFileRemoteOperation("test", remoteFile.localId)
32-
.execute(client)
32+
.execute(nextcloudClient)
3333
.isSuccess
3434
)
3535

library/src/main/java/com/owncloud/android/lib/resources/comments/CommentFileRemoteOperation.java

Lines changed: 18 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,22 @@
99

1010
import android.util.Log;
1111

12-
import com.google.gson.GsonBuilder;
13-
import com.owncloud.android.lib.common.OwnCloudClient;
12+
import com.nextcloud.common.JSONRequestBody;
13+
import com.nextcloud.common.NextcloudClient;
14+
import com.nextcloud.operations.PostMethod;
1415
import com.owncloud.android.lib.common.operations.RemoteOperation;
1516
import com.owncloud.android.lib.common.operations.RemoteOperationResult;
1617

1718
import org.apache.commons.httpclient.HttpStatus;
18-
import org.apache.commons.httpclient.methods.StringRequestEntity;
19-
import org.apache.commons.httpclient.methods.Utf8PostMethod;
2019

2120
import java.io.IOException;
22-
import java.util.HashMap;
23-
import java.util.Map;
2421

2522
/**
2623
* Comment file
2724
*/
28-
public class CommentFileRemoteOperation extends RemoteOperation {
25+
public class CommentFileRemoteOperation extends RemoteOperation<Void> {
2926

3027
private static final String TAG = CommentFileRemoteOperation.class.getSimpleName();
31-
private static final int POST_READ_TIMEOUT = 30000;
32-
private static final int POST_CONNECTION_TIMEOUT = 5000;
33-
3428
private static final String ACTOR_ID = "actorId";
3529
private static final String ACTOR_TYPE = "actorType";
3630
private static final String ACTOR_TYPE_VALUE = "users";
@@ -57,32 +51,27 @@ public CommentFileRemoteOperation(String message, long fileId) {
5751
* @param client Client object to communicate with the remote ownCloud server.
5852
*/
5953
@Override
60-
protected RemoteOperationResult run(OwnCloudClient client) {
54+
public RemoteOperationResult<Void> run(NextcloudClient client) {
6155

62-
Utf8PostMethod postMethod = null;
63-
RemoteOperationResult result;
56+
PostMethod postMethod = null;
57+
RemoteOperationResult<Void> result;
6458
try {
65-
String url = client.getCommentsUri(fileId);
66-
postMethod = new Utf8PostMethod(url);
67-
postMethod.addRequestHeader("Content-type", "application/json");
68-
69-
Map<String, String> values = new HashMap<>();
70-
values.put(ACTOR_ID, client.getUserId());
71-
values.put(ACTOR_TYPE, ACTOR_TYPE_VALUE);
72-
values.put(VERB, VERB_VALUE);
73-
values.put(MESSAGE, message);
74-
75-
String json = new GsonBuilder().create().toJson(values, Map.class);
59+
// request body
60+
JSONRequestBody jsonRequestBody = new JSONRequestBody(ACTOR_ID, client.getUserId());
61+
jsonRequestBody.put(ACTOR_TYPE, ACTOR_TYPE_VALUE);
62+
jsonRequestBody.put(VERB, VERB_VALUE);
63+
jsonRequestBody.put(MESSAGE, message);
7664

77-
postMethod.setRequestEntity(new StringRequestEntity(json));
65+
// post method
66+
String url = client.getCommentsUri(fileId);
67+
postMethod = new PostMethod(url, false, jsonRequestBody.get());
7868

79-
int status = client.executeMethod(postMethod, POST_READ_TIMEOUT, POST_CONNECTION_TIMEOUT);
69+
int status = client.execute(postMethod);
8070

81-
result = new RemoteOperationResult(isSuccess(status), postMethod);
71+
result = new RemoteOperationResult<>(status == HttpStatus.SC_CREATED, postMethod);
8272

83-
client.exhaustResponse(postMethod.getResponseBodyAsStream());
8473
} catch (IOException e) {
85-
result = new RemoteOperationResult(e);
74+
result = new RemoteOperationResult<>(e);
8675
Log.e(TAG, "Post comment to file with id " + fileId + " failed: " + result.getLogMessage(), e);
8776
} finally {
8877
if (postMethod != null) {
@@ -92,8 +81,4 @@ protected RemoteOperationResult run(OwnCloudClient client) {
9281

9382
return result;
9483
}
95-
96-
private boolean isSuccess(int status) {
97-
return status == HttpStatus.SC_CREATED;
98-
}
9984
}

0 commit comments

Comments
 (0)