use of com.microsoft.graph.models.ChangeNotification in project java-spring-webhooks-sample by microsoftgraph.
the class ListenController method handleNotification.
/**
* This method receives and processes incoming notifications from Microsoft Graph
*
* @param jsonPayload the JSON body of the request
* @return A 202 Accepted response
*/
@PostMapping("/listen")
public CompletableFuture<ResponseEntity<String>> handleNotification(@RequestBody final String jsonPayload) {
// Deserialize the JSON body into a ChangeNotificationCollection
final var serializer = new DefaultSerializer(new DefaultLogger());
final var notifications = serializer.deserializeObject(jsonPayload, ChangeNotificationCollection.class);
// Check for validation tokens
boolean areTokensValid = true;
if (notifications.validationTokens != null && !notifications.validationTokens.isEmpty()) {
areTokensValid = TokenHelper.areValidationTokensValid(new String[] { clientId }, new String[] { tenantId }, notifications.validationTokens, keyDiscoveryUrl);
}
if (areTokensValid) {
for (ChangeNotification notification : notifications.value) {
// Look up subscription in store
var subscription = subscriptionStore.getSubscription(notification.subscriptionId.toString());
// the client state in the notification matches
if (subscription != null && subscription.clientState.equals(notification.clientState)) {
if (notification.encryptedContent == null) {
// No encrypted content, this is a new message notification
// without resource data
processNewMessageNotification(notification, subscription);
} else {
// With encrypted content, this is a new channel message
// notification with encrypted resource data
processNewChannelMessageNotification(notification, subscription);
}
}
}
}
return CompletableFuture.completedFuture(ResponseEntity.accepted().body(""));
}
Aggregations