- java.lang.Object
-
- co.tryterra.terraclient.WebhookHandlerUtility
-
public class WebhookHandlerUtility extends Object
Class which provides various utilities to assist in handling webhook payloads received from Terra.
-
-
Constructor Summary
Constructors Constructor Description WebhookHandlerUtility(String secret)
Constructor for this class
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description WebhookHandlerUtility
addDefaultEventHandler(Consumer<TerraWebhookPayload> consumer)
Registers a handler method that will be called if no other handler method can be resolved for a received event type.WebhookHandlerUtility
addEventHandler(String eventName, Consumer<TerraWebhookPayload> consumer)
Registers a handler method for the given event type.void
parseAndDispatchWebhookPayload(String rawPayload)
Parses the raw payload sent with a webhook request into aTerraWebhookPayload
and then dispatches it to the correct handler method for its event type.@Nullable TerraWebhookPayload
parseWebhookPayload(String rawPayload)
Parses the raw payload sent with a webhook request into aTerraWebhookPayload
.boolean
verifySignature(String signatureHeader, String requestBody)
Verify the signature of the incoming webhook to ensure that it was sent by Terra.
-
-
-
Constructor Detail
-
WebhookHandlerUtility
public WebhookHandlerUtility(String secret)
Constructor for this class- Parameters:
secret
- the value of your webhook signing secret - found on the developer dashboard
-
-
Method Detail
-
verifySignature
public boolean verifySignature(String signatureHeader, String requestBody)
Verify the signature of the incoming webhook to ensure that it was sent by Terra.- Parameters:
signatureHeader
- the value of theterra-signature
header sent with the webhook requestrequestBody
- the raw string request body sent with the webhook request- Returns:
- boolean indicating whether the signature could be verified successfully
- Throws:
IllegalStateException
- if your jdk installation is missing the HmacSHA256 algorithmIllegalStateException
- if your secret key is invalid
-
parseWebhookPayload
@Nullable public @Nullable TerraWebhookPayload parseWebhookPayload(String rawPayload)
Parses the raw payload sent with a webhook request into aTerraWebhookPayload
.- Parameters:
rawPayload
- the raw payload to parse- Returns:
- the created
TerraWebhookPayload
, ornull
if the payload was malformed
-
parseAndDispatchWebhookPayload
public void parseAndDispatchWebhookPayload(String rawPayload)
Parses the raw payload sent with a webhook request into aTerraWebhookPayload
and then dispatches it to the correct handler method for its event type. If no handler exists for the event type then it will be dispatched to the default handler, or discarded if no default handler exists.- Parameters:
rawPayload
- the raw string payload to parse and dispatch to a handler- See Also:
addEventHandler(String, Consumer)
,addDefaultEventHandler(Consumer)
-
addEventHandler
public WebhookHandlerUtility addEventHandler(String eventName, Consumer<TerraWebhookPayload> consumer)
Registers a handler method for the given event type. This will be called with an instance ofTerraWebhookPayload
when an event of that type is received byparseAndDispatchWebhookPayload(String)
.- Parameters:
eventName
- the event to register the handler method forconsumer
- the handler method to call when the event is received- Returns:
- this instance, for method chaining
-
addDefaultEventHandler
public WebhookHandlerUtility addDefaultEventHandler(Consumer<TerraWebhookPayload> consumer)
Registers a handler method that will be called if no other handler method can be resolved for a received event type.- Parameters:
consumer
- the handler method to call with previously unhandled events- Returns:
- this instance, for method chaining
-
-