Search in sources :

Example 1 with SimpMessageHeaderAccessor

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

the class SimpAnnotationMethodMessageHandlerTests method createMessage.

private Message<?> createMessage(String destination, Map<String, Object> headers) {
    SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.create();
    accessor.setSessionId("session1");
    accessor.setSessionAttributes(new HashMap<>());
    accessor.setDestination(destination);
    if (headers != null) {
        for (Map.Entry<String, Object> entry : headers.entrySet()) {
            accessor.setHeader(entry.getKey(), entry.getValue());
        }
    }
    return MessageBuilder.withPayload(new byte[0]).setHeaders(accessor).build();
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) SimpMessageHeaderAccessor(org.springframework.messaging.simp.SimpMessageHeaderAccessor)

Example 2 with SimpMessageHeaderAccessor

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

the class SimpleBrokerMessageHandlerTests method subcribeDisconnectPublish.

@Test
public void subcribeDisconnectPublish() {
    String sess1 = "sess1";
    String sess2 = "sess2";
    this.messageHandler.start();
    this.messageHandler.handleMessage(createSubscriptionMessage(sess1, "sub1", "/foo"));
    this.messageHandler.handleMessage(createSubscriptionMessage(sess1, "sub2", "/foo"));
    this.messageHandler.handleMessage(createSubscriptionMessage(sess1, "sub3", "/bar"));
    this.messageHandler.handleMessage(createSubscriptionMessage(sess2, "sub1", "/foo"));
    this.messageHandler.handleMessage(createSubscriptionMessage(sess2, "sub2", "/foo"));
    this.messageHandler.handleMessage(createSubscriptionMessage(sess2, "sub3", "/bar"));
    SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.create(SimpMessageType.DISCONNECT);
    headers.setSessionId(sess1);
    headers.setUser(new TestPrincipal("joe"));
    Message<byte[]> message = MessageBuilder.createMessage(new byte[0], headers.getMessageHeaders());
    this.messageHandler.handleMessage(message);
    this.messageHandler.handleMessage(createMessage("/foo", "message1"));
    this.messageHandler.handleMessage(createMessage("/bar", "message2"));
    verify(this.clientOutboundChannel, times(4)).send(this.messageCaptor.capture());
    Message<?> captured = this.messageCaptor.getAllValues().get(0);
    assertEquals(SimpMessageType.DISCONNECT_ACK, SimpMessageHeaderAccessor.getMessageType(captured.getHeaders()));
    assertSame(message, captured.getHeaders().get(SimpMessageHeaderAccessor.DISCONNECT_MESSAGE_HEADER));
    assertEquals(sess1, SimpMessageHeaderAccessor.getSessionId(captured.getHeaders()));
    assertEquals("joe", SimpMessageHeaderAccessor.getUser(captured.getHeaders()).getName());
    assertTrue(messageCaptured(sess2, "sub1", "/foo"));
    assertTrue(messageCaptured(sess2, "sub2", "/foo"));
    assertTrue(messageCaptured(sess2, "sub3", "/bar"));
}
Also used : TestPrincipal(org.springframework.messaging.simp.TestPrincipal) SimpMessageHeaderAccessor(org.springframework.messaging.simp.SimpMessageHeaderAccessor) Test(org.junit.Test)

Example 3 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) throws Exception {
    String sessionId = null;
    if (!this.client) {
        sessionId = this.subProtocolHandlerRegistry.resolveSessionId(message);
        if (sessionId == null) {
            throw new IllegalArgumentException("The WebSocket 'sessionId' is required in the MessageHeaders");
        }
    }
    WebSocketSession session = this.webSocketContainer.getSession(sessionId);
    try {
        SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.wrap(message);
        headers.setLeaveMutable(true);
        headers.setMessageTypeIfNotSet(SimpMessageType.MESSAGE);
        Message<?> messageToSend = this.messageConverter.toMessage(message.getPayload(), headers.getMessageHeaders());
        this.subProtocolHandlerRegistry.findProtocolHandler(session).handleMessageToClient(session, messageToSend);
    } catch (SessionLimitExceededException ex) {
        try {
            logger.error("Terminating session id '" + sessionId + "'", ex);
            this.webSocketContainer.closeSession(session, ex.getStatus());
        } catch (Exception secondException) {
            logger.error("Exception terminating session id '" + sessionId + "'", secondException);
        }
    }
}
Also used : SessionLimitExceededException(org.springframework.web.socket.handler.SessionLimitExceededException) SessionLimitExceededException(org.springframework.web.socket.handler.SessionLimitExceededException) WebSocketSession(org.springframework.web.socket.WebSocketSession) SimpMessageHeaderAccessor(org.springframework.messaging.simp.SimpMessageHeaderAccessor)

Example 4 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) throws Exception {
    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)

Example 5 with SimpMessageHeaderAccessor

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

the class TestServerConfig method clientOutboundChannel.

@Bean
public AbstractSubscribableChannel clientOutboundChannel() {
    DirectChannel directChannel = new DirectChannel();
    directChannel.addInterceptor(new ChannelInterceptorAdapter() {

        @Override
        public Message<?> preSend(Message<?> message, MessageChannel channel) {
            SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.wrap(message);
            headers.setLeaveMutable(true);
            return MessageBuilder.createMessage(message.getPayload(), headers.getMessageHeaders());
        }
    });
    return directChannel;
}
Also used : ChannelInterceptorAdapter(org.springframework.messaging.support.ChannelInterceptorAdapter) MessageChannel(org.springframework.messaging.MessageChannel) Message(org.springframework.messaging.Message) DirectChannel(org.springframework.integration.channel.DirectChannel) SimpMessageHeaderAccessor(org.springframework.messaging.simp.SimpMessageHeaderAccessor) Bean(org.springframework.context.annotation.Bean)

Aggregations

SimpMessageHeaderAccessor (org.springframework.messaging.simp.SimpMessageHeaderAccessor)70 Test (org.junit.jupiter.api.Test)41 Message (org.springframework.messaging.Message)19 MessageChannel (org.springframework.messaging.MessageChannel)6 StompHeaderAccessor (org.springframework.messaging.simp.stomp.StompHeaderAccessor)6 MessageHeaders (org.springframework.messaging.MessageHeaders)5 TextMessage (org.springframework.web.socket.TextMessage)5 Principal (java.security.Principal)3 LinkedHashMap (java.util.LinkedHashMap)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 AbstractMessageChannel (org.springframework.messaging.support.AbstractMessageChannel)3 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)3 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)3 HttpRequestHandler (org.springframework.web.HttpRequestHandler)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 TestPrincipal (org.springframework.core.testfixture.security.TestPrincipal)2 SimpMessageType (org.springframework.messaging.simp.SimpMessageType)2 GenericMessage (org.springframework.messaging.support.GenericMessage)2 WebSocketSession (org.springframework.web.socket.WebSocketSession)2