Search in sources :

Example 1 with SendToUser

use of org.springframework.messaging.simp.annotation.SendToUser in project spring-framework by spring-projects.

the class SendToMethodReturnValueHandler method handleReturnValue.

@Override
public void handleReturnValue(Object returnValue, MethodParameter returnType, Message<?> message) throws Exception {
    if (returnValue == null) {
        return;
    }
    MessageHeaders headers = message.getHeaders();
    String sessionId = SimpMessageHeaderAccessor.getSessionId(headers);
    PlaceholderResolver varResolver = initVarResolver(headers);
    Object annotation = findAnnotation(returnType);
    if (annotation != null && annotation instanceof SendToUser) {
        SendToUser sendToUser = (SendToUser) annotation;
        boolean broadcast = sendToUser.broadcast();
        String user = getUserName(message, headers);
        if (user == null) {
            if (sessionId == null) {
                throw new MissingSessionUserException(message);
            }
            user = sessionId;
            broadcast = false;
        }
        String[] destinations = getTargetDestinations(sendToUser, message, this.defaultUserDestinationPrefix);
        for (String destination : destinations) {
            destination = this.placeholderHelper.replacePlaceholders(destination, varResolver);
            if (broadcast) {
                this.messagingTemplate.convertAndSendToUser(user, destination, returnValue, createHeaders(null, returnType));
            } else {
                this.messagingTemplate.convertAndSendToUser(user, destination, returnValue, createHeaders(sessionId, returnType));
            }
        }
    } else {
        SendTo sendTo = (SendTo) annotation;
        String[] destinations = getTargetDestinations(sendTo, message, this.defaultDestinationPrefix);
        for (String destination : destinations) {
            destination = this.placeholderHelper.replacePlaceholders(destination, varResolver);
            this.messagingTemplate.convertAndSend(destination, returnValue, createHeaders(sessionId, returnType));
        }
    }
}
Also used : SendToUser(org.springframework.messaging.simp.annotation.SendToUser) SendTo(org.springframework.messaging.handler.annotation.SendTo) MessageHeaders(org.springframework.messaging.MessageHeaders) PlaceholderResolver(org.springframework.util.PropertyPlaceholderHelper.PlaceholderResolver)

Aggregations

MessageHeaders (org.springframework.messaging.MessageHeaders)1 SendTo (org.springframework.messaging.handler.annotation.SendTo)1 SendToUser (org.springframework.messaging.simp.annotation.SendToUser)1 PlaceholderResolver (org.springframework.util.PropertyPlaceholderHelper.PlaceholderResolver)1