Search in sources :

Example 61 with SimpMessageHeaderAccessor

use of org.springframework.messaging.simp.SimpMessageHeaderAccessor in project spring-framework-5.2.9.RELEASE by somepeopleHavingDream.

the class StompSubProtocolHandlerTests method handleMessageToClientWithSimpDisconnectAckAndReceipt.

@Test
public void handleMessageToClientWithSimpDisconnectAckAndReceipt() {
    StompHeaderAccessor accessor = StompHeaderAccessor.create(StompCommand.DISCONNECT);
    accessor.setReceipt("message-123");
    Message<?> connectMessage = MessageBuilder.createMessage(EMPTY_PAYLOAD, accessor.getMessageHeaders());
    SimpMessageHeaderAccessor ackAccessor = SimpMessageHeaderAccessor.create(SimpMessageType.DISCONNECT_ACK);
    ackAccessor.setHeader(SimpMessageHeaderAccessor.DISCONNECT_MESSAGE_HEADER, connectMessage);
    Message<byte[]> ackMessage = MessageBuilder.createMessage(EMPTY_PAYLOAD, ackAccessor.getMessageHeaders());
    this.protocolHandler.handleMessageToClient(this.session, ackMessage);
    assertThat(this.session.getSentMessages().size()).isEqualTo(1);
    TextMessage actual = (TextMessage) this.session.getSentMessages().get(0);
    assertThat(actual.getPayload()).isEqualTo(("RECEIPT\n" + "receipt-id:message-123\n" + "\n\u0000"));
}
Also used : StompHeaderAccessor(org.springframework.messaging.simp.stomp.StompHeaderAccessor) TextMessage(org.springframework.web.socket.TextMessage) SimpMessageHeaderAccessor(org.springframework.messaging.simp.SimpMessageHeaderAccessor) Test(org.junit.jupiter.api.Test)

Example 62 with SimpMessageHeaderAccessor

use of org.springframework.messaging.simp.SimpMessageHeaderAccessor in project spring-framework-5.2.9.RELEASE by somepeopleHavingDream.

the class StompSubProtocolHandlerTests method handleMessageToClientWithSimpConnectAck.

@Test
public void handleMessageToClientWithSimpConnectAck() {
    StompHeaderAccessor accessor = StompHeaderAccessor.create(StompCommand.CONNECT);
    accessor.setHeartbeat(10000, 10000);
    accessor.setAcceptVersion("1.0,1.1,1.2");
    Message<?> connectMessage = MessageBuilder.createMessage(EMPTY_PAYLOAD, accessor.getMessageHeaders());
    SimpMessageHeaderAccessor ackAccessor = SimpMessageHeaderAccessor.create(SimpMessageType.CONNECT_ACK);
    ackAccessor.setHeader(SimpMessageHeaderAccessor.CONNECT_MESSAGE_HEADER, connectMessage);
    ackAccessor.setHeader(SimpMessageHeaderAccessor.HEART_BEAT_HEADER, new long[] { 15000, 15000 });
    Message<byte[]> ackMessage = MessageBuilder.createMessage(EMPTY_PAYLOAD, ackAccessor.getMessageHeaders());
    this.protocolHandler.handleMessageToClient(this.session, ackMessage);
    assertThat(this.session.getSentMessages().size()).isEqualTo(1);
    TextMessage actual = (TextMessage) this.session.getSentMessages().get(0);
    assertThat(actual.getPayload()).isEqualTo(("CONNECTED\n" + "version:1.2\n" + "heart-beat:15000,15000\n" + "user-name:joe\n" + "\n" + "\u0000"));
}
Also used : StompHeaderAccessor(org.springframework.messaging.simp.stomp.StompHeaderAccessor) TextMessage(org.springframework.web.socket.TextMessage) SimpMessageHeaderAccessor(org.springframework.messaging.simp.SimpMessageHeaderAccessor) Test(org.junit.jupiter.api.Test)

Example 63 with SimpMessageHeaderAccessor

use of org.springframework.messaging.simp.SimpMessageHeaderAccessor in project spring-framework-debug by Joker-5.

the class UserDestinationMessageHandler method handleMessage.

@Override
public void handleMessage(Message<?> message) throws MessagingException {
    Message<?> messageToUse = message;
    if (this.broadcastHandler != null) {
        messageToUse = this.broadcastHandler.preHandle(message);
        if (messageToUse == null) {
            return;
        }
    }
    UserDestinationResult result = this.destinationResolver.resolveDestination(messageToUse);
    if (result == null) {
        return;
    }
    if (result.getTargetDestinations().isEmpty()) {
        if (logger.isTraceEnabled()) {
            logger.trace("No active sessions for user destination: " + result.getSourceDestination());
        }
        if (this.broadcastHandler != null) {
            this.broadcastHandler.handleUnresolved(messageToUse);
        }
        return;
    }
    SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.wrap(messageToUse);
    initHeaders(accessor);
    accessor.setNativeHeader(SimpMessageHeaderAccessor.ORIGINAL_DESTINATION, result.getSubscribeDestination());
    accessor.setLeaveMutable(true);
    messageToUse = MessageBuilder.createMessage(messageToUse.getPayload(), accessor.getMessageHeaders());
    if (logger.isTraceEnabled()) {
        logger.trace("Translated " + result.getSourceDestination() + " -> " + result.getTargetDestinations());
    }
    for (String target : result.getTargetDestinations()) {
        this.messagingTemplate.send(target, messageToUse);
    }
}
Also used : SimpMessageHeaderAccessor(org.springframework.messaging.simp.SimpMessageHeaderAccessor)

Example 64 with SimpMessageHeaderAccessor

use of org.springframework.messaging.simp.SimpMessageHeaderAccessor in project spring-framework-debug by Joker-5.

the class MessageConverterTests method toMessageWithMutableMessageHeaders.

@Test
public void toMessageWithMutableMessageHeaders() {
    SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.create(SimpMessageType.MESSAGE);
    accessor.setHeader("foo", "bar");
    accessor.setNativeHeader("fooNative", "barNative");
    accessor.setLeaveMutable(true);
    MessageHeaders headers = accessor.getMessageHeaders();
    Message<?> message = this.converter.toMessage("ABC", headers);
    assertThat(message.getHeaders()).isSameAs(headers);
    assertThat(message.getHeaders().getId()).isNull();
    assertThat(message.getHeaders().getTimestamp()).isNull();
    assertThat(message.getHeaders().get(MessageHeaders.CONTENT_TYPE)).isEqualTo(MimeTypeUtils.TEXT_PLAIN);
}
Also used : MessageHeaders(org.springframework.messaging.MessageHeaders) SimpMessageHeaderAccessor(org.springframework.messaging.simp.SimpMessageHeaderAccessor) Test(org.junit.jupiter.api.Test)

Example 65 with SimpMessageHeaderAccessor

use of org.springframework.messaging.simp.SimpMessageHeaderAccessor in project spring-framework-debug by Joker-5.

the class StompSubProtocolHandlerTests method handleMessageToClientWithSimpConnectAckDefaultHeartBeat.

@Test
public void handleMessageToClientWithSimpConnectAckDefaultHeartBeat() {
    StompHeaderAccessor accessor = StompHeaderAccessor.create(StompCommand.CONNECT);
    accessor.setHeartbeat(10000, 10000);
    accessor.setAcceptVersion("1.0");
    Message<?> connectMessage = MessageBuilder.createMessage(EMPTY_PAYLOAD, accessor.getMessageHeaders());
    SimpMessageHeaderAccessor ackAccessor = SimpMessageHeaderAccessor.create(SimpMessageType.CONNECT_ACK);
    ackAccessor.setHeader(SimpMessageHeaderAccessor.CONNECT_MESSAGE_HEADER, connectMessage);
    Message<byte[]> ackMessage = MessageBuilder.createMessage(EMPTY_PAYLOAD, ackAccessor.getMessageHeaders());
    this.protocolHandler.handleMessageToClient(this.session, ackMessage);
    assertThat(this.session.getSentMessages().size()).isEqualTo(1);
    TextMessage actual = (TextMessage) this.session.getSentMessages().get(0);
    assertThat(actual.getPayload()).isEqualTo(("CONNECTED\n" + "version:1.0\n" + "heart-beat:0,0\n" + "user-name:joe\n" + "\n" + "\u0000"));
}
Also used : StompHeaderAccessor(org.springframework.messaging.simp.stomp.StompHeaderAccessor) TextMessage(org.springframework.web.socket.TextMessage) SimpMessageHeaderAccessor(org.springframework.messaging.simp.SimpMessageHeaderAccessor) Test(org.junit.jupiter.api.Test)

Aggregations

SimpMessageHeaderAccessor (org.springframework.messaging.simp.SimpMessageHeaderAccessor)198 Test (org.junit.jupiter.api.Test)103 Message (org.springframework.messaging.Message)50 StompHeaderAccessor (org.springframework.messaging.simp.stomp.StompHeaderAccessor)19 TextMessage (org.springframework.web.socket.TextMessage)16 MessageHeaders (org.springframework.messaging.MessageHeaders)15 Principal (java.security.Principal)13 MessageChannel (org.springframework.messaging.MessageChannel)11 GraphQLMessage (org.activiti.cloud.services.notifications.graphql.ws.api.GraphQLMessage)9 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)8 LinkedHashMap (java.util.LinkedHashMap)7 HashMap (java.util.HashMap)6 TestPrincipal (org.springframework.core.testfixture.security.TestPrincipal)6 List (java.util.List)5 SimpMessageType (org.springframework.messaging.simp.SimpMessageType)5 Map (java.util.Map)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 SimpMessagingTemplate (org.springframework.messaging.simp.SimpMessagingTemplate)4 RoomMessage (com.github.chipolaris.bootforum.messaging.RoomMessage)3 CountDownLatch (java.util.concurrent.CountDownLatch)3