Search in sources :

Example 41 with Message

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

the class SubscriptionMethodReturnValueHandlerTests method testMessageSentToChannel.

@Test
public void testMessageSentToChannel() throws Exception {
    given(this.messageChannel.send(any(Message.class))).willReturn(true);
    String sessionId = "sess1";
    String subscriptionId = "subs1";
    String destination = "/dest";
    Message<?> inputMessage = createInputMessage(sessionId, subscriptionId, destination, null);
    this.handler.handleReturnValue(PAYLOAD, this.subscribeEventReturnType, inputMessage);
    verify(this.messageChannel).send(this.messageCaptor.capture());
    assertNotNull(this.messageCaptor.getValue());
    Message<?> message = this.messageCaptor.getValue();
    SimpMessageHeaderAccessor headerAccessor = SimpMessageHeaderAccessor.wrap(message);
    assertNull("SimpMessageHeaderAccessor should have disabled id", headerAccessor.getId());
    assertNull("SimpMessageHeaderAccessor should have disabled timestamp", headerAccessor.getTimestamp());
    assertEquals(sessionId, headerAccessor.getSessionId());
    assertEquals(subscriptionId, headerAccessor.getSubscriptionId());
    assertEquals(destination, headerAccessor.getDestination());
    assertEquals(MIME_TYPE, headerAccessor.getContentType());
    assertEquals(this.subscribeEventReturnType, headerAccessor.getHeader(SimpMessagingTemplate.CONVERSION_HINT_HEADER));
}
Also used : Message(org.springframework.messaging.Message) SimpMessageHeaderAccessor(org.springframework.messaging.simp.SimpMessageHeaderAccessor) Test(org.junit.Test)

Example 42 with Message

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

the class MessageMethodArgumentResolverTests method resolveWithConversion.

@Test
public void resolveWithConversion() throws Exception {
    Message<String> message = MessageBuilder.withPayload("test").build();
    MethodParameter parameter = new MethodParameter(this.method, 1);
    when(this.converter.fromMessage(message, Integer.class)).thenReturn(4);
    @SuppressWarnings("unchecked") Message<Integer> actual = (Message<Integer>) this.resolver.resolveArgument(parameter, message);
    assertNotNull(actual);
    assertSame(message.getHeaders(), actual.getHeaders());
    assertEquals(new Integer(4), actual.getPayload());
}
Also used : ErrorMessage(org.springframework.messaging.support.ErrorMessage) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) MethodParameter(org.springframework.core.MethodParameter) Test(org.junit.Test)

Example 43 with Message

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

the class StompSubProtocolHandlerTests method handleMessageFromClientWithImmutableMessageInterceptor.

@Test
public void handleMessageFromClientWithImmutableMessageInterceptor() {
    AtomicReference<Boolean> mutable = new AtomicReference<>();
    ExecutorSubscribableChannel channel = new ExecutorSubscribableChannel();
    channel.addInterceptor(new ChannelInterceptorAdapter() {

        @Override
        public Message<?> preSend(Message<?> message, MessageChannel channel) {
            mutable.set(MessageHeaderAccessor.getAccessor(message, MessageHeaderAccessor.class).isMutable());
            return message;
        }
    });
    channel.addInterceptor(new ImmutableMessageChannelInterceptor());
    StompSubProtocolHandler handler = new StompSubProtocolHandler();
    handler.afterSessionStarted(this.session, channel);
    TextMessage message = StompTextMessageBuilder.create(StompCommand.CONNECT).build();
    handler.handleMessageFromClient(this.session, message, channel);
    assertNotNull(mutable.get());
    assertTrue(mutable.get());
}
Also used : ExecutorSubscribableChannel(org.springframework.messaging.support.ExecutorSubscribableChannel) ChannelInterceptorAdapter(org.springframework.messaging.support.ChannelInterceptorAdapter) TextMessage(org.springframework.web.socket.TextMessage) Message(org.springframework.messaging.Message) WebSocketMessage(org.springframework.web.socket.WebSocketMessage) BinaryMessage(org.springframework.web.socket.BinaryMessage) ImmutableMessageChannelInterceptor(org.springframework.messaging.support.ImmutableMessageChannelInterceptor) AtomicReference(java.util.concurrent.atomic.AtomicReference) MessageChannel(org.springframework.messaging.MessageChannel) SimpMessageHeaderAccessor(org.springframework.messaging.simp.SimpMessageHeaderAccessor) MessageHeaderAccessor(org.springframework.messaging.support.MessageHeaderAccessor) TextMessage(org.springframework.web.socket.TextMessage) Test(org.junit.Test)

Example 44 with Message

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

the class StompSubProtocolHandlerTests method handleMessageFromClientWithoutImmutableMessageInterceptor.

@Test
public void handleMessageFromClientWithoutImmutableMessageInterceptor() {
    AtomicReference<Boolean> mutable = new AtomicReference<>();
    ExecutorSubscribableChannel channel = new ExecutorSubscribableChannel();
    channel.addInterceptor(new ChannelInterceptorAdapter() {

        @Override
        public Message<?> preSend(Message<?> message, MessageChannel channel) {
            mutable.set(MessageHeaderAccessor.getAccessor(message, MessageHeaderAccessor.class).isMutable());
            return message;
        }
    });
    StompSubProtocolHandler handler = new StompSubProtocolHandler();
    handler.afterSessionStarted(this.session, channel);
    TextMessage message = StompTextMessageBuilder.create(StompCommand.CONNECT).build();
    handler.handleMessageFromClient(this.session, message, channel);
    assertNotNull(mutable.get());
    assertFalse(mutable.get());
}
Also used : ExecutorSubscribableChannel(org.springframework.messaging.support.ExecutorSubscribableChannel) ChannelInterceptorAdapter(org.springframework.messaging.support.ChannelInterceptorAdapter) MessageChannel(org.springframework.messaging.MessageChannel) TextMessage(org.springframework.web.socket.TextMessage) Message(org.springframework.messaging.Message) WebSocketMessage(org.springframework.web.socket.WebSocketMessage) BinaryMessage(org.springframework.web.socket.BinaryMessage) AtomicReference(java.util.concurrent.atomic.AtomicReference) SimpMessageHeaderAccessor(org.springframework.messaging.simp.SimpMessageHeaderAccessor) MessageHeaderAccessor(org.springframework.messaging.support.MessageHeaderAccessor) TextMessage(org.springframework.web.socket.TextMessage) Test(org.junit.Test)

Example 45 with Message

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

the class WebSocketStompClientTests method handleWebSocketMessageBinary.

@Test
@SuppressWarnings({ "unchecked", "rawtypes" })
public void handleWebSocketMessageBinary() throws Exception {
    String text = "SEND\na:alpha\n\nMessage payload\0";
    connect().handleMessage(this.webSocketSession, new BinaryMessage(text.getBytes(StandardCharsets.UTF_8)));
    ArgumentCaptor<Message> captor = ArgumentCaptor.forClass(Message.class);
    verify(this.stompSession).handleMessage(captor.capture());
    Message<byte[]> message = captor.getValue();
    assertNotNull(message);
    StompHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(message, StompHeaderAccessor.class);
    StompHeaders headers = StompHeaders.readOnlyStompHeaders(accessor.toNativeHeaderMap());
    assertEquals(StompCommand.SEND, accessor.getCommand());
    assertEquals("alpha", headers.getFirst("a"));
    assertEquals("Message payload", new String(message.getPayload(), StandardCharsets.UTF_8));
}
Also used : TextMessage(org.springframework.web.socket.TextMessage) PongMessage(org.springframework.web.socket.PongMessage) Message(org.springframework.messaging.Message) BinaryMessage(org.springframework.web.socket.BinaryMessage) StompHeaderAccessor(org.springframework.messaging.simp.stomp.StompHeaderAccessor) StompHeaders(org.springframework.messaging.simp.stomp.StompHeaders) BinaryMessage(org.springframework.web.socket.BinaryMessage) Test(org.junit.Test)

Aggregations

Message (org.springframework.messaging.Message)51 Test (org.junit.Test)41 SimpMessageHeaderAccessor (org.springframework.messaging.simp.SimpMessageHeaderAccessor)16 MessageHeaders (org.springframework.messaging.MessageHeaders)14 StompHeaderAccessor (org.springframework.messaging.simp.stomp.StompHeaderAccessor)9 BinaryMessage (org.springframework.web.socket.BinaryMessage)9 TextMessage (org.springframework.web.socket.TextMessage)9 WebSocketMessage (org.springframework.web.socket.WebSocketMessage)6 ByteBuffer (java.nio.ByteBuffer)5 MessageChannel (org.springframework.messaging.MessageChannel)4 MessageHeaderAccessor (org.springframework.messaging.support.MessageHeaderAccessor)4 Method (java.lang.reflect.Method)3 MethodParameter (org.springframework.core.MethodParameter)3 TestPrincipal (org.springframework.messaging.simp.TestPrincipal)3 Principal (java.security.Principal)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 TextMessage (javax.jms.TextMessage)2 SynthesizingMethodParameter (org.springframework.core.annotation.SynthesizingMethodParameter)2 StubTextMessage (org.springframework.jms.StubTextMessage)2 MessagingException (org.springframework.messaging.MessagingException)2