From 27899abcb6c1651b0b25e196ae9768a264474b70 Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Fri, 21 Aug 2015 10:46:54 -0400 Subject: [PATCH] Publish events only after successful channel send The StompSubProtcolHandler now checks the outcome of the send to the inbound client channel. If the message was prevented from being sent, e.g. as part of authorization, events are not published Issue: SPR-13339 --- .../messaging/StompSubProtocolHandler.java | 5 ++-- .../StompSubProtocolHandlerTests.java | 23 +++++++++++++++---- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/messaging/StompSubProtocolHandler.java b/spring-websocket/src/main/java/org/springframework/web/socket/messaging/StompSubProtocolHandler.java index 157ba1de69..c5d0414f4d 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/messaging/StompSubProtocolHandler.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/messaging/StompSubProtocolHandler.java @@ -277,7 +277,9 @@ public class StompSubProtocolHandler implements SubProtocolHandler, ApplicationE try { SimpAttributesContextHolder.setAttributesFromMessage(message); - if (this.eventPublisher != null) { + boolean sent = outputChannel.send(message); + + if (sent && this.eventPublisher != null) { if (StompCommand.CONNECT.equals(headerAccessor.getCommand())) { publishEvent(new SessionConnectEvent(this, message, user)); } @@ -288,7 +290,6 @@ public class StompSubProtocolHandler implements SubProtocolHandler, ApplicationE publishEvent(new SessionUnsubscribeEvent(this, message, user)); } } - outputChannel.send(message); } finally { SimpAttributesContextHolder.resetAttributes(); diff --git a/spring-websocket/src/test/java/org/springframework/web/socket/messaging/StompSubProtocolHandlerTests.java b/spring-websocket/src/test/java/org/springframework/web/socket/messaging/StompSubProtocolHandlerTests.java index 002e6e16f3..22214eafab 100644 --- a/spring-websocket/src/test/java/org/springframework/web/socket/messaging/StompSubProtocolHandlerTests.java +++ b/spring-websocket/src/test/java/org/springframework/web/socket/messaging/StompSubProtocolHandlerTests.java @@ -16,11 +16,6 @@ package org.springframework.web.socket.messaging; -import static org.hamcrest.Matchers.*; -import static org.junit.Assert.*; -import static org.mockito.Matchers.any; -import static org.mockito.Mockito.*; - import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; @@ -61,6 +56,22 @@ import org.springframework.web.socket.WebSocketMessage; import org.springframework.web.socket.handler.TestWebSocketSession; import org.springframework.web.socket.sockjs.transport.SockJsSession; +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.reset; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyNoMoreInteractions; +import static org.mockito.Mockito.verifyZeroInteractions; +import static org.mockito.Mockito.when; + /** * Test fixture for {@link StompSubProtocolHandler} tests. * @@ -86,6 +97,8 @@ public class StompSubProtocolHandlerTests { this.channel = Mockito.mock(MessageChannel.class); this.messageCaptor = ArgumentCaptor.forClass(Message.class); + when(this.channel.send(any())).thenReturn(true); + this.session = new TestWebSocketSession(); this.session.setId("s1"); this.session.setPrincipal(new TestPrincipal("joe"));