diff --git a/src/main/java/io/cryostat/messaging/notifications/Notification.java b/src/main/java/io/cryostat/messaging/notifications/Notification.java index 44a4f6ea5e..a1d8981c1f 100644 --- a/src/main/java/io/cryostat/messaging/notifications/Notification.java +++ b/src/main/java/io/cryostat/messaging/notifications/Notification.java @@ -15,6 +15,9 @@ */ package io.cryostat.messaging.notifications; +import java.util.HashMap; +import java.util.Map; + import io.cryostat.net.web.http.HttpMimeType; public class Notification { @@ -82,6 +85,27 @@ public Notification build() { } } + public static class OwnedResourceBuilder extends Builder> { + + private final Map map = new HashMap<>(); + + OwnedResourceBuilder(NotificationSource source, String category) { + super(source); + metaType(HttpMimeType.JSON); + metaCategory(category); + message(map); + } + + public OwnedResourceBuilder messageEntry(String key, Object value) { + if (value == null) { + this.map.remove(key); + } else { + this.map.put(key, value); + } + return this; + } + } + public static class Meta { private final String category; private final MetaType type; diff --git a/src/main/java/io/cryostat/messaging/notifications/NotificationFactory.java b/src/main/java/io/cryostat/messaging/notifications/NotificationFactory.java index 1167e17f8d..c929db2e56 100644 --- a/src/main/java/io/cryostat/messaging/notifications/NotificationFactory.java +++ b/src/main/java/io/cryostat/messaging/notifications/NotificationFactory.java @@ -15,23 +15,35 @@ */ package io.cryostat.messaging.notifications; -import io.cryostat.net.web.http.HttpMimeType; +import io.cryostat.recordings.JvmIdHelper; +import io.cryostat.recordings.JvmIdHelper.JvmIdGetException; public class NotificationFactory { private final NotificationSource source; + private final JvmIdHelper jvmIdHelper; - NotificationFactory(NotificationSource source) { + NotificationFactory(NotificationSource source, JvmIdHelper jvmIdHelper) { this.source = source; + this.jvmIdHelper = jvmIdHelper; } public Notification.Builder createBuilder() { return new Notification.Builder(source); } - public Notification.Builder createOwnedResourceBuilder(String notificationCategory) { - return new Notification.Builder(source) - .metaType(HttpMimeType.JSON) - .metaCategory(notificationCategory); + public Notification.OwnedResourceBuilder createOwnedResourceBuilder( + String notificationCategory) { + return new Notification.OwnedResourceBuilder(source, notificationCategory); + } + + public Notification.OwnedResourceBuilder createOwnedResourceBuilder( + String targetId, String notificationCategory) throws JvmIdGetException { + return new Notification.OwnedResourceBuilder(source, notificationCategory) + // FIXME the websocket notification system should only emit the targetId under one + // named key or the other, not both - are we already consistent with this? + .messageEntry("target", targetId) + .messageEntry("connectUrl", targetId) + .messageEntry("jvmId", jvmIdHelper.getJvmId(targetId)); } } diff --git a/src/main/java/io/cryostat/messaging/notifications/NotificationsModule.java b/src/main/java/io/cryostat/messaging/notifications/NotificationsModule.java index 9777bfffd6..120f72c3e3 100644 --- a/src/main/java/io/cryostat/messaging/notifications/NotificationsModule.java +++ b/src/main/java/io/cryostat/messaging/notifications/NotificationsModule.java @@ -19,6 +19,8 @@ import javax.inject.Singleton; +import io.cryostat.recordings.JvmIdHelper; + import dagger.Lazy; import dagger.Module; import dagger.Provides; @@ -34,7 +36,8 @@ static NotificationSource provideNotificationSource(Lazy