Browse Source

Do not raise exception for undelivered empty messages

Closes gh-23828
pull/24697/head
Rossen Stoyanchev 6 years ago
parent
commit
fa6ccc066d
  1. 12
      spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/session/AbstractSockJsSession.java
  2. 19
      spring-websocket/src/test/java/org/springframework/web/socket/sockjs/transport/session/SockJsSessionTests.java

12
spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/session/AbstractSockJsSession.java

@ -380,11 +380,17 @@ public abstract class AbstractSockJsSession implements SockJsSession { @@ -380,11 +380,17 @@ public abstract class AbstractSockJsSession implements SockJsSession {
public void delegateMessages(String... messages) throws SockJsMessageDeliveryException {
for (int i = 0; i < messages.length; i++) {
try {
if (isClosed()) {
throw new SockJsMessageDeliveryException(this.id, getUndelivered(messages, i), "Session closed");
}
if (!isClosed()) {
this.handler.handleMessage(this, new TextMessage(messages[i]));
}
else {
List<String> undelivered = getUndelivered(messages, i);
if (undelivered.isEmpty()) {
return;
}
throw new SockJsMessageDeliveryException(this.id, undelivered, "Session closed");
}
}
catch (Exception ex) {
throw new SockJsMessageDeliveryException(this.id, getUndelivered(messages, i), ex);
}

19
spring-websocket/src/test/java/org/springframework/web/socket/sockjs/transport/session/SockJsSessionTests.java

@ -106,7 +106,7 @@ public class SockJsSessionTests extends AbstractSockJsSessionTests<TestSockJsSes @@ -106,7 +106,7 @@ public class SockJsSessionTests extends AbstractSockJsSessionTests<TestSockJsSes
}
@Test
public void delegateMessagesWithErrorAndConnectionClosing() throws Exception {
public void delegateMessagesWithError() throws Exception {
TestSockJsSession session = new TestSockJsSession("1", this.sockJsConfig,
new ExceptionWebSocketHandlerDecorator(this.webSocketHandler), Collections.emptyMap());
@ -130,6 +130,23 @@ public class SockJsSessionTests extends AbstractSockJsSessionTests<TestSockJsSes @@ -130,6 +130,23 @@ public class SockJsSessionTests extends AbstractSockJsSessionTests<TestSockJsSes
verifyNoMoreInteractions(this.webSocketHandler);
}
@Test // gh-23828
public void delegateMessagesEmptyAfterConnectionClosed() throws Exception {
TestSockJsSession session = new TestSockJsSession("1", this.sockJsConfig,
new ExceptionWebSocketHandlerDecorator(this.webSocketHandler), Collections.emptyMap());
session.delegateConnectionEstablished();
session.close(CloseStatus.NORMAL);
session.delegateMessages("", " ", "\n");
// No exception for empty messages
verify(this.webSocketHandler).afterConnectionEstablished(session);
verify(this.webSocketHandler).afterConnectionClosed(session, CloseStatus.NORMAL);
verifyNoMoreInteractions(this.webSocketHandler);
}
@Test
public void delegateConnectionClosed() throws Exception {
this.session.delegateConnectionEstablished();

Loading…
Cancel
Save