99
1010import 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 ;
1415import com .owncloud .android .lib .common .operations .RemoteOperation ;
1516import com .owncloud .android .lib .common .operations .RemoteOperationResult ;
1617
1718import org .apache .commons .httpclient .HttpStatus ;
18- import org .apache .commons .httpclient .methods .StringRequestEntity ;
19- import org .apache .commons .httpclient .methods .Utf8PostMethod ;
2019
2120import 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