org.springframework.messaging.simp.user

## Class DefaultUserDestinationResolver

- java.lang.Object
- - org.springframework.messaging.simp.user.DefaultUserDestinationResolver

- All Implemented Interfaces:
    UserDestinationResolver

      public class DefaultUserDestinationResolver
      extends Object
      implements UserDestinationResolver

  A default implementation of `UserDestinationResolver` that relies on the `UserSessionRegistry` provided to the constructor to find the sessionIds associated with a user and then uses the sessionId to make the target destination unique.

  When a user attempts to subscribe to "/user/queue/position-updates", the "/user" prefix is removed and a unique suffix added, resulting in something like "/queue/position-updates-useri9oqdfzo" where the suffix is based on the user's session and ensures it does not collide with any other users attempting to subscribe to "/user/queue/position-updates".

  When a message is sent to a user with a destination such as "/user/{username}/queue/position-updates", the "/user/{username}" prefix is removed and the suffix added, resulting in something like "/queue/position-updates-useri9oqdfzo".

  Since:
    4.0
  Author:
    Rossen Stoyanchev

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `DefaultUserDestinationResolver(UserSessionRegistry userSessionRegistry)` Create an instance that will access user session id information through the provided registry.

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `protected boolean` | `checkDestination(String destination, String requiredPrefix)`
    `String` | `getDestinationPrefix()` Return the prefix used to identify user destinations.
    `protected String` | `getTargetDestination(String sourceDestination, String sourceDestinationWithoutPrefix, String sessionId, String user)` Return the target destination to use.
    `UserSessionRegistry` | `getUserSessionRegistry()` Return the configured `UserSessionRegistry`.
    `UserDestinationResult` | `resolveDestination(Message<?> message)` Resolve the destination of the message to a set of actual target destinations.
    `void` | `setUserDestinationPrefix(String prefix)` The prefix used to identify user destinations.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Constructor Detail

    - #### DefaultUserDestinationResolver

          public DefaultUserDestinationResolver(UserSessionRegistry userSessionRegistry)

      Create an instance that will access user session id information through the provided registry.
      Parameters:
        `userSessionRegistry` - the registry, never `null`

  - ### Method Detail

    - #### setUserDestinationPrefix

          public void setUserDestinationPrefix(String prefix)

      The prefix used to identify user destinations. Any destinations that do not start with the given prefix are not be resolved.

      The default value is "/user/".

      Parameters:
        `prefix` - the prefix to use

    - #### getDestinationPrefix

          public String getDestinationPrefix()

      Return the prefix used to identify user destinations. Any destinations that do not start with the given prefix are not be resolved.

      By default "/user/queue/".

    - #### getUserSessionRegistry

          public UserSessionRegistry getUserSessionRegistry()

      Return the configured `UserSessionRegistry`.

    - #### resolveDestination

          public UserDestinationResult resolveDestination(Message<?> message)

      Description copied from interface: `UserDestinationResolver`
      Resolve the destination of the message to a set of actual target destinations.

      If the message is SUBSCRIBE/UNSUBSCRIBE, the returned set will contain a single translated target destination.

      If the message represents data being sent to a user, the returned set may contain multiple target destinations, one for each active user session.

      Specified by:
        `resolveDestination` in interface `UserDestinationResolver`
      Parameters:
        `message` - the message with a user destination to be resolved
      Returns:
        the result of the resolution, or `null` if the resolution fails (e.g. not a user destination, or no user info available, etc)

    - #### checkDestination

          protected boolean checkDestination(String destination,
                                             String requiredPrefix)

    - #### getTargetDestination

          protected String getTargetDestination(String sourceDestination,
                                                String sourceDestinationWithoutPrefix,
                                                String sessionId,
                                                String user)

      Return the target destination to use. Provided as input are the original source destination, as well as the same destination with the target prefix removed.
      Parameters:
        `sourceDestination` - the source destination from the input message
        `sourceDestinationWithoutPrefix` - the source destination with the target prefix removed
        `sessionId` - an active user session id
        `user` - the user
      Returns:
        the target destination