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