Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Webhook processing: 202 response with empty body #198

Merged
merged 2 commits into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
Expand Down Expand Up @@ -42,7 +43,7 @@ public WebhookController(ApplicationProperty applicationProperty) {

/**
* Process incoming Webhook notification: get NotificationRequestItem, validate HMAC signature,
* consume the event asynchronously, send response ["accepted"]
* consume the event asynchronously, send response status 202
*
* @param json Payload of the webhook event
* @return
Expand Down Expand Up @@ -71,24 +72,25 @@ public ResponseEntity<String> webhooks(@RequestBody String json) throws Exceptio
consumeEvent(item);

} else {
// invalid HMAC signature: do not send [accepted] response
// invalid HMAC signature
log.warn("Could not validate HMAC signature for incoming webhook message: {}", item);
throw new RuntimeException("Invalid HMAC signature");
}
} catch (SignatureException e) {
// Unexpected error during HMAC validation: do not send [accepted] response
// Unexpected error during HMAC validation
log.error("Error while validating HMAC Key", e);
throw new SignatureException(e);
}

} else {
// Unexpected event with no payload: do not send [accepted] response
// Unexpected event with no payload
log.warn("Empty NotificationItem");
throw new Exception("empty");
throw new Exception("empty payload");
}

// Acknowledge event has been consumed
return ResponseEntity.ok().body("[accepted]");
return ResponseEntity.status(HttpStatus.ACCEPTED).build();

}

// process payload asynchronously
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
Expand Down Expand Up @@ -39,7 +40,7 @@ public WebhookResource(ApplicationProperty applicationProperty) {

/**
* Process incoming Webhook notification: get NotificationRequestItem, validate HMAC signature,
* consume the event asynchronously, send response ["accepted"]
* consume the event asynchronously, send response status 202
*
* @param json Payload of the webhook event
* @return
Expand Down Expand Up @@ -70,24 +71,24 @@ public ResponseEntity<String> webhooks(@RequestBody String json) throws Exceptio
consumeEvent(item);

} else {
// invalid HMAC signature: do not send [accepted] response
// invalid HMAC signature
log.warn("Could not validate HMAC signature for incoming webhook message: {}", item);
throw new RuntimeException("Invalid HMAC signature");
}
} catch (SignatureException e) {
// Unexpected error during HMAC validation: do not send [accepted] response
// Unexpected error during HMAC validation
log.error("Error while validating HMAC Key", e);
throw new SignatureException(e);
}

} else {
// Unexpected event with no payload: do not send [accepted] response
// Unexpected event with no payload
log.warn("Empty NotificationItem");
throw new Exception("empty");
throw new Exception("empty payload");
}

// Acknowledge event has been consumed
return ResponseEntity.ok().body("[accepted]");
return ResponseEntity.status(HttpStatus.ACCEPTED).build();
}

// process payload asynchronously
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
Expand Down Expand Up @@ -39,7 +40,7 @@ public WebhookResource(ApplicationProperty applicationProperty) {

/**
* Process the incoming Webhook event: get NotificationRequestItem, validate HMAC signature,
* consume the event asynchronously, send response ["accepted"]
* consume the event asynchronously, send response status 202
*
* @param json Payload of the webhook event
* @return
Expand Down Expand Up @@ -69,24 +70,24 @@ public ResponseEntity<String> webhooks(@RequestBody String json) throws Exceptio
consumeEvent(item);

} else {
// invalid HMAC signature: do not send [accepted] response
// invalid HMAC signature
log.warn("Could not validate HMAC signature for incoming webhook message: {}", item);
throw new RuntimeException("Invalid HMAC signature");
}
} catch (SignatureException e) {
// Unexpected error during HMAC validation: do not send [accepted] response
// Unexpected error during HMAC validation
log.error("Error while validating HMAC Key", e);
throw new SignatureException(e);
}

} else {
// Unexpected event with no payload: do not send [accepted] response
// Unexpected event with no payload
log.warn("Empty NotificationItem");
throw new Exception("empty");
throw new Exception("Event with empty payload");
}

// Acknowledge event has been consumed
return ResponseEntity.ok().body("[accepted]");
return ResponseEntity.status(HttpStatus.ACCEPTED).build();
}

// process payload asynchronously
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
Expand Down Expand Up @@ -39,7 +40,7 @@ public WebhookResource(ApplicationProperty applicationProperty) {

/**
* Process the incoming Webhook event: get NotificationRequestItem, validate HMAC signature,
* consume the event asynchronously, send response ["accepted"]
* consume the event asynchronously, send response status 202
*
* @param json Payload of the webhook event
* @return
Expand All @@ -59,7 +60,7 @@ public ResponseEntity<String> webhooks(@RequestBody String json) throws Exceptio

try {
if (!getHmacValidator().validateHMAC(item, this.applicationProperty.getHmacKey())) {
// invalid HMAC signature: do not send [accepted] response
// invalid HMAC signature
log.warn("Could not validate HMAC signature for incoming webhook message: {}", item);
throw new RuntimeException("Invalid HMAC signature");
}
Expand Down Expand Up @@ -105,19 +106,19 @@ public ResponseEntity<String> webhooks(@RequestBody String json) throws Exceptio
}

} catch (SignatureException e) {
// Unexpected error during HMAC validation: do not send [accepted] response
// Unexpected error during HMAC validation
log.error("Error while validating HMAC Key", e);
throw new SignatureException(e);
}

} else {
// Unexpected event with no payload: do not send [accepted] response
// Unexpected event with no payload
log.warn("Empty NotificationItem");
throw new Exception("empty");
throw new Exception("empty payload");
}

// Acknowledge event has been consumed
return ResponseEntity.ok().body("[accepted]");
return ResponseEntity.status(HttpStatus.ACCEPTED).build();
}

// process payload asynchronously
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
Expand Down Expand Up @@ -66,12 +67,12 @@ public ResponseEntity<String> webhooks(@RequestBody String json) throws IOExcept
consumeEvent(item);

} else {
// Unexpected event with no payload: do not send [accepted] response
// Unexpected event with no payload
log.warn("Empty NotificationItem");
}

// Acknowledge event has been consumed
return ResponseEntity.ok().body("[accepted]");
return ResponseEntity.status(HttpStatus.ACCEPTED).build();
}

// process payload asynchronously
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
Expand Down Expand Up @@ -39,7 +40,7 @@ public WebhookResource(ApplicationProperty applicationProperty) {

/**
* Process incoming Webhook notification: get NotificationRequestItem, validate HMAC signature,
* consume the event asynchronously, send response ["accepted"]
* consume the event asynchronously, send response status 202
*
* @param json Payload of the webhook event
* @return
Expand Down Expand Up @@ -70,24 +71,24 @@ public ResponseEntity<String> webhooks(@RequestBody String json) throws Exceptio
consumeEvent(item);

} else {
// invalid HMAC signature: do not send [accepted] response
// invalid HMAC signature
log.warn("Could not validate HMAC signature for incoming webhook message: {}", item);
throw new RuntimeException("Invalid HMAC signature");
}
} catch (SignatureException e) {
// Unexpected error during HMAC validation: do not send [accepted] response
// Unexpected error during HMAC validation
log.error("Error while validating HMAC Key", e);
throw new SignatureException(e);
}

} else {
// Unexpected event with no payload: do not send [accepted] response
// Unexpected event with no payload
log.warn("Empty NotificationItem");
throw new Exception("empty");
throw new Exception("empty payload");
}

// Acknowledge event has been consumed
return ResponseEntity.ok().body("[accepted]");
return ResponseEntity.status(HttpStatus.ACCEPTED).build();
}

// process payload asynchronously
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

Expand Down Expand Up @@ -34,7 +35,7 @@ public WebhookController(ApplicationProperty applicationProperty) {

/**
* Process the incoming Webhook event: get NotificationRequestItem, validate HMAC signature,
* consume the event asynchronously, send response ["accepted"]
* consume the event asynchronously, send response status 202
*
* @param json Payload of the webhook event
* @return
Expand All @@ -54,7 +55,7 @@ public ResponseEntity<String> webhooks(@RequestBody String json) throws IOExcept

try {
if (!getHmacValidator().validateHMAC(item, this.applicationProperty.getHmacKey())) {
// invalid HMAC signature: do not send [accepted] response
// invalid HMAC signature
log.warn("Could not validate HMAC signature for incoming webhook message: {}", item);
throw new RuntimeException("Invalid HMAC signature");
}
Expand Down Expand Up @@ -85,15 +86,15 @@ public ResponseEntity<String> webhooks(@RequestBody String json) throws IOExcept
}

} catch (SignatureException e) {
// Unexpected error during HMAC validation: do not send [accepted] response
// Unexpected error during HMAC validation
log.error("Error while validating HMAC Key", e);
throw new RuntimeException(e.getMessage());
}

}

// Acknowledge event has been consumed
return ResponseEntity.ok().body("[accepted]");
return ResponseEntity.status(HttpStatus.ACCEPTED).build();
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
Expand Down Expand Up @@ -40,7 +41,7 @@ public WebhookController(ApplicationProperty applicationProperty) {

/** Process incoming Webhook event: get NotificationRequestItem, validate HMAC signature,
* Process the incoming Webhook: get NotificationRequestItem, validate HMAC signature,
* consume the event asynchronously, send response ["accepted"]
* consume the event asynchronously, send response status 202
*
* @param json Payload of the webhook event
* @return
Expand Down Expand Up @@ -70,24 +71,24 @@ public ResponseEntity<String> webhooks(@RequestBody String json) throws Exceptio
consumeEvent(item);

} else {
// invalid HMAC signature: do not send [accepted] response
// invalid HMAC signature
log.warn("Could not validate HMAC signature for incoming webhook message: {}", item);
throw new RuntimeException("Invalid HMAC signature");
}
} catch (SignatureException e) {
// Unexpected error during HMAC validation: do not send [accepted] response
// Unexpected error during HMAC validation
log.error("Error while validating HMAC Key", e);
throw new SignatureException(e);
}

} else {
// Unexpected event with no payload: do not send [accepted] response
// Unexpected event with no payload
log.warn("Empty NotificationItem");
throw new Exception("empty");
}

// Acknowledge event has been consumed
return ResponseEntity.ok().body("[accepted]");
return ResponseEntity.status(HttpStatus.ACCEPTED).build();
}

// process payload asynchronously
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

Expand Down Expand Up @@ -37,7 +38,7 @@ public WebhookResource(ApplicationProperty applicationProperty) {

/**
* Process the incoming Webhook event: get NotificationRequestItem, validate HMAC signature,
* consume the event asynchronously, send response ["accepted"]
* consume the event asynchronously, send response status 202
*
* @param json Payload of the webhook event
* @return
Expand All @@ -57,7 +58,7 @@ public ResponseEntity<String> webhooks(@RequestBody String json) throws IOExcept

try {
if (!getHmacValidator().validateHMAC(item, this.applicationProperty.getHmacKey())) {
// invalid HMAC signature: do not send [accepted] response
// invalid HMAC signature
log.warn("Could not validate HMAC signature for incoming webhook message: {}", item);
throw new RuntimeException("Invalid HMAC signature");
}
Expand Down Expand Up @@ -86,15 +87,15 @@ public ResponseEntity<String> webhooks(@RequestBody String json) throws IOExcept
}

} catch (SignatureException e) {
// Unexpected error during HMAC validation: do not send [accepted] response
// Unexpected error during HMAC validation
log.error("Error while validating HMAC Key", e);
throw new RuntimeException(e.getMessage());
}

}

// Acknowledge event has been consumed
return ResponseEntity.ok().body("[accepted]");
return ResponseEntity.status(HttpStatus.ACCEPTED).build();
}


Expand Down
Loading