Class MQTTHandler

java.lang.Object
org.openremote.manager.mqtt.MQTTHandler
Direct Known Subclasses:
ConnectionMonitorHandler, DefaultMQTTHandler, UserAssetProvisioningMQTTHandler

public abstract class MQTTHandler extends Object
This allows custom handlers to be discovered by the MQTTBrokerService during system startup using the ServiceLoader mechanism. This allows topic(s) can be handled in a custom way. Any instances must have a no-arg constructor.
  • Field Details

    • TOKEN_MULTI_LEVEL_WILDCARD

      public static final String TOKEN_MULTI_LEVEL_WILDCARD
      See Also:
    • TOKEN_SINGLE_LEVEL_WILDCARD

      public static final String TOKEN_SINGLE_LEVEL_WILDCARD
      See Also:
    • clientEventService

      protected ClientEventService clientEventService
    • mqttBrokerService

      protected MQTTBrokerService mqttBrokerService
    • messageBrokerService

      protected org.openremote.container.message.MessageBrokerService messageBrokerService
    • identityProvider

      protected ManagerKeycloakIdentityProvider identityProvider
    • executorService

      protected ExecutorService executorService
    • timerService

      protected org.openremote.container.timer.TimerService timerService
    • isKeycloak

      protected boolean isKeycloak
    • clientSession

      protected org.apache.activemq.artemis.api.core.client.ClientSession clientSession
    • producer

      protected org.apache.activemq.artemis.api.core.client.ClientProducer producer
  • Constructor Details

    • MQTTHandler

      public MQTTHandler()
  • Method Details

    • getPriority

      public int getPriority()
      Gets the priority of this handler which is used to determine the call order; handlers with a lower priority are initialised and started first.
    • getName

      public String getName()
      Provides a name to identify this custom handler for logging purposes etc.
    • init

      public void init(org.openremote.model.Container container, org.apache.activemq.artemis.core.config.Configuration serverConfiguration) throws Exception
      Called when the system starts to allow for initialisation.
      Throws:
      Exception
    • start

      public void start(org.openremote.model.Container container) throws Exception
      Called when the system starts to allow for initialisation.
      Throws:
      Exception
    • stop

      public void stop() throws Exception
      Called when the system stops to allow for any cleanup.
      Throws:
      Exception
    • addPublishTopicServerConfiguration

      protected void addPublishTopicServerConfiguration(org.openremote.model.Container container, org.apache.activemq.artemis.core.config.Configuration serverConfiguration)
    • getPublishTopicAddressSettings

      protected org.apache.activemq.artemis.core.settings.impl.AddressSettings getPublishTopicAddressSettings(org.openremote.model.Container container, String publishTopic)
      Just enable metrics on the topic by default but allow custom handlers to add more configuration as required
    • addPublishConsumer

      protected void addPublishConsumer(String topic) throws Exception
      Throws:
      Exception
    • onConnect

      public void onConnect(org.apache.activemq.artemis.spi.core.protocol.RemotingConnection connection)
      Will be called when any client connects; if returns false then subsequent handlers will not be called
    • onDisconnect

      public void onDisconnect(org.apache.activemq.artemis.spi.core.protocol.RemotingConnection connection)
      Will be called when any client disconnects
    • onConnectionLost

      public void onConnectionLost(org.apache.activemq.artemis.spi.core.protocol.RemotingConnection connection)
      Will be called when any client loses connection
    • onConnectionAuthenticated

      public void onConnectionAuthenticated(org.apache.activemq.artemis.spi.core.protocol.RemotingConnection connection)
      Called when an existing RemotingConnection is authenticated sometime after initially connecting (see UserAssetProvisioningMQTTHandler); the authenticated Subject can be retrieved using RemotingConnection.getSubject()
    • handlesTopic

      public boolean handlesTopic(Topic topic)
    • checkCanSubscribe

      public boolean checkCanSubscribe(org.apache.activemq.artemis.spi.core.protocol.RemotingConnection connection, org.keycloak.KeycloakSecurityContext securityContext, Topic topic)
      Checks that authenticated session and topic realm matches the authenticated user and also that topic client ID matches the connection client ID.
    • checkCanPublish

      public boolean checkCanPublish(org.apache.activemq.artemis.spi.core.protocol.RemotingConnection connection, org.keycloak.KeycloakSecurityContext securityContext, Topic topic)
      Checks that authenticated sessions and topic realm matches the authenticated user and also that topic client ID matches the connection client ID.
    • onUserAssetLinksChanged

      public void onUserAssetLinksChanged(org.apache.activemq.artemis.spi.core.protocol.RemotingConnection connection, List<org.openremote.model.PersistenceEvent<org.openremote.model.asset.UserAssetLink>> changes)
      Called when UserAssetLinks for a restricted user are changed and that user has an active connection (subject can be accessed from the connection).
    • publishMessage

      public void publishMessage(String topic, Object data, io.netty.handler.codec.mqtt.MqttQoS qoS)
    • topicMatches

      protected abstract boolean topicMatches(Topic topic)
      Indicates if this handler will handle the specified topic; independent of whether it is a publish or subscribe. Should generally check the third token onwards unless handlesTopic(org.openremote.manager.mqtt.Topic) has been overridden.
    • getLogger

      protected abstract Logger getLogger()
    • canSubscribe

      public abstract boolean canSubscribe(org.apache.activemq.artemis.spi.core.protocol.RemotingConnection connection, org.keycloak.KeycloakSecurityContext securityContext, Topic topic)
      Called to authorise a subscription if handlesTopic(org.openremote.manager.mqtt.Topic) returned true; should return true if the subscription is allowed otherwise return false.
    • canPublish

      public abstract boolean canPublish(org.apache.activemq.artemis.spi.core.protocol.RemotingConnection connection, org.keycloak.KeycloakSecurityContext securityContext, Topic topic)
      Called to authorise a publish if handlesTopic(org.openremote.manager.mqtt.Topic) returned true; should return true if the publish is allowed otherwise return false.
    • onSubscribe

      public abstract void onSubscribe(org.apache.activemq.artemis.spi.core.protocol.RemotingConnection connection, Topic topic)
    • onUnsubscribe

      public abstract void onUnsubscribe(org.apache.activemq.artemis.spi.core.protocol.RemotingConnection connection, Topic topic)
      Called to handle unsubscribe if handlesTopic(org.openremote.manager.mqtt.Topic) returned true.
    • getPublishListenerTopics

      public abstract Set<String> getPublishListenerTopics()
      Get the set of topics this handler wants to subscribe to for incoming publish messages; messages that match these topics will be passed to onPublish(org.apache.activemq.artemis.spi.core.protocol.RemotingConnection, org.openremote.manager.mqtt.Topic, io.netty.buffer.ByteBuf).
    • onPublish

      public abstract void onPublish(org.apache.activemq.artemis.spi.core.protocol.RemotingConnection connection, Topic topic, io.netty.buffer.ByteBuf body)
    • topicRealm

      public static String topicRealm(Topic topic)
    • topicClientID

      public static String topicClientID(Topic topic)
    • topicRealmAllowed

      public static boolean topicRealmAllowed(org.keycloak.KeycloakSecurityContext securityContext, Topic topic)
    • topicClientIdMatches

      public static boolean topicClientIdMatches(org.apache.activemq.artemis.spi.core.protocol.RemotingConnection connection, Topic topic)
    • topicTokenCountGreaterThan

      public static boolean topicTokenCountGreaterThan(Topic topic, int size)
    • topicTokenIndexToString

      public static String topicTokenIndexToString(Topic topic, int tokenNumber)
    • getSubjectFromConnection

      protected static Subject getSubjectFromConnection(org.apache.activemq.artemis.spi.core.protocol.RemotingConnection connection)
    • getSecurityContextFromSubject

      protected static org.keycloak.KeycloakSecurityContext getSecurityContextFromSubject(Subject subject)
    • getAuthContextFromConnection

      protected static Optional<org.openremote.container.security.AuthContext> getAuthContextFromConnection(org.apache.activemq.artemis.spi.core.protocol.RemotingConnection connection)
    • getAuthContextFromSecurityContext

      protected static org.openremote.container.security.AuthContext getAuthContextFromSecurityContext(org.keycloak.KeycloakSecurityContext securityContext)