Java Code Examples for org.springframework.messaging.simp.user.UserDestinationResult

Following code examples demonstrate how to use org.springframework.messaging.simp.user.UserDestinationResultfrom spring-framework. These examples are extracted from various highly rated open source projects. You can directly use these code snippets or view their entire linked source code. These snippets are extracted to provide contextual information about how to use this class in the real world. These samples also let you understand some good practices on how to use org.springframework.messaging.simp.user.UserDestinationResultand various code implementation of this class.

    public UserDestinationResult resolveDestination(Message<?> message) {
        SimpMessageHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(
                message, SimpMessageHeaderAccessor.class);

        if (accessor == null) {
            return super.resolveDestination(message);
        }

        final String destination = accessor.getDestination();
        final String username = accessor.getUser() != null ? accessor.getUser().getName() : null;

        if (destination != null && destination.endsWith("echo.user")) {
            if (username != null) {
                String targetDestination = String.format("/exchange/amq.direct/echo.user-user%s", username);
                return new UserDestinationResult(destination, Collections.singleton(targetDestination),
                        destination, username);
            }
        }

        return super.resolveDestination(message);
    } 


    public UserDestinationResult resolveDestination(Message<?> message) {
        SimpMessageHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(message, SimpMessageHeaderAccessor.class);

        final String destination = accessor.getDestination();
        final String authUser = accessor.getUser() != null ? accessor.getUser().getName() : null;
        LOGGER.trace("Resolving user destination {} for authUser={}, messageType={}",
                destination, authUser, accessor.getMessageType());

        if (destination != null) {
            if (SimpMessageType.SUBSCRIBE.equals(accessor.getMessageType()) ||
                    SimpMessageType.UNSUBSCRIBE.equals(accessor.getMessageType())) {
                if (authUser != null) {
                    final Matcher authMatcher = USER_AUTHENTICATED_PATTERN.matcher(destination);
                    if (authMatcher.matches()) {
                        String result = String.format("/%s/users.%s.%s",
                                authMatcher.group("routing"), authUser, authMatcher.group("dest"));
                        UserDestinationResult userDestinationResult =
                                new UserDestinationResult(destination, Collections.singleton(result), result, authUser);
                        LOGGER.debug("Resolved {} for {} into {}", destination, authUser, userDestinationResult);
                        return userDestinationResult;
                    }
                }
            }
            else if (accessor.getMessageType().equals(SimpMessageType.MESSAGE)) {
                final Matcher prefixMatcher = USER_DEST_PREFIXING_PATTERN.matcher(destination);
                if (prefixMatcher.matches()) {
                    String user = prefixMatcher.group("name");
                    String result = String.format("/%s/users.%s.%s",
                            prefixMatcher.group("routing"), user, prefixMatcher.group("dest"));
                    UserDestinationResult userDestinationResult =
                            new UserDestinationResult(destination, Collections.singleton(result), result, user);
                    LOGGER.debug("Resolved {} into {}", destination, userDestinationResult);
                    return userDestinationResult;
                }
            }

        }

        LOGGER.trace("Destination {} is not user-based", destination);
        return null;
    } 

Advertisement
Javadoc
Contains the result from parsing a "user" destination from a source message and translating it to ta

rget destinations (one per active user session). @author Rossen Stoyanchev @since 4.0.2 @see org.springframework.messaging.simp.user.UserDestinationResolver

Read More
Advertisement