Class Webhook
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classWebhook event type constants.static classThrown for every webhook handling failure: signature mismatch, invalid JSON, missing/non-stringtypefield, gzip-prefixed body that fails to decompress, invalid base64 in a queue body, or a malformed SNS envelope.static classReturned byparseEvent(byte[])when the type discriminator is well-formed but unknown to this SDK version.static classDeprecated. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic byte[]decodeSnsPayload(String notificationBody) Decode an SNS notification body.static byte[]decodeSqsPayload(String messageBody) Decode an SQS Message Body: try base64 first, fall back to raw bytes if base64 fails, then gunzip if gzip-prefixed.static StringgetEventType(byte[] rawEvent) Extract the event type from a raw webhook payload.static StringgetEventType(String rawEvent) Extract the event type from a raw webhook payload.static byte[]gunzipPayload(byte[] body) Decompress the body if it is gzip-prefixed, otherwise return it as-is.static ObjectparseEvent(byte[] payload) Parse a webhook payload and return the typed event for known discriminators orWebhook.UnknownEventfor well-formed-but-unknown ones.static ObjectSNS composite: parse SNS envelope then base64-decode then gunzip then parse.static ObjectSQS composite: base64-decode then gunzip (if gzip-prefixed) then parse.static ObjectparseWebhookEvent(byte[] rawEvent) Deserialize a raw webhook payload into a typed event object.static ObjectparseWebhookEvent(String rawEvent) Deserialize a raw webhook payload into a typed event object.static ObjectverifyAndParseWebhook(byte[] body, String signature, String secret) HTTP composite: gunzip (if gzip-prefixed) then verify HMAC-SHA256 then parse.static booleanverifySignature(byte[] body, String signature, String secret) Verify the HMAC-SHA256 signature of a webhook payload.static booleanverifySignature(String body, String signature, String secret) Verify the HMAC-SHA256 signature of a webhook payload.
-
Constructor Details
-
Webhook
public Webhook()
-
-
Method Details
-
getEventType
Extract the event type from a raw webhook payload.- Parameters:
rawEvent- The raw webhook payload as a byte array- Returns:
- The event type string (e.g., "message.new"), or null if not found
-
getEventType
Extract the event type from a raw webhook payload.- Parameters:
rawEvent- The raw webhook payload as a string- Returns:
- The event type string (e.g., "message.new"), or null if not found
-
parseWebhookEvent
Deserialize a raw webhook payload into a typed event object.- Parameters:
rawEvent- The raw webhook payload as a byte array- Returns:
- A typed event object corresponding to the event type
- Throws:
Webhook.InvalidWebhookError- if the event type is unknown or deserialization fails
-
parseWebhookEvent
Deserialize a raw webhook payload into a typed event object.- Parameters:
rawEvent- The raw webhook payload as a string- Returns:
- A typed event object corresponding to the event type
- Throws:
Webhook.InvalidWebhookError- if the event type is unknown or deserialization fails
-
verifySignature
Verify the HMAC-SHA256 signature of a webhook payload.- Parameters:
body- The raw request body as a byte arraysignature- The signature from the X-Signature headersecret- Your webhook secret (found in the Stream Dashboard)- Returns:
- true if the signature is valid, false otherwise
-
verifySignature
Verify the HMAC-SHA256 signature of a webhook payload.- Parameters:
body- The raw request body as a stringsignature- The signature from the X-Signature headersecret- Your webhook secret (found in the Stream Dashboard)- Returns:
- true if the signature is valid, false otherwise
-
gunzipPayload
Decompress the body if it is gzip-prefixed, otherwise return it as-is.Detection uses the first two bytes (0x1F 0x8B). This is reliable because Stream webhook bodies are always JSON, and JSON cannot start with 0x1F.
- Parameters:
body- the raw request body, possibly gzip-compressed- Returns:
- the uncompressed bytes (or
bodyunchanged if no gzip prefix) - Throws:
Webhook.InvalidWebhookError- if the body has the gzip magic prefix but isn't a valid gzip stream
-
decodeSqsPayload
Decode an SQS Message Body: try base64 first, fall back to raw bytes if base64 fails, then gunzip if gzip-prefixed.Wire format (per CHA-3071): SQS bodies are raw JSON when
enable_hook_payload_compressionis 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, thengunzipPayload(byte[])'s magic-byte detection decides whether to decompress.parseSqs(String)sits on top of this and works transparently for both wire formats: no caller code change, no flag, no header.- Throws:
Webhook.InvalidWebhookError- if gzip decompression fails (only when input has gzip magic prefix)
-
decodeSnsPayload
Decode an SNS notification body. Accepts either:- a full SNS HTTP notification envelope JSON (
{"Type":"Notification","Message":"<base64>",...}), or - a pre-extracted Message string (forwarded-through-SQS path).
decodeSqsPayload(String).- Throws:
Webhook.InvalidWebhookError
- a full SNS HTTP notification envelope JSON (
-
parseEvent
Parse a webhook payload and return the typed event for known discriminators orWebhook.UnknownEventfor well-formed-but-unknown ones.Distinct from
parseWebhookEvent(byte[]):parseEventreturns anUnknownEventon unrecognized discriminators (forward-compat);parseWebhookEventthrows.- Returns:
- the typed event (a generated event class) or an
Webhook.UnknownEventinstance - Throws:
Webhook.InvalidWebhookError- for invalid JSON, missing/non-string type field, or any deserialization failure on a known type
-
verifyAndParseWebhook
public static Object verifyAndParseWebhook(byte[] body, String signature, String secret) throws Webhook.InvalidWebhookError HTTP composite: gunzip (if gzip-prefixed) then verify HMAC-SHA256 then parse.The signature header is X-Signature. The signature is HMAC-SHA256 of the uncompressed JSON body, hex-encoded. Magic-byte detection means callers can pass either the raw HTTP body or already-decompressed bytes; both work.
- Throws:
Webhook.InvalidWebhookError- for every failure mode: signature mismatch, gunzip failure, JSON parse, type dispatch, or event deserialization failure. Filter on the message text (or the failure-mode constants onWebhook.InvalidWebhookError) to differentiate modes.
-
parseSqs
SQS composite: base64-decode then gunzip (if gzip-prefixed) then parse.The backend emits no signature attribute on SQS messages today; this helper therefore performs no signature verification. If a signed variant is added later, it'll be a separate function rather than retrofitting this signature.
- Throws:
Webhook.InvalidWebhookError
-
parseSns
SNS composite: parse SNS envelope then base64-decode then gunzip then parse. Same no-signature posture asparseSqs(String).- Throws:
Webhook.InvalidWebhookError
-
Webhook.InvalidWebhookError.