@Test public void handleMessageToClientConnectAck() { StompHeaderAccessor connectHeaders = StompHeaderAccessor.create(StompCommand.CONNECT); connectHeaders.setHeartbeat(10000, 10000); connectHeaders.setNativeHeader(StompHeaderAccessor.STOMP_ACCEPT_VERSION_HEADER, "1.0,1.1"); Message<?> connectMessage = MessageBuilder.withPayload(new byte[0]).setHeaders(connectHeaders).build(); SimpMessageHeaderAccessor connectAckHeaders = SimpMessageHeaderAccessor.create(SimpMessageType.CONNECT_ACK); connectAckHeaders.setHeader(SimpMessageHeaderAccessor.CONNECT_MESSAGE_HEADER, connectMessage); Message<byte[]> connectAckMessage = MessageBuilder.withPayload(new byte[0]).setHeaders(connectAckHeaders).build(); this.protocolHandler.handleMessageToClient(this.session, connectAckMessage); verifyNoMoreInteractions(this.channel); // Check CONNECTED reply assertEquals(1, this.session.getSentMessages().size()); TextMessage textMessage = (TextMessage) this.session.getSentMessages().get(0); Message<?> message = new StompDecoder().decode(ByteBuffer.wrap(textMessage.getPayload().getBytes())); StompHeaderAccessor replyHeaders = StompHeaderAccessor.wrap(message); assertEquals(StompCommand.CONNECTED, replyHeaders.getCommand()); assertEquals("1.1", replyHeaders.getVersion()); assertArrayEquals(new long[] {0, 0}, replyHeaders.getHeartbeat()); assertEquals("joe", replyHeaders.getNativeHeader("user-name").get(0)); }
@Override public void afterConnectionEstablished(WebSocketSession session) throws Exception { StompHeaderAccessor headers = StompHeaderAccessor.create(StompCommand.CONNECT); headers.setAcceptVersion("1.1,1.2"); headers.setHeartbeat(0, 0); Message<byte[]> message = MessageBuilder.withPayload(new byte[0]).setHeaders(headers).build(); TextMessage textMessage = new TextMessage(new String(this.encoder.encode(message), UTF_8)); session.sendMessage(textMessage); }
@Override public void handleException( StompSession session, StompCommand command, StompHeaders headers, byte[] payload, Throwable exception) { Message<byte[]> message = MessageBuilder.createMessage( payload, StompHeaderAccessor.create(command, headers).getMessageHeaders()); logger.error( "The exception for session [" + session + "] on message [" + message + "]", exception); }
@Test public void handleMessageToClientConnected() { UserSessionRegistry registry = new DefaultUserSessionRegistry(); this.protocolHandler.setUserSessionRegistry(registry); StompHeaderAccessor headers = StompHeaderAccessor.create(StompCommand.CONNECTED); Message<byte[]> message = MessageBuilder.withPayload(new byte[0]).setHeaders(headers).build(); this.protocolHandler.handleMessageToClient(this.session, message); assertEquals(1, this.session.getSentMessages().size()); WebSocketMessage<?> textMessage = this.session.getSentMessages().get(0); assertEquals("CONNECTED\n" + "user-name:joe\n" + "\n" + "\u0000", textMessage.getPayload()); assertEquals(Collections.singleton("s1"), registry.getSessionIds("joe")); }
/** Decode a single STOMP frame from the given {@code buffer} into a {@link Message}. */ private Message<byte[]> decodeMessage(ByteBuffer buffer, MultiValueMap<String, String> headers) { Message<byte[]> decodedMessage = null; skipLeadingEol(buffer); buffer.mark(); String command = readCommand(buffer); if (command.length() > 0) { StompHeaderAccessor headerAccessor = null; byte[] payload = null; if (buffer.remaining() > 0) { StompCommand stompCommand = StompCommand.valueOf(command); headerAccessor = StompHeaderAccessor.create(stompCommand); initHeaders(headerAccessor); readHeaders(buffer, headerAccessor); payload = readPayload(buffer, headerAccessor); } if (payload != null) { if ((payload.length > 0) && (!headerAccessor.getCommand().isBodyAllowed())) { throw new StompConversionException( headerAccessor.getCommand() + " shouldn't have a payload: length=" + payload.length + ", headers=" + headers); } headerAccessor.updateSimpMessageHeadersFromStompHeaders(); headerAccessor.setLeaveMutable(true); decodedMessage = MessageBuilder.createMessage(payload, headerAccessor.getMessageHeaders()); if (logger.isDebugEnabled()) { logger.debug("Decoded " + decodedMessage); } } else { if (logger.isTraceEnabled()) { logger.trace("Received incomplete frame. Resetting buffer."); } if (headers != null && headerAccessor != null) { String name = NativeMessageHeaderAccessor.NATIVE_HEADERS; @SuppressWarnings("unchecked") MultiValueMap<String, String> map = (MultiValueMap<String, String>) headerAccessor.getHeader(name); if (map != null) { headers.putAll(map); } } buffer.reset(); } } else { if (logger.isTraceEnabled()) { logger.trace("Decoded heartbeat"); } StompHeaderAccessor headerAccessor = StompHeaderAccessor.createForHeartbeat(); initHeaders(headerAccessor); headerAccessor.setLeaveMutable(true); decodedMessage = MessageBuilder.createMessage(HEARTBEAT_PAYLOAD, headerAccessor.getMessageHeaders()); } return decodedMessage; }