Skip to content

Commit 27899ab

Browse files
committed
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
1 parent 473dd5e commit 27899ab

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

spring-websocket/src/main/java/org/springframework/web/socket/messaging/StompSubProtocolHandler.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,9 @@ else if (StompCommand.DISCONNECT.equals(headerAccessor.getCommand())) {
277277

278278
try {
279279
SimpAttributesContextHolder.setAttributesFromMessage(message);
280-
if (this.eventPublisher != null) {
280+
boolean sent = outputChannel.send(message);
281+
282+
if (sent && this.eventPublisher != null) {
281283
if (StompCommand.CONNECT.equals(headerAccessor.getCommand())) {
282284
publishEvent(new SessionConnectEvent(this, message, user));
283285
}
@@ -288,7 +290,6 @@ else if (StompCommand.UNSUBSCRIBE.equals(headerAccessor.getCommand())) {
288290
publishEvent(new SessionUnsubscribeEvent(this, message, user));
289291
}
290292
}
291-
outputChannel.send(message);
292293
}
293294
finally {
294295
SimpAttributesContextHolder.resetAttributes();

spring-websocket/src/test/java/org/springframework/web/socket/messaging/StompSubProtocolHandlerTests.java

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,6 @@
1616

1717
package org.springframework.web.socket.messaging;
1818

19-
import static org.hamcrest.Matchers.*;
20-
import static org.junit.Assert.*;
21-
import static org.mockito.Matchers.any;
22-
import static org.mockito.Mockito.*;
23-
2419
import java.io.IOException;
2520
import java.util.ArrayList;
2621
import java.util.Arrays;
@@ -61,6 +56,22 @@
6156
import org.springframework.web.socket.handler.TestWebSocketSession;
6257
import org.springframework.web.socket.sockjs.transport.SockJsSession;
6358

59+
import static org.hamcrest.Matchers.is;
60+
import static org.junit.Assert.assertArrayEquals;
61+
import static org.junit.Assert.assertEquals;
62+
import static org.junit.Assert.assertFalse;
63+
import static org.junit.Assert.assertNotNull;
64+
import static org.junit.Assert.assertThat;
65+
import static org.junit.Assert.assertTrue;
66+
import static org.mockito.Matchers.any;
67+
import static org.mockito.Mockito.mock;
68+
import static org.mockito.Mockito.reset;
69+
import static org.mockito.Mockito.times;
70+
import static org.mockito.Mockito.verify;
71+
import static org.mockito.Mockito.verifyNoMoreInteractions;
72+
import static org.mockito.Mockito.verifyZeroInteractions;
73+
import static org.mockito.Mockito.when;
74+
6475
/**
6576
* Test fixture for {@link StompSubProtocolHandler} tests.
6677
*
@@ -86,6 +97,8 @@ public void setup() {
8697
this.channel = Mockito.mock(MessageChannel.class);
8798
this.messageCaptor = ArgumentCaptor.forClass(Message.class);
8899

100+
when(this.channel.send(any())).thenReturn(true);
101+
89102
this.session = new TestWebSocketSession();
90103
this.session.setId("s1");
91104
this.session.setPrincipal(new TestPrincipal("joe"));

0 commit comments

Comments
 (0)