Example usage for org.springframework.messaging.simp SimpMessageHeaderAccessor getUser

List of usage examples for org.springframework.messaging.simp SimpMessageHeaderAccessor getUser

Introduction

In this page you can find the example usage for org.springframework.messaging.simp SimpMessageHeaderAccessor getUser.

Prototype

@Nullable
public Principal getUser() 

Source Link

Document

Return the user associated with the current session.

Usage

From source file:org.springframework.messaging.simp.handler.DefaultUserDestinationResolver.java

private UserDestinationInfo getUserDestinationInfo(SimpMessageHeaderAccessor headers) {

    String destination = headers.getDestination();

    String targetUser;/*from   w ww  . ja v a2 s  . c o  m*/
    String targetDestination;

    Principal user = headers.getUser();
    SimpMessageType messageType = headers.getMessageType();

    if (SimpMessageType.SUBSCRIBE.equals(messageType) || SimpMessageType.UNSUBSCRIBE.equals(messageType)) {
        if (!checkDestination(destination, this.subscriptionDestinationPrefix)) {
            return null;
        }
        if (user == null) {
            logger.warn("Ignoring message, no user information");
            return null;
        }
        targetUser = user.getName();
        targetDestination = destination.substring(this.destinationPrefix.length() - 1);
    } else if (SimpMessageType.MESSAGE.equals(messageType)) {
        if (!checkDestination(destination, this.destinationPrefix)) {
            return null;
        }
        int startIndex = this.destinationPrefix.length();
        int endIndex = destination.indexOf('/', startIndex);
        Assert.isTrue(endIndex > 0, "Expected destination pattern \"/user/{userId}/**\"");
        targetUser = destination.substring(startIndex, endIndex);
        targetDestination = destination.substring(endIndex);

    } else {
        if (logger.isTraceEnabled()) {
            logger.trace("Ignoring " + messageType + " message");
        }
        return null;
    }

    return new UserDestinationInfo(targetUser, targetDestination);
}