001package com.box.sdk;
002
003import com.eclipsesource.json.JsonObject;
004import com.eclipsesource.json.JsonValue;
005
006/**
007 * Represents an event that was fired off by the Box events API.
008 */
009public class BoxEvent extends BoxResource {
010    private BoxResource.Info sourceInfo;
011    private BoxEvent.Type type;
012    private JsonObject sourceJSON;
013
014    /**
015     * Constructs a BoxEvent from a JSON string.
016     * @param  api  the API connection to be used by the file.
017     * @param  json the JSON encoded event.
018     */
019    public BoxEvent(BoxAPIConnection api, String json) {
020        this(api, JsonObject.readFrom(json));
021    }
022
023    BoxEvent(BoxAPIConnection api, JsonObject jsonObject) {
024        super(api, jsonObject.get("event_id").asString());
025
026        for (JsonObject.Member member : jsonObject) {
027            if (member.getValue().isNull()) {
028                continue;
029            }
030
031            this.parseJsonMember(member);
032        }
033    }
034
035    /**
036     * Gets info about the source of this event.
037     *
038     * <p>Note that there is a bug in the enterprise event stream where certain event sources don't correctly map to a
039     * BoxResource.Info. In the case where the event source JSON cannot be mapped to a BoxResource.Info, you can use the
040     * {@link #getSourceJSON} method to access the raw JSON representation of the event source.</p>
041     *
042     * @return info about the source of this event.
043     */
044    public BoxResource.Info getSourceInfo() {
045        return this.sourceInfo;
046    }
047
048    /**
049     * Gets the raw JSON object containing information about the source of this event.
050     *
051     * <p>This method can be used to work around bugs in the enterprise events API where some enterprise event sources
052     * don't correctly map to a BoxResource.Info. In this case, this method can be used to access the raw JSON
053     * directly.</p>
054     *
055     * @return the JSON representation of the source of this event.
056     */
057    public JsonObject getSourceJSON() {
058        return this.sourceJSON;
059    }
060
061    /**
062     * Gets the type of this event.
063     * @return the type of this event.
064     */
065    public BoxEvent.Type getType() {
066        return this.type;
067    }
068
069    void parseJsonMember(JsonObject.Member member) {
070        JsonValue value = member.getValue();
071        if (value.isNull()) {
072            return;
073        }
074
075        String memberName = member.getName();
076        if (memberName.equals("source")) {
077            // Parsing the source might fail due to a bug in the enterprise event stream where the API returns JSON that
078            // doesn't correctly map to a BoxResource.Info. If this happens, we set the sourceInfo to null and expect
079            // the caller to use the getSourceJSON() method instead.
080            try {
081                this.sourceInfo = BoxResource.parseInfo(this.getAPI(), value.asObject());
082            } catch (Exception e) {
083                this.sourceInfo = null;
084            }
085            this.sourceJSON = JsonObject.unmodifiableObject(value.asObject());
086        } else if (memberName.equals("event_type")) {
087            String stringValue = value.asString();
088            for (Type t : Type.values()) {
089                if (t.name().equals(stringValue)) {
090                    this.type = t;
091                    break;
092                }
093            }
094
095            if (this.type == null) {
096                this.type = Type.UNKNOWN;
097            }
098        }
099    }
100
101    /**
102     * Enumerates the possible types for an event.
103     */
104    public enum Type {
105        /**
106         * The type of the event is unknown.
107         */
108        UNKNOWN,
109
110        /**
111         * An file or folder was created.
112         */
113        ITEM_CREATE,
114
115        /**
116         * An file or folder was uploaded.
117         */
118        ITEM_UPLOAD,
119
120        /**
121         * A comment was created on a folder, file, or other comment.
122         */
123        COMMENT_CREATE,
124
125        /**
126         * An file or folder was downloaded.
127         */
128        ITEM_DOWNLOAD,
129
130        /**
131         * A file was previewed.
132         */
133        ITEM_PREVIEW,
134
135        /**
136         * A file or folder was moved.
137         */
138        ITEM_MOVE,
139
140        /**
141         * A file or folder was copied.
142         */
143        ITEM_COPY,
144
145        /**
146         * A task was assigned.
147         */
148        TASK_ASSIGNMENT_CREATE,
149
150        /**
151         * A file was locked.
152         */
153        LOCK_CREATE,
154
155        /**
156         * A file was unlocked.
157         */
158        LOCK_DESTROY,
159
160        /**
161         * A file or folder was deleted.
162         */
163        ITEM_TRASH,
164
165        /**
166         * A file or folder was recovered from the trash.
167         */
168        ITEM_UNDELETE_VIA_TRASH,
169
170        /**
171         * A collaborator was added to a folder.
172         */
173        COLLAB_ADD_COLLABORATOR,
174
175        /**
176         * A collaborator was removed from a folder.
177         */
178        COLLAB_REMOVE_COLLABORATOR,
179
180        /**
181         * A collaborator was invited to a folder.
182         */
183        COLLAB_INVITE_COLLABORATOR,
184
185        /**
186         * A collaborator's role was change in a folder.
187         */
188        COLLAB_ROLE_CHANGE,
189
190        /**
191         * A folder was marked for sync.
192         */
193        ITEM_SYNC,
194
195        /**
196         * A folder was un-marked for sync.
197         */
198        ITEM_UNSYNC,
199
200        /**
201         * A file or folder was renamed.
202         */
203        ITEM_RENAME,
204
205        /**
206         * A file or folder was enabled for sharing.
207         */
208        ITEM_SHARED_CREATE,
209
210        /**
211         * A file or folder was disabled for sharing.
212         */
213        ITEM_SHARED_UNSHARE,
214
215        /**
216         * A folder was shared.
217         */
218        ITEM_SHARED,
219
220        /**
221         * A tag was added to a file or folder.
222         */
223        TAG_ITEM_CREATE,
224
225        /**
226         * A user logged in from a new device.
227         */
228        ADD_LOGIN_ACTIVITY_DEVICE,
229
230        /**
231         * A user session associated with an app was invalidated.
232         */
233        REMOVE_LOGIN_ACTIVITY_DEVICE,
234
235        /**
236         * An admin role changed for a user.
237         */
238        CHANGE_ADMIN_ROLE,
239
240        /**
241         * A user was added to a group. This is an enterprise-only event.
242         */
243        GROUP_ADD_USER,
244
245        /**
246         * A user was created. This is an enterprise-only event.
247         */
248        NEW_USER,
249
250        /**
251         * A group was created. This is an enterprise-only event.
252         */
253        GROUP_CREATION,
254
255        /**
256         * A group was deleted. This is an enterprise-only event.
257         */
258        GROUP_DELETION,
259
260        /**
261         * A user was deleted. This is an enterprise-only event.
262         */
263        DELETE_USER,
264
265        /**
266         * A group was edited. This is an enterprise-only event.
267         */
268        GROUP_EDITED,
269
270        /**
271         * A user was edited. This is an enterprise-only event.
272         */
273        EDIT_USER,
274
275        /**
276         * A group was granted access to a folder. This is an enterprise-only event.
277         */
278        GROUP_ADD_FOLDER,
279
280        /**
281         * A user was removed from a group. This is an enterprise-only event.
282         */
283        GROUP_REMOVE_USER,
284
285        /**
286         * A group had its access to a folder removed. This is an enterprise-only event.
287         */
288        GROUP_REMOVE_FOLDER,
289
290        /**
291         * An administrator logged in. This is an enterprise-only event.
292         */
293        ADMIN_LOGIN,
294
295        /**
296         * A device was associated with a user. This is an enterprise-only event.
297         */
298        ADD_DEVICE_ASSOCIATION,
299
300        /**
301         * There was a failed login attempt. This is an enterprise-only event.
302         */
303        FAILED_LOGIN,
304
305        /**
306         * There was a successful login. This is an enterprise-only event.
307         */
308        LOGIN,
309
310        /**
311         * A user's OAuth2 access token was refreshed. This is an enterprise-only event.
312         */
313        USER_AUTHENTICATE_OAUTH2_TOKEN_REFRESH,
314
315        /**
316         * A device was disassociated with a user. This is an enterprise-only event.
317         */
318        REMOVE_DEVICE_ASSOCIATION,
319
320        /**
321         * A user agreed to the terms of service. This is an enterprise-only event.
322         */
323        TERMS_OF_SERVICE_AGREE,
324
325        /**
326         * A user rejected the terms of service. This is an enterprise-only event.
327         */
328        TERMS_OF_SERVICE_REJECT,
329
330        /**
331         * An item was copied. This is an enterprise-only event.
332         */
333        COPY,
334
335        /**
336         * An item was deleted. This is an enterprise-only event.
337         */
338        DELETE,
339
340        /**
341         * An item was downloaded. This is an enterprise-only event.
342         */
343        DOWNLOAD,
344
345        /**
346         * An item was edited. This is an enterprise-only event.
347         */
348        EDIT,
349
350        /**
351         * An item was locked. This is an enterprise-only event.
352         */
353        LOCK,
354
355        /**
356         * An item was moved. This is an enterprise-only event.
357         */
358        MOVE,
359
360        /**
361         * An item was previewed. This is an enterprise-only event.
362         */
363        PREVIEW,
364
365        /**
366         * An item was renamed. This is an enterprise-only event.
367         */
368        RENAME,
369
370        /**
371         * An item was set to be auto-deleted. This is an enterprise-only event.
372         */
373        STORAGE_EXPIRATION,
374
375        /**
376         * An item was undeleted. This is an enterprise-only event.
377         */
378        UNDELETE,
379
380        /**
381         * An item was unlocked. This is an enterprise-only event.
382         */
383        UNLOCK,
384
385        /**
386         * An item was uploaded. This is an enterprise-only event.
387         */
388        UPLOAD,
389
390        /**
391         * An shared link was created for an item. This is an enterprise-only event.
392         */
393        SHARE,
394
395        /**
396         * The shared link for an item was updated. This is an enterprise-only event.
397         */
398        ITEM_SHARED_UPDATE,
399
400        /**
401         * The expiration time for a shared link was extended. This is an enterprise-only event.
402         */
403        UPDATE_SHARE_EXPIRATION,
404
405        /**
406         * The expiration time was set for a shared link. This is an enterprise-only event.
407         */
408        SHARE_EXPIRATION,
409
410        /**
411         * The shared link for an item was REMOVE_DEVICE_ASSOCIATION. This is an enterprise-only event.
412         */
413        UNSHARE,
414
415        /**
416         * A user accepted a collaboration invite. This is an enterprise-only event.
417         */
418        COLLABORATION_ACCEPT,
419
420        /**
421         * A user's collaboration role was changed. This is an enterprise-only event.
422         */
423        COLLABORATION_ROLE_CHANGE,
424
425        /**
426         * The expiration time for a collaboration was extended. This is an enterprise-only event.
427         */
428        UPDATE_COLLABORATION_EXPIRATION,
429
430        /**
431         * A collaboration was removed from a folder. This is an enterprise-only event.
432         */
433        COLLABORATION_REMOVE,
434
435        /**
436         * A user was invited to collaborate on a folder. This is an enterprise-only event.
437         */
438        COLLABORATION_INVITE,
439
440        /**
441         * An expiration time was set for a collaboration. This is an enterprise-only event.
442         */
443        COLLABORATION_EXPIRATION;
444    }
445}