Search in sources :

Example 11 with SimpMessageHeaderAccessor

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

the class SendToMethodReturnValueHandler method createHeaders.

private MessageHeaders createHeaders(String sessionId, MethodParameter returnType) {
    SimpMessageHeaderAccessor headerAccessor = SimpMessageHeaderAccessor.create(SimpMessageType.MESSAGE);
    if (getHeaderInitializer() != null) {
        getHeaderInitializer().initHeaders(headerAccessor);
    }
    if (sessionId != null) {
        headerAccessor.setSessionId(sessionId);
    }
    headerAccessor.setHeader(SimpMessagingTemplate.CONVERSION_HINT_HEADER, returnType);
    headerAccessor.setLeaveMutable(true);
    return headerAccessor.getMessageHeaders();
}
Also used : SimpMessageHeaderAccessor(org.springframework.messaging.simp.SimpMessageHeaderAccessor)

Example 12 with SimpMessageHeaderAccessor

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

the class SendToMethodReturnValueHandlerTests method sendToUserSingleSession.

@Test
public void sendToUserSingleSession() throws Exception {
    given(this.messageChannel.send(any(Message.class))).willReturn(true);
    String sessionId = "sess1";
    TestUser user = new TestUser();
    Message<?> inputMessage = createMessage(sessionId, "sub1", null, null, user);
    this.handler.handleReturnValue(PAYLOAD, this.sendToUserSingleSessionReturnType, inputMessage);
    verify(this.messageChannel, times(2)).send(this.messageCaptor.capture());
    SimpMessageHeaderAccessor accessor = getCapturedAccessor(0);
    assertEquals(sessionId, accessor.getSessionId());
    assertEquals(MIME_TYPE, accessor.getContentType());
    assertEquals("/user/" + user.getName() + "/dest1", accessor.getDestination());
    assertNull("Subscription id should not be copied", accessor.getSubscriptionId());
    assertEquals(this.sendToUserSingleSessionReturnType, accessor.getHeader(SimpMessagingTemplate.CONVERSION_HINT_HEADER));
    accessor = getCapturedAccessor(1);
    assertEquals(sessionId, accessor.getSessionId());
    assertEquals("/user/" + user.getName() + "/dest2", accessor.getDestination());
    assertEquals(MIME_TYPE, accessor.getContentType());
    assertNull("Subscription id should not be copied", accessor.getSubscriptionId());
    assertEquals(this.sendToUserSingleSessionReturnType, accessor.getHeader(SimpMessagingTemplate.CONVERSION_HINT_HEADER));
}
Also used : Message(org.springframework.messaging.Message) SimpMessageHeaderAccessor(org.springframework.messaging.simp.SimpMessageHeaderAccessor) Test(org.junit.Test)

Example 13 with SimpMessageHeaderAccessor

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

the class SendToMethodReturnValueHandlerTests method sendToUser.

@Test
public void sendToUser() throws Exception {
    given(this.messageChannel.send(any(Message.class))).willReturn(true);
    String sessionId = "sess1";
    TestUser user = new TestUser();
    Message<?> inputMessage = createMessage(sessionId, "sub1", null, null, user);
    this.handler.handleReturnValue(PAYLOAD, this.sendToUserReturnType, inputMessage);
    verify(this.messageChannel, times(2)).send(this.messageCaptor.capture());
    SimpMessageHeaderAccessor accessor = getCapturedAccessor(0);
    assertNull(accessor.getSessionId());
    assertNull(accessor.getSubscriptionId());
    assertEquals("/user/" + user.getName() + "/dest1", accessor.getDestination());
    accessor = getCapturedAccessor(1);
    assertNull(accessor.getSessionId());
    assertNull(accessor.getSubscriptionId());
    assertEquals("/user/" + user.getName() + "/dest2", accessor.getDestination());
}
Also used : Message(org.springframework.messaging.Message) SimpMessageHeaderAccessor(org.springframework.messaging.simp.SimpMessageHeaderAccessor) Test(org.junit.Test)

Example 14 with SimpMessageHeaderAccessor

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

the class SendToMethodReturnValueHandlerTests method sendToUserDefaultDestinationSingleSession.

@Test
public void sendToUserDefaultDestinationSingleSession() throws Exception {
    given(this.messageChannel.send(any(Message.class))).willReturn(true);
    String sessionId = "sess1";
    TestUser user = new TestUser();
    Message<?> message = createMessage(sessionId, "sub1", "/app", "/dest", user);
    this.handler.handleReturnValue(PAYLOAD, this.sendToUserSingleSessionDefaultDestReturnType, message);
    verify(this.messageChannel, times(1)).send(this.messageCaptor.capture());
    SimpMessageHeaderAccessor accessor = getCapturedAccessor(0);
    assertEquals(sessionId, accessor.getSessionId());
    assertEquals("/user/" + user.getName() + "/queue/dest", accessor.getDestination());
    assertEquals(MIME_TYPE, accessor.getContentType());
    assertNull("Subscription id should not be copied", accessor.getSubscriptionId());
    assertEquals(this.sendToUserSingleSessionDefaultDestReturnType, accessor.getHeader(SimpMessagingTemplate.CONVERSION_HINT_HEADER));
}
Also used : Message(org.springframework.messaging.Message) SimpMessageHeaderAccessor(org.springframework.messaging.simp.SimpMessageHeaderAccessor) Test(org.junit.Test)

Example 15 with SimpMessageHeaderAccessor

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

the class SendToMethodReturnValueHandlerTests method sendToDefaultDestinationWhenUsingDotPathSeparator.

@Test
public void sendToDefaultDestinationWhenUsingDotPathSeparator() throws Exception {
    given(this.messageChannel.send(any(Message.class))).willReturn(true);
    Message<?> inputMessage = createMessage("sess1", "sub1", "/app/", "dest.foo.bar", null);
    this.handler.handleReturnValue(PAYLOAD, this.sendToDefaultDestReturnType, inputMessage);
    verify(this.messageChannel, times(1)).send(this.messageCaptor.capture());
    SimpMessageHeaderAccessor accessor = getCapturedAccessor(0);
    assertEquals("/topic/dest.foo.bar", accessor.getDestination());
}
Also used : Message(org.springframework.messaging.Message) SimpMessageHeaderAccessor(org.springframework.messaging.simp.SimpMessageHeaderAccessor) Test(org.junit.Test)

Aggregations

SimpMessageHeaderAccessor (org.springframework.messaging.simp.SimpMessageHeaderAccessor)54 Test (org.junit.Test)34 Message (org.springframework.messaging.Message)14 MessageHeaders (org.springframework.messaging.MessageHeaders)5 StompHeaderAccessor (org.springframework.messaging.simp.stomp.StompHeaderAccessor)5 TextMessage (org.springframework.web.socket.TextMessage)5 MessageChannel (org.springframework.messaging.MessageChannel)3 MessageDeliveryException (org.springframework.messaging.MessageDeliveryException)3 TestPrincipal (org.springframework.messaging.simp.TestPrincipal)3 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)3 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)3 HttpRequestHandler (org.springframework.web.HttpRequestHandler)3 Principal (java.security.Principal)2 LinkedHashMap (java.util.LinkedHashMap)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 MissingCsrfTokenException (org.springframework.security.web.csrf.MissingCsrfTokenException)2 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 MessageSendingOperations (org.springframework.messaging.core.MessageSendingOperations)1