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();
}
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"));
}
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);
}
}
}
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();
}
}
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;
}
Aggregations