Skip to content

armgit / core

Public Imported from GitHub
0

100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps

DefaultMQTTHandler and associate MQTT classs cleanup/internal refactor

Rich Turner64c16d989e39
diff --git a/build.gradle b/build.gradle
index e14cc6833..2908ee45d 100644
--- a/build.gradle
+++ b/build.gradle
@@ -1,3 +1,15 @@
+// Build plugins
+buildscript {
+ repositories {
+ maven {
+ url "https://plugins.gradle.org/m2/"
+ }
+ }
+ dependencies {
+ classpath "gradle.plugin.org.jetbrains.gradle.plugin.idea-ext:gradle-idea-ext:1.1"
+ }
+}
+
allprojects {
apply from: "${findProject(":openremote") != null ? project(":openremote").projectDir : rootDir}/project.gradle"
}
diff --git a/manager/src/main/java/org/openremote/manager/event/EventSubscriptions.java b/manager/src/main/java/org/openremote/manager/event/EventSubscriptions.java
index 96a39a8ae..092ddc06e 100644
--- a/manager/src/main/java/org/openremote/manager/event/EventSubscriptions.java
+++ b/manager/src/main/java/org/openremote/manager/event/EventSubscriptions.java
@@ -132,6 +132,9 @@ public class EventSubscriptions {
} else {
sessionSubscriptions.cancelByType(subscription.getEventType());
}
+ if (sessionSubscriptions.isEmpty()) {
+ this.sessionSubscriptionIdMap.remove(sessionKey);
+ }
}
}
diff --git a/manager/src/main/java/org/openremote/manager/mqtt/DefaultMQTTHandler.java b/manager/src/main/java/org/openremote/manager/mqtt/DefaultMQTTHandler.java
index 595eedb5e..04cfa00e7 100644
--- a/manager/src/main/java/org/openremote/manager/mqtt/DefaultMQTTHandler.java
+++ b/manager/src/main/java/org/openremote/manager/mqtt/DefaultMQTTHandler.java
@@ -21,20 +21,16 @@ package org.openremote.manager.mqtt;
import io.moquette.broker.subscriptions.Token;
import io.moquette.broker.subscriptions.Topic;
-import io.moquette.interception.messages.InterceptPublishMessage;
-import io.moquette.interception.messages.InterceptSubscribeMessage;
-import io.moquette.interception.messages.InterceptUnsubscribeMessage;
-import io.netty.buffer.ByteBuf;
-import io.netty.buffer.Unpooled;
-import io.netty.handler.codec.mqtt.MqttMessageBuilders;
-import io.netty.handler.codec.mqtt.MqttPublishMessage;
+import io.moquette.interception.messages.*;
import io.netty.handler.codec.mqtt.MqttQoS;
import org.openremote.container.message.MessageBrokerService;
import org.openremote.container.security.AuthContext;
+import org.openremote.container.web.ConnectionConstants;
import org.openremote.manager.asset.AssetStorageService;
import org.openremote.manager.event.ClientEventService;
import org.openremote.manager.security.ManagerIdentityService;
import org.openremote.manager.security.ManagerKeycloakIdentityProvider;
+import org.openremote.model.Constants;
import org.openremote.model.Container;
import org.openremote.model.asset.AssetEvent;
import org.openremote.model.asset.AssetFilter;
@@ -45,20 +41,24 @@ import org.openremote.model.event.shared.SharedEvent;
import org.openremote.model.syslog.SyslogCategory;
import org.openremote.model.util.ValueUtil;
-import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
-import java.util.*;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Function;
-import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
-import static org.openremote.manager.mqtt.MqttBrokerService.prepareHeaders;
import static org.openremote.model.Constants.ASSET_ID_REGEXP;
import static org.openremote.model.syslog.SyslogCategory.API;
+/**
+ * This handler uses the {@link ClientEventService} to publish and subscribe to asset and attribute events; converting
+ * subscription topics into {@link AssetFilter}s to ensure only the correct events are returned for the subscription.
+ */
public class DefaultMQTTHandler extends MQTTHandler {
public static final int PRIORITY = 1000;
@@ -115,6 +115,35 @@ public class DefaultMQTTHandler extends MQTTHandler {
}
}
+ @Override
+ public void onConnect(MqttConnection connection, InterceptConnectMessage msg) {
+ super.onConnect(connection, msg);
+
+ Map<String, Object> headers = prepareHeaders(connection);
+ headers.put(ConnectionConstants.SESSION_OPEN, true);
+ messageBrokerService.getProducerTemplate().sendBodyAndHeaders(ClientEventService.CLIENT_EVENT_QUEUE, null, headers);
+ LOG.fine("Connected: " + connection);
+ }
+
+ @Override
+ public void onDisconnect(MqttConnection connection, InterceptDisconnectMessage msg) {
+ super.onDisconnect(connection, msg);
+
+ Map<String, Object> headers = prepareHeaders(connection);
+ headers.put(ConnectionConstants.SESSION_CLOSE, true);
+ messageBrokerService.getProducerTemplate().sendBodyAndHeaders(ClientEventService.CLIENT_EVENT_QUEUE, null, headers);
+ LOG.fine("Connection closed: " + connection);
+ }
+
+ @Override
+ public void onConnectionLost(MqttConnection connection, InterceptConnectionLostMessage msg) {
+ super.onConnectionLost(connection, msg);
+ Map<String, Object> headers = prepareHeaders(connection);
+ headers.put(ConnectionConstants.SESSION_CLOSE_ERROR, true);
+ messageBrokerService.getProducerTemplate().sendBodyAndHeaders(ClientEventService.CLIENT_EVENT_QUEUE, null, headers);
+ LOG.fine("Connection lost: " + connection);
+ }
+
@Override
public boolean topicMatches(Topic topic) {
return isAttributeTopic(topic) || isAssetTopic(topic) || isAttributeWriteTopic(topic) || isAttributeValueWriteTopic(topic);
@@ -290,14 +319,15 @@ public class DefaultMQTTHandler extends MQTTHandler {
return;
}
+ Consumer<SharedEvent> eventConsumer = getSubscriptionEventConsumer(connection, topic, msg.getRequestedQos());
+
EventSubscription subscription = new EventSubscription(
subscriptionClass,
filter,
- subscriptionId
+ subscriptionId,
+ eventConsumer
);
- Consumer<SharedEvent> eventConsumer = getSubscriptionEventConsumer(connection, topic, msg.getRequestedQos());
- connection.subscriptionHandlerMap.put(subscriptionId, eventConsumer);
Map<String, Object> headers = prepareHeaders(connection);
messageBrokerService.getProducerTemplate().sendBodyAndHeaders(ClientEventService.CLIENT_EVENT_QUEUE, subscription, headers);
}
@@ -306,16 +336,11 @@ public class DefaultMQTTHandler extends MQTTHandler {
@Override
public void doUnsubscribe(MqttConnection connection, Topic topic, InterceptUnsubscribeMessage msg) {
String subscriptionId = topic.toString();
- Consumer<? extends SharedEvent> eventConsumer = connection.subscriptionHandlerMap.remove(subscriptionId);
-
- if (eventConsumer != null) {
- boolean isAssetTopic = subscriptionId.startsWith(ASSET_TOPIC);
-
- Map<String, Object> headers = prepareHeaders(connection);
- Class<SharedEvent> subscriptionClass = (Class) (isAssetTopic ? AssetEvent.class : AttributeEvent.class);
- CancelEventSubscription cancelEventSubscription = new CancelEventSubscription(subscriptionClass, subscriptionId);
- messageBrokerService.getProducerTemplate().sendBodyAndHeaders(ClientEventService.CLIENT_EVENT_QUEUE, cancelEventSubscription, headers);
- }
+ boolean isAssetTopic = subscriptionId.startsWith(ASSET_TOPIC);
+ Map<String, Object> headers = prepareHeaders(connection);
+ Class<SharedEvent> subscriptionClass = (Class) (isAssetTopic ? AssetEvent.class : AttributeEvent.class);
+ CancelEventSubscription cancelEventSubscription = new CancelEventSubscription(subscriptionClass, subscriptionId);
+ messageBrokerService.getProducerTemplate().sendBodyAndHeaders(ClientEventService.CLIENT_EVENT_QUEUE, cancelEventSubscription, headers);
}
@Override
@@ -426,22 +451,6 @@ public class DefaultMQTTHandler extends MQTTHandler {
return assetFilter;
}
- public void sendToSession(String sessionId, String topic, Object data, MqttQoS qoS) {
- try {
- ByteBuf payload = Unpooled.copiedBuffer(ValueUtil.asJSON(data).orElseThrow(() -> new IllegalStateException("Failed to convert payload to JSON string: " + data)), Charset.defaultCharset());
-
- MqttPublishMessage publishMessage = MqttMessageBuilders.publish()
- .qos(qoS)
- .topicName(topic)
- .payload(payload)
- .build();
-
- mqttBrokerService.mqttBroker.internalPublish(publishMessage, sessionId);
- } catch (Exception e) {
- LOG.log(Level.WARNING, "Couldn't send AttributeEvent to MQTT client", e);
- }
- }
-
protected Consumer<SharedEvent> getSubscriptionEventConsumer(MqttConnection connection, Topic topic, MqttQoS mqttQoS) {
boolean isValueSubscription = ATTRIBUTE_VALUE_TOPIC.equalsIgnoreCase(topicTokenIndexToString(topic, 2));
boolean isAssetTopic = isAssetTopic(topic);
@@ -477,19 +486,27 @@ public class DefaultMQTTHandler extends MQTTHandler {
if (isAssetTopic) {
if (ev instanceof AssetEvent) {
- sendToSession(connection.getClientId(), topicExpander.apply(ev), ev, mqttQoS);
+ mqttBrokerService.publishMessage(topicExpander.apply(ev), ev, mqttQoS);
}
} else {
if (ev instanceof AttributeEvent) {
AttributeEvent attributeEvent = (AttributeEvent) ev;
if (isValueSubscription) {
- sendToSession(connection.getClientId(), topicExpander.apply(ev), attributeEvent.getValue().orElse(null), mqttQoS);
+ mqttBrokerService.publishMessage(topicExpander.apply(ev), attributeEvent.getValue().orElse(null), mqttQoS);
} else {
- sendToSession(connection.getClientId(), topicExpander.apply(ev), ev, mqttQoS);
+ mqttBrokerService.publishMessage(topicExpander.apply(ev), ev, mqttQoS);
}
}
}
};
}
+
+ public static Map<String, Object> prepareHeaders(MqttConnection connection) {
+ Map<String, Object> headers = new HashMap<>();
+ headers.put(ConnectionConstants.SESSION_KEY, connection.getClientId());
+ headers.put(ClientEventService.HEADER_CONNECTION_TYPE, ClientEventService.HEADER_CONNECTION_TYPE_MQTT);
+ headers.put(Constants.AUTH_CONTEXT, connection.getAuthContext());
+ return headers;
+ }
}
diff --git a/manager/src/main/java/org/openremote/manager/mqtt/MqttBrokerService.java b/manager/src/main/java/org/openremote/manager/mqtt/MqttBrokerService.java
index b92d8ceab..e26a3d6cd 100644
--- a/manager/src/main/java/org/openremote/manager/mqtt/MqttBrokerService.java
+++ b/manager/src/main/java/org/openremote/manager/mqtt/MqttBrokerService.java
@@ -24,28 +24,28 @@ import io.moquette.broker.Server;
import io.moquette.broker.config.MemoryConfig;
import io.moquette.broker.security.IAuthenticator;
import io.moquette.interception.InterceptHandler;
-import org.apache.camel.builder.RouteBuilder;
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
+import io.netty.handler.codec.mqtt.MqttMessageBuilders;
+import io.netty.handler.codec.mqtt.MqttPublishMessage;
+import io.netty.handler.codec.mqtt.MqttQoS;
import org.openremote.container.message.MessageBrokerService;
-import org.openremote.container.web.ConnectionConstants;
import org.openremote.manager.asset.AssetStorageService;
import org.openremote.manager.event.ClientEventService;
import org.openremote.manager.security.ManagerIdentityService;
import org.openremote.manager.security.ManagerKeycloakIdentityProvider;
-import org.openremote.model.Constants;
import org.openremote.model.Container;
import org.openremote.model.ContainerService;
-import org.openremote.model.event.TriggeredEventSubscription;
-import org.openremote.model.event.shared.SharedEvent;
import org.openremote.model.security.Tenant;
import org.openremote.model.security.User;
import org.openremote.model.syslog.SyslogCategory;
import org.openremote.model.util.TextUtil;
+import org.openremote.model.util.ValueUtil;
+import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.*;
-import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ScheduledExecutorService;
-import java.util.function.Consumer;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
@@ -53,12 +53,12 @@ import java.util.stream.Collectors;
import static java.util.stream.StreamSupport.stream;
import static org.openremote.container.util.MapAccess.getInteger;
import static org.openremote.container.util.MapAccess.getString;
-import static org.openremote.manager.event.ClientEventService.getSessionKey;
import static org.openremote.model.syslog.SyslogCategory.API;
public class MqttBrokerService implements ContainerService, IAuthenticator {
public static final int PRIORITY = MED_PRIORITY;
+ public static final String INTERNAL_CLIENT_ID = "ManagerInternal";
private static final Logger LOG = SyslogCategory.getLogger(API, MqttBrokerService.class);
public static final String MQTT_CLIENT_QUEUE = "seda://MqttClientQueue?waitForTaskToComplete=IfReplyExpected&timeout=10000&purgeWhenStopping=true&discardIfNoConsumers=false&size=25000";
@@ -99,32 +99,6 @@ public class MqttBrokerService implements ContainerService, IAuthenticator {
active = true;
identityProvider = (ManagerKeycloakIdentityProvider) identityService.getIdentityProvider();
}
-
- mqttBroker = new Server();
-
- messageBrokerService.getContext().addRoutes(new RouteBuilder() {
- @Override
- public void configure() throws Exception {
-
- from(MQTT_CLIENT_QUEUE)
- .routeId("MqttClientEvents")
- .choice()
- .when(body().isInstanceOf(TriggeredEventSubscription.class))
- .process(exchange -> {
- String clientId = getSessionKey(exchange);
- @SuppressWarnings("unchecked")
- TriggeredEventSubscription<SharedEvent> triggeredEventSubscription = exchange.getIn().getBody(TriggeredEventSubscription.class);
- MqttConnection connection = clientIdConnectionMap.get(clientId);
-
- if (connection == null) {
- return;
- }
-
- onSubscriptionTriggered(connection, triggeredEventSubscription);
- })
- .end();
- }
- });
}
@Override
@@ -151,6 +125,7 @@ public class MqttBrokerService implements ContainerService, IAuthenticator {
}
}
+ mqttBroker = new Server();
mqttBroker.startServer(new MemoryConfig(properties), interceptHandlers, null, this, new ORAuthorizatorPolicy(identityProvider, this, assetStorageService, clientEventService));
LOG.fine("Started MQTT broker");
}
@@ -233,21 +208,19 @@ public class MqttBrokerService implements ContainerService, IAuthenticator {
}
}
- public static Map<String, Object> prepareHeaders(MqttConnection connection) {
- Map<String, Object> headers = new HashMap<>();
- headers.put(ConnectionConstants.SESSION_KEY, connection.getClientId());
- headers.put(ClientEventService.HEADER_CONNECTION_TYPE, ClientEventService.HEADER_CONNECTION_TYPE_MQTT);
- headers.put(Constants.AUTH_CONTEXT, connection.getAuthContext());
- return headers;
- }
+ public void publishMessage(String topic, Object data, MqttQoS qoS) {
+ try {
+ ByteBuf payload = Unpooled.copiedBuffer(ValueUtil.asJSON(data).orElseThrow(() -> new IllegalStateException("Failed to convert payload to JSON string: " + data)), Charset.defaultCharset());
- protected void onSubscriptionTriggered(MqttConnection connection, TriggeredEventSubscription<SharedEvent> triggeredEventSubscription) {
- triggeredEventSubscription.getEvents()
- .forEach(event -> {
- Consumer<SharedEvent> eventConsumer = connection.subscriptionHandlerMap.get(triggeredEventSubscription.getSubscriptionId());
- if (eventConsumer != null) {
- eventConsumer.accept(event);
- }
- });
+ MqttPublishMessage publishMessage = MqttMessageBuilders.publish()
+ .qos(qoS)
+ .topicName(topic)
+ .payload(payload)
+ .build();
+
+ mqttBroker.internalPublish(publishMessage, INTERNAL_CLIENT_ID);
+ } catch (Exception e) {
+ LOG.log(Level.WARNING, "Couldn't send AttributeEvent to MQTT client", e);
+ }
}
}
diff --git a/manager/src/main/java/org/openremote/manager/mqtt/MqttConnection.java b/manager/src/main/java/org/openremote/manager/mqtt/MqttConnection.java
index a16b55d88..8e33b658a 100644
--- a/manager/src/main/java/org/openremote/manager/mqtt/MqttConnection.java
+++ b/manager/src/main/java/org/openremote/manager/mqtt/MqttConnection.java
@@ -27,32 +27,18 @@ public class MqttConnection {
protected static final Logger LOG = Logger.getLogger(MqttConnection.class.getSimpleName());
protected final String realm;
- protected final String username; // This is OAuth clientId
- protected final String password;
- protected final boolean credentials;
- protected final Map<String, Consumer<SharedEvent>> subscriptionHandlerMap = new HashMap<>();
+ protected String username; // This is OAuth clientId
+ protected String password;
+ protected boolean credentials;
protected final String clientId;
protected Supplier<String> tokenSupplier;
protected ManagerKeycloakIdentityProvider identityProvider;
public MqttConnection(ManagerKeycloakIdentityProvider identityProvider, String clientId, String realm, String username, String password) {
this.realm = realm;
- this.username = username;
- this.password = password;
this.clientId = clientId;
this.identityProvider = identityProvider;
-
- credentials = !TextUtil.isNullOrEmpty(realm)
- && !TextUtil.isNullOrEmpty(username)
- && !TextUtil.isNullOrEmpty(password);
-
- if (credentials) {
- String tokenEndpointUri = identityProvider.getTokenUri(realm).toString();
- OAuthGrant grant = new OAuthClientCredentialsGrant(tokenEndpointUri, username, password, null);
- tokenSupplier = identityProvider.getAccessTokenSupplier(grant);
- } else {
- LOG.fine("MQTT connection with no credentials so will have limited capabilities: " + this);
- }
+ setCredentials(username, password);
}
public String getRealm() {
@@ -67,10 +53,6 @@ public class MqttConnection {
return this.password;
}
- public Map<String, Consumer<SharedEvent>> getSubscriptionHandlerMap() {
- return this.subscriptionHandlerMap;
- }
-
public String getAccessToken() {
if (tokenSupplier == null) {
return null;
@@ -108,6 +90,24 @@ public class MqttConnection {
return credentials;
}
+ public void setCredentials(String username, String password) {
+
+ this.username = username;
+ this.password = password;
+
+ credentials = !TextUtil.isNullOrEmpty(realm)
+ && !TextUtil.isNullOrEmpty(username)
+ && !TextUtil.isNullOrEmpty(password);
+
+ if (credentials) {
+ String tokenEndpointUri = identityProvider.getTokenUri(realm).toString();
+ OAuthGrant grant = new OAuthClientCredentialsGrant(tokenEndpointUri, username, password, null);
+ tokenSupplier = identityProvider.getAccessTokenSupplier(grant);
+ } else {
+ LOG.fine("MQTT connection with no credentials so will have limited capabilities: " + this);
+ }
+ }
+
@Override
public String toString() {
return this.getClass().getSimpleName() + "{" +
diff --git a/manager/src/main/java/org/openremote/manager/mqtt/ORInterceptHandler.java b/manager/src/main/java/org/openremote/manager/mqtt/ORInterceptHandler.java
index 6888a6e70..e3abb7bc5 100644
--- a/manager/src/main/java/org/openremote/manager/mqtt/ORInterceptHandler.java
+++ b/manager/src/main/java/org/openremote/manager/mqtt/ORInterceptHandler.java
@@ -24,15 +24,11 @@ import io.moquette.broker.subscriptions.Topic;
import io.moquette.interception.AbstractInterceptHandler;
import io.moquette.interception.messages.*;
import org.openremote.container.message.MessageBrokerService;
-import org.openremote.container.web.ConnectionConstants;
-import org.openremote.manager.event.ClientEventService;
import org.openremote.manager.security.ManagerKeycloakIdentityProvider;
import org.openremote.model.syslog.SyslogCategory;
-import java.util.Map;
import java.util.logging.Logger;
-import static org.openremote.manager.mqtt.MqttBrokerService.prepareHeaders;
import static org.openremote.model.syslog.SyslogCategory.API;
public class ORInterceptHandler extends AbstractInterceptHandler {
@@ -80,12 +76,7 @@ public class ORInterceptHandler extends AbstractInterceptHandler {
}
MqttConnection connection = new MqttConnection(identityProvider, msg.getClientID(), realm, username, password);
-
brokerService.addConnection(connection.getClientId(), connection);
- Map<String, Object> headers = prepareHeaders(connection);
- headers.put(ConnectionConstants.SESSION_OPEN, true);
- messageBrokerService.getProducerTemplate().sendBodyAndHeaders(ClientEventService.CLIENT_EVENT_QUEUE, null, headers);
- LOG.fine("Connected: " + connection);
// Notify all custom handlers
brokerService.getCustomHandlers().forEach(customHandler -> customHandler.onConnect(connection, msg));
@@ -96,11 +87,6 @@ public class ORInterceptHandler extends AbstractInterceptHandler {
MqttConnection connection = brokerService.removeConnection(msg.getClientID());
if (connection != null) {
- Map<String, Object> headers = prepareHeaders(connection);
- headers.put(ConnectionConstants.SESSION_CLOSE, true);
- messageBrokerService.getProducerTemplate().sendBodyAndHeaders(ClientEventService.CLIENT_EVENT_QUEUE, null, headers);
- LOG.fine("Connection closed: " + connection);
-
// No topic info here so just notify all custom handlers
brokerService.getCustomHandlers().forEach(customHandler -> customHandler.onDisconnect(connection, msg));
}
@@ -111,11 +97,6 @@ public class ORInterceptHandler extends AbstractInterceptHandler {
MqttConnection connection = brokerService.removeConnection(msg.getClientID());
if (connection != null) {
- Map<String, Object> headers = prepareHeaders(connection);
- headers.put(ConnectionConstants.SESSION_CLOSE_ERROR, true);
- messageBrokerService.getProducerTemplate().sendBodyAndHeaders(ClientEventService.CLIENT_EVENT_QUEUE, null, headers);
- LOG.fine("Connection lost: " + connection);
-
// No topic info here so just notify all custom handlers
brokerService.getCustomHandlers().forEach(customHandler -> customHandler.onConnectionLost(connection, msg));
}
@@ -135,7 +116,7 @@ public class ORInterceptHandler extends AbstractInterceptHandler {
for (MQTTHandler handler : brokerService.getCustomHandlers()) {
if (handler.handlesTopic(topic)) {
- LOG.info("Handler has handled subscribe: handler=" + handler.getName() + ", topic=" + topic + ", connection=" + connection);
+ LOG.fine("Handler has handled subscribe: handler=" + handler.getName() + ", topic=" + topic + ", connection=" + connection);
handler.doSubscribe(connection, topic, msg);
break;
}
@@ -156,7 +137,7 @@ public class ORInterceptHandler extends AbstractInterceptHandler {
for (MQTTHandler handler : brokerService.getCustomHandlers()) {
if (handler.handlesTopic(topic)) {
- LOG.info("Handler has handled unsubscribe: handler=" + handler.getName() + ", topic=" + topic + ", connection=" + connection);
+ LOG.fine("Handler has handled unsubscribe: handler=" + handler.getName() + ", topic=" + topic + ", connection=" + connection);
handler.doUnsubscribe(connection, topic, msg);
break;
}
@@ -165,13 +146,19 @@ public class ORInterceptHandler extends AbstractInterceptHandler {
@Override
public void onPublish(InterceptPublishMessage msg) {
- String[] realmAndUsername = msg.getUsername().split(":");
- String realm = realmAndUsername[0];
- String username = realmAndUsername[1];
+ String realm = null;
+ String username = null;
+
+ if (msg.getUsername() != null) {
+ String[] realmAndUsername = msg.getUsername().split(":");
+ realm = realmAndUsername[0];
+ username = realmAndUsername[1];
+ }
+
MqttConnection connection = brokerService.getConnection(msg.getClientID());
if (connection == null) {
- LOG.warning("No connection found: realm=" + realm + ", username=" + username + ", clientID=" + msg.getClientID());
+ LOG.warning("No connection found: clientID=" + msg.getClientID() + ", realm=" + realm + ", username=" + username);
return;
}
@@ -179,7 +166,7 @@ public class ORInterceptHandler extends AbstractInterceptHandler {
for (MQTTHandler handler : brokerService.getCustomHandlers()) {
if (handler.handlesTopic(topic)) {
- LOG.info("Handler has handled publish: handler=" + handler.getName() + ", topic=" + topic + ", connection=" + connection);
+ LOG.fine("Handler has handled publish: handler=" + handler.getName() + ", topic=" + topic + ", connection=" + connection);
handler.doPublish(connection, topic, msg);
break;
}
diff --git a/model/build.gradle b/model/build.gradle
index 977690f02..063ab2848 100644
--- a/model/build.gradle
+++ b/model/build.gradle
@@ -12,6 +12,7 @@ dependencies {
compile "org.hibernate.javax.persistence:hibernate-jpa-2.1-api:$javaxPersistenceVersion"
compile "org.hibernate:hibernate-core:$hibernateVersion"
compile "org.hibernate:hibernate-validator:$hibernateValidatorVersion"
+ compile "com.vladmihalcea:hibernate-types-52:$hibernateTypesVersion"
compile "org.glassfish.web:javax.el:$javaxELVersion"
compile "org.jboss.logging:jboss-logging-annotations:$jbossLoggingAnnotationsVersion"
diff --git a/model/src/main/java/org/openremote/model/util/ValueUtil.java b/model/src/main/java/org/openremote/model/util/ValueUtil.java
index 4128c00e9..1d04c1997 100644
--- a/model/src/main/java/org/openremote/model/util/ValueUtil.java
+++ b/model/src/main/java/org/openremote/model/util/ValueUtil.java
@@ -22,6 +22,7 @@ import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.*;
+import com.fasterxml.jackson.databind.cfg.ConstructorDetector;
import com.fasterxml.jackson.databind.jsontype.NamedType;
import com.fasterxml.jackson.databind.node.*;
import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider;
@@ -149,6 +150,7 @@ public class ValueUtil {
public static ObjectMapper configureObjectMapper(ObjectMapper objectMapper) {
objectMapper
+ .setConstructorDetector(ConstructorDetector.USE_PROPERTIES_BASED)
.setSerializationInclusion(JsonInclude.Include.NON_NULL)
.configure(SerializationFeature.WRITE_EMPTY_JSON_ARRAYS, false) // see https://github.com/FasterXML/jackson-databind/issues/1547
.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, false)
diff --git a/project.gradle b/project.gradle
index 94228708c..c00e4afee 100644
--- a/project.gradle
+++ b/project.gradle
@@ -1,6 +1,9 @@
// Common configuration applied to all projects
import static org.apache.tools.ant.taskdefs.condition.Os.FAMILY_WINDOWS
import static org.apache.tools.ant.taskdefs.condition.Os.isFamily
+import static org.jetbrains.gradle.ext.ShortenCommandLine.MANIFEST
+import org.jetbrains.gradle.ext.Application
+import org.jetbrains.gradle.ext.JUnit
// Build plugins
buildscript {
@@ -9,6 +12,39 @@ buildscript {
url "https://plugins.gradle.org/m2/"
}
}
+ dependencies {
+ classpath "gradle.plugin.org.jetbrains.gradle.plugin.idea-ext:gradle-idea-ext:1.1"
+ }
+}
+
+if (project == rootProject) {
+ try {
+ apply plugin: "org.jetbrains.gradle.plugin.idea-ext"
+ } catch(Exception e) {
+
+ }
+
+ if (project.hasProperty("idea") && idea.project) {
+ // IDEA settings
+ idea.project.settings {
+ compiler {
+ javac {
+ javacAdditionalOptions "-parameters"
+ }
+ }
+ runConfigurations {
+ defaults(JUnit) {
+ shortenCommandLine = MANIFEST
+ workingDirectory = (isCustomProject() ? project(":openremote").projectDir.toString() : projectDir.toString())
+ }
+ defaults(Application) {
+ mainClass = 'org.openremote.manager.Main'
+ shortenCommandLine = MANIFEST
+ workingDirectory = (isCustomProject() ? project(":openremote").projectDir.toString() : projectDir.toString())
+ }
+ }
+ }
+ }
}
// Give test projects more memory (Gradle 5 reduced this to 512MB)
@@ -50,6 +86,8 @@ idea {
}
}
+
+
// Helper functions for project/task resolution when the main
// repo is checked out as a git submodule and therefore a subproject
def isCustomProject() {
diff --git a/test/src/test/groovy/org/openremote/test/mqtt/MqttBrokerTest.groovy b/test/src/test/groovy/org/openremote/test/mqtt/MqttBrokerTest.groovy
index a5b1759a8..8d564b255 100644
--- a/test/src/test/groovy/org/openremote/test/mqtt/MqttBrokerTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/mqtt/MqttBrokerTest.groovy
@@ -1,6 +1,6 @@
package org.openremote.test.mqtt
-import com.google.common.collect.Lists
+
import io.moquette.BrokerConstants
import org.openremote.agent.protocol.mqtt.MQTTMessage
import org.openremote.agent.protocol.mqtt.MQTT_IOClient
@@ -8,6 +8,7 @@ import org.openremote.agent.protocol.simulator.SimulatorProtocol
import org.openremote.container.util.UniqueIdentifierGenerator
import org.openremote.manager.agent.AgentService
import org.openremote.manager.asset.AssetStorageService
+import org.openremote.manager.event.ClientEventService
import org.openremote.manager.mqtt.DefaultMQTTHandler
import org.openremote.manager.mqtt.MQTTHandler
import org.openremote.manager.mqtt.MqttBrokerService
@@ -47,6 +48,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {
def keycloakTestSetup = container.getService(SetupService.class).getTaskOfType(KeycloakTestSetup.class)
def mqttBrokerService = container.getService(MqttBrokerService.class)
def assetStorageService = container.getService(AssetStorageService.class)
+ def clientEventService = container.getService(ClientEventService.class)
def agentService = container.getService(AgentService.class)
def mqttClientId = UniqueIdentifierGenerator.generateId()
def username = keycloakTestSetup.tenantBuilding.realm + ":" + KeycloakTestSetup.serviceUserId // realm and OAuth client id
@@ -84,7 +86,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {
then: "No subscription should exist"
conditions.eventually {
assert client.topicConsumerMap.get(topic) == null // Consumer added and removed on failure
- assert mqttBrokerService.clientIdConnectionMap.get(mqttClientId).subscriptionHandlerMap.size() == 0
+ assert !clientEventService.eventSubscriptions.sessionSubscriptionIdMap.containsKey(mqttClientId)
}
when: "a mqtt client subscribes with clientId missing"
@@ -94,7 +96,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {
then: "No subscription should exist"
conditions.eventually {
assert client.topicConsumerMap.get(topic) == null // Consumer added and removed on failure
- assert mqttBrokerService.clientIdConnectionMap.get(mqttClientId).subscriptionHandlerMap.size() == 0
+ assert !clientEventService.eventSubscriptions.sessionSubscriptionIdMap.containsKey(mqttClientId)
}
when: "a mqtt client subscribes with different clientId"
@@ -106,7 +108,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {
conditions.eventually {
assert client.topicConsumerMap.get(topic) == null // Consumer added and removed on failure
assert mqttBrokerService.clientIdConnectionMap.get(mqttClientId) != null
- assert mqttBrokerService.clientIdConnectionMap.get(mqttClientId).subscriptionHandlerMap.isEmpty()
+ assert !clientEventService.eventSubscriptions.sessionSubscriptionIdMap.containsKey(mqttClientId)
}
when: "a mqtt client subscribes to all attributes of an asset"
@@ -122,7 +124,8 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {
assert client.topicConsumerMap.get(topic) != null
assert client.topicConsumerMap.get(topic).size() == 1
assert mqttBrokerService.clientIdConnectionMap.get(mqttClientId) != null
- assert mqttBrokerService.clientIdConnectionMap.get(mqttClientId).subscriptionHandlerMap.size() == 1
+ assert clientEventService.eventSubscriptions.sessionSubscriptionIdMap.containsKey(mqttClientId)
+ assert clientEventService.eventSubscriptions.sessionSubscriptionIdMap.get(mqttClientId).size() == 1
}
when: "An attribute event occurs for a subscribed attribute"
@@ -204,7 +207,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {
then: "No subscription should exist"
conditions.eventually {
assert client.topicConsumerMap.get(topic) == null
- assert mqttBrokerService.clientIdConnectionMap.get(mqttClientId).subscriptionHandlerMap.size() == 0
+ assert !clientEventService.eventSubscriptions.sessionSubscriptionIdMap.containsKey(mqttClientId)
}
when: "another asset attribute changed without any subscriptions"
@@ -224,7 +227,8 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {
conditions.eventually {
assert client.topicConsumerMap.get(topic) != null
assert client.topicConsumerMap.get(topic).size() == 1
- assert mqttBrokerService.clientIdConnectionMap.get(mqttClientId).subscriptionHandlerMap.containsKey(topic)
+ assert clientEventService.eventSubscriptions.sessionSubscriptionIdMap.containsKey(mqttClientId)
+ assert clientEventService.eventSubscriptions.sessionSubscriptionIdMap.get(mqttClientId).size() == 1
}
when: "that attribute changes"
@@ -256,7 +260,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {
then: "No subscription should exist"
conditions.eventually {
assert client.topicConsumerMap.get(topic) == null
- assert mqttBrokerService.clientIdConnectionMap.get(mqttClientId).subscriptionHandlerMap.size() == 0
+ assert !clientEventService.eventSubscriptions.sessionSubscriptionIdMap.containsKey(mqttClientId)
}
when: "a mqtt client subscribes to attributes for descendants of an asset"
@@ -267,7 +271,8 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {
conditions.eventually {
assert client.topicConsumerMap.get(topic) != null
assert client.topicConsumerMap.get(topic).size() == 1
- assert mqttBrokerService.clientIdConnectionMap.get(mqttClientId).subscriptionHandlerMap.size() == 1
+ assert clientEventService.eventSubscriptions.sessionSubscriptionIdMap.containsKey(mqttClientId)
+ assert clientEventService.eventSubscriptions.sessionSubscriptionIdMap.get(mqttClientId).size() == 1
}
when: "a child asset of the subscription attribute event occurs"
@@ -295,8 +300,8 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {
conditions.eventually {
assert client.topicConsumerMap.get(topic) != null
assert client.topicConsumerMap.get(topic).size() == 1
- assert mqttBrokerService.clientIdConnectionMap.get(mqttClientId).subscriptionHandlerMap.size() == 2
- assert mqttBrokerService.clientIdConnectionMap.get(mqttClientId).subscriptionHandlerMap.containsKey(topic)
+ assert clientEventService.eventSubscriptions.sessionSubscriptionIdMap.containsKey(mqttClientId)
+ assert clientEventService.eventSubscriptions.sessionSubscriptionIdMap.get(mqttClientId).size() == 2
}
when: "a subscribed attribute changes"
@@ -337,7 +342,8 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {
conditions.eventually {
assert client.topicConsumerMap.get(topic) == null
assert client.topicConsumerMap.size() == 1
- assert mqttBrokerService.clientIdConnectionMap.get(mqttClientId).subscriptionHandlerMap.size() == 1
+ assert clientEventService.eventSubscriptions.sessionSubscriptionIdMap.containsKey(mqttClientId)
+ assert clientEventService.eventSubscriptions.sessionSubscriptionIdMap.get(mqttClientId).size() == 1
}
when: "a client disconnects"
@@ -365,7 +371,8 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {
then: "A subscription should exist"
conditions.eventually {
- assert mqttBrokerService.clientIdConnectionMap.get(mqttClientId).subscriptionHandlerMap.containsKey(topic)
+ assert clientEventService.eventSubscriptions.sessionSubscriptionIdMap.containsKey(mqttClientId)
+ assert clientEventService.eventSubscriptions.sessionSubscriptionIdMap.get(mqttClientId).size() == 1
}
when: "an asset is updated with a new attribute"
@@ -390,8 +397,8 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {
then: "the subscription should exist"
conditions.eventually {
assert client.topicConsumerMap.get(topic) != null
- assert mqttBrokerService.clientIdConnectionMap.get(mqttClientId).subscriptionHandlerMap.size() == 2
- assert mqttBrokerService.clientIdConnectionMap.get(mqttClientId).subscriptionHandlerMap.containsKey(topic)
+ assert clientEventService.eventSubscriptions.sessionSubscriptionIdMap.containsKey(mqttClientId)
+ assert clientEventService.eventSubscriptions.sessionSubscriptionIdMap.get(mqttClientId).size() == 2
}
when: "an asset is added as a descendant to the subscribed asset"
@@ -415,7 +422,8 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {
then: "only one subscription should be left"
conditions.eventually {
assert client.topicConsumerMap.get(topic) == null
- assert mqttBrokerService.clientIdConnectionMap.get(mqttClientId).subscriptionHandlerMap.size() == 1
+ assert clientEventService.eventSubscriptions.sessionSubscriptionIdMap.containsKey(mqttClientId)
+ assert clientEventService.eventSubscriptions.sessionSubscriptionIdMap.get(mqttClientId).size() == 1
}
when: "the descendant asset is modified"