Class MqttContext

  • All Implemented Interfaces:
    org.eclipse.hono.util.ExecutionContext, org.eclipse.hono.util.TelemetryExecutionContext

    public final class MqttContext
    extends org.eclipse.hono.util.MapBasedTelemetryExecutionContext
    A dictionary of relevant information required during the processing of an MQTT message published by a device.
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  MqttContext.ErrorHandlingMode
      Modes defining how to handle errors raised when a device publishes a message.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void acknowledge()
      Sends a PUBACK for the message to the device.
      void applyMappedTargetAddress​(org.eclipse.hono.util.ResourceIdentifier mappedTargetAddress)
      Apply a mapped targetAddress to this mqtt context.
      org.eclipse.hono.auth.Device authenticatedDevice()
      Gets the identity of the authenticated device that has published the message.
      String contentType()
      Gets the content type of the message payload.
      String correlationId()
      Gets the correlation identifier taken either from a corresponding property bag value or the message packet id.
      io.vertx.mqtt.MqttEndpoint deviceEndpoint()
      Gets the MQTT endpoint over which the message has been received.
      String deviceId()
      Gets the identifier of the device that the message originates from.
      MetricsTags.EndpointType endpoint()
      Gets the endpoint that the message has been published to.
      static MqttContext fromPublishPacket​(io.vertx.mqtt.messages.MqttPublishMessage publishedMessage, io.vertx.mqtt.MqttEndpoint deviceEndpoint, io.opentracing.Span span)
      Creates a new context for a published message.
      static MqttContext fromPublishPacket​(io.vertx.mqtt.messages.MqttPublishMessage publishedMessage, io.vertx.mqtt.MqttEndpoint deviceEndpoint, io.opentracing.Span span, org.eclipse.hono.auth.Device authenticatedDevice)
      Creates a new context for a published message.
      MqttContext.ErrorHandlingMode getErrorHandlingMode​(boolean errorSubscriptionExists)
      Gets the mode defining how to handle errors raised when a device publishes a message.
      String getOrigAddress()
      org.eclipse.hono.util.QoS getRequestedQos()  
      io.micrometer.core.instrument.Timer.Sample getTimer()
      Gets the object used for measuring the time it takes to process this request.
      Optional<Duration> getTimeToLive()
      boolean isAtLeastOnce()
      Checks if the message has been published using QoS 1.
      io.vertx.mqtt.messages.MqttPublishMessage message()
      Gets the MQTT message to process.
      PropertyBag propertyBag()
      Gets the property bag object from the property-bag set in the message's topic.
      void setContentType​(String contentType)
      Sets the content type of the message payload.
      void setTimer​(io.micrometer.core.instrument.Timer.Sample timer)
      Sets the object to use for measuring the time it takes to process this request.
      String tenant()
      Gets the tenant that the device belongs to that published the message.
      org.eclipse.hono.util.ResourceIdentifier topic()
      Gets the topic that the message has been published to.
      • Methods inherited from class org.eclipse.hono.util.MapBasedTelemetryExecutionContext

        getAuthenticatedDevice
      • Methods inherited from class org.eclipse.hono.util.MapBasedExecutionContext

        get, get, getTracingContext, getTracingSpan, put
      • Methods inherited from interface org.eclipse.hono.util.ExecutionContext

        get, get, getTracingContext, getTracingSpan, put
      • Methods inherited from interface org.eclipse.hono.util.TelemetryExecutionContext

        getDownstreamMessageProperties, isDeviceAuthenticated
    • Method Detail

      • fromPublishPacket

        public static MqttContext fromPublishPacket​(io.vertx.mqtt.messages.MqttPublishMessage publishedMessage,
                                                    io.vertx.mqtt.MqttEndpoint deviceEndpoint,
                                                    io.opentracing.Span span)
        Creates a new context for a published message.
        Parameters:
        publishedMessage - The MQTT message to process.
        deviceEndpoint - The endpoint representing the device that has published the message.
        span - The OpenTracing root span that is used to track the processing of this context.
        Returns:
        The context.
        Throws:
        NullPointerException - if any of the parameters is null.
      • fromPublishPacket

        public static MqttContext fromPublishPacket​(io.vertx.mqtt.messages.MqttPublishMessage publishedMessage,
                                                    io.vertx.mqtt.MqttEndpoint deviceEndpoint,
                                                    io.opentracing.Span span,
                                                    org.eclipse.hono.auth.Device authenticatedDevice)
        Creates a new context for a published message.
        Parameters:
        publishedMessage - The published MQTT message.
        deviceEndpoint - The endpoint representing the device that has published the message.
        span - The OpenTracing root span that is used to track the processing of this context.
        authenticatedDevice - The authenticated device identity (may be null).
        Returns:
        The context.
        Throws:
        NullPointerException - if any of the parameters except authenticatedDevice is null.
      • getRequestedQos

        public org.eclipse.hono.util.QoS getRequestedQos()
      • message

        public io.vertx.mqtt.messages.MqttPublishMessage message()
        Gets the MQTT message to process.
        Returns:
        The message.
      • deviceEndpoint

        public io.vertx.mqtt.MqttEndpoint deviceEndpoint()
        Gets the MQTT endpoint over which the message has been received.
        Returns:
        The endpoint.
      • authenticatedDevice

        public org.eclipse.hono.auth.Device authenticatedDevice()
        Gets the identity of the authenticated device that has published the message.
        Returns:
        The identity or null if the device has not been authenticated.
      • contentType

        public String contentType()
        Gets the content type of the message payload.

        The type has either been set via setContentType(String) or it is the type determined from the message topic's property bag, if it contains a content type.

        Returns:
        The type of the message payload or null if the type is unknown.
      • setContentType

        public void setContentType​(String contentType)
        Sets the content type of the message payload.

        This overwrites the content type determined from the message topic's property bag (if any).

        Parameters:
        contentType - The type.
        Throws:
        NullPointerException - if contentType is null.
      • topic

        public org.eclipse.hono.util.ResourceIdentifier topic()
        Gets the topic that the message has been published to.
        Returns:
        The topic or null if the topic could not be parsed into a resource identifier.
      • applyMappedTargetAddress

        public void applyMappedTargetAddress​(org.eclipse.hono.util.ResourceIdentifier mappedTargetAddress)
        Apply a mapped targetAddress to this mqtt context.

        The device identifier included in the address overrides the device identifier derived from the message topic.

        Parameters:
        mappedTargetAddress - The mappedTargetAddress.
      • tenant

        public String tenant()
        Gets the tenant that the device belongs to that published the message.
        Returns:
        The tenant identifier or null if the device is not authenticated and the message's topic does not contain a tenant identifier.
      • deviceId

        public String deviceId()
        Gets the identifier of the device that the message originates from.

        It is taken from the message topic or, if not set there, from the authenticated device.

        In a scenario of a gateway sending a message on behalf of an edge device, the returned identifier is the edge device identifier.

        Returns:
        The device identifier or null if the topic of the message does not contain a device identifier and the device is not authenticated.
      • correlationId

        public String correlationId()
        Gets the correlation identifier taken either from a corresponding property bag value or the message packet id.
        Returns:
        The correlation identifier or null in case of a QoS 0 message with no corresponding property bag value.
      • propertyBag

        public PropertyBag propertyBag()
        Gets the property bag object from the property-bag set in the message's topic.
        Returns:
        The property bag (which may be empty) or null if the topic is null or empty.
      • endpoint

        public MetricsTags.EndpointType endpoint()
        Gets the endpoint that the message has been published to.

        The name is determined from the endpoint path segment of the topic that the message has been published to.

        Returns:
        The endpoint or null if the message does not contain a topic.
      • isAtLeastOnce

        public boolean isAtLeastOnce()
        Checks if the message has been published using QoS 1.
        Returns:
        true if the message has been published using QoS 1.
      • acknowledge

        public void acknowledge()
        Sends a PUBACK for the message to the device.
      • setTimer

        public void setTimer​(io.micrometer.core.instrument.Timer.Sample timer)
        Sets the object to use for measuring the time it takes to process this request.
        Parameters:
        timer - The timer.
      • getTimer

        public io.micrometer.core.instrument.Timer.Sample getTimer()
        Gets the object used for measuring the time it takes to process this request.
        Returns:
        The timer or null if not set.
      • getErrorHandlingMode

        public MqttContext.ErrorHandlingMode getErrorHandlingMode​(boolean errorSubscriptionExists)
        Gets the mode defining how to handle errors raised when a device publishes a message.
        Parameters:
        errorSubscriptionExists - true if the device has an error topic subscription.
        Returns:
        the effective mode.
      • getOrigAddress

        public String getOrigAddress()
        Returns:
        The message topic.
      • getTimeToLive

        public Optional<Duration> getTimeToLive()
        Returns:
        An optional containing the time-to-live duration or an empty optional if
        • the topic has no property bag
        • the property bag does not contain a Constants.HEADER_TIME_TO_LIVE property
        • the contained value cannot be parsed as a positive long