3434import com .google .gson .JsonObject ;
3535import com .google .gson .JsonParser ;
3636import com .google .gson .reflect .TypeToken ;
37- import com .owncloud .android .lib .common .OwnCloudClient ;
37+ import com .nextcloud .common .NextcloudClient ;
38+ import com .nextcloud .operations .GetMethod ;
3839import com .owncloud .android .lib .common .operations .RemoteOperation ;
3940import com .owncloud .android .lib .common .operations .RemoteOperationResult ;
4041import com .owncloud .android .lib .common .utils .Log_OC ;
4142import com .owncloud .android .lib .resources .notifications .models .Notification ;
4243
4344import org .apache .commons .httpclient .HttpStatus ;
44- import org .apache .commons .httpclient .methods .GetMethod ;
45- import org .json .JSONException ;
4645
4746import java .lang .reflect .Type ;
47+ import java .util .HashMap ;
4848import java .util .List ;
4949
5050/**
5151 * Provides the remote notifications from the server handling the following data structure
5252 * accessible via the notifications endpoint at {@value OCS_ROUTE_LIST_V12_AND_UP}, specified at
5353 * {@link "https://github.com/nextcloud/notifications/blob/master/docs/ocs-endpoint-v2.md"}.
5454 */
55- public class GetNotificationsRemoteOperation extends RemoteOperation {
55+ public class GetNotificationsRemoteOperation extends RemoteOperation < List < Notification >> {
5656
5757 // OCS Route
5858 private static final String OCS_ROUTE_LIST_V12_AND_UP =
@@ -66,39 +66,38 @@ public class GetNotificationsRemoteOperation extends RemoteOperation {
6666 private static final String NODE_DATA = "data" ;
6767
6868 @ Override
69- protected RemoteOperationResult run (OwnCloudClient client ) {
70- RemoteOperationResult result = null ;
71- int status = - 1 ;
69+ public RemoteOperationResult < List < Notification >> run (NextcloudClient client ) {
70+ RemoteOperationResult < List < Notification >> result ;
71+ int status ;
7272 GetMethod get = null ;
7373 List <Notification > notifications ;
7474 String url = client .getBaseUri () + OCS_ROUTE_LIST_V12_AND_UP ;
7575
7676 // get the notifications
7777 try {
78- get = new GetMethod (url );
79- get .addRequestHeader (OCS_API_HEADER , OCS_API_HEADER_VALUE );
78+ get = new GetMethod (url , true );
79+ HashMap <String , String > map = new HashMap <>();
80+ map .put ("format" , "json" );
81+
82+ get .setQueryString (map );
83+ status = client .execute (get );
8084
81- status = client .executeMethod (get );
8285 String response = get .getResponseBodyAsString ();
8386
8487 if (isSuccess (status )) {
85- result = new RemoteOperationResult (true , status , get . getResponseHeaders () );
88+ result = new RemoteOperationResult <> (true , get );
8689 Log_OC .d (TAG , "Successful response: " + response );
8790
8891 // Parse the response
8992 notifications = parseResult (response );
90- result .setNotificationData (notifications );
93+ result .setResultData (notifications );
9194 } else {
92- result = new RemoteOperationResult (false , status , get . getResponseHeaders () );
95+ result = new RemoteOperationResult <> (false , get );
9396 Log_OC .e (TAG , "Failed response while getting user notifications " );
94- if (response != null ) {
95- Log_OC .e (TAG , "*** status code: " + status + " ; response message: " + response );
96- } else {
97- Log_OC .e (TAG , "*** status code: " + status );
98- }
97+ Log_OC .e (TAG , "*** status code: " + status + " ; response message: " + response );
9998 }
10099 } catch (Exception e ) {
101- result = new RemoteOperationResult (e );
100+ result = new RemoteOperationResult <> (e );
102101 Log_OC .e (TAG , "Exception while getting remote notifications" , e );
103102 } finally {
104103 if (get != null ) {
@@ -109,7 +108,7 @@ protected RemoteOperationResult run(OwnCloudClient client) {
109108 return result ;
110109 }
111110
112- private List <Notification > parseResult (String response ) throws JSONException {
111+ private List <Notification > parseResult (String response ) {
113112 JsonParser jsonParser = new JsonParser ();
114113 JsonObject jo = (JsonObject )jsonParser .parse (response );
115114 JsonArray jsonDataArray = jo .getAsJsonObject (NODE_OCS ).getAsJsonArray (NODE_DATA );
0 commit comments