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