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