Add SubProtocolCapable interface

The addition of SubProtocolCapable simplifies configuration since it is
no longer necessary to explicitly configure DefaultHandshakeHandler
with a list of supported sub-protocols. We will not also check if the
WebSocketHandler to use for the WebSocket request is an instance of
SubProtocolCapable and obtain the list of sub-protocols that way. The
provided SubProtocolWebSocketHandler does implement this interface.

Issue: SPR-11111
This commit is contained in:
Rossen Stoyanchev
2013-11-25 18:02:35 -05:00
parent 59002f2456
commit 4e82416ba9
7 changed files with 161 additions and 52 deletions

View File

@@ -17,7 +17,6 @@
package org.springframework.web.socket.messaging.config;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
@@ -62,7 +61,7 @@ public class WebMvcStompEndpointRegistrationTests {
WebMvcStompWebSocketEndpointRegistration registration = new WebMvcStompWebSocketEndpointRegistration(
new String[] {"/foo"}, this.wsHandler, Collections.<String>emptySet(), this.scheduler);
new String[] {"/foo"}, this.wsHandler, this.scheduler);
MultiValueMap<HttpRequestHandler, String> mappings = registration.getMappings();
assertEquals(1, mappings.size());
@@ -78,7 +77,7 @@ public class WebMvcStompEndpointRegistrationTests {
DefaultHandshakeHandler handshakeHandler = new DefaultHandshakeHandler();
WebMvcStompWebSocketEndpointRegistration registration = new WebMvcStompWebSocketEndpointRegistration(
new String[] {"/foo"}, this.wsHandler, Collections.<String>emptySet(), this.scheduler);
new String[] {"/foo"}, this.wsHandler, this.scheduler);
registration.setHandshakeHandler(handshakeHandler);
@@ -99,7 +98,7 @@ public class WebMvcStompEndpointRegistrationTests {
DefaultHandshakeHandler handshakeHandler = new DefaultHandshakeHandler();
WebMvcStompWebSocketEndpointRegistration registration = new WebMvcStompWebSocketEndpointRegistration(
new String[] {"/foo"}, this.wsHandler, Collections.<String>emptySet(), this.scheduler);
new String[] {"/foo"}, this.wsHandler, this.scheduler);
registration.setHandshakeHandler(handshakeHandler);
registration.withSockJS();

View File

@@ -16,7 +16,9 @@
package org.springframework.web.socket.server;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import org.junit.Before;
@@ -24,11 +26,13 @@ import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.web.socket.AbstractHttpRequestTests;
import org.springframework.web.socket.support.SubProtocolCapable;
import org.springframework.web.socket.support.WebSocketExtension;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.adapter.TextWebSocketHandlerAdapter;
import org.springframework.web.socket.support.WebSocketHttpHeaders;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.*;
@@ -53,15 +57,15 @@ public class DefaultHandshakeHandlerTests extends AbstractHttpRequestTests {
@Test
public void selectSubProtocol() throws Exception {
public void supportedSubProtocols() throws Exception {
this.handshakeHandler.setSupportedProtocols("stomp", "mqtt");
when(this.upgradeStrategy.getSupportedVersions()).thenReturn(new String[] { "13" });
WebSocketHttpHeaders headers = new WebSocketHttpHeaders(this.request.getHeaders());
this.servletRequest.setMethod("GET");
WebSocketHttpHeaders headers = new WebSocketHttpHeaders(this.request.getHeaders());
headers.setUpgrade("WebSocket");
headers.setConnection("Upgrade");
headers.setSecWebSocketVersion("13");
@@ -70,11 +74,70 @@ public class DefaultHandshakeHandlerTests extends AbstractHttpRequestTests {
WebSocketHandler handler = new TextWebSocketHandlerAdapter();
Map<String, Object> attributes = Collections.<String, Object>emptyMap();
this.handshakeHandler.doHandshake(this.request, this.response, handler, attributes);
verify(this.upgradeStrategy).upgrade(this.request, this.response,
"STOMP", Collections.<WebSocketExtension>emptyList(), handler, attributes);
}
@Test
public void subProtocolCapableHandler() throws Exception {
when(this.upgradeStrategy.getSupportedVersions()).thenReturn(new String[]{"13"});
this.servletRequest.setMethod("GET");
WebSocketHttpHeaders headers = new WebSocketHttpHeaders(this.request.getHeaders());
headers.setUpgrade("WebSocket");
headers.setConnection("Upgrade");
headers.setSecWebSocketVersion("13");
headers.setSecWebSocketKey("82/ZS2YHjEnUN97HLL8tbw==");
headers.setSecWebSocketProtocol("v11.stomp");
WebSocketHandler handler = new SubProtocolCapableHandler("v12.stomp", "v11.stomp");
Map<String, Object> attributes = Collections.<String, Object>emptyMap();
this.handshakeHandler.doHandshake(this.request, this.response, handler, attributes);
verify(this.upgradeStrategy).upgrade(this.request, this.response,
"v11.stomp", Collections.<WebSocketExtension>emptyList(), handler, attributes);
}
@Test
public void subProtocolCapableHandlerNoMatch() throws Exception {
when(this.upgradeStrategy.getSupportedVersions()).thenReturn(new String[]{"13"});
this.servletRequest.setMethod("GET");
WebSocketHttpHeaders headers = new WebSocketHttpHeaders(this.request.getHeaders());
headers.setUpgrade("WebSocket");
headers.setConnection("Upgrade");
headers.setSecWebSocketVersion("13");
headers.setSecWebSocketKey("82/ZS2YHjEnUN97HLL8tbw==");
headers.setSecWebSocketProtocol("v10.stomp");
WebSocketHandler handler = new SubProtocolCapableHandler("v12.stomp", "v11.stomp");
Map<String, Object> attributes = Collections.<String, Object>emptyMap();
this.handshakeHandler.doHandshake(this.request, this.response, handler, attributes);
verify(this.upgradeStrategy).upgrade(this.request, this.response,
null, Collections.<WebSocketExtension>emptyList(), handler, attributes);
}
private static class SubProtocolCapableHandler extends TextWebSocketHandlerAdapter implements SubProtocolCapable {
private final List<String> subProtocols;
private SubProtocolCapableHandler(String... subProtocols) {
this.subProtocols = Arrays.asList(subProtocols);
}
@Override
public List<String> getSubProtocols() {
return this.subProtocols;
}
}
}