Class MqttTopicFilter

java.lang.Object
org.mockserver.async.subscribe.MqttTopicFilter

public final class MqttTopicFilter extends Object
Matching of concrete MQTT topic names against MQTT topic filters, per MQTT 3.1.1 §4.7 (the same wildcard semantics apply to MQTT 5).

A subscription may use wildcards, but a message always arrives on a concrete topic. Recorded messages are therefore stored under the concrete topic, while verification asks for the filter the user subscribed with — so the two must be reconciled by filter matching rather than by string equality.

Wildcards

  • + — single-level wildcard; matches exactly one topic level and must occupy an entire level (sport/+/score).
  • # — multi-level wildcard; must be the last level of the filter and matches that level and all levels beneath it. Because # includes the parent level, sport/# matches sport as well as sport/tennis/player1.

Reserved topics

Per §4.7.2 a leading wildcard does not match a topic beginning with $ (e.g. $SYS/...), so # does not match $SYS/broker/uptime while $SYS/# does.
  • Method Details

    • containsWildcard

      public static boolean containsWildcard(String filter)
      Returns:
      true if the given topic filter contains an MQTT wildcard character, and so cannot be compared to a concrete topic by equality alone.
    • isValidFilter

      public static boolean isValidFilter(String filter)
      Returns:
      true if the filter is well formed: + occupies a whole level and # occupies a whole level and appears only as the final level.
    • matches

      public static boolean matches(String filter, String topic)
      Match a concrete topic name against a topic filter.
      Parameters:
      filter - the (possibly wildcarded) topic filter subscribed with
      topic - the concrete topic a message arrived on
      Returns:
      true if the filter selects the topic