diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/session/AbstractSockJsSession.java b/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/session/AbstractSockJsSession.java index cc950ea4870..666dc6d254b 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/session/AbstractSockJsSession.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/session/AbstractSockJsSession.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,8 +17,8 @@ package org.springframework.web.socket.sockjs.transport.session; import java.io.IOException; -import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; import java.util.Date; import java.util.HashSet; import java.util.List; @@ -26,6 +26,7 @@ import java.util.Map; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ScheduledFuture; +import java.util.stream.Collectors; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -377,23 +378,33 @@ public abstract class AbstractSockJsSession implements SockJsSession { } public void delegateMessages(String... messages) throws SockJsMessageDeliveryException { - List undelivered = new ArrayList<>(Arrays.asList(messages)); - for (String message : messages) { + for (int i = 0; i < messages.length; i++) { try { if (isClosed()) { - throw new SockJsMessageDeliveryException(this.id, undelivered, "Session closed"); - } - else { - this.handler.handleMessage(this, new TextMessage(message)); - undelivered.remove(0); + throw new SockJsMessageDeliveryException(this.id, getUndelivered(messages, i), "Session closed"); } + this.handler.handleMessage(this, new TextMessage(messages[i])); } catch (Exception ex) { - throw new SockJsMessageDeliveryException(this.id, undelivered, ex); + throw new SockJsMessageDeliveryException(this.id, getUndelivered(messages, i), ex); } } } + private static List getUndelivered(String[] messages, int i) { + switch (messages.length - i) { + case 0: + return Collections.emptyList(); + case 1: + return (messages[i].trim().isEmpty() ? + Collections.emptyList() : Collections.singletonList(messages[i])); + default: + return Arrays.stream(Arrays.copyOfRange(messages, i, messages.length)) + .filter(message -> !message.trim().isEmpty()) + .collect(Collectors.toList()); + } + } + /** * Invoked when the underlying connection is closed. */ diff --git a/spring-websocket/src/test/java/org/springframework/web/socket/sockjs/transport/session/SockJsSessionTests.java b/spring-websocket/src/test/java/org/springframework/web/socket/sockjs/transport/session/SockJsSessionTests.java index c65061c03da..dafe2e69cee 100644 --- a/spring-websocket/src/test/java/org/springframework/web/socket/sockjs/transport/session/SockJsSessionTests.java +++ b/spring-websocket/src/test/java/org/springframework/web/socket/sockjs/transport/session/SockJsSessionTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -25,7 +25,6 @@ import org.junit.jupiter.api.Test; import org.springframework.web.socket.CloseStatus; import org.springframework.web.socket.TextMessage; -import org.springframework.web.socket.WebSocketHandler; import org.springframework.web.socket.handler.ExceptionWebSocketHandlerDecorator; import org.springframework.web.socket.sockjs.SockJsMessageDeliveryException; import org.springframework.web.socket.sockjs.SockJsTransportFailureException; @@ -48,9 +47,10 @@ import static org.mockito.Mockito.verifyNoMoreInteractions; */ public class SockJsSessionTests extends AbstractSockJsSessionTests { + @Override protected TestSockJsSession initSockJsSession() { - return new TestSockJsSession("1", this.sockJsConfig, this.webSocketHandler, Collections.emptyMap()); + return new TestSockJsSession("1", this.sockJsConfig, this.webSocketHandler, Collections.emptyMap()); } @@ -94,8 +94,10 @@ public class SockJsSessionTests extends AbstractSockJsSessionTestsemptyMap()); + + TestSockJsSession session = new TestSockJsSession("1", this.sockJsConfig, + new ExceptionWebSocketHandlerDecorator(this.webSocketHandler), Collections.emptyMap()); String msg1 = "message 1"; String msg2 = "message 2"; String msg3 = "message 3"; - willThrow(new IOException()).given(this.webSocketHandler).handleMessage(sockJsSession, new TextMessage(msg2)); + willThrow(new IOException()).given(this.webSocketHandler).handleMessage(session, new TextMessage(msg2)); - sockJsSession.delegateConnectionEstablished(); - assertThatExceptionOfType(SockJsMessageDeliveryException.class).isThrownBy(() -> - sockJsSession.delegateMessages(msg1, msg2, msg3)) - .satisfies(ex -> assertThat(ex.getUndeliveredMessages()).containsExactly(msg3)); - verify(this.webSocketHandler).afterConnectionEstablished(sockJsSession); - verify(this.webSocketHandler).handleMessage(sockJsSession, new TextMessage(msg1)); - verify(this.webSocketHandler).handleMessage(sockJsSession, new TextMessage(msg2)); - verify(this.webSocketHandler).afterConnectionClosed(sockJsSession, CloseStatus.SERVER_ERROR); + session.delegateConnectionEstablished(); + + assertThatExceptionOfType(SockJsMessageDeliveryException.class) + .isThrownBy(() -> session.delegateMessages(msg1, msg2, msg3)) + .satisfies(ex -> assertThat(ex.getUndeliveredMessages()).containsExactly(msg3)); + + verify(this.webSocketHandler).afterConnectionEstablished(session); + verify(this.webSocketHandler).handleMessage(session, new TextMessage(msg1)); + verify(this.webSocketHandler).handleMessage(session, new TextMessage(msg2)); + verify(this.webSocketHandler).afterConnectionClosed(session, CloseStatus.SERVER_ERROR); verifyNoMoreInteractions(this.webSocketHandler); } @@ -151,7 +155,7 @@ public class SockJsSessionTests extends AbstractSockJsSessionTests task = mock(ScheduledFuture.class); willReturn(task).given(this.taskScheduler).schedule(any(Runnable.class), any(Date.class));