Welcome to mirror list, hosted at ThFree Co, Russian Federation.

GetActivitiesRemoteOperation.java « activities « resources « lib « android « owncloud « com « java « main « src « library - github.com/nextcloud/android-library.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 642ca5c09e86015b3461ccf6e98ecce3bd8b942d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/*  Nextcloud Android Library is available under MIT license
 *
 *   Copyright (C) 2017 Alejandro Bautista
 *   @author Alejandro Bautista
 *
 *   Permission is hereby granted, free of charge, to any person obtaining a copy
 *   of this software and associated documentation files (the "Software"), to deal
 *   in the Software without restriction, including without limitation the rights
 *   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 *   copies of the Software, and to permit persons to whom the Software is
 *   furnished to do so, subject to the following conditions:
 *
 *   The above copyright notice and this permission notice shall be included in
 *   all copies or substantial portions of the Software.
 *
 *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 *   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 *   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 *   BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 *   ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 *   CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 *   THE SOFTWARE.
 *
 */

package com.owncloud.android.lib.resources.activities;


import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;
import com.nextcloud.common.NextcloudClient;
import com.nextcloud.operations.GetMethod;
import com.owncloud.android.lib.common.OwnCloudClient;
import com.owncloud.android.lib.common.operations.RemoteOperation;
import com.owncloud.android.lib.common.operations.RemoteOperationResult;
import com.owncloud.android.lib.common.utils.Log_OC;
import com.owncloud.android.lib.resources.activities.model.Activity;
import com.owncloud.android.lib.resources.activities.model.RichElement;
import com.owncloud.android.lib.resources.activities.model.RichElementTypeAdapter;
import com.owncloud.android.lib.resources.activities.models.PreviewObject;
import com.owncloud.android.lib.resources.activities.models.PreviewObjectAdapter;

import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Provides the remote activities from the server handling the following data structure
 * accessible via the activities endpoint at {@value OCS_ROUTE_V12_AND_UP}, specified at
 * {@link "https://github.com/nextcloud/activity/blob/master/docs/endpoint-v2.md"}.
 */
public class GetActivitiesRemoteOperation extends RemoteOperation {

    private static final String TAG = GetActivitiesRemoteOperation.class.getSimpleName();

    // OCS Routes
    private static final String OCS_ROUTE_V12_AND_UP = "/ocs/v2.php/apps/activity/api/v2/activity";

    // JSON Node names
    private static final String NODE_OCS = "ocs";

    private static final String NODE_DATA = "data";

    private int lastGiven = -1;
    
    private String fileId = "";

    public GetActivitiesRemoteOperation() {
    }

    public GetActivitiesRemoteOperation(String fileId) {
        this.fileId = fileId;
    }
    
    public GetActivitiesRemoteOperation(String fileId, int lastGiven) {
        this.fileId = fileId;
        this.lastGiven = lastGiven;
    }
    
    public GetActivitiesRemoteOperation(int lastGiven) {
        this.lastGiven = lastGiven;
    }

    @Override
    public RemoteOperationResult run(NextcloudClient client) {
        RemoteOperationResult result;
        int status;
        GetMethod get = null;
        ArrayList<Activity> activities;
        String url = client.getBaseUri() + OCS_ROUTE_V12_AND_UP;

        // add filter for fileId, if available
        if (!fileId.isEmpty()) {
            url = url + "/filter";
        }

        Log_OC.d(TAG, "URL: " + url);

        try {
            get = new GetMethod(url, true);

            HashMap<String, String> parameters = new HashMap<>();
            parameters.put("format", "json");
            parameters.put("previews", "true");

            if (lastGiven != -1) {
                parameters.put("since", String.valueOf(lastGiven));
            }

            if (!fileId.isEmpty()) {
                parameters.put("sort", "desc");
                parameters.put("object_type", "files");
                parameters.put("object_id", fileId);
            }

            get.setQueryString(parameters);

            status = client.execute(get);
            String response = get.getResponseBodyAsString();

            if (isSuccess(status)) {
                String nextPageHeader = get.getResponseHeader("X-Activity-Last-Given");
                if (nextPageHeader != null) {
                    lastGiven = Integer.parseInt(nextPageHeader);
                } else {
                    lastGiven = -1;
                }

                Log_OC.d(TAG, "Successful response: " + response);
                result = new RemoteOperationResult(true, get);
                // Parse the response
                activities = parseResult(response);

                ArrayList<Object> data = new ArrayList<>();
                data.add(activities);
                data.add(lastGiven);
                result.setData(data);
            } else {
                result = new RemoteOperationResult(false, get);
                Log_OC.e(TAG, "Failed response while getting user activities");
                Log_OC.e(TAG, "*** status code: " + status + " ; response message: " + response);
            }
        } catch (IOException e) {
            Log_OC.e(TAG, "Error getting user activities", e);
            return new RemoteOperationResult(e);
        } finally {
            if (get != null) {
                get.releaseConnection();
            }
        }

        return result;
    }
    
    @Override
    protected RemoteOperationResult run(OwnCloudClient client) {
        RemoteOperationResult result;
        int status;
        org.apache.commons.httpclient.methods.GetMethod get = null;
        ArrayList<Activity> activities;
        String url = client.getBaseUri() + OCS_ROUTE_V12_AND_UP;
        
        // add filter for fileId, if available
        if (!fileId.isEmpty()) {
            url = url + "/filter";
        }
        
        Log_OC.d(TAG, "URL: " + url);

        try {
            get = new org.apache.commons.httpclient.methods.GetMethod(url);
            get.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE);

            ArrayList<NameValuePair> parameters = new ArrayList<>();
            parameters.add(new NameValuePair("format", "json"));
            parameters.add(new NameValuePair("previews", "true"));
            
            if (lastGiven != -1) {
                parameters.add(new NameValuePair("since", String.valueOf(lastGiven)));
            }

            if (!fileId.isEmpty()) {
                parameters.add(new NameValuePair("sort", "desc"));
                parameters.add(new NameValuePair("object_type", "files"));
                parameters.add(new NameValuePair("object_id", fileId));
            }

            get.setQueryString(parameters.toArray(new NameValuePair[]{}));

            status = client.executeMethod(get);
            String response = get.getResponseBodyAsString();

            Header nextPageHeader = get.getResponseHeader("X-Activity-Last-Given");
            if (nextPageHeader != null) {
                lastGiven = Integer.parseInt(nextPageHeader.getValue());
            } else {
                lastGiven = -1;
            }

            if (isSuccess(status)) {
                Log_OC.d(TAG, "Successful response: " + response);
                result = new RemoteOperationResult(true, status, get.getResponseHeaders());
                // Parse the response
                if (response == null) {
                    activities = new ArrayList<>();
                } else {
                    activities = parseResult(response);
                }

                ArrayList<Object> data = new ArrayList<>();
                data.add(activities);
                data.add(lastGiven);
                result.setData(data);
            } else {
                result = new RemoteOperationResult(false, status, get.getResponseHeaders());
                Log_OC.e(TAG, "Failed response while getting user activities ");
                if (response != null) {
                    Log_OC.e(TAG, "*** status code: " + status + " ; response message: " + response);
                } else {
                    Log_OC.e(TAG, "*** status code: " + status);
                }
            }
        } catch (IOException e) {
            result = new RemoteOperationResult(e);
            Log_OC.e(TAG, "Exception while getting remote activities", e);
        } finally {
            if (get != null) {
                get.releaseConnection();
            }
        }

        return result;
    }

    public boolean hasMoreActivities() {
        return lastGiven> 0;
    }

    protected ArrayList<Activity> parseResult(String response) {
        if (response == null || response.isEmpty()) {
            return new ArrayList<>();
        }

        try {
            JsonParser jsonParser = new JsonParser();
            JsonObject jo = (JsonObject) jsonParser.parse(response);
            JsonArray jsonDataArray = jo.getAsJsonObject(NODE_OCS).getAsJsonArray(NODE_DATA);

            Gson gson = new GsonBuilder()
                    .registerTypeAdapter(RichElement.class, new RichElementTypeAdapter())
                    .registerTypeAdapter(PreviewObject.class, new PreviewObjectAdapter())
                    .create();
            Type listType = new TypeToken<List<Activity>>() {
            }.getType();

            return gson.fromJson(jsonDataArray, listType);

        } catch (JsonSyntaxException e) {
            Log_OC.e(TAG, "Not a valid json: " + response, e);
            return new ArrayList<>();
        }
    }

    private boolean isSuccess(int status) {
        return (status == HttpStatus.SC_OK || status == HttpStatus.SC_NOT_MODIFIED || status == HttpStatus.SC_NOT_FOUND);
    }
}