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:
Brian Clozel
2014-11-03 14:25:27 +01:00
parent 330897b411
commit 1fff631daa
2 changed files with 8 additions and 3 deletions

View File

@@ -19,8 +19,10 @@ package org.springframework.web.socket.messaging;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
@@ -86,7 +88,7 @@ public class SubProtocolWebSocketHandler implements WebSocketHandler,
private final Map<String, SubProtocolHandler> protocolHandlerLookup =
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;
@@ -129,7 +131,7 @@ public class SubProtocolWebSocketHandler implements WebSocketHandler,
}
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;
}
else if (this.protocolHandlers.size() == 1) {
handler = this.protocolHandlers.get(0);
handler = this.protocolHandlers.iterator().next();
}
else {
throw new IllegalStateException("Multiple protocol handlers configured and " +

View File

@@ -84,6 +84,9 @@ public class WebMvcStompEndpointRegistryTests {
this.endpointRegistry.addEndpoint("/stompOverWebSocket");
this.endpointRegistry.addEndpoint("/stompOverSockJS").withSockJS();
//SPR-12403
assertEquals(1, this.webSocketHandler.getProtocolHandlers().size());
hm = (SimpleUrlHandlerMapping) this.endpointRegistry.getHandlerMapping();
assertEquals(2, hm.getUrlMap().size());
assertNotNull(hm.getUrlMap().get("/stompOverWebSocket"));