Skip to content

Commit 6d6cc0e

Browse files
committed
Polish WebSocket Java config
1 parent 85c1750 commit 6d6cc0e

File tree

6 files changed

+78
-104
lines changed

6 files changed

+78
-104
lines changed

spring-websocket/src/main/java/org/springframework/web/socket/config/annotation/AbstractWebSocketHandlerRegistration.java

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,15 @@
3838
*/
3939
public abstract class AbstractWebSocketHandlerRegistration<M> implements WebSocketHandlerRegistration {
4040

41-
private MultiValueMap<WebSocketHandler, String> handlerMap = new LinkedMultiValueMap<WebSocketHandler, String>();
41+
private final TaskScheduler sockJsTaskScheduler;
4242

43-
private HandshakeInterceptor[] interceptors;
43+
private MultiValueMap<WebSocketHandler, String> handlerMap = new LinkedMultiValueMap<WebSocketHandler, String>();
4444

4545
private HandshakeHandler handshakeHandler;
4646

47-
private SockJsServiceRegistration sockJsServiceRegistration;
47+
private HandshakeInterceptor[] interceptors;
4848

49-
private final TaskScheduler sockJsTaskScheduler;
49+
private SockJsServiceRegistration sockJsServiceRegistration;
5050

5151

5252
public AbstractWebSocketHandlerRegistration(TaskScheduler defaultTaskScheduler) {
@@ -68,8 +68,8 @@ public WebSocketHandlerRegistration setHandshakeHandler(HandshakeHandler handsha
6868
return this;
6969
}
7070

71-
public HandshakeHandler getHandshakeHandler() {
72-
return handshakeHandler;
71+
protected HandshakeHandler getHandshakeHandler() {
72+
return this.handshakeHandler;
7373
}
7474

7575
@Override
@@ -82,30 +82,21 @@ protected HandshakeInterceptor[] getInterceptors() {
8282
return this.interceptors;
8383
}
8484

85-
/**
86-
* @param interceptors the interceptors to set
87-
*/
88-
public void setInterceptors(HandshakeInterceptor[] interceptors) {
89-
this.interceptors = interceptors;
90-
}
91-
9285
@Override
9386
public SockJsServiceRegistration withSockJS() {
94-
9587
this.sockJsServiceRegistration = new SockJsServiceRegistration(this.sockJsTaskScheduler);
96-
this.sockJsServiceRegistration.setInterceptors(this.interceptors);
97-
88+
if (this.interceptors != null) {
89+
this.sockJsServiceRegistration.setInterceptors(this.interceptors);
90+
}
9891
if (this.handshakeHandler != null) {
9992
WebSocketTransportHandler transportHandler = new WebSocketTransportHandler(this.handshakeHandler);
10093
this.sockJsServiceRegistration.setTransportHandlerOverrides(transportHandler);
10194
}
102-
10395
return this.sockJsServiceRegistration;
10496
}
10597

106-
public final M getMappings() {
98+
protected final M getMappings() {
10799
M mappings = createMappings();
108-
109100
if (this.sockJsServiceRegistration != null) {
110101
SockJsService sockJsService = this.sockJsServiceRegistration.getSockJsService();
111102
for (WebSocketHandler wsHandler : this.handlerMap.keySet()) {

spring-websocket/src/main/java/org/springframework/web/socket/config/annotation/WebSocketHandlerRegistration.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,14 @@ public interface WebSocketHandlerRegistration {
3535
WebSocketHandlerRegistration addHandler(WebSocketHandler handler, String... paths);
3636

3737
/**
38-
* Configure interceptors for the handshake request.
38+
* Configure the HandshakeHandler to use.
3939
*/
40-
WebSocketHandlerRegistration addInterceptors(HandshakeInterceptor... interceptors);
40+
WebSocketHandlerRegistration setHandshakeHandler(HandshakeHandler handshakeHandler);
4141

4242
/**
43-
* Configure the HandshakeHandler to use.
43+
* Configure interceptors for the handshake request.
4444
*/
45-
WebSocketHandlerRegistration setHandshakeHandler(HandshakeHandler handshakeHandler);
45+
WebSocketHandlerRegistration addInterceptors(HandshakeInterceptor... interceptors);
4646

4747
/**
4848
* Enable SockJS fallback options.

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

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,14 @@
3535
import static org.junit.Assert.*;
3636

3737
/**
38-
* Test fixture for {@link org.springframework.web.socket.config.annotation.WebMvcStompEndpointRegistry}.
38+
* Test fixture for
39+
* {@link org.springframework.web.socket.config.annotation.WebMvcStompEndpointRegistry}.
3940
*
4041
* @author Rossen Stoyanchev
4142
*/
4243
public class WebMvcStompEndpointRegistryTests {
4344

44-
private WebMvcStompEndpointRegistry registry;
45+
private WebMvcStompEndpointRegistry endpointRegistry;
4546

4647
private SubProtocolWebSocketHandler webSocketHandler;
4748

@@ -50,22 +51,18 @@ public class WebMvcStompEndpointRegistryTests {
5051

5152
@Before
5253
public void setup() {
53-
5454
SubscribableChannel inChannel = Mockito.mock(SubscribableChannel.class);
5555
SubscribableChannel outChannel = Mockito.mock(SubscribableChannel.class);
56-
5756
this.webSocketHandler = new SubProtocolWebSocketHandler(inChannel, outChannel);
5857
this.userSessionRegistry = new DefaultUserSessionRegistry();
59-
60-
this.registry = new WebMvcStompEndpointRegistry(this.webSocketHandler,
58+
this.endpointRegistry = new WebMvcStompEndpointRegistry(this.webSocketHandler,
6159
new WebSocketTransportRegistration(), this.userSessionRegistry, Mockito.mock(TaskScheduler.class));
6260
}
6361

6462

6563
@Test
6664
public void stompProtocolHandler() {
67-
68-
this.registry.addEndpoint("/stomp");
65+
this.endpointRegistry.addEndpoint("/stomp");
6966

7067
Map<String, SubProtocolHandler> protocolHandlers = webSocketHandler.getProtocolHandlerMap();
7168
assertEquals(3, protocolHandlers.size());
@@ -79,16 +76,15 @@ public void stompProtocolHandler() {
7976

8077
@Test
8178
public void handlerMapping() {
82-
83-
SimpleUrlHandlerMapping hm = (SimpleUrlHandlerMapping) this.registry.getHandlerMapping();
79+
SimpleUrlHandlerMapping hm = (SimpleUrlHandlerMapping) this.endpointRegistry.getHandlerMapping();
8480
assertEquals(0, hm.getUrlMap().size());
8581

8682
UrlPathHelper pathHelper = new UrlPathHelper();
87-
this.registry.setUrlPathHelper(pathHelper);
88-
this.registry.addEndpoint("/stompOverWebSocket");
89-
this.registry.addEndpoint("/stompOverSockJS").withSockJS();
83+
this.endpointRegistry.setUrlPathHelper(pathHelper);
84+
this.endpointRegistry.addEndpoint("/stompOverWebSocket");
85+
this.endpointRegistry.addEndpoint("/stompOverSockJS").withSockJS();
9086

91-
hm = (SimpleUrlHandlerMapping) this.registry.getHandlerMapping();
87+
hm = (SimpleUrlHandlerMapping) this.endpointRegistry.getHandlerMapping();
9288
assertEquals(2, hm.getUrlMap().size());
9389
assertNotNull(hm.getUrlMap().get("/stompOverWebSocket"));
9490
assertNotNull(hm.getUrlMap().get("/stompOverSockJS/**"));
Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -22,47 +22,47 @@
2222

2323
import org.junit.Before;
2424
import org.junit.Test;
25-
import org.mockito.Mockito;
2625

27-
import org.springframework.messaging.support.ExecutorSubscribableChannel;
26+
import org.springframework.messaging.MessageChannel;
27+
import org.springframework.messaging.SubscribableChannel;
2828
import org.springframework.scheduling.TaskScheduler;
2929
import org.springframework.util.MultiValueMap;
3030
import org.springframework.web.HttpRequestHandler;
3131
import org.springframework.web.socket.messaging.SubProtocolWebSocketHandler;
3232
import org.springframework.web.socket.server.support.DefaultHandshakeHandler;
3333
import org.springframework.web.socket.server.support.WebSocketHttpRequestHandler;
3434
import org.springframework.web.socket.sockjs.support.SockJsHttpRequestHandler;
35+
import org.springframework.web.socket.sockjs.transport.TransportHandler;
3536
import org.springframework.web.socket.sockjs.transport.TransportType;
3637
import org.springframework.web.socket.sockjs.transport.handler.DefaultSockJsService;
3738
import org.springframework.web.socket.sockjs.transport.handler.WebSocketTransportHandler;
3839

3940
import static org.junit.Assert.*;
41+
import static org.mockito.Mockito.mock;
4042

4143
/**
42-
* Test fixture for {@link org.springframework.web.socket.config.annotation.WebMvcStompWebSocketEndpointRegistration}.
44+
* Test fixture for
45+
* {@link org.springframework.web.socket.config.annotation.WebMvcStompWebSocketEndpointRegistration}.
4346
*
4447
* @author Rossen Stoyanchev
4548
*/
46-
public class WebMvcStompEndpointRegistrationTests {
49+
public class WebMvcStompWebSocketEndpointRegistrationTests {
4750

48-
private SubProtocolWebSocketHandler wsHandler;
51+
private SubProtocolWebSocketHandler handler;
4952

5053
private TaskScheduler scheduler;
5154

5255

5356
@Before
5457
public void setup() {
55-
this.wsHandler = new SubProtocolWebSocketHandler(
56-
new ExecutorSubscribableChannel(), new ExecutorSubscribableChannel());
57-
this.scheduler = Mockito.mock(TaskScheduler.class);
58+
this.handler = new SubProtocolWebSocketHandler(mock(MessageChannel.class), mock(SubscribableChannel.class));
59+
this.scheduler = mock(TaskScheduler.class);
5860
}
5961

6062
@Test
6163
public void minimalRegistration() {
62-
63-
64-
WebMvcStompWebSocketEndpointRegistration registration = new WebMvcStompWebSocketEndpointRegistration(
65-
new String[] {"/foo"}, this.wsHandler, this.scheduler);
64+
WebMvcStompWebSocketEndpointRegistration registration =
65+
new WebMvcStompWebSocketEndpointRegistration(new String[] {"/foo"}, this.handler, this.scheduler);
6666

6767
MultiValueMap<HttpRequestHandler, String> mappings = registration.getMappings();
6868
assertEquals(1, mappings.size());
@@ -74,12 +74,10 @@ public void minimalRegistration() {
7474

7575
@Test
7676
public void customHandshakeHandler() {
77+
WebMvcStompWebSocketEndpointRegistration registration =
78+
new WebMvcStompWebSocketEndpointRegistration(new String[] {"/foo"}, this.handler, this.scheduler);
7779

7880
DefaultHandshakeHandler handshakeHandler = new DefaultHandshakeHandler();
79-
80-
WebMvcStompWebSocketEndpointRegistration registration = new WebMvcStompWebSocketEndpointRegistration(
81-
new String[] {"/foo"}, this.wsHandler, this.scheduler);
82-
8381
registration.setHandshakeHandler(handshakeHandler);
8482

8583
MultiValueMap<HttpRequestHandler, String> mappings = registration.getMappings();
@@ -95,12 +93,10 @@ public void customHandshakeHandler() {
9593

9694
@Test
9795
public void customHandshakeHandlerPassedToSockJsService() {
96+
WebMvcStompWebSocketEndpointRegistration registration =
97+
new WebMvcStompWebSocketEndpointRegistration(new String[] {"/foo"}, this.handler, this.scheduler);
9898

9999
DefaultHandshakeHandler handshakeHandler = new DefaultHandshakeHandler();
100-
101-
WebMvcStompWebSocketEndpointRegistration registration = new WebMvcStompWebSocketEndpointRegistration(
102-
new String[] {"/foo"}, this.wsHandler, this.scheduler);
103-
104100
registration.setHandshakeHandler(handshakeHandler);
105101
registration.withSockJS();
106102

@@ -116,8 +112,8 @@ public void customHandshakeHandlerPassedToSockJsService() {
116112
DefaultSockJsService sockJsService = (DefaultSockJsService) requestHandler.getSockJsService();
117113
assertNotNull(sockJsService);
118114

119-
WebSocketTransportHandler transportHandler =
120-
(WebSocketTransportHandler) sockJsService.getTransportHandlers().get(TransportType.WEBSOCKET);
115+
Map<TransportType, TransportHandler> handlers = sockJsService.getTransportHandlers();
116+
WebSocketTransportHandler transportHandler = (WebSocketTransportHandler) handlers.get(TransportType.WEBSOCKET);
121117
assertSame(handshakeHandler, transportHandler.getHandshakeHandler());
122118
}
123119

0 commit comments

Comments
 (0)