Search in sources :

Example 91 with SimpMessageHeaderAccessor

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

the class UserDestinationMessageHandlerTests method handleMessage.

@Test
@SuppressWarnings("rawtypes")
void handleMessage() {
    TestSimpUser simpUser = new TestSimpUser("joe");
    simpUser.addSessions(new TestSimpSession("123"));
    given(this.registry.getUser("joe")).willReturn(simpUser);
    given(this.brokerChannel.send(Mockito.any(Message.class))).willReturn(true);
    this.handler.handleMessage(createWith(SimpMessageType.MESSAGE, "joe", "123", "/user/joe/queue/foo"));
    ArgumentCaptor<Message> captor = ArgumentCaptor.forClass(Message.class);
    Mockito.verify(this.brokerChannel).send(captor.capture());
    SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.wrap(captor.getValue());
    assertThat(accessor.getDestination()).isEqualTo("/queue/foo-user123");
    assertThat(accessor.getFirstNativeHeader(ORIGINAL_DESTINATION)).isEqualTo("/user/queue/foo");
}
Also used : Message(org.springframework.messaging.Message) SimpMessageHeaderAccessor(org.springframework.messaging.simp.SimpMessageHeaderAccessor) Test(org.junit.jupiter.api.Test)

Example 92 with SimpMessageHeaderAccessor

use of org.springframework.messaging.simp.SimpMessageHeaderAccessor in project powerauth-webflow by wultra.

the class WebSocketMessageService method createHeaders.

/**
 * Create a MessageHeaders object for session.
 *
 * @param webSocketSessionId WebSocket session ID.
 * @return Message headers.
 */
private MessageHeaders createHeaders(String webSocketSessionId) {
    SimpMessageHeaderAccessor headerAccessor = SimpMessageHeaderAccessor.create(SimpMessageType.MESSAGE);
    headerAccessor.setSessionId(webSocketSessionId);
    headerAccessor.setLeaveMutable(true);
    return headerAccessor.getMessageHeaders();
}
Also used : SimpMessageHeaderAccessor(org.springframework.messaging.simp.SimpMessageHeaderAccessor)

Example 93 with SimpMessageHeaderAccessor

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

the class WebSocketInboundChannelAdapter method produceConnectAckMessage.

private void produceConnectAckMessage(Message<?> message, SimpMessageHeaderAccessor headerAccessor) {
    String sessionId = headerAccessor.getSessionId();
    SimpMessageHeaderAccessor connectAck = SimpMessageHeaderAccessor.create(SimpMessageType.CONNECT_ACK);
    connectAck.setSessionId(sessionId);
    connectAck.setHeader(SimpMessageHeaderAccessor.CONNECT_MESSAGE_HEADER, message);
    Message<byte[]> ackMessage = MessageBuilder.createMessage(EMPTY_PAYLOAD, connectAck.getMessageHeaders());
    WebSocketSession session = this.webSocketContainer.getSession(sessionId);
    try {
        this.subProtocolHandlerRegistry.findProtocolHandler(session).handleMessageToClient(session, ackMessage);
    } catch (Exception e) {
        throw IntegrationUtils.wrapInHandlingExceptionIfNecessary(message, () -> "Error sending connect ack message in the [" + this + ']', e);
    }
}
Also used : SimpMessageHeaderAccessor(org.springframework.messaging.simp.SimpMessageHeaderAccessor) WebSocketSession(org.springframework.web.socket.WebSocketSession)

Example 94 with SimpMessageHeaderAccessor

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

the class WebSocketOutboundMessageHandler method handleMessageInternal.

@Override
protected void handleMessageInternal(Message<?> message) {
    String sessionId;
    if (!this.client) {
        sessionId = this.subProtocolHandlerRegistry.resolveSessionId(message);
        if (sessionId == null) {
            throw new IllegalArgumentException("The WebSocket 'sessionId' is required in the MessageHeaders");
        }
    } else {
        sessionId = null;
    }
    WebSocketSession session = this.webSocketContainer.getSession(sessionId);
    try {
        SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.wrap(message);
        headers.setLeaveMutable(true);
        headers.setMessageTypeIfNotSet(SimpMessageType.MESSAGE);
        Object payload = message.getPayload();
        Message<?> messageToSend = this.messageConverter.toMessage(payload, headers.getMessageHeaders());
        Assert.state(messageToSend != null, () -> "The message converter '" + this.messageConverter + "' produced no message to send based on the request message: '" + message + "'");
        this.subProtocolHandlerRegistry.findProtocolHandler(session).handleMessageToClient(session, messageToSend);
    } catch (SessionLimitExceededException ex) {
        try {
            logger.error(ex, () -> "Terminating session id '" + sessionId + "'");
            this.webSocketContainer.closeSession(session, ex.getStatus());
        } catch (Exception secondException) {
            logger.error(secondException, () -> "Exception terminating session id '" + sessionId + "'");
        }
    } catch (Exception e) {
        throw new MessageHandlingException(message, "Failed to handle message in the [" + this + ']', e);
    }
}
Also used : SessionLimitExceededException(org.springframework.web.socket.handler.SessionLimitExceededException) SessionLimitExceededException(org.springframework.web.socket.handler.SessionLimitExceededException) MessageHandlingException(org.springframework.messaging.MessageHandlingException) MessageHandlingException(org.springframework.messaging.MessageHandlingException) WebSocketSession(org.springframework.web.socket.WebSocketSession) SimpMessageHeaderAccessor(org.springframework.messaging.simp.SimpMessageHeaderAccessor)

Example 95 with SimpMessageHeaderAccessor

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

the class PassThruSubProtocolHandler method handleMessageFromClient.

@Override
public void handleMessageFromClient(WebSocketSession session, WebSocketMessage<?> webSocketMessage, MessageChannel outputChannel) {
    SimpMessageHeaderAccessor headerAccessor = SimpMessageHeaderAccessor.create(SimpMessageType.MESSAGE);
    headerAccessor.setSessionId(session.getId());
    headerAccessor.setSessionAttributes(session.getAttributes());
    headerAccessor.setUser(session.getPrincipal());
    headerAccessor.setHeader("content-length", webSocketMessage.getPayloadLength());
    headerAccessor.setLeaveMutable(true);
    Message<?> message = MessageBuilder.createMessage(webSocketMessage.getPayload(), headerAccessor.getMessageHeaders());
    try {
        SimpAttributesContextHolder.setAttributesFromMessage(message);
        outputChannel.send(message);
    } finally {
        SimpAttributesContextHolder.resetAttributes();
    }
}
Also used : SimpMessageHeaderAccessor(org.springframework.messaging.simp.SimpMessageHeaderAccessor)

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