Skip to content
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

Deprecate OwncloudClient - Comments #1295

Merged
merged 1 commit into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class CommentFileRemoteOperationIT : AbstractIT() {

assertTrue(
CommentFileRemoteOperation("test", remoteFile.localId)
.execute(client)
.execute(nextcloudClient)
.isSuccess
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,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<Void> {

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";
Expand All @@ -57,32 +51,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<Void> run(NextcloudClient client) {

Utf8PostMethod postMethod = null;
RemoteOperationResult result;
PostMethod postMethod = null;
RemoteOperationResult<Void> result;
try {
String url = client.getCommentsUri(fileId);
postMethod = new Utf8PostMethod(url);
postMethod.addRequestHeader("Content-type", "application/json");

Map<String, String> 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) {
Expand All @@ -92,8 +81,4 @@ protected RemoteOperationResult run(OwnCloudClient client) {

return result;
}

private boolean isSuccess(int status) {
return status == HttpStatus.SC_CREATED;
}
}
Loading