Search in sources :

Example 1 with ChangeNotification

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(""));
}
Also used : DefaultSerializer(com.microsoft.graph.serializer.DefaultSerializer) ChangeNotification(com.microsoft.graph.models.ChangeNotification) DefaultLogger(com.microsoft.graph.logger.DefaultLogger) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Aggregations

DefaultLogger (com.microsoft.graph.logger.DefaultLogger)1 ChangeNotification (com.microsoft.graph.models.ChangeNotification)1 DefaultSerializer (com.microsoft.graph.serializer.DefaultSerializer)1 PostMapping (org.springframework.web.bind.annotation.PostMapping)1