Polish WebSocket Java config
This commit is contained in:
parent
85c175059a
commit
6d6cc0ecec
|
@ -38,15 +38,15 @@ import org.springframework.web.socket.sockjs.transport.handler.WebSocketTranspor
|
|||
*/
|
||||
public abstract class AbstractWebSocketHandlerRegistration<M> implements WebSocketHandlerRegistration {
|
||||
|
||||
private MultiValueMap<WebSocketHandler, String> handlerMap = new LinkedMultiValueMap<WebSocketHandler, String>();
|
||||
private final TaskScheduler sockJsTaskScheduler;
|
||||
|
||||
private HandshakeInterceptor[] interceptors;
|
||||
private MultiValueMap<WebSocketHandler, String> handlerMap = new LinkedMultiValueMap<WebSocketHandler, String>();
|
||||
|
||||
private HandshakeHandler handshakeHandler;
|
||||
|
||||
private SockJsServiceRegistration sockJsServiceRegistration;
|
||||
private HandshakeInterceptor[] interceptors;
|
||||
|
||||
private final TaskScheduler sockJsTaskScheduler;
|
||||
private SockJsServiceRegistration sockJsServiceRegistration;
|
||||
|
||||
|
||||
public AbstractWebSocketHandlerRegistration(TaskScheduler defaultTaskScheduler) {
|
||||
|
@ -68,8 +68,8 @@ public abstract class AbstractWebSocketHandlerRegistration<M> implements WebSock
|
|||
return this;
|
||||
}
|
||||
|
||||
public HandshakeHandler getHandshakeHandler() {
|
||||
return handshakeHandler;
|
||||
protected HandshakeHandler getHandshakeHandler() {
|
||||
return this.handshakeHandler;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -82,30 +82,21 @@ public abstract class AbstractWebSocketHandlerRegistration<M> implements WebSock
|
|||
return this.interceptors;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param interceptors the interceptors to set
|
||||
*/
|
||||
public void setInterceptors(HandshakeInterceptor[] interceptors) {
|
||||
this.interceptors = interceptors;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SockJsServiceRegistration withSockJS() {
|
||||
|
||||
this.sockJsServiceRegistration = new SockJsServiceRegistration(this.sockJsTaskScheduler);
|
||||
this.sockJsServiceRegistration.setInterceptors(this.interceptors);
|
||||
|
||||
if (this.interceptors != null) {
|
||||
this.sockJsServiceRegistration.setInterceptors(this.interceptors);
|
||||
}
|
||||
if (this.handshakeHandler != null) {
|
||||
WebSocketTransportHandler transportHandler = new WebSocketTransportHandler(this.handshakeHandler);
|
||||
this.sockJsServiceRegistration.setTransportHandlerOverrides(transportHandler);
|
||||
}
|
||||
|
||||
return this.sockJsServiceRegistration;
|
||||
}
|
||||
|
||||
public final M getMappings() {
|
||||
protected final M getMappings() {
|
||||
M mappings = createMappings();
|
||||
|
||||
if (this.sockJsServiceRegistration != null) {
|
||||
SockJsService sockJsService = this.sockJsServiceRegistration.getSockJsService();
|
||||
for (WebSocketHandler wsHandler : this.handlerMap.keySet()) {
|
||||
|
|
|
@ -34,16 +34,16 @@ public interface WebSocketHandlerRegistration {
|
|||
*/
|
||||
WebSocketHandlerRegistration addHandler(WebSocketHandler handler, String... paths);
|
||||
|
||||
/**
|
||||
* Configure interceptors for the handshake request.
|
||||
*/
|
||||
WebSocketHandlerRegistration addInterceptors(HandshakeInterceptor... interceptors);
|
||||
|
||||
/**
|
||||
* Configure the HandshakeHandler to use.
|
||||
*/
|
||||
WebSocketHandlerRegistration setHandshakeHandler(HandshakeHandler handshakeHandler);
|
||||
|
||||
/**
|
||||
* Configure interceptors for the handshake request.
|
||||
*/
|
||||
WebSocketHandlerRegistration addInterceptors(HandshakeInterceptor... interceptors);
|
||||
|
||||
/**
|
||||
* Enable SockJS fallback options.
|
||||
*/
|
||||
|
|
|
@ -35,13 +35,14 @@ import org.springframework.web.util.UrlPathHelper;
|
|||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* Test fixture for {@link org.springframework.web.socket.config.annotation.WebMvcStompEndpointRegistry}.
|
||||
* Test fixture for
|
||||
* {@link org.springframework.web.socket.config.annotation.WebMvcStompEndpointRegistry}.
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
*/
|
||||
public class WebMvcStompEndpointRegistryTests {
|
||||
|
||||
private WebMvcStompEndpointRegistry registry;
|
||||
private WebMvcStompEndpointRegistry endpointRegistry;
|
||||
|
||||
private SubProtocolWebSocketHandler webSocketHandler;
|
||||
|
||||
|
@ -50,22 +51,18 @@ public class WebMvcStompEndpointRegistryTests {
|
|||
|
||||
@Before
|
||||
public void setup() {
|
||||
|
||||
SubscribableChannel inChannel = Mockito.mock(SubscribableChannel.class);
|
||||
SubscribableChannel outChannel = Mockito.mock(SubscribableChannel.class);
|
||||
|
||||
this.webSocketHandler = new SubProtocolWebSocketHandler(inChannel, outChannel);
|
||||
this.userSessionRegistry = new DefaultUserSessionRegistry();
|
||||
|
||||
this.registry = new WebMvcStompEndpointRegistry(this.webSocketHandler,
|
||||
this.endpointRegistry = new WebMvcStompEndpointRegistry(this.webSocketHandler,
|
||||
new WebSocketTransportRegistration(), this.userSessionRegistry, Mockito.mock(TaskScheduler.class));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void stompProtocolHandler() {
|
||||
|
||||
this.registry.addEndpoint("/stomp");
|
||||
this.endpointRegistry.addEndpoint("/stomp");
|
||||
|
||||
Map<String, SubProtocolHandler> protocolHandlers = webSocketHandler.getProtocolHandlerMap();
|
||||
assertEquals(3, protocolHandlers.size());
|
||||
|
@ -79,16 +76,15 @@ public class WebMvcStompEndpointRegistryTests {
|
|||
|
||||
@Test
|
||||
public void handlerMapping() {
|
||||
|
||||
SimpleUrlHandlerMapping hm = (SimpleUrlHandlerMapping) this.registry.getHandlerMapping();
|
||||
SimpleUrlHandlerMapping hm = (SimpleUrlHandlerMapping) this.endpointRegistry.getHandlerMapping();
|
||||
assertEquals(0, hm.getUrlMap().size());
|
||||
|
||||
UrlPathHelper pathHelper = new UrlPathHelper();
|
||||
this.registry.setUrlPathHelper(pathHelper);
|
||||
this.registry.addEndpoint("/stompOverWebSocket");
|
||||
this.registry.addEndpoint("/stompOverSockJS").withSockJS();
|
||||
this.endpointRegistry.setUrlPathHelper(pathHelper);
|
||||
this.endpointRegistry.addEndpoint("/stompOverWebSocket");
|
||||
this.endpointRegistry.addEndpoint("/stompOverSockJS").withSockJS();
|
||||
|
||||
hm = (SimpleUrlHandlerMapping) this.registry.getHandlerMapping();
|
||||
hm = (SimpleUrlHandlerMapping) this.endpointRegistry.getHandlerMapping();
|
||||
assertEquals(2, hm.getUrlMap().size());
|
||||
assertNotNull(hm.getUrlMap().get("/stompOverWebSocket"));
|
||||
assertNotNull(hm.getUrlMap().get("/stompOverSockJS/**"));
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2013 the original author or authors.
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
@ -22,9 +22,9 @@ import java.util.Map;
|
|||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
import org.springframework.messaging.support.ExecutorSubscribableChannel;
|
||||
import org.springframework.messaging.MessageChannel;
|
||||
import org.springframework.messaging.SubscribableChannel;
|
||||
import org.springframework.scheduling.TaskScheduler;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
import org.springframework.web.HttpRequestHandler;
|
||||
|
@ -32,37 +32,37 @@ import org.springframework.web.socket.messaging.SubProtocolWebSocketHandler;
|
|||
import org.springframework.web.socket.server.support.DefaultHandshakeHandler;
|
||||
import org.springframework.web.socket.server.support.WebSocketHttpRequestHandler;
|
||||
import org.springframework.web.socket.sockjs.support.SockJsHttpRequestHandler;
|
||||
import org.springframework.web.socket.sockjs.transport.TransportHandler;
|
||||
import org.springframework.web.socket.sockjs.transport.TransportType;
|
||||
import org.springframework.web.socket.sockjs.transport.handler.DefaultSockJsService;
|
||||
import org.springframework.web.socket.sockjs.transport.handler.WebSocketTransportHandler;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
/**
|
||||
* Test fixture for {@link org.springframework.web.socket.config.annotation.WebMvcStompWebSocketEndpointRegistration}.
|
||||
* Test fixture for
|
||||
* {@link org.springframework.web.socket.config.annotation.WebMvcStompWebSocketEndpointRegistration}.
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
*/
|
||||
public class WebMvcStompEndpointRegistrationTests {
|
||||
public class WebMvcStompWebSocketEndpointRegistrationTests {
|
||||
|
||||
private SubProtocolWebSocketHandler wsHandler;
|
||||
private SubProtocolWebSocketHandler handler;
|
||||
|
||||
private TaskScheduler scheduler;
|
||||
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
this.wsHandler = new SubProtocolWebSocketHandler(
|
||||
new ExecutorSubscribableChannel(), new ExecutorSubscribableChannel());
|
||||
this.scheduler = Mockito.mock(TaskScheduler.class);
|
||||
this.handler = new SubProtocolWebSocketHandler(mock(MessageChannel.class), mock(SubscribableChannel.class));
|
||||
this.scheduler = mock(TaskScheduler.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void minimalRegistration() {
|
||||
|
||||
|
||||
WebMvcStompWebSocketEndpointRegistration registration = new WebMvcStompWebSocketEndpointRegistration(
|
||||
new String[] {"/foo"}, this.wsHandler, this.scheduler);
|
||||
WebMvcStompWebSocketEndpointRegistration registration =
|
||||
new WebMvcStompWebSocketEndpointRegistration(new String[] {"/foo"}, this.handler, this.scheduler);
|
||||
|
||||
MultiValueMap<HttpRequestHandler, String> mappings = registration.getMappings();
|
||||
assertEquals(1, mappings.size());
|
||||
|
@ -74,12 +74,10 @@ public class WebMvcStompEndpointRegistrationTests {
|
|||
|
||||
@Test
|
||||
public void customHandshakeHandler() {
|
||||
WebMvcStompWebSocketEndpointRegistration registration =
|
||||
new WebMvcStompWebSocketEndpointRegistration(new String[] {"/foo"}, this.handler, this.scheduler);
|
||||
|
||||
DefaultHandshakeHandler handshakeHandler = new DefaultHandshakeHandler();
|
||||
|
||||
WebMvcStompWebSocketEndpointRegistration registration = new WebMvcStompWebSocketEndpointRegistration(
|
||||
new String[] {"/foo"}, this.wsHandler, this.scheduler);
|
||||
|
||||
registration.setHandshakeHandler(handshakeHandler);
|
||||
|
||||
MultiValueMap<HttpRequestHandler, String> mappings = registration.getMappings();
|
||||
|
@ -95,12 +93,10 @@ public class WebMvcStompEndpointRegistrationTests {
|
|||
|
||||
@Test
|
||||
public void customHandshakeHandlerPassedToSockJsService() {
|
||||
WebMvcStompWebSocketEndpointRegistration registration =
|
||||
new WebMvcStompWebSocketEndpointRegistration(new String[] {"/foo"}, this.handler, this.scheduler);
|
||||
|
||||
DefaultHandshakeHandler handshakeHandler = new DefaultHandshakeHandler();
|
||||
|
||||
WebMvcStompWebSocketEndpointRegistration registration = new WebMvcStompWebSocketEndpointRegistration(
|
||||
new String[] {"/foo"}, this.wsHandler, this.scheduler);
|
||||
|
||||
registration.setHandshakeHandler(handshakeHandler);
|
||||
registration.withSockJS();
|
||||
|
||||
|
@ -116,8 +112,8 @@ public class WebMvcStompEndpointRegistrationTests {
|
|||
DefaultSockJsService sockJsService = (DefaultSockJsService) requestHandler.getSockJsService();
|
||||
assertNotNull(sockJsService);
|
||||
|
||||
WebSocketTransportHandler transportHandler =
|
||||
(WebSocketTransportHandler) sockJsService.getTransportHandlers().get(TransportType.WEBSOCKET);
|
||||
Map<TransportType, TransportHandler> handlers = sockJsService.getTransportHandlers();
|
||||
WebSocketTransportHandler transportHandler = (WebSocketTransportHandler) handlers.get(TransportType.WEBSOCKET);
|
||||
assertSame(handshakeHandler, transportHandler.getHandshakeHandler());
|
||||
}
|
||||
|
|
@ -38,7 +38,8 @@ import org.springframework.web.socket.sockjs.transport.handler.WebSocketTranspor
|
|||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* Test fixture for {@link org.springframework.web.socket.config.annotation.AbstractWebSocketHandlerRegistration}.
|
||||
* Test fixture for
|
||||
* {@link org.springframework.web.socket.config.annotation.AbstractWebSocketHandlerRegistration}.
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
*/
|
||||
|
@ -57,99 +58,93 @@ public class WebSocketHandlerRegistrationTests {
|
|||
|
||||
@Test
|
||||
public void minimal() {
|
||||
|
||||
WebSocketHandler wsHandler = new TextWebSocketHandler();
|
||||
this.registration.addHandler(wsHandler, "/foo", "/bar");
|
||||
WebSocketHandler handler = new TextWebSocketHandler();
|
||||
this.registration.addHandler(handler, "/foo", "/bar");
|
||||
|
||||
List<Mapping> mappings = this.registration.getMappings();
|
||||
assertEquals(2, mappings.size());
|
||||
|
||||
Mapping m1 = mappings.get(0);
|
||||
assertEquals(wsHandler, m1.webSocketHandler);
|
||||
assertEquals(handler, m1.webSocketHandler);
|
||||
assertEquals("/foo", m1.path);
|
||||
|
||||
Mapping m2 = mappings.get(1);
|
||||
assertEquals(wsHandler, m2.webSocketHandler);
|
||||
assertEquals(handler, m2.webSocketHandler);
|
||||
assertEquals("/bar", m2.path);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void interceptors() {
|
||||
|
||||
WebSocketHandler wsHandler = new TextWebSocketHandler();
|
||||
WebSocketHandler handler = new TextWebSocketHandler();
|
||||
HttpSessionHandshakeInterceptor interceptor = new HttpSessionHandshakeInterceptor();
|
||||
|
||||
this.registration.addHandler(wsHandler, "/foo").addInterceptors(interceptor);
|
||||
this.registration.addHandler(handler, "/foo").addInterceptors(interceptor);
|
||||
|
||||
List<Mapping> mappings = this.registration.getMappings();
|
||||
assertEquals(1, mappings.size());
|
||||
|
||||
Mapping m1 = mappings.get(0);
|
||||
assertEquals(wsHandler, m1.webSocketHandler);
|
||||
assertEquals("/foo", m1.path);
|
||||
assertArrayEquals(new HandshakeInterceptor[] { interceptor }, m1.interceptors);
|
||||
Mapping mapping = mappings.get(0);
|
||||
assertEquals(handler, mapping.webSocketHandler);
|
||||
assertEquals("/foo", mapping.path);
|
||||
assertArrayEquals(new HandshakeInterceptor[] {interceptor}, mapping.interceptors);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void interceptorsPassedToSockJsRegistration() {
|
||||
|
||||
WebSocketHandler wsHandler = new TextWebSocketHandler();
|
||||
WebSocketHandler handler = new TextWebSocketHandler();
|
||||
HttpSessionHandshakeInterceptor interceptor = new HttpSessionHandshakeInterceptor();
|
||||
|
||||
this.registration.addHandler(wsHandler, "/foo").addInterceptors(interceptor).withSockJS();
|
||||
this.registration.addHandler(handler, "/foo").addInterceptors(interceptor).withSockJS();
|
||||
|
||||
List<Mapping> mappings = this.registration.getMappings();
|
||||
assertEquals(1, mappings.size());
|
||||
|
||||
Mapping m1 = mappings.get(0);
|
||||
assertEquals(wsHandler, m1.webSocketHandler);
|
||||
assertEquals("/foo/**", m1.path);
|
||||
assertNotNull(m1.sockJsService);
|
||||
assertEquals(Arrays.asList(interceptor), m1.sockJsService.getHandshakeInterceptors());
|
||||
Mapping mapping = mappings.get(0);
|
||||
assertEquals(handler, mapping.webSocketHandler);
|
||||
assertEquals("/foo/**", mapping.path);
|
||||
assertNotNull(mapping.sockJsService);
|
||||
assertEquals(Arrays.asList(interceptor), mapping.sockJsService.getHandshakeInterceptors());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void handshakeHandler() {
|
||||
|
||||
WebSocketHandler wsHandler = new TextWebSocketHandler();
|
||||
WebSocketHandler handler = new TextWebSocketHandler();
|
||||
HandshakeHandler handshakeHandler = new DefaultHandshakeHandler();
|
||||
|
||||
this.registration.addHandler(wsHandler, "/foo").setHandshakeHandler(handshakeHandler);
|
||||
this.registration.addHandler(handler, "/foo").setHandshakeHandler(handshakeHandler);
|
||||
|
||||
List<Mapping> mappings = this.registration.getMappings();
|
||||
assertEquals(1, mappings.size());
|
||||
|
||||
Mapping m1 = mappings.get(0);
|
||||
assertEquals(wsHandler, m1.webSocketHandler);
|
||||
assertEquals("/foo", m1.path);
|
||||
assertSame(handshakeHandler, m1.handshakeHandler);
|
||||
Mapping mapping = mappings.get(0);
|
||||
assertEquals(handler, mapping.webSocketHandler);
|
||||
assertEquals("/foo", mapping.path);
|
||||
assertSame(handshakeHandler, mapping.handshakeHandler);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void handshakeHandlerPassedToSockJsRegistration() {
|
||||
|
||||
WebSocketHandler wsHandler = new TextWebSocketHandler();
|
||||
WebSocketHandler handler = new TextWebSocketHandler();
|
||||
HandshakeHandler handshakeHandler = new DefaultHandshakeHandler();
|
||||
|
||||
this.registration.addHandler(wsHandler, "/foo").setHandshakeHandler(handshakeHandler).withSockJS();
|
||||
this.registration.addHandler(handler, "/foo").setHandshakeHandler(handshakeHandler).withSockJS();
|
||||
|
||||
List<Mapping> mappings = this.registration.getMappings();
|
||||
assertEquals(1, mappings.size());
|
||||
|
||||
Mapping m1 = mappings.get(0);
|
||||
assertEquals(wsHandler, m1.webSocketHandler);
|
||||
assertEquals("/foo/**", m1.path);
|
||||
assertNotNull(m1.sockJsService);
|
||||
Mapping mapping = mappings.get(0);
|
||||
assertEquals(handler, mapping.webSocketHandler);
|
||||
assertEquals("/foo/**", mapping.path);
|
||||
assertNotNull(mapping.sockJsService);
|
||||
|
||||
WebSocketTransportHandler transportHandler =
|
||||
(WebSocketTransportHandler) m1.sockJsService.getTransportHandlers().get(TransportType.WEBSOCKET);
|
||||
(WebSocketTransportHandler) mapping.sockJsService.getTransportHandlers().get(TransportType.WEBSOCKET);
|
||||
assertSame(handshakeHandler, transportHandler.getHandshakeHandler());
|
||||
}
|
||||
|
||||
|
||||
private static class TestWebSocketHandlerRegistration extends AbstractWebSocketHandlerRegistration<List<Mapping>> {
|
||||
|
||||
|
||||
public TestWebSocketHandlerRegistration(TaskScheduler sockJsTaskScheduler) {
|
||||
super(sockJsTaskScheduler);
|
||||
}
|
||||
|
@ -167,15 +162,13 @@ public class WebSocketHandlerRegistrationTests {
|
|||
}
|
||||
|
||||
@Override
|
||||
protected void addWebSocketHandlerMapping(List<Mapping> mappings,
|
||||
WebSocketHandler wsHandler, HandshakeHandler handshakeHandler,
|
||||
HandshakeInterceptor[] interceptors, String path) {
|
||||
protected void addWebSocketHandlerMapping(List<Mapping> mappings, WebSocketHandler handler,
|
||||
HandshakeHandler handshakeHandler, HandshakeInterceptor[] interceptors, String path) {
|
||||
|
||||
mappings.add(new Mapping(wsHandler, path, handshakeHandler, interceptors));
|
||||
mappings.add(new Mapping(handler, path, handshakeHandler, interceptors));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static class Mapping {
|
||||
|
||||
private final WebSocketHandler webSocketHandler;
|
||||
|
|
|
@ -74,7 +74,6 @@ public class WebSocketMessageBrokerConfigurationSupportTests {
|
|||
|
||||
@Test
|
||||
public void handlerMapping() {
|
||||
|
||||
SimpleUrlHandlerMapping hm = (SimpleUrlHandlerMapping) this.config.getBean(HandlerMapping.class);
|
||||
assertEquals(1, hm.getOrder());
|
||||
|
||||
|
@ -85,7 +84,6 @@ public class WebSocketMessageBrokerConfigurationSupportTests {
|
|||
|
||||
@Test
|
||||
public void clientInboundChannelSendMessage() throws Exception {
|
||||
|
||||
TestChannel channel = this.config.getBean("clientInboundChannel", TestChannel.class);
|
||||
SubProtocolWebSocketHandler webSocketHandler = this.config.getBean(SubProtocolWebSocketHandler.class);
|
||||
|
||||
|
|
Loading…
Reference in New Issue