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 */
012@BoxResourceType("event")
013public class BoxEvent extends BoxResource {
014    private BoxResource.Info sourceInfo;
015    private BoxEvent.Type type;
016    private JsonObject sourceJSON;
017    private Date createdAt;
018    private String ipAddress;
019    private JsonObject additionalDetails;
020    private BoxCollaborator.Info accessibleBy;
021    private BoxUser.Info createdBy;
022    private String sessionID;
023    private BoxUser.Info actionBy;
024
025    /**
026     * Constructs a BoxEvent from a JSON string.
027     * @param  api  the API connection to be used by the file.
028     * @param  json the JSON encoded event.
029     */
030    public BoxEvent(BoxAPIConnection api, String json) {
031        this(api, JsonObject.readFrom(json));
032    }
033
034    BoxEvent(BoxAPIConnection api, JsonObject jsonObject) {
035        super(api, jsonObject.get("event_id").asString());
036
037        for (JsonObject.Member member : jsonObject) {
038            if (member.getValue().isNull()) {
039                continue;
040            }
041
042            this.parseJsonMember(member);
043        }
044    }
045
046    /**
047     * Gets info about the source of this event.
048     *
049     * <p>Note that there is a bug in the enterprise event stream where certain event sources don't correctly map to a
050     * BoxResource.Info. In the case where the event source JSON cannot be mapped to a BoxResource.Info, you can use the
051     * {@link #getSourceJSON} method to access the raw JSON representation of the event source.</p>
052     *
053     * @return info about the source of this event.
054     */
055    public BoxResource.Info getSourceInfo() {
056        return this.sourceInfo;
057    }
058
059    /**
060     * Gets the raw JSON object containing information about the source of this event.
061     *
062     * <p>This method can be used to work around bugs in the enterprise events API where some enterprise event sources
063     * don't correctly map to a BoxResource.Info. In this case, this method can be used to access the raw JSON
064     * directly.</p>
065     *
066     * @return the JSON representation of the source of this event.
067     */
068    public JsonObject getSourceJSON() {
069        return this.sourceJSON;
070    }
071
072    /**
073     * Gets the type of this event.
074     * @return the type of this event.
075     */
076    public BoxEvent.Type getType() {
077        return this.type;
078    }
079
080
081    /**
082     * Gets the time that this event was created.
083     * @return the time that this event was created.
084     */
085    public Date getCreatedAt() {
086        return this.createdAt;
087    }
088
089    /**
090     * Gets the IP address of the user that triggered this event.
091     * @return the IP address of the user that triggered this event.
092     */
093    public String getIPAddress() {
094        return this.ipAddress;
095    }
096
097    /**
098     * Gets a JSON object containing additional details about this event.
099     *
100     * <p>The fields and data within the returned JSON object will vary depending on the type of the event.</p>
101     *
102     * @return a JSON object containing additional details about this event.
103     */
104    public JsonObject getAdditionalDetails() {
105        return this.additionalDetails;
106    }
107
108    /**
109     * Gets info about the collaborator who was given access to a folder within the current enterprise.
110     *
111     * <p>This field is only populated when the event is related to a collaboration that occurred within an enterprise.
112     * </p>
113     *
114     * @return info about the collaborator who was given access to a folder within the current enterprise.
115     */
116    public BoxCollaborator.Info getAccessibleBy() {
117        return this.accessibleBy;
118    }
119
120    /**
121     * Gets info about the user that triggered this event.
122     * @return info about the user that triggered this event.
123     */
124    public BoxUser.Info getCreatedBy() {
125        return this.createdBy;
126    }
127
128    /**
129     * Gets the session ID of the user that triggered this event.
130     * @return the session ID of the user that triggered this event.
131     */
132    public String getSessionID() {
133        return this.sessionID;
134    }
135
136    /**
137     * Gets the user that performed the action for this event.
138     * @return info about the user that performed that action for this event.
139     */
140    public BoxUser.Info getActionBy() {
141        return this.actionBy;
142    }
143
144    void parseJsonMember(JsonObject.Member member) {
145        JsonValue value = member.getValue();
146        if (value.isNull()) {
147            return;
148        }
149
150        String memberName = member.getName();
151        if (memberName.equals("source")) {
152            // Parsing the source might fail due to a bug in the enterprise event stream where the API returns JSON that
153            // doesn't correctly map to a BoxResource.Info. If this happens, we set the sourceInfo to null and expect
154            // the caller to use the getSourceJSON() method instead.
155            try {
156                this.sourceInfo = BoxResource.parseInfo(this.getAPI(), value.asObject());
157            } catch (Exception e) {
158                this.sourceInfo = null;
159            }
160            this.sourceJSON = JsonObject.unmodifiableObject(value.asObject());
161        } else if (memberName.equals("event_type")) {
162            String stringValue = value.asString();
163            for (Type t : Type.values()) {
164                if (t.name().equals(stringValue)) {
165                    this.type = t;
166                    break;
167                }
168            }
169
170            if (this.type == null) {
171                this.type = Type.UNKNOWN;
172            }
173        } else if (memberName.equals("created_at")) {
174            try {
175                this.createdAt = BoxDateFormat.parse(value.asString());
176            } catch (ParseException e) {
177                assert false : "A ParseException indicates a bug in the SDK.";
178            }
179        } else if (memberName.equals("ip_address")) {
180            this.ipAddress = value.asString();
181        } else if (memberName.equals("additional_details")) {
182            this.additionalDetails = value.asObject();
183        } else if (memberName.equals("accessible_by")) {
184            this.accessibleBy = (BoxCollaborator.Info) BoxResource.parseInfo(this.getAPI(), value.asObject());
185        } else if (memberName.equals("created_by")) {
186            this.createdBy = (BoxUser.Info) BoxResource.parseInfo(this.getAPI(), value.asObject());
187        } else if (memberName.equals("session_id")) {
188            this.sessionID = value.asString();
189        } else if (memberName.equals("action_by")) {
190            this.actionBy = (BoxUser.Info) BoxResource.parseInfo(this.getAPI(), value.asObject());
191        }
192    }
193
194    /**
195     * Enumerates the possible types for an event.
196     */
197    public enum Type {
198        /**
199         * The type of the event is unknown.
200         */
201        UNKNOWN,
202
203        /**
204         * An file or folder was created.
205         */
206        ITEM_CREATE,
207
208        /**
209         * An file or folder was uploaded.
210         */
211        ITEM_UPLOAD,
212
213        /**
214         * A comment was created on a folder, file, or other comment.
215         */
216        COMMENT_CREATE,
217
218        /**
219         * A comment was deleted on a folder, file, or other comment.
220         */
221        COMMENT_DELETE,
222
223        /**
224         * An file or folder was downloaded.
225         */
226        ITEM_DOWNLOAD,
227
228        /**
229         * A file was previewed.
230         */
231        ITEM_PREVIEW,
232
233        /**
234         * A file or folder was moved.
235         */
236        ITEM_MOVE,
237
238        /**
239         * A file or folder was copied.
240         */
241        ITEM_COPY,
242
243        /**
244         * A task was assigned.
245         */
246        TASK_ASSIGNMENT_CREATE,
247
248        /**
249         * A task was assignment was completed.
250         */
251        TASK_ASSIGNMENT_COMPLETE,
252
253        /**
254         * A task was assignment was updated.
255         */
256        TASK_ASSIGNMENT_UPDATE,
257
258
259        /**
260         * A task was created.
261         */
262        TASK_CREATE,
263
264        /**
265         * A file was locked.
266         */
267        LOCK_CREATE,
268
269        /**
270         * A file was unlocked.
271         */
272        LOCK_DESTROY,
273
274        /**
275         * A file or folder was deleted.
276         */
277        ITEM_TRASH,
278
279        /**
280         * A file or folder was recovered from the trash.
281         */
282        ITEM_UNDELETE_VIA_TRASH,
283
284        /**
285         * A collaborator was added to a folder.
286         */
287        COLLAB_ADD_COLLABORATOR,
288
289        /**
290         * A collaborator's role was change in a folder.
291         */
292        COLLAB_ROLE_CHANGE,
293
294        /**
295         * A collaborator was invited to a folder.
296         */
297        COLLAB_INVITE_COLLABORATOR,
298
299        /**
300         * A collaborator was removed from a folder.
301         */
302        COLLAB_REMOVE_COLLABORATOR,
303
304        /**
305         * A folder was marked for sync.
306         */
307        ITEM_SYNC,
308
309        /**
310         * A folder was un-marked for sync.
311         */
312        ITEM_UNSYNC,
313
314        /**
315         * A file or folder was renamed.
316         */
317        ITEM_RENAME,
318
319        /**
320         * A file or folder was enabled for sharing.
321         */
322        ITEM_SHARED_CREATE,
323
324        /**
325         * A file or folder was disabled for sharing.
326         */
327        ITEM_SHARED_UNSHARE,
328
329        /**
330         * A folder was shared.
331         */
332        ITEM_SHARED,
333
334        /**
335         * A previous version of a file was promoted to the current version.
336         */
337        ITEM_MAKE_CURRENT_VERSION,
338
339        /**
340         * A tag was added to a file or folder.
341         */
342        TAG_ITEM_CREATE,
343
344        /**
345         * 2 factor authentication enabled by user.
346         */
347        ENABLE_TWO_FACTOR_AUTH,
348
349        /**
350         * Free user accepts invitation to become a managed user.
351         */
352        MASTER_INVITE_ACCEPT,
353
354        /**
355         * Free user rejects invitation to become a managed user.
356         */
357        MASTER_INVITE_REJECT,
358
359        /**
360         * Granted Box access to account.
361         */
362        ACCESS_GRANTED,
363
364        /**
365         * Revoke Box access to account.
366         */
367        ACCESS_REVOKED,
368
369        /**
370         * A user logged in from a new device.
371         */
372        ADD_LOGIN_ACTIVITY_DEVICE,
373
374        /**
375         * A user session associated with an app was invalidated.
376         */
377        REMOVE_LOGIN_ACTIVITY_DEVICE,
378
379        /**
380         * An admin role changed for a user.
381         */
382        CHANGE_ADMIN_ROLE,
383
384        /**
385         * A user was added to a group. This is an enterprise-only event.
386         */
387        GROUP_ADD_USER,
388
389        /**
390         * A user was created. This is an enterprise-only event.
391         */
392        NEW_USER,
393
394        /**
395         * A group was created. This is an enterprise-only event.
396         */
397        GROUP_CREATION,
398
399        /**
400         * A group was deleted. This is an enterprise-only event.
401         */
402        GROUP_DELETION,
403
404        /**
405         * A user was deleted. This is an enterprise-only event.
406         */
407        DELETE_USER,
408
409        /**
410         * A group was edited. This is an enterprise-only event.
411         */
412        GROUP_EDITED,
413
414        /**
415         * A user was edited. This is an enterprise-only event.
416         */
417        EDIT_USER,
418
419        /**
420         * A group was granted access to a folder. This is an enterprise-only event.
421         */
422        GROUP_ADD_FOLDER,
423
424        /**
425         * A group was granted access to a file. This is an enterprise-only event.
426         */
427        GROUP_ADD_FILE,
428
429        /**
430         * A user was removed from a group. This is an enterprise-only event.
431         */
432        GROUP_REMOVE_USER,
433
434        /**
435         * A group had its access to a folder removed. This is an enterprise-only event.
436         */
437        GROUP_REMOVE_FOLDER,
438
439        /**
440         * A group had its access to a file removed. This is an enterprise-only event.
441         */
442        GROUP_REMOVE_FILE,
443
444        /**
445         * An administrator logged in. This is an enterprise-only event.
446         */
447        ADMIN_LOGIN,
448
449        /**
450         * A device was associated with a user. This is an enterprise-only event.
451         */
452        ADD_DEVICE_ASSOCIATION,
453
454        /**
455         * There was a failed login attempt. This is an enterprise-only event.
456         */
457        FAILED_LOGIN,
458
459        /**
460         * There was a successful login. This is an enterprise-only event.
461         */
462        LOGIN,
463
464        /**
465         * A user's OAuth2 access token was refreshed. This is an enterprise-only event.
466         */
467        USER_AUTHENTICATE_OAUTH2_TOKEN_REFRESH,
468
469        /**
470         * A device was disassociated with a user. This is an enterprise-only event.
471         */
472        REMOVE_DEVICE_ASSOCIATION,
473
474        /**
475         * A user agreed to the terms of service. This is an enterprise-only event.
476         */
477        TERMS_OF_SERVICE_AGREE,
478
479        /**
480         * A user rejected the terms of service. This is an enterprise-only event.
481         */
482        TERMS_OF_SERVICE_REJECT,
483
484        /**
485         * Virus found on a file. Event is only received by enterprises that have opted in to be notified.
486         * This is an enterprise-only event.
487         */
488        FILE_MARKED_MALICIOUS,
489
490        /**
491         * An item was copied. This is an enterprise-only event.
492         */
493        COPY,
494
495        /**
496         * An item was deleted. This is an enterprise-only event.
497         */
498        DELETE,
499
500        /**
501         * An item was downloaded. This is an enterprise-only event.
502         */
503        DOWNLOAD,
504
505        /**
506         * An item was edited. This is an enterprise-only event.
507         */
508        EDIT,
509
510        /**
511         * An item was locked. This is an enterprise-only event.
512         */
513        LOCK,
514
515        /**
516         * An item was moved. This is an enterprise-only event.
517         */
518        MOVE,
519
520        /**
521         * An item was previewed. This is an enterprise-only event.
522         */
523        PREVIEW,
524
525        /**
526         * An item was renamed. This is an enterprise-only event.
527         */
528        RENAME,
529
530        /**
531         * An item was set to be auto-deleted. This is an enterprise-only event.
532         */
533        STORAGE_EXPIRATION,
534
535        /**
536         * An item was undeleted. This is an enterprise-only event.
537         */
538        UNDELETE,
539
540        /**
541         * An item was unlocked. This is an enterprise-only event.
542         */
543        UNLOCK,
544
545        /**
546         * An item was uploaded. This is an enterprise-only event.
547         */
548        UPLOAD,
549
550        /**
551         * An shared link was created for an item. This is an enterprise-only event.
552         */
553        SHARE,
554
555        /**
556         * The shared link for an item was updated. This is an enterprise-only event.
557         */
558        ITEM_SHARED_UPDATE,
559
560        /**
561         * The expiration time for a shared link was extended. This is an enterprise-only event.
562         */
563        UPDATE_SHARE_EXPIRATION,
564
565        /**
566         * The expiration time was set for a shared link. This is an enterprise-only event.
567         */
568        SHARE_EXPIRATION,
569
570        /**
571         * The shared link for an item was REMOVE_DEVICE_ASSOCIATION. This is an enterprise-only event.
572         */
573        UNSHARE,
574
575        /**
576         * A user accepted a collaboration invite. This is an enterprise-only event.
577         */
578        COLLABORATION_ACCEPT,
579
580        /**
581         * A user's collaboration role was changed. This is an enterprise-only event.
582         */
583        COLLABORATION_ROLE_CHANGE,
584
585        /**
586         * The expiration time for a collaboration was extended. This is an enterprise-only event.
587         */
588        UPDATE_COLLABORATION_EXPIRATION,
589
590        /**
591         * A collaboration was removed from a folder. This is an enterprise-only event.
592         */
593        COLLABORATION_REMOVE,
594
595        /**
596         * A user was invited to collaborate on a folder. This is an enterprise-only event.
597         */
598        COLLABORATION_INVITE,
599
600        /**
601         * An expiration time was set for a collaboration. This is an enterprise-only event.
602         */
603        COLLABORATION_EXPIRATION,
604
605        /**
606         * Creation of metadata instance. This is an enterprise-only event.
607         */
608        METADATA_INSTANCE_CREATE,
609
610        /**
611         * Update of metadata instance. This is an enterprise-only event.
612         */
613        METADATA_INSTANCE_UPDATE,
614
615        /**
616         * Deletion of metadata instance. This is an enterprise-only event.
617         */
618        METADATA_INSTANCE_DELETE,
619
620        /**
621         * Content Workflow upload policy violation. This is an enterprise-only event.
622         */
623        CONTENT_WORKFLOW_UPLOAD_POLICY_VIOLATION,
624
625        /**
626         * Edit the permissions on a folder.  This is an enterprise-only-event.
627         */
628        CHANGE_FOLDER_PERMISSION,
629
630        /**
631         * A task assignment is deleted.  This is an enterprise-only event.
632         */
633        TASK_ASSIGNMENT_DELETE,
634
635        /**
636         * Retention is removed.  This is an enterprise-only event.
637         */
638        DATA_RETENTION_REMOVE_RETENTION,
639
640        /**
641         * Retention is created.  This is an enterprise-only event.
642         */
643        DATA_RETENTION_CREATE_RETENTION,
644
645        /**
646         * A retention policy assignment is added.  This is an enterprise-only event.
647         */
648        RETENTION_POLICY_ASSIGNMENT_ADD,
649
650        /**
651         * A legal hold assignment is created.  This is an enterprise-only event.
652         */
653        LEGAL_HOLD_ASSIGNMENT_CREATE,
654
655        /**
656         * A legal hold assignment is deleted.  This is an enterprise-only event.
657         */
658        LEGAL_HOLD_ASSIGNMENT_DELETE,
659
660        /**
661         * A legal hold policy is deleted.  This is an enterprise-only event.
662         */
663        LEGAL_HOLD_POLICY_DELETE,
664
665        /**
666         * There is a sharing policy violation.  This is an enterprise-only event.
667         */
668        CONTENT_WORKFLOW_SHARING_POLICY_VIOLATION,
669
670        /**
671         * An application public key is added.  This is an enterprise-only event.
672         */
673        APPLICATION_PUBLIC_KEY_ADDED,
674
675        /**
676         * An application public key is deleted.  This is an enterprise-only event.
677         */
678        APPLICATION_PUBLIC_KEY_DELETED,
679
680        /**
681         * A content policy is added.  This is an enterprise-only event.
682         */
683        CONTENT_WORKFLOW_POLICY_ADD,
684
685        /**
686         * An automation is added.  This is an enterprise-only event.
687         */
688        CONTENT_WORKFLOW_AUTOMATION_ADD,
689
690        /**
691         * An automation is deleted.  This is an enterprise-only event.
692         */
693        CONTENT_WORKFLOW_AUTOMATION_DELETE,
694
695        /**
696         * A user email alias is confirmed.  This is an enterprise-only event.
697         */
698        EMAIL_ALIAS_CONFIRM,
699
700        /**
701         * A user email alias is removed.  This is an enterprise-only event.
702         */
703        EMAIL_ALIAS_REMOVE,
704
705        /**
706         * A watermark is added to a file.  This is an enterprise-only event.
707         */
708        WATERMARK_LABEL_CREATE,
709
710        /**
711         * A watermark is removed from a file.  This is an enterprise-only event.
712         */
713        WATERMARK_LABEL_DELETE,
714
715        /**
716         * Creation of metadata template instance.  This is an enterprise-only event.
717         */
718        METADATA_TEMPLATE_CREATE,
719
720        /**
721         * Update of metadata template instance.  This is an enterprise-only event.
722         */
723        METADATA_TEMPLATE_UPDATE,
724
725        /**
726         * Deletion of metadata template instance.  This is an enterprise-only event.
727         */
728        METADATA_TEMPLATE_DELETE,
729
730        /**
731         * Item was opened.  This is an enterprise-only event.
732         */
733        ITEM_OPEN,
734
735        /**
736         * Item was modified.  This is an enterprise-only event.
737         */
738        ITEM_MODIFY,
739
740        /**
741         * When a policy set in the Admin console is triggered.  This is an enterprise-only event,
742         */
743        CONTENT_WORKFLOW_ABNORMAL_DOWNLOAD_ACTIVITY,
744
745        /**
746         * Folders were removed from a group in the Admin console.  This is an enterprise-only event.
747         */
748        GROUP_REMOVE_ITEM,
749
750        /**
751         * Folders were added to a group in the Admin console.  This is an enterprise-only event.
752         */
753        GROUP_ADD_ITEM,
754
755        /**
756         * An OAuth2 access token was created for a user.  This is an enterprise-only event.
757         */
758        USER_AUTHENTICATE_OAUTH2_ACCESS_TOKEN_CREATE,
759
760        /**
761         * Event for file tag updates.
762         */
763        CONTENT_ACCESS;
764    }
765}