Skip to content

Commit 1fff631

Browse files
committed
Fix SubProtocolHandler duplicate registration
Prior to this change, duplicate SubProtocolHandlers could be registered when configuring STOMP with several registrations: public void registerStompEndpoints (final StompEndpointRegistry registry) { this.endpointRegistry.addEndpoint("/stompOverWebSocket"); this.endpointRegistry.addEndpoint("/stompOverSockJS").withSockJS(); } This commit registers sub-protocols in a Set instead of a list (see SubProtocolWebSocketHandler), thus fixing the issue. Issue: SPR-12403
1 parent 330897b commit 1fff631

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@
1919
import java.io.IOException;
2020
import java.util.ArrayList;
2121
import java.util.Arrays;
22+
import java.util.HashSet;
2223
import java.util.List;
2324
import java.util.Map;
25+
import java.util.Set;
2426
import java.util.TreeMap;
2527
import java.util.concurrent.ConcurrentHashMap;
2628
import java.util.concurrent.atomic.AtomicInteger;
@@ -86,7 +88,7 @@ public class SubProtocolWebSocketHandler implements WebSocketHandler,
8688
private final Map<String, SubProtocolHandler> protocolHandlerLookup =
8789
new TreeMap<String, SubProtocolHandler>(String.CASE_INSENSITIVE_ORDER);
8890

89-
private final List<SubProtocolHandler> protocolHandlers = new ArrayList<SubProtocolHandler>();
91+
private final Set<SubProtocolHandler> protocolHandlers = new HashSet<SubProtocolHandler>();
9092

9193
private SubProtocolHandler defaultProtocolHandler;
9294

@@ -129,7 +131,7 @@ public void setProtocolHandlers(List<SubProtocolHandler> protocolHandlers) {
129131
}
130132

131133
public List<SubProtocolHandler> getProtocolHandlers() {
132-
return new ArrayList<SubProtocolHandler>(this.protocolHandlerLookup.values());
134+
return new ArrayList<SubProtocolHandler>(this.protocolHandlers);
133135
}
134136

135137

@@ -288,7 +290,7 @@ protected final SubProtocolHandler findProtocolHandler(WebSocketSession session)
288290
handler = this.defaultProtocolHandler;
289291
}
290292
else if (this.protocolHandlers.size() == 1) {
291-
handler = this.protocolHandlers.get(0);
293+
handler = this.protocolHandlers.iterator().next();
292294
}
293295
else {
294296
throw new IllegalStateException("Multiple protocol handlers configured and " +

spring-websocket/src/test/java/org/springframework/web/socket/config/annotation/WebMvcStompEndpointRegistryTests.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ public void handlerMapping() {
8484
this.endpointRegistry.addEndpoint("/stompOverWebSocket");
8585
this.endpointRegistry.addEndpoint("/stompOverSockJS").withSockJS();
8686

87+
//SPR-12403
88+
assertEquals(1, this.webSocketHandler.getProtocolHandlers().size());
89+
8790
hm = (SimpleUrlHandlerMapping) this.endpointRegistry.getHandlerMapping();
8891
assertEquals(2, hm.getUrlMap().size());
8992
assertNotNull(hm.getUrlMap().get("/stompOverWebSocket"));

0 commit comments

Comments
 (0)