Search in sources :

Example 6 with SendTo

use of org.springframework.messaging.handler.annotation.SendTo 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

SendTo (org.springframework.messaging.handler.annotation.SendTo)6 Room (com.mapia.domain.Room)4 MessageMapping (org.springframework.messaging.handler.annotation.MessageMapping)4 Method (java.lang.reflect.Method)1 MessageHeaders (org.springframework.messaging.MessageHeaders)1 InvocableHandlerMethod (org.springframework.messaging.handler.invocation.InvocableHandlerMethod)1 SendToUser (org.springframework.messaging.simp.annotation.SendToUser)1 PlaceholderResolver (org.springframework.util.PropertyPlaceholderHelper.PlaceholderResolver)1