Search in sources :

Example 46 with MessageHeaders

use of org.springframework.messaging.MessageHeaders in project spring-framework by spring-projects.

the class RestTemplateXhrTransportTests method connectReceiveAndCloseWithStompFrame.

@Test
public void connectReceiveAndCloseWithStompFrame() throws Exception {
    StompHeaderAccessor accessor = StompHeaderAccessor.create(StompCommand.SEND);
    accessor.setDestination("/destination");
    MessageHeaders headers = accessor.getMessageHeaders();
    Message<byte[]> message = MessageBuilder.createMessage("body".getBytes(StandardCharsets.UTF_8), headers);
    byte[] bytes = new StompEncoder().encode(message);
    TextMessage textMessage = new TextMessage(bytes);
    SockJsFrame frame = SockJsFrame.messageFrame(new Jackson2SockJsMessageCodec(), textMessage.getPayload());
    String body = "o\n" + frame.getContent() + "\n" + "c[3000,\"Go away!\"]";
    ClientHttpResponse response = response(HttpStatus.OK, body);
    connect(response);
    verify(this.webSocketHandler).afterConnectionEstablished(any());
    verify(this.webSocketHandler).handleMessage(any(), eq(textMessage));
    verify(this.webSocketHandler).afterConnectionClosed(any(), eq(new CloseStatus(3000, "Go away!")));
    verifyNoMoreInteractions(this.webSocketHandler);
}
Also used : Jackson2SockJsMessageCodec(org.springframework.web.socket.sockjs.frame.Jackson2SockJsMessageCodec) StompEncoder(org.springframework.messaging.simp.stomp.StompEncoder) StompHeaderAccessor(org.springframework.messaging.simp.stomp.StompHeaderAccessor) MessageHeaders(org.springframework.messaging.MessageHeaders) CloseStatus(org.springframework.web.socket.CloseStatus) SockJsFrame(org.springframework.web.socket.sockjs.frame.SockJsFrame) ClientHttpResponse(org.springframework.http.client.ClientHttpResponse) TextMessage(org.springframework.web.socket.TextMessage) Test(org.junit.Test)

Example 47 with MessageHeaders

use of org.springframework.messaging.MessageHeaders in project spring-framework by spring-projects.

the class SimpAttributes method fromMessage.

/**
	 * Extract the SiMP session attributes from the given message and
	 * wrap them in a {@link SimpAttributes} instance.
	 * @param message the message to extract session attributes from
	 */
public static SimpAttributes fromMessage(Message<?> message) {
    Assert.notNull(message, "Message must not be null");
    MessageHeaders headers = message.getHeaders();
    String sessionId = SimpMessageHeaderAccessor.getSessionId(headers);
    Map<String, Object> sessionAttributes = SimpMessageHeaderAccessor.getSessionAttributes(headers);
    if (sessionId == null) {
        throw new IllegalStateException("No session id in " + message);
    }
    if (sessionAttributes == null) {
        throw new IllegalStateException("No session attributes in " + message);
    }
    return new SimpAttributes(sessionId, sessionAttributes);
}
Also used : MessageHeaders(org.springframework.messaging.MessageHeaders)

Example 48 with MessageHeaders

use of org.springframework.messaging.MessageHeaders 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)

Example 49 with MessageHeaders

use of org.springframework.messaging.MessageHeaders in project spring-framework by spring-projects.

the class SubscriptionMethodReturnValueHandler method handleReturnValue.

@Override
public void handleReturnValue(Object returnValue, MethodParameter returnType, Message<?> message) throws Exception {
    if (returnValue == null) {
        return;
    }
    MessageHeaders headers = message.getHeaders();
    String destination = SimpMessageHeaderAccessor.getDestination(headers);
    String sessionId = SimpMessageHeaderAccessor.getSessionId(headers);
    String subscriptionId = SimpMessageHeaderAccessor.getSubscriptionId(headers);
    if (subscriptionId == null) {
        throw new IllegalStateException("No subscriptionId in " + message + " returned by: " + returnType.getMethod());
    }
    if (logger.isDebugEnabled()) {
        logger.debug("Reply to @SubscribeMapping: " + returnValue);
    }
    MessageHeaders headersToSend = createHeaders(sessionId, subscriptionId, returnType);
    this.messagingTemplate.convertAndSend(destination, returnValue, headersToSend);
}
Also used : MessageHeaders(org.springframework.messaging.MessageHeaders)

Example 50 with MessageHeaders

use of org.springframework.messaging.MessageHeaders in project spring-framework by spring-projects.

the class AbstractSubscriptionRegistry method registerSubscription.

@Override
public final void registerSubscription(Message<?> message) {
    MessageHeaders headers = message.getHeaders();
    SimpMessageType messageType = SimpMessageHeaderAccessor.getMessageType(headers);
    if (!SimpMessageType.SUBSCRIBE.equals(messageType)) {
        throw new IllegalArgumentException("Expected SUBSCRIBE: " + message);
    }
    String sessionId = SimpMessageHeaderAccessor.getSessionId(headers);
    if (sessionId == null) {
        if (logger.isErrorEnabled()) {
            logger.error("No sessionId in  " + message);
        }
        return;
    }
    String subscriptionId = SimpMessageHeaderAccessor.getSubscriptionId(headers);
    if (subscriptionId == null) {
        if (logger.isErrorEnabled()) {
            logger.error("No subscriptionId in " + message);
        }
        return;
    }
    String destination = SimpMessageHeaderAccessor.getDestination(headers);
    if (destination == null) {
        if (logger.isErrorEnabled()) {
            logger.error("No destination in " + message);
        }
        return;
    }
    addSubscriptionInternal(sessionId, subscriptionId, destination, message);
}
Also used : SimpMessageType(org.springframework.messaging.simp.SimpMessageType) MessageHeaders(org.springframework.messaging.MessageHeaders)

Aggregations

MessageHeaders (org.springframework.messaging.MessageHeaders)55 Test (org.junit.Test)39 HashMap (java.util.HashMap)15 Message (org.springframework.messaging.Message)10 SimpMessageType (org.springframework.messaging.simp.SimpMessageType)6 SimpMessageHeaderAccessor (org.springframework.messaging.simp.SimpMessageHeaderAccessor)5 MessageHeaderAccessor (org.springframework.messaging.support.MessageHeaderAccessor)4 MimeType (org.springframework.util.MimeType)3 Principal (java.security.Principal)2 HashSet (java.util.HashSet)2 MessageConverter (org.springframework.messaging.converter.MessageConverter)2 StringMessageConverter (org.springframework.messaging.converter.StringMessageConverter)2 TestPrincipal (org.springframework.messaging.simp.TestPrincipal)2 StompHeaderAccessor (org.springframework.messaging.simp.stomp.StompHeaderAccessor)2 GenericMessage (org.springframework.messaging.support.GenericMessage)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Method (java.lang.reflect.Method)1 Charset (java.nio.charset.Charset)1 Map (java.util.Map)1