Search in sources :

Example 36 with SimpMessageHeaderAccessor

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

the class SendToMethodReturnValueHandlerTests method createMessage.

private Message<?> createMessage(String sessId, String subsId, String destPrefix, String dest, Principal user) {
    SimpMessageHeaderAccessor headerAccessor = SimpMessageHeaderAccessor.create();
    headerAccessor.setSessionId(sessId);
    headerAccessor.setSubscriptionId(subsId);
    if (dest != null && destPrefix != null) {
        headerAccessor.setDestination(destPrefix + dest);
        headerAccessor.setHeader(DestinationPatternsMessageCondition.LOOKUP_DESTINATION_HEADER, dest);
    }
    if (user != null) {
        headerAccessor.setUser(user);
    }
    return MessageBuilder.createMessage(new byte[0], headerAccessor.getMessageHeaders());
}
Also used : SimpMessageHeaderAccessor(org.springframework.messaging.simp.SimpMessageHeaderAccessor)

Example 37 with SimpMessageHeaderAccessor

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

the class SendToMethodReturnValueHandlerTests method testHeadersToSend.

@Test
public void testHeadersToSend() throws Exception {
    Message<?> message = createMessage("sess1", "sub1", "/app", "/dest", null);
    SimpMessageSendingOperations messagingTemplate = Mockito.mock(SimpMessageSendingOperations.class);
    SendToMethodReturnValueHandler handler = new SendToMethodReturnValueHandler(messagingTemplate, false);
    handler.handleReturnValue(PAYLOAD, this.noAnnotationsReturnType, message);
    ArgumentCaptor<MessageHeaders> captor = ArgumentCaptor.forClass(MessageHeaders.class);
    verify(messagingTemplate).convertAndSend(eq("/topic/dest"), eq(PAYLOAD), captor.capture());
    MessageHeaders headers = captor.getValue();
    SimpMessageHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(headers, SimpMessageHeaderAccessor.class);
    assertNotNull(accessor);
    assertTrue(accessor.isMutable());
    assertEquals("sess1", accessor.getSessionId());
    assertNull("Subscription id should not be copied", accessor.getSubscriptionId());
    assertEquals(this.noAnnotationsReturnType, accessor.getHeader(SimpMessagingTemplate.CONVERSION_HINT_HEADER));
}
Also used : SimpMessageSendingOperations(org.springframework.messaging.simp.SimpMessageSendingOperations) MessageHeaders(org.springframework.messaging.MessageHeaders) SimpMessageHeaderAccessor(org.springframework.messaging.simp.SimpMessageHeaderAccessor) Test(org.junit.Test)

Example 38 with SimpMessageHeaderAccessor

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

the class SimpAnnotationMethodMessageHandlerTests method simpScope.

@Test
public void simpScope() {
    Map<String, Object> sessionAttributes = new ConcurrentHashMap<>();
    sessionAttributes.put("name", "value");
    SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.create();
    headers.setSessionId("session1");
    headers.setSessionAttributes(sessionAttributes);
    headers.setDestination("/pre/scope");
    Message<?> message = MessageBuilder.withPayload(new byte[0]).setHeaders(headers).build();
    this.messageHandler.registerHandler(this.testController);
    this.messageHandler.handleMessage(message);
    assertEquals("scope", this.testController.method);
}
Also used : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) SimpMessageHeaderAccessor(org.springframework.messaging.simp.SimpMessageHeaderAccessor) Test(org.junit.Test)

Example 39 with SimpMessageHeaderAccessor

use of org.springframework.messaging.simp.SimpMessageHeaderAccessor 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 40 with SimpMessageHeaderAccessor

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

the class DefaultSubscriptionRegistryTests method createMessage.

private Message<?> createMessage(String destination) {
    SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.create();
    accessor.setDestination(destination);
    return MessageBuilder.createMessage("", accessor.getMessageHeaders());
}
Also used : SimpMessageHeaderAccessor(org.springframework.messaging.simp.SimpMessageHeaderAccessor)

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