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
This commit is contained in:
@@ -19,8 +19,10 @@ package org.springframework.web.socket.messaging;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
import java.util.TreeMap;
|
import java.util.TreeMap;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
import java.util.concurrent.atomic.AtomicInteger;
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
@@ -86,7 +88,7 @@ public class SubProtocolWebSocketHandler implements WebSocketHandler,
|
|||||||
private final Map<String, SubProtocolHandler> protocolHandlerLookup =
|
private final Map<String, SubProtocolHandler> protocolHandlerLookup =
|
||||||
new TreeMap<String, SubProtocolHandler>(String.CASE_INSENSITIVE_ORDER);
|
new TreeMap<String, SubProtocolHandler>(String.CASE_INSENSITIVE_ORDER);
|
||||||
|
|
||||||
private final List<SubProtocolHandler> protocolHandlers = new ArrayList<SubProtocolHandler>();
|
private final Set<SubProtocolHandler> protocolHandlers = new HashSet<SubProtocolHandler>();
|
||||||
|
|
||||||
private SubProtocolHandler defaultProtocolHandler;
|
private SubProtocolHandler defaultProtocolHandler;
|
||||||
|
|
||||||
@@ -129,7 +131,7 @@ public class SubProtocolWebSocketHandler implements WebSocketHandler,
|
|||||||
}
|
}
|
||||||
|
|
||||||
public List<SubProtocolHandler> getProtocolHandlers() {
|
public List<SubProtocolHandler> getProtocolHandlers() {
|
||||||
return new ArrayList<SubProtocolHandler>(this.protocolHandlerLookup.values());
|
return new ArrayList<SubProtocolHandler>(this.protocolHandlers);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -288,7 +290,7 @@ public class SubProtocolWebSocketHandler implements WebSocketHandler,
|
|||||||
handler = this.defaultProtocolHandler;
|
handler = this.defaultProtocolHandler;
|
||||||
}
|
}
|
||||||
else if (this.protocolHandlers.size() == 1) {
|
else if (this.protocolHandlers.size() == 1) {
|
||||||
handler = this.protocolHandlers.get(0);
|
handler = this.protocolHandlers.iterator().next();
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
throw new IllegalStateException("Multiple protocol handlers configured and " +
|
throw new IllegalStateException("Multiple protocol handlers configured and " +
|
||||||
|
|||||||
@@ -84,6 +84,9 @@ public class WebMvcStompEndpointRegistryTests {
|
|||||||
this.endpointRegistry.addEndpoint("/stompOverWebSocket");
|
this.endpointRegistry.addEndpoint("/stompOverWebSocket");
|
||||||
this.endpointRegistry.addEndpoint("/stompOverSockJS").withSockJS();
|
this.endpointRegistry.addEndpoint("/stompOverSockJS").withSockJS();
|
||||||
|
|
||||||
|
//SPR-12403
|
||||||
|
assertEquals(1, this.webSocketHandler.getProtocolHandlers().size());
|
||||||
|
|
||||||
hm = (SimpleUrlHandlerMapping) this.endpointRegistry.getHandlerMapping();
|
hm = (SimpleUrlHandlerMapping) this.endpointRegistry.getHandlerMapping();
|
||||||
assertEquals(2, hm.getUrlMap().size());
|
assertEquals(2, hm.getUrlMap().size());
|
||||||
assertNotNull(hm.getUrlMap().get("/stompOverWebSocket"));
|
assertNotNull(hm.getUrlMap().get("/stompOverWebSocket"));
|
||||||
|
|||||||
Reference in New Issue
Block a user