-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathWebhook.java
More file actions
1122 lines (1088 loc) · 51.5 KB
/
Copy pathWebhook.java
File metadata and controls
1122 lines (1088 loc) · 51.5 KB
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Code generated by GetStream internal OpenAPI code generator. DO NOT EDIT.
package io.getstream;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.getstream.exceptions.StreamException;
import io.getstream.models.ActivityAddedEvent;
import io.getstream.models.ActivityDeletedEvent;
import io.getstream.models.ActivityFeedbackEvent;
import io.getstream.models.ActivityMarkEvent;
import io.getstream.models.ActivityPinnedEvent;
import io.getstream.models.ActivityReactionAddedEvent;
import io.getstream.models.ActivityReactionDeletedEvent;
import io.getstream.models.ActivityReactionUpdatedEvent;
import io.getstream.models.ActivityRemovedFromFeedEvent;
import io.getstream.models.ActivityRestoredEvent;
import io.getstream.models.ActivityUnpinnedEvent;
import io.getstream.models.ActivityUpdatedEvent;
import io.getstream.models.AppealAcceptedEvent;
import io.getstream.models.AppealCreatedEvent;
import io.getstream.models.AppealRejectedEvent;
import io.getstream.models.AsyncBulkImageModerationEvent;
import io.getstream.models.AsyncExportChannelsEvent;
import io.getstream.models.AsyncExportErrorEvent;
import io.getstream.models.AsyncExportModerationLogsEvent;
import io.getstream.models.AsyncExportUsersEvent;
import io.getstream.models.BlockedUserEvent;
import io.getstream.models.BookmarkAddedEvent;
import io.getstream.models.BookmarkDeletedEvent;
import io.getstream.models.BookmarkFolderDeletedEvent;
import io.getstream.models.BookmarkFolderUpdatedEvent;
import io.getstream.models.BookmarkUpdatedEvent;
import io.getstream.models.CallAcceptedEvent;
import io.getstream.models.CallClosedCaptionsFailedEvent;
import io.getstream.models.CallClosedCaptionsStartedEvent;
import io.getstream.models.CallClosedCaptionsStoppedEvent;
import io.getstream.models.CallCreatedEvent;
import io.getstream.models.CallDTMFEvent;
import io.getstream.models.CallDeletedEvent;
import io.getstream.models.CallEndedEvent;
import io.getstream.models.CallFrameRecordingFailedEvent;
import io.getstream.models.CallFrameRecordingFrameReadyEvent;
import io.getstream.models.CallFrameRecordingStartedEvent;
import io.getstream.models.CallFrameRecordingStoppedEvent;
import io.getstream.models.CallHLSBroadcastingFailedEvent;
import io.getstream.models.CallHLSBroadcastingStartedEvent;
import io.getstream.models.CallHLSBroadcastingStoppedEvent;
import io.getstream.models.CallLiveStartedEvent;
import io.getstream.models.CallMemberAddedEvent;
import io.getstream.models.CallMemberRemovedEvent;
import io.getstream.models.CallMemberUpdatedEvent;
import io.getstream.models.CallMemberUpdatedPermissionEvent;
import io.getstream.models.CallMissedEvent;
import io.getstream.models.CallModerationBlurEvent;
import io.getstream.models.CallModerationWarningEvent;
import io.getstream.models.CallNotificationEvent;
import io.getstream.models.CallReactionEvent;
import io.getstream.models.CallRecordingFailedEvent;
import io.getstream.models.CallRecordingReadyEvent;
import io.getstream.models.CallRecordingStartedEvent;
import io.getstream.models.CallRecordingStoppedEvent;
import io.getstream.models.CallRejectedEvent;
import io.getstream.models.CallRingEvent;
import io.getstream.models.CallRtmpBroadcastFailedEvent;
import io.getstream.models.CallRtmpBroadcastStartedEvent;
import io.getstream.models.CallRtmpBroadcastStoppedEvent;
import io.getstream.models.CallSessionEndedEvent;
import io.getstream.models.CallSessionParticipantCountsUpdatedEvent;
import io.getstream.models.CallSessionParticipantJoinedEvent;
import io.getstream.models.CallSessionParticipantLeftEvent;
import io.getstream.models.CallSessionStartedEvent;
import io.getstream.models.CallStatsReportReadyEvent;
import io.getstream.models.CallTranscriptionFailedEvent;
import io.getstream.models.CallTranscriptionReadyEvent;
import io.getstream.models.CallTranscriptionStartedEvent;
import io.getstream.models.CallTranscriptionStoppedEvent;
import io.getstream.models.CallUpdatedEvent;
import io.getstream.models.CallUserFeedbackSubmittedEvent;
import io.getstream.models.CallUserMutedEvent;
import io.getstream.models.CampaignCompletedEvent;
import io.getstream.models.CampaignStartedEvent;
import io.getstream.models.ChannelBatchCompletedEvent;
import io.getstream.models.ChannelBatchStartedEvent;
import io.getstream.models.ChannelCreatedEvent;
import io.getstream.models.ChannelDeletedEvent;
import io.getstream.models.ChannelFrozenEvent;
import io.getstream.models.ChannelHiddenEvent;
import io.getstream.models.ChannelMutedEvent;
import io.getstream.models.ChannelTruncatedEvent;
import io.getstream.models.ChannelUnFrozenEvent;
import io.getstream.models.ChannelUnmutedEvent;
import io.getstream.models.ChannelUpdatedEvent;
import io.getstream.models.ChannelVisibleEvent;
import io.getstream.models.ClosedCaptionEvent;
import io.getstream.models.CommentAddedEvent;
import io.getstream.models.CommentDeletedEvent;
import io.getstream.models.CommentReactionAddedEvent;
import io.getstream.models.CommentReactionDeletedEvent;
import io.getstream.models.CommentReactionUpdatedEvent;
import io.getstream.models.CommentRestoredEvent;
import io.getstream.models.CommentUpdatedEvent;
import io.getstream.models.CustomEvent;
import io.getstream.models.CustomVideoEvent;
import io.getstream.models.FeedCreatedEvent;
import io.getstream.models.FeedDeletedEvent;
import io.getstream.models.FeedGroupChangedEvent;
import io.getstream.models.FeedGroupDeletedEvent;
import io.getstream.models.FeedGroupRestoredEvent;
import io.getstream.models.FeedMemberAddedEvent;
import io.getstream.models.FeedMemberRemovedEvent;
import io.getstream.models.FeedMemberUpdatedEvent;
import io.getstream.models.FeedUpdatedEvent;
import io.getstream.models.FlagUpdatedEvent;
import io.getstream.models.FollowCreatedEvent;
import io.getstream.models.FollowDeletedEvent;
import io.getstream.models.FollowUpdatedEvent;
import io.getstream.models.IngressErrorEvent;
import io.getstream.models.IngressStartedEvent;
import io.getstream.models.IngressStoppedEvent;
import io.getstream.models.KickedUserEvent;
import io.getstream.models.MaxStreakChangedEvent;
import io.getstream.models.MemberAddedEvent;
import io.getstream.models.MemberRemovedEvent;
import io.getstream.models.MemberUpdatedEvent;
import io.getstream.models.MessageDeletedEvent;
import io.getstream.models.MessageFlaggedEvent;
import io.getstream.models.MessageNewEvent;
import io.getstream.models.MessageReadEvent;
import io.getstream.models.MessageUnblockedEvent;
import io.getstream.models.MessageUndeletedEvent;
import io.getstream.models.MessageUpdatedEvent;
import io.getstream.models.ModerationCheckCompletedEvent;
import io.getstream.models.ModerationCustomActionEvent;
import io.getstream.models.ModerationFlaggedEvent;
import io.getstream.models.ModerationImageAnalysisCompleteEvent;
import io.getstream.models.ModerationMarkReviewedEvent;
import io.getstream.models.ModerationRulesTriggeredEvent;
import io.getstream.models.ModerationTextAnalysisCompleteEvent;
import io.getstream.models.NotificationFeedUpdatedEvent;
import io.getstream.models.NotificationMarkUnreadEvent;
import io.getstream.models.NotificationThreadMessageNewEvent;
import io.getstream.models.PendingMessageEvent;
import io.getstream.models.PermissionRequestEvent;
import io.getstream.models.ReactionDeletedEvent;
import io.getstream.models.ReactionNewEvent;
import io.getstream.models.ReactionUpdatedEvent;
import io.getstream.models.ReminderCreatedEvent;
import io.getstream.models.ReminderDeletedEvent;
import io.getstream.models.ReminderNotificationEvent;
import io.getstream.models.ReminderUpdatedEvent;
import io.getstream.models.ReviewQueueItemNewEvent;
import io.getstream.models.ReviewQueueItemUpdatedEvent;
import io.getstream.models.StoriesFeedUpdatedEvent;
import io.getstream.models.ThreadUpdatedEvent;
import io.getstream.models.UnblockedUserEvent;
import io.getstream.models.UpdatedCallPermissionsEvent;
import io.getstream.models.UserBannedEvent;
import io.getstream.models.UserDeactivatedEvent;
import io.getstream.models.UserDeletedEvent;
import io.getstream.models.UserFlaggedEvent;
import io.getstream.models.UserGroupCreatedEvent;
import io.getstream.models.UserGroupDeletedEvent;
import io.getstream.models.UserGroupMemberAddedEvent;
import io.getstream.models.UserGroupMemberRemovedEvent;
import io.getstream.models.UserGroupUpdatedEvent;
import io.getstream.models.UserMessagesDeletedEvent;
import io.getstream.models.UserMutedEvent;
import io.getstream.models.UserReactivatedEvent;
import io.getstream.models.UserUnbannedEvent;
import io.getstream.models.UserUnmutedEvent;
import io.getstream.models.UserUnreadReminderEvent;
import io.getstream.models.UserUpdatedEvent;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.time.OffsetDateTime;
import java.time.format.DateTimeParseException;
import java.util.Base64;
import java.util.zip.GZIPInputStream;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
/** Webhook utilities for Stream webhooks. */
public class Webhook {
private static final String HMAC_SHA256 = "HmacSHA256";
private static final ObjectMapper objectMapper =
new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
// gzipMagic is the two-byte gzip magic prefix (RFC 1952 §2.3.1).
// JSON cannot start with these bytes, so this gives unambiguous detection
// for Stream's always-JSON payloads.
private static final byte[] GZIP_MAGIC = {0x1F, (byte) 0x8B};
/** Webhook event type constants. */
public static class EventType {
public static final String WILDCARD = "*";
public static final String APPEAL_ACCEPTED = "appeal.accepted";
public static final String APPEAL_CREATED = "appeal.created";
public static final String APPEAL_REJECTED = "appeal.rejected";
public static final String CALL_ACCEPTED = "call.accepted";
public static final String CALL_BLOCKED_USER = "call.blocked_user";
public static final String CALL_CLOSED_CAPTION = "call.closed_caption";
public static final String CALL_CLOSED_CAPTIONS_FAILED = "call.closed_captions_failed";
public static final String CALL_CLOSED_CAPTIONS_STARTED = "call.closed_captions_started";
public static final String CALL_CLOSED_CAPTIONS_STOPPED = "call.closed_captions_stopped";
public static final String CALL_CREATED = "call.created";
public static final String CALL_DELETED = "call.deleted";
public static final String CALL_DTMF = "call.dtmf";
public static final String CALL_ENDED = "call.ended";
public static final String CALL_FRAME_RECORDING_FAILED = "call.frame_recording_failed";
public static final String CALL_FRAME_RECORDING_READY = "call.frame_recording_ready";
public static final String CALL_FRAME_RECORDING_STARTED = "call.frame_recording_started";
public static final String CALL_FRAME_RECORDING_STOPPED = "call.frame_recording_stopped";
public static final String CALL_HLS_BROADCASTING_FAILED = "call.hls_broadcasting_failed";
public static final String CALL_HLS_BROADCASTING_STARTED = "call.hls_broadcasting_started";
public static final String CALL_HLS_BROADCASTING_STOPPED = "call.hls_broadcasting_stopped";
public static final String CALL_KICKED_USER = "call.kicked_user";
public static final String CALL_LIVE_STARTED = "call.live_started";
public static final String CALL_MEMBER_ADDED = "call.member_added";
public static final String CALL_MEMBER_REMOVED = "call.member_removed";
public static final String CALL_MEMBER_UPDATED = "call.member_updated";
public static final String CALL_MEMBER_UPDATED_PERMISSION = "call.member_updated_permission";
public static final String CALL_MISSED = "call.missed";
public static final String CALL_MODERATION_BLUR = "call.moderation_blur";
public static final String CALL_MODERATION_WARNING = "call.moderation_warning";
public static final String CALL_NOTIFICATION = "call.notification";
public static final String CALL_PERMISSION_REQUEST = "call.permission_request";
public static final String CALL_PERMISSIONS_UPDATED = "call.permissions_updated";
public static final String CALL_REACTION_NEW = "call.reaction_new";
public static final String CALL_RECORDING_FAILED = "call.recording_failed";
public static final String CALL_RECORDING_READY = "call.recording_ready";
public static final String CALL_RECORDING_STARTED = "call.recording_started";
public static final String CALL_RECORDING_STOPPED = "call.recording_stopped";
public static final String CALL_REJECTED = "call.rejected";
public static final String CALL_RING = "call.ring";
public static final String CALL_RTMP_BROADCAST_FAILED = "call.rtmp_broadcast_failed";
public static final String CALL_RTMP_BROADCAST_STARTED = "call.rtmp_broadcast_started";
public static final String CALL_RTMP_BROADCAST_STOPPED = "call.rtmp_broadcast_stopped";
public static final String CALL_SESSION_ENDED = "call.session_ended";
public static final String CALL_SESSION_PARTICIPANT_COUNT_UPDATED =
"call.session_participant_count_updated";
public static final String CALL_SESSION_PARTICIPANT_JOINED = "call.session_participant_joined";
public static final String CALL_SESSION_PARTICIPANT_LEFT = "call.session_participant_left";
public static final String CALL_SESSION_STARTED = "call.session_started";
public static final String CALL_STATS_REPORT_READY = "call.stats_report_ready";
public static final String CALL_TRANSCRIPTION_FAILED = "call.transcription_failed";
public static final String CALL_TRANSCRIPTION_READY = "call.transcription_ready";
public static final String CALL_TRANSCRIPTION_STARTED = "call.transcription_started";
public static final String CALL_TRANSCRIPTION_STOPPED = "call.transcription_stopped";
public static final String CALL_UNBLOCKED_USER = "call.unblocked_user";
public static final String CALL_UPDATED = "call.updated";
public static final String CALL_USER_FEEDBACK_SUBMITTED = "call.user_feedback_submitted";
public static final String CALL_USER_MUTED = "call.user_muted";
public static final String CAMPAIGN_COMPLETED = "campaign.completed";
public static final String CAMPAIGN_STARTED = "campaign.started";
public static final String CHANNEL_CREATED = "channel.created";
public static final String CHANNEL_DELETED = "channel.deleted";
public static final String CHANNEL_FROZEN = "channel.frozen";
public static final String CHANNEL_HIDDEN = "channel.hidden";
public static final String CHANNEL_MAX_STREAK_CHANGED = "channel.max_streak_changed";
public static final String CHANNEL_MUTED = "channel.muted";
public static final String CHANNEL_TRUNCATED = "channel.truncated";
public static final String CHANNEL_UNFROZEN = "channel.unfrozen";
public static final String CHANNEL_UNMUTED = "channel.unmuted";
public static final String CHANNEL_UPDATED = "channel.updated";
public static final String CHANNEL_VISIBLE = "channel.visible";
public static final String CHANNEL_BATCH_UPDATE_COMPLETED = "channel_batch_update.completed";
public static final String CHANNEL_BATCH_UPDATE_STARTED = "channel_batch_update.started";
public static final String CUSTOM = "custom";
public static final String EXPORT_BULK_IMAGE_MODERATION_ERROR =
"export.bulk_image_moderation.error";
public static final String EXPORT_BULK_IMAGE_MODERATION_SUCCESS =
"export.bulk_image_moderation.success";
public static final String EXPORT_CHANNELS_ERROR = "export.channels.error";
public static final String EXPORT_CHANNELS_SUCCESS = "export.channels.success";
public static final String EXPORT_MODERATION_LOGS_ERROR = "export.moderation_logs.error";
public static final String EXPORT_MODERATION_LOGS_SUCCESS = "export.moderation_logs.success";
public static final String EXPORT_USERS_ERROR = "export.users.error";
public static final String EXPORT_USERS_SUCCESS = "export.users.success";
public static final String FEEDS_ACTIVITY_ADDED = "feeds.activity.added";
public static final String FEEDS_ACTIVITY_DELETED = "feeds.activity.deleted";
public static final String FEEDS_ACTIVITY_FEEDBACK = "feeds.activity.feedback";
public static final String FEEDS_ACTIVITY_MARKED = "feeds.activity.marked";
public static final String FEEDS_ACTIVITY_PINNED = "feeds.activity.pinned";
public static final String FEEDS_ACTIVITY_REACTION_ADDED = "feeds.activity.reaction.added";
public static final String FEEDS_ACTIVITY_REACTION_DELETED = "feeds.activity.reaction.deleted";
public static final String FEEDS_ACTIVITY_REACTION_UPDATED = "feeds.activity.reaction.updated";
public static final String FEEDS_ACTIVITY_REMOVED_FROM_FEED =
"feeds.activity.removed_from_feed";
public static final String FEEDS_ACTIVITY_RESTORED = "feeds.activity.restored";
public static final String FEEDS_ACTIVITY_UNPINNED = "feeds.activity.unpinned";
public static final String FEEDS_ACTIVITY_UPDATED = "feeds.activity.updated";
public static final String FEEDS_BOOKMARK_ADDED = "feeds.bookmark.added";
public static final String FEEDS_BOOKMARK_DELETED = "feeds.bookmark.deleted";
public static final String FEEDS_BOOKMARK_UPDATED = "feeds.bookmark.updated";
public static final String FEEDS_BOOKMARK_FOLDER_DELETED = "feeds.bookmark_folder.deleted";
public static final String FEEDS_BOOKMARK_FOLDER_UPDATED = "feeds.bookmark_folder.updated";
public static final String FEEDS_COMMENT_ADDED = "feeds.comment.added";
public static final String FEEDS_COMMENT_DELETED = "feeds.comment.deleted";
public static final String FEEDS_COMMENT_REACTION_ADDED = "feeds.comment.reaction.added";
public static final String FEEDS_COMMENT_REACTION_DELETED = "feeds.comment.reaction.deleted";
public static final String FEEDS_COMMENT_REACTION_UPDATED = "feeds.comment.reaction.updated";
public static final String FEEDS_COMMENT_RESTORED = "feeds.comment.restored";
public static final String FEEDS_COMMENT_UPDATED = "feeds.comment.updated";
public static final String FEEDS_FEED_CREATED = "feeds.feed.created";
public static final String FEEDS_FEED_DELETED = "feeds.feed.deleted";
public static final String FEEDS_FEED_UPDATED = "feeds.feed.updated";
public static final String FEEDS_FEED_GROUP_CHANGED = "feeds.feed_group.changed";
public static final String FEEDS_FEED_GROUP_DELETED = "feeds.feed_group.deleted";
public static final String FEEDS_FEED_GROUP_RESTORED = "feeds.feed_group.restored";
public static final String FEEDS_FEED_MEMBER_ADDED = "feeds.feed_member.added";
public static final String FEEDS_FEED_MEMBER_REMOVED = "feeds.feed_member.removed";
public static final String FEEDS_FEED_MEMBER_UPDATED = "feeds.feed_member.updated";
public static final String FEEDS_FOLLOW_CREATED = "feeds.follow.created";
public static final String FEEDS_FOLLOW_DELETED = "feeds.follow.deleted";
public static final String FEEDS_FOLLOW_UPDATED = "feeds.follow.updated";
public static final String FEEDS_NOTIFICATION_FEED_UPDATED = "feeds.notification_feed.updated";
public static final String FEEDS_STORIES_FEED_UPDATED = "feeds.stories_feed.updated";
public static final String FLAG_UPDATED = "flag.updated";
public static final String INGRESS_ERROR = "ingress.error";
public static final String INGRESS_STARTED = "ingress.started";
public static final String INGRESS_STOPPED = "ingress.stopped";
public static final String MEMBER_ADDED = "member.added";
public static final String MEMBER_REMOVED = "member.removed";
public static final String MEMBER_UPDATED = "member.updated";
public static final String MESSAGE_DELETED = "message.deleted";
public static final String MESSAGE_FLAGGED = "message.flagged";
public static final String MESSAGE_NEW = "message.new";
public static final String MESSAGE_PENDING = "message.pending";
public static final String MESSAGE_READ = "message.read";
public static final String MESSAGE_UNBLOCKED = "message.unblocked";
public static final String MESSAGE_UNDELETED = "message.undeleted";
public static final String MESSAGE_UPDATED = "message.updated";
public static final String MODERATION_CUSTOM_ACTION = "moderation.custom_action";
public static final String MODERATION_FLAGGED = "moderation.flagged";
public static final String MODERATION_IMAGE_ANALYSIS_COMPLETE =
"moderation.image_analysis.complete";
public static final String MODERATION_MARK_REVIEWED = "moderation.mark_reviewed";
public static final String MODERATION_TEXT_ANALYSIS_COMPLETE =
"moderation.text_analysis.complete";
public static final String MODERATION_CHECK_COMPLETED = "moderation_check.completed";
public static final String MODERATION_RULE_TRIGGERED = "moderation_rule.triggered";
public static final String NOTIFICATION_MARK_UNREAD = "notification.mark_unread";
public static final String NOTIFICATION_REMINDER_DUE = "notification.reminder_due";
public static final String NOTIFICATION_THREAD_MESSAGE_NEW = "notification.thread_message_new";
public static final String REACTION_DELETED = "reaction.deleted";
public static final String REACTION_NEW = "reaction.new";
public static final String REACTION_UPDATED = "reaction.updated";
public static final String REMINDER_CREATED = "reminder.created";
public static final String REMINDER_DELETED = "reminder.deleted";
public static final String REMINDER_UPDATED = "reminder.updated";
public static final String REVIEW_QUEUE_ITEM_NEW = "review_queue_item.new";
public static final String REVIEW_QUEUE_ITEM_UPDATED = "review_queue_item.updated";
public static final String THREAD_UPDATED = "thread.updated";
public static final String USER_BANNED = "user.banned";
public static final String USER_DEACTIVATED = "user.deactivated";
public static final String USER_DELETED = "user.deleted";
public static final String USER_FLAGGED = "user.flagged";
public static final String USER_MESSAGES_DELETED = "user.messages.deleted";
public static final String USER_MUTED = "user.muted";
public static final String USER_REACTIVATED = "user.reactivated";
public static final String USER_UNBANNED = "user.unbanned";
public static final String USER_UNMUTED = "user.unmuted";
public static final String USER_UNREAD_MESSAGE_REMINDER = "user.unread_message_reminder";
public static final String USER_UPDATED = "user.updated";
public static final String USER_GROUP_CREATED = "user_group.created";
public static final String USER_GROUP_DELETED = "user_group.deleted";
public static final String USER_GROUP_MEMBER_ADDED = "user_group.member_added";
public static final String USER_GROUP_MEMBER_REMOVED = "user_group.member_removed";
public static final String USER_GROUP_UPDATED = "user_group.updated";
}
/**
* Extract the event type from a raw webhook payload.
*
* @param rawEvent The raw webhook payload as a byte array
* @return The event type string (e.g., "message.new"), or null if not found
*/
public static String getEventType(byte[] rawEvent) {
try {
JsonNode node = objectMapper.readTree(rawEvent);
JsonNode typeNode = node.get("type");
return typeNode != null ? typeNode.asText() : null;
} catch (IOException e) {
return null;
}
}
/**
* Extract the event type from a raw webhook payload.
*
* @param rawEvent The raw webhook payload as a string
* @return The event type string (e.g., "message.new"), or null if not found
*/
public static String getEventType(String rawEvent) {
return getEventType(rawEvent.getBytes(StandardCharsets.UTF_8));
}
/**
* Deserialize a raw webhook payload into a typed event object.
*
* @param rawEvent The raw webhook payload as a byte array
* @return A typed event object corresponding to the event type
* @throws InvalidWebhookError if the event type is unknown or deserialization fails
*/
public static Object parseWebhookEvent(byte[] rawEvent) throws InvalidWebhookError {
String eventType = extractEventType(rawEvent);
Class<?> eventClass = getEventClass(eventType);
if (eventClass == null) {
throw new InvalidWebhookError(
InvalidWebhookError.INVALID_JSON + ": Unknown webhook event type: " + eventType);
}
try {
return objectMapper.readValue(rawEvent, eventClass);
} catch (IOException e) {
throw new InvalidWebhookError(
InvalidWebhookError.INVALID_JSON
+ ": Failed to deserialize webhook event: "
+ e.getMessage(),
e);
}
}
private static String extractEventType(byte[] rawEvent) throws InvalidWebhookError {
JsonNode node;
try {
node = objectMapper.readTree(rawEvent);
} catch (IOException e) {
throw new InvalidWebhookError(
InvalidWebhookError.INVALID_JSON
+ ": Webhook payload is not valid JSON: "
+ e.getMessage(),
e);
}
JsonNode typeNode = node.get("type");
if (typeNode == null || typeNode.asText().isEmpty()) {
throw new InvalidWebhookError(
InvalidWebhookError.INVALID_JSON + ": Webhook payload missing 'type' field");
}
return typeNode.asText();
}
/**
* Deserialize a raw webhook payload into a typed event object.
*
* @param rawEvent The raw webhook payload as a string
* @return A typed event object corresponding to the event type
* @throws InvalidWebhookError if the event type is unknown or deserialization fails
*/
public static Object parseWebhookEvent(String rawEvent) throws InvalidWebhookError {
return parseWebhookEvent(rawEvent.getBytes(StandardCharsets.UTF_8));
}
/**
* Map an event type discriminator to its generated event class. Returns null when the
* discriminator is not recognized; callers decide whether to throw (parseWebhookEvent) or fall
* back to UnknownEvent (parseEvent).
*/
private static Class<?> getEventClass(String eventType) {
switch (eventType) {
case "*":
return CustomEvent.class;
case "appeal.accepted":
return AppealAcceptedEvent.class;
case "appeal.created":
return AppealCreatedEvent.class;
case "appeal.rejected":
return AppealRejectedEvent.class;
case "call.accepted":
return CallAcceptedEvent.class;
case "call.blocked_user":
return BlockedUserEvent.class;
case "call.closed_caption":
return ClosedCaptionEvent.class;
case "call.closed_captions_failed":
return CallClosedCaptionsFailedEvent.class;
case "call.closed_captions_started":
return CallClosedCaptionsStartedEvent.class;
case "call.closed_captions_stopped":
return CallClosedCaptionsStoppedEvent.class;
case "call.created":
return CallCreatedEvent.class;
case "call.deleted":
return CallDeletedEvent.class;
case "call.dtmf":
return CallDTMFEvent.class;
case "call.ended":
return CallEndedEvent.class;
case "call.frame_recording_failed":
return CallFrameRecordingFailedEvent.class;
case "call.frame_recording_ready":
return CallFrameRecordingFrameReadyEvent.class;
case "call.frame_recording_started":
return CallFrameRecordingStartedEvent.class;
case "call.frame_recording_stopped":
return CallFrameRecordingStoppedEvent.class;
case "call.hls_broadcasting_failed":
return CallHLSBroadcastingFailedEvent.class;
case "call.hls_broadcasting_started":
return CallHLSBroadcastingStartedEvent.class;
case "call.hls_broadcasting_stopped":
return CallHLSBroadcastingStoppedEvent.class;
case "call.kicked_user":
return KickedUserEvent.class;
case "call.live_started":
return CallLiveStartedEvent.class;
case "call.member_added":
return CallMemberAddedEvent.class;
case "call.member_removed":
return CallMemberRemovedEvent.class;
case "call.member_updated":
return CallMemberUpdatedEvent.class;
case "call.member_updated_permission":
return CallMemberUpdatedPermissionEvent.class;
case "call.missed":
return CallMissedEvent.class;
case "call.moderation_blur":
return CallModerationBlurEvent.class;
case "call.moderation_warning":
return CallModerationWarningEvent.class;
case "call.notification":
return CallNotificationEvent.class;
case "call.permission_request":
return PermissionRequestEvent.class;
case "call.permissions_updated":
return UpdatedCallPermissionsEvent.class;
case "call.reaction_new":
return CallReactionEvent.class;
case "call.recording_failed":
return CallRecordingFailedEvent.class;
case "call.recording_ready":
return CallRecordingReadyEvent.class;
case "call.recording_started":
return CallRecordingStartedEvent.class;
case "call.recording_stopped":
return CallRecordingStoppedEvent.class;
case "call.rejected":
return CallRejectedEvent.class;
case "call.ring":
return CallRingEvent.class;
case "call.rtmp_broadcast_failed":
return CallRtmpBroadcastFailedEvent.class;
case "call.rtmp_broadcast_started":
return CallRtmpBroadcastStartedEvent.class;
case "call.rtmp_broadcast_stopped":
return CallRtmpBroadcastStoppedEvent.class;
case "call.session_ended":
return CallSessionEndedEvent.class;
case "call.session_participant_count_updated":
return CallSessionParticipantCountsUpdatedEvent.class;
case "call.session_participant_joined":
return CallSessionParticipantJoinedEvent.class;
case "call.session_participant_left":
return CallSessionParticipantLeftEvent.class;
case "call.session_started":
return CallSessionStartedEvent.class;
case "call.stats_report_ready":
return CallStatsReportReadyEvent.class;
case "call.transcription_failed":
return CallTranscriptionFailedEvent.class;
case "call.transcription_ready":
return CallTranscriptionReadyEvent.class;
case "call.transcription_started":
return CallTranscriptionStartedEvent.class;
case "call.transcription_stopped":
return CallTranscriptionStoppedEvent.class;
case "call.unblocked_user":
return UnblockedUserEvent.class;
case "call.updated":
return CallUpdatedEvent.class;
case "call.user_feedback_submitted":
return CallUserFeedbackSubmittedEvent.class;
case "call.user_muted":
return CallUserMutedEvent.class;
case "campaign.completed":
return CampaignCompletedEvent.class;
case "campaign.started":
return CampaignStartedEvent.class;
case "channel.created":
return ChannelCreatedEvent.class;
case "channel.deleted":
return ChannelDeletedEvent.class;
case "channel.frozen":
return ChannelFrozenEvent.class;
case "channel.hidden":
return ChannelHiddenEvent.class;
case "channel.max_streak_changed":
return MaxStreakChangedEvent.class;
case "channel.muted":
return ChannelMutedEvent.class;
case "channel.truncated":
return ChannelTruncatedEvent.class;
case "channel.unfrozen":
return ChannelUnFrozenEvent.class;
case "channel.unmuted":
return ChannelUnmutedEvent.class;
case "channel.updated":
return ChannelUpdatedEvent.class;
case "channel.visible":
return ChannelVisibleEvent.class;
case "channel_batch_update.completed":
return ChannelBatchCompletedEvent.class;
case "channel_batch_update.started":
return ChannelBatchStartedEvent.class;
case "custom":
return CustomVideoEvent.class;
case "export.bulk_image_moderation.error":
return AsyncExportErrorEvent.class;
case "export.bulk_image_moderation.success":
return AsyncBulkImageModerationEvent.class;
case "export.channels.error":
return AsyncExportErrorEvent.class;
case "export.channels.success":
return AsyncExportChannelsEvent.class;
case "export.moderation_logs.error":
return AsyncExportErrorEvent.class;
case "export.moderation_logs.success":
return AsyncExportModerationLogsEvent.class;
case "export.users.error":
return AsyncExportErrorEvent.class;
case "export.users.success":
return AsyncExportUsersEvent.class;
case "feeds.activity.added":
return ActivityAddedEvent.class;
case "feeds.activity.deleted":
return ActivityDeletedEvent.class;
case "feeds.activity.feedback":
return ActivityFeedbackEvent.class;
case "feeds.activity.marked":
return ActivityMarkEvent.class;
case "feeds.activity.pinned":
return ActivityPinnedEvent.class;
case "feeds.activity.reaction.added":
return ActivityReactionAddedEvent.class;
case "feeds.activity.reaction.deleted":
return ActivityReactionDeletedEvent.class;
case "feeds.activity.reaction.updated":
return ActivityReactionUpdatedEvent.class;
case "feeds.activity.removed_from_feed":
return ActivityRemovedFromFeedEvent.class;
case "feeds.activity.restored":
return ActivityRestoredEvent.class;
case "feeds.activity.unpinned":
return ActivityUnpinnedEvent.class;
case "feeds.activity.updated":
return ActivityUpdatedEvent.class;
case "feeds.bookmark.added":
return BookmarkAddedEvent.class;
case "feeds.bookmark.deleted":
return BookmarkDeletedEvent.class;
case "feeds.bookmark.updated":
return BookmarkUpdatedEvent.class;
case "feeds.bookmark_folder.deleted":
return BookmarkFolderDeletedEvent.class;
case "feeds.bookmark_folder.updated":
return BookmarkFolderUpdatedEvent.class;
case "feeds.comment.added":
return CommentAddedEvent.class;
case "feeds.comment.deleted":
return CommentDeletedEvent.class;
case "feeds.comment.reaction.added":
return CommentReactionAddedEvent.class;
case "feeds.comment.reaction.deleted":
return CommentReactionDeletedEvent.class;
case "feeds.comment.reaction.updated":
return CommentReactionUpdatedEvent.class;
case "feeds.comment.restored":
return CommentRestoredEvent.class;
case "feeds.comment.updated":
return CommentUpdatedEvent.class;
case "feeds.feed.created":
return FeedCreatedEvent.class;
case "feeds.feed.deleted":
return FeedDeletedEvent.class;
case "feeds.feed.updated":
return FeedUpdatedEvent.class;
case "feeds.feed_group.changed":
return FeedGroupChangedEvent.class;
case "feeds.feed_group.deleted":
return FeedGroupDeletedEvent.class;
case "feeds.feed_group.restored":
return FeedGroupRestoredEvent.class;
case "feeds.feed_member.added":
return FeedMemberAddedEvent.class;
case "feeds.feed_member.removed":
return FeedMemberRemovedEvent.class;
case "feeds.feed_member.updated":
return FeedMemberUpdatedEvent.class;
case "feeds.follow.created":
return FollowCreatedEvent.class;
case "feeds.follow.deleted":
return FollowDeletedEvent.class;
case "feeds.follow.updated":
return FollowUpdatedEvent.class;
case "feeds.notification_feed.updated":
return NotificationFeedUpdatedEvent.class;
case "feeds.stories_feed.updated":
return StoriesFeedUpdatedEvent.class;
case "flag.updated":
return FlagUpdatedEvent.class;
case "ingress.error":
return IngressErrorEvent.class;
case "ingress.started":
return IngressStartedEvent.class;
case "ingress.stopped":
return IngressStoppedEvent.class;
case "member.added":
return MemberAddedEvent.class;
case "member.removed":
return MemberRemovedEvent.class;
case "member.updated":
return MemberUpdatedEvent.class;
case "message.deleted":
return MessageDeletedEvent.class;
case "message.flagged":
return MessageFlaggedEvent.class;
case "message.new":
return MessageNewEvent.class;
case "message.pending":
return PendingMessageEvent.class;
case "message.read":
return MessageReadEvent.class;
case "message.unblocked":
return MessageUnblockedEvent.class;
case "message.undeleted":
return MessageUndeletedEvent.class;
case "message.updated":
return MessageUpdatedEvent.class;
case "moderation.custom_action":
return ModerationCustomActionEvent.class;
case "moderation.flagged":
return ModerationFlaggedEvent.class;
case "moderation.image_analysis.complete":
return ModerationImageAnalysisCompleteEvent.class;
case "moderation.mark_reviewed":
return ModerationMarkReviewedEvent.class;
case "moderation.text_analysis.complete":
return ModerationTextAnalysisCompleteEvent.class;
case "moderation_check.completed":
return ModerationCheckCompletedEvent.class;
case "moderation_rule.triggered":
return ModerationRulesTriggeredEvent.class;
case "notification.mark_unread":
return NotificationMarkUnreadEvent.class;
case "notification.reminder_due":
return ReminderNotificationEvent.class;
case "notification.thread_message_new":
return NotificationThreadMessageNewEvent.class;
case "reaction.deleted":
return ReactionDeletedEvent.class;
case "reaction.new":
return ReactionNewEvent.class;
case "reaction.updated":
return ReactionUpdatedEvent.class;
case "reminder.created":
return ReminderCreatedEvent.class;
case "reminder.deleted":
return ReminderDeletedEvent.class;
case "reminder.updated":
return ReminderUpdatedEvent.class;
case "review_queue_item.new":
return ReviewQueueItemNewEvent.class;
case "review_queue_item.updated":
return ReviewQueueItemUpdatedEvent.class;
case "thread.updated":
return ThreadUpdatedEvent.class;
case "user.banned":
return UserBannedEvent.class;
case "user.deactivated":
return UserDeactivatedEvent.class;
case "user.deleted":
return UserDeletedEvent.class;
case "user.flagged":
return UserFlaggedEvent.class;
case "user.messages.deleted":
return UserMessagesDeletedEvent.class;
case "user.muted":
return UserMutedEvent.class;
case "user.reactivated":
return UserReactivatedEvent.class;
case "user.unbanned":
return UserUnbannedEvent.class;
case "user.unmuted":
return UserUnmutedEvent.class;
case "user.unread_message_reminder":
return UserUnreadReminderEvent.class;
case "user.updated":
return UserUpdatedEvent.class;
case "user_group.created":
return UserGroupCreatedEvent.class;
case "user_group.deleted":
return UserGroupDeletedEvent.class;
case "user_group.member_added":
return UserGroupMemberAddedEvent.class;
case "user_group.member_removed":
return UserGroupMemberRemovedEvent.class;
case "user_group.updated":
return UserGroupUpdatedEvent.class;
default:
return null;
}
}
/**
* Thrown for every webhook handling failure: signature mismatch, invalid JSON, missing/non-string
* {@code type} field, gzip-prefixed body that fails to decompress, invalid base64 in a queue
* body, or a malformed SNS envelope.
*
* <p>Catch this single class for any webhook problem; filter on the message text or against the
* failure-mode constants below to differentiate.
*/
public static class InvalidWebhookError extends StreamException {
public static final String SIGNATURE_MISMATCH = "signature mismatch";
public static final String INVALID_BASE64 = "invalid base64 encoding";
public static final String GZIP_FAILED = "gzip decompression failed";
public static final String INVALID_JSON = "invalid JSON payload";
public InvalidWebhookError(String message) {
super(message, (Throwable) null);
}
public InvalidWebhookError(String message, Throwable cause) {
super(message, cause);
}
}
/**
* @deprecated Renamed to {@link InvalidWebhookError}. Will be removed in the next minor release.
* Kept as a subclass so existing {@code catch (WebhookException e)} clauses continue to
* compile and catch the new class.
*/
@Deprecated
public static class WebhookException extends InvalidWebhookError {
public WebhookException(String message) {
super(message);
}
public WebhookException(String message, Throwable cause) {
super(message, cause);
}
}
/**
* Returned by {@link #parseEvent(byte[])} when the type discriminator is well-formed but unknown
* to this SDK version. Forward-compat surface for new event types.
*
* <p>To handle unknown events, check the result type with {@code instanceof UnknownEvent} after
* calling {@code parseEvent}.
*/
public static class UnknownEvent {
private final String type;
private final OffsetDateTime createdAt;
private final JsonNode raw;
public UnknownEvent(String type, OffsetDateTime createdAt, JsonNode raw) {
this.type = type;
this.createdAt = createdAt;
this.raw = raw;
}
/** The unrecognized discriminator value. */
public String getType() {
return type;
}
/** Parsed timestamp from the envelope, or null if the field was missing or unparseable. */
public OffsetDateTime getCreatedAt() {
return createdAt;
}
/** The full parsed JSON payload, for inspection. */
public JsonNode getRaw() {
return raw;
}
}
/**
* Verify the HMAC-SHA256 signature of a webhook payload.
*
* @param body The raw request body as a byte array
* @param signature The signature from the X-Signature header
* @param secret Your webhook secret (found in the Stream Dashboard)
* @return true if the signature is valid, false otherwise
*/
public static boolean verifySignature(byte[] body, String signature, String secret) {
try {
Mac mac = Mac.getInstance(HMAC_SHA256);
SecretKeySpec secretKey =
new SecretKeySpec(secret.getBytes(StandardCharsets.UTF_8), HMAC_SHA256);
mac.init(secretKey);
byte[] hash = mac.doFinal(body);
String expected = bytesToHex(hash);
return MessageDigest.isEqual(
signature.getBytes(StandardCharsets.UTF_8), expected.getBytes(StandardCharsets.UTF_8));
} catch (NoSuchAlgorithmException | InvalidKeyException e) {
return false;
}
}
/**
* Verify the HMAC-SHA256 signature of a webhook payload.
*
* @param body The raw request body as a string
* @param signature The signature from the X-Signature header
* @param secret Your webhook secret (found in the Stream Dashboard)
* @return true if the signature is valid, false otherwise
*/
public static boolean verifySignature(String body, String signature, String secret) {
return verifySignature(body.getBytes(StandardCharsets.UTF_8), signature, secret);
}
/**
* Decompress the body if it is gzip-prefixed, otherwise return it as-is.
*
* <p>Detection uses the first two bytes (0x1F 0x8B). This is reliable because Stream webhook
* bodies are always JSON, and JSON cannot start with 0x1F.
*
* @param body the raw request body, possibly gzip-compressed
* @return the uncompressed bytes (or {@code body} unchanged if no gzip prefix)
* @throws InvalidWebhookError if the body has the gzip magic prefix but isn't a valid gzip stream
*/
public static byte[] gunzipPayload(byte[] body) throws InvalidWebhookError {
if (body == null || body.length < 2 || body[0] != GZIP_MAGIC[0] || body[1] != GZIP_MAGIC[1]) {
return body == null ? new byte[0] : body;
}
try (ByteArrayInputStream bin = new ByteArrayInputStream(body);
GZIPInputStream gz = new GZIPInputStream(bin);
ByteArrayOutputStream out = new ByteArrayOutputStream()) {
byte[] buf = new byte[4096];
int n;
while ((n = gz.read(buf)) != -1) {
out.write(buf, 0, n);
}
return out.toByteArray();
} catch (IOException e) {
throw new InvalidWebhookError(InvalidWebhookError.GZIP_FAILED + ": " + e.getMessage(), e);
}
}
/**
* Decode an SQS Message Body: try base64 first, fall back to raw bytes if base64 fails, then
* gunzip if gzip-prefixed.
*
* <p>Wire format (per CHA-3071): SQS bodies are raw JSON when {@code
* enable_hook_payload_compression} is off (today's default for all existing apps), and
* base64(gzip(json)) when it's on. This helper handles both: raw JSON starts with '{' which is
* not valid base64, so the base64 decode fails and we fall through to raw bytes, then {@link
* #gunzipPayload(byte[])}'s magic-byte detection decides whether to decompress.
*
* <p>{@link #parseSqs(String)} sits on top of this and works transparently for both wire formats:
* no caller code change, no flag, no header.
*
* @throws InvalidWebhookError if gzip decompression fails (only when input has gzip magic prefix)
*/
public static byte[] decodeSqsPayload(String messageBody) throws InvalidWebhookError {
if (messageBody == null) {
throw new InvalidWebhookError(
InvalidWebhookError.INVALID_JSON + ": messageBody must not be null");
}
byte[] decoded;
try {
decoded = Base64.getDecoder().decode(messageBody);
} catch (IllegalArgumentException e) {
// Not base64, so treat input as raw bytes (uncompressed wire format).
decoded = messageBody.getBytes(StandardCharsets.UTF_8);
}
return gunzipPayload(decoded);
}
/**
* Return the inner {@code Message} field when {@code body} is a standard SNS notification
* envelope JSON; otherwise return {@code body} unchanged so a pre-extracted Message string flows
* through.
*
* <p>Heuristic: try to JSON-parse the input. If it yields an object with a string {@code Message}
* field, that's the envelope shape; return the Message. Otherwise the input is presumed to BE the
* pre-extracted Message (base64-encoded bytes are not valid JSON, so this falls through cleanly).
*/
private static String unwrapSnsNotificationBody(String body) {
try {
JsonNode env = objectMapper.readTree(body);
JsonNode msg = env != null ? env.get("Message") : null;
if (msg != null && msg.isTextual()) {
return msg.asText();
}
} catch (IOException e) {
// not JSON, so fall through to treating body as a bare Message string
}
return body;
}
/**
* Decode an SNS notification body. Accepts either: