diff --git a/application/src/main/java/org/thingsboard/server/controller/NotificationController.java b/application/src/main/java/org/thingsboard/server/controller/NotificationController.java index fb32ebd30b..c1248a1fbf 100644 --- a/application/src/main/java/org/thingsboard/server/controller/NotificationController.java +++ b/application/src/main/java/org/thingsboard/server/controller/NotificationController.java @@ -300,9 +300,15 @@ public class NotificationController extends BaseController { request.setOriginatorEntityId(user.getId()); List targets = request.getTargets().stream() .map(NotificationTargetId::new) - .map(targetId -> notificationTargetService.findNotificationTargetById(user.getTenantId(), targetId)) + .map(targetId -> { + NotificationTarget target = notificationTargetService.findNotificationTargetById(user.getTenantId(), targetId); + if (target == null) { + throw new IllegalArgumentException("Notification target for id " + targetId + " not found"); + } + return target; + }) .sorted(Comparator.comparing(target -> target.getConfiguration().getType())) - .collect(Collectors.toList()); + .toList(); NotificationRequestPreview preview = new NotificationRequestPreview(); diff --git a/application/src/main/java/org/thingsboard/server/service/notification/DefaultNotificationCenter.java b/application/src/main/java/org/thingsboard/server/service/notification/DefaultNotificationCenter.java index f20b666b28..84d3b6c378 100644 --- a/application/src/main/java/org/thingsboard/server/service/notification/DefaultNotificationCenter.java +++ b/application/src/main/java/org/thingsboard/server/service/notification/DefaultNotificationCenter.java @@ -73,6 +73,7 @@ import org.thingsboard.server.service.telemetry.AbstractSubscriptionService; import org.thingsboard.server.service.ws.notification.sub.NotificationRequestUpdate; import org.thingsboard.server.service.ws.notification.sub.NotificationUpdate; +import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; import java.util.List; @@ -115,12 +116,23 @@ public class DefaultNotificationCenter extends AbstractSubscriptionService imple } else { notificationTemplate = request.getTemplate(); } - if (notificationTemplate == null) throw new IllegalArgumentException("Template is missing"); + if (notificationTemplate == null) { + throw new IllegalArgumentException("Template is missing"); + } Set deliveryMethods = new HashSet<>(); - List targets = request.getTargets().stream().map(NotificationTargetId::new) - .map(id -> notificationTargetService.findNotificationTargetById(tenantId, id)) - .collect(Collectors.toList()); + List targets = new ArrayList<>(); + for (UUID targetId : request.getTargets()) { + NotificationTarget target = notificationTargetService.findNotificationTargetById(tenantId, new NotificationTargetId(targetId)); + if (target != null) { + targets.add(target); + } else { + log.debug("Unknown notification target {} in request {}", targetId, request); + } + } + if (targets.isEmpty()) { + throw new IllegalArgumentException("No recipients chosen"); + } NotificationRuleId ruleId = request.getRuleId(); notificationTemplate.getConfiguration().getDeliveryMethodsTemplates().forEach((deliveryMethod, template) -> {