Skip to content

Commit 05084d5

Browse files
committed
Add spring-websocket module tests
1 parent 6a5acb9 commit 05084d5

File tree

52 files changed

+2927
-387
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+2927
-387
lines changed

spring-web/src/main/java/org/springframework/http/server/ServletServerHttpRequest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ public Cookies getCookies() {
166166
@Override
167167
public MultiValueMap<String, String> getQueryParams() {
168168
if (this.queryParams == null) {
169+
// TODO: extract from query string
169170
this.queryParams = new LinkedMultiValueMap<String, String>(this.servletRequest.getParameterMap().size());
170171
for (String name : this.servletRequest.getParameterMap().keySet()) {
171172
for (String value : this.servletRequest.getParameterValues(name)) {

spring-websocket/src/main/java/org/springframework/web/socket/client/ConnectionManagerSupport.java

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import org.springframework.web.util.UriComponentsBuilder;
2727

2828
/**
29-
* Abstract base class for WebSocketConnection managers.
29+
* Abstract base class for WebSocket connection managers.
3030
*
3131
* @author Rossen Stoyanchev
3232
* @since 4.0
@@ -147,25 +147,25 @@ public void run() {
147147
public final void stop() {
148148
synchronized (this.lifecycleMonitor) {
149149
if (isRunning()) {
150-
stopInternal();
150+
if (logger.isDebugEnabled()) {
151+
logger.debug("Stopping " + this.getClass().getSimpleName());
152+
}
153+
try {
154+
stopInternal();
155+
}
156+
catch (Throwable e) {
157+
logger.error("Failed to stop WebSocket connection", e);
158+
}
159+
finally {
160+
this.isRunning = false;
161+
}
151162
}
152163
}
153164
}
154165

155-
protected void stopInternal() {
156-
if (logger.isDebugEnabled()) {
157-
logger.debug("Stopping " + this.getClass().getSimpleName());
158-
}
159-
try {
160-
if (isConnected()) {
161-
closeConnection();
162-
}
163-
}
164-
catch (Throwable e) {
165-
logger.error("Failed to stop WebSocket connection", e);
166-
}
167-
finally {
168-
this.isRunning = false;
166+
protected void stopInternal() throws Exception {
167+
if (isConnected()) {
168+
closeConnection();
169169
}
170170
}
171171

spring-websocket/src/main/java/org/springframework/web/socket/client/WebSocketConnectionManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public void startInternal() {
8484
}
8585

8686
@Override
87-
public void stopInternal() {
87+
public void stopInternal() throws Exception {
8888
if (this.syncClientLifecycle) {
8989
((SmartLifecycle) client).stop();
9090
}

spring-websocket/src/main/java/org/springframework/web/socket/client/endpoint/StandardWebSocketClient.java

Lines changed: 47 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,15 @@ public class StandardWebSocketClient implements WebSocketClient {
5252

5353
private static final Log logger = LogFactory.getLog(StandardWebSocketClient.class);
5454

55-
private static final Set<String> EXCLUDED_HEADERS = new HashSet<String>(
56-
Arrays.asList("Sec-WebSocket-Accept", "Sec-WebSocket-Extensions", "Sec-WebSocket-Key",
57-
"Sec-WebSocket-Protocol", "Sec-WebSocket-Version"));
55+
private WebSocketContainer webSocketContainer;
5856

59-
private WebSocketContainer webSocketContainer = ContainerProvider.getWebSocketContainer();
6057

58+
public WebSocketContainer getWebSocketContainer() {
59+
if (this.webSocketContainer == null) {
60+
this.webSocketContainer = ContainerProvider.getWebSocketContainer();
61+
}
62+
return this.webSocketContainer;
63+
}
6164

6265
public void setWebSocketContainer(WebSocketContainer container) {
6366
this.webSocketContainer = container;
@@ -72,8 +75,8 @@ public WebSocketSession doHandshake(WebSocketHandler webSocketHandler, String ur
7275
}
7376

7477
@Override
75-
public WebSocketSession doHandshake(WebSocketHandler webSocketHandler,
76-
final HttpHeaders httpHeaders, URI uri) throws WebSocketConnectFailureException {
78+
public WebSocketSession doHandshake(WebSocketHandler webSocketHandler, HttpHeaders httpHeaders, URI uri)
79+
throws WebSocketConnectFailureException {
7780

7881
StandardWebSocketSessionAdapter session = new StandardWebSocketSessionAdapter();
7982
session.setUri(uri);
@@ -86,29 +89,7 @@ public WebSocketSession doHandshake(WebSocketHandler webSocketHandler,
8689
if (!protocols.isEmpty()) {
8790
configBuidler.preferredSubprotocols(protocols);
8891
}
89-
configBuidler.configurator(new Configurator() {
90-
@Override
91-
public void beforeRequest(Map<String, List<String>> headers) {
92-
for (String headerName : httpHeaders.keySet()) {
93-
if (!EXCLUDED_HEADERS.contains(headerName)) {
94-
List<String> value = httpHeaders.get(headerName);
95-
if (logger.isTraceEnabled()) {
96-
logger.trace("Adding header [" + headerName + "=" + value + "]");
97-
}
98-
headers.put(headerName, value);
99-
}
100-
}
101-
if (logger.isTraceEnabled()) {
102-
logger.trace("Handshake request headers: " + headers);
103-
}
104-
}
105-
@Override
106-
public void afterResponse(HandshakeResponse handshakeResponse) {
107-
if (logger.isTraceEnabled()) {
108-
logger.trace("Handshake response headers: " + handshakeResponse.getHeaders());
109-
}
110-
}
111-
});
92+
configBuidler.configurator(new StandardWebSocketClientConfigurator(httpHeaders));
11293
}
11394

11495
try {
@@ -121,4 +102,41 @@ public void afterResponse(HandshakeResponse handshakeResponse) {
121102
}
122103
}
123104

105+
106+
private static class StandardWebSocketClientConfigurator extends Configurator {
107+
108+
private static final Set<String> EXCLUDED_HEADERS = new HashSet<String>(
109+
Arrays.asList("Sec-WebSocket-Accept", "Sec-WebSocket-Extensions", "Sec-WebSocket-Key",
110+
"Sec-WebSocket-Protocol", "Sec-WebSocket-Version"));
111+
112+
private final HttpHeaders httpHeaders;
113+
114+
115+
public StandardWebSocketClientConfigurator(HttpHeaders httpHeaders) {
116+
this.httpHeaders = httpHeaders;
117+
}
118+
119+
@Override
120+
public void beforeRequest(Map<String, List<String>> headers) {
121+
for (String headerName : this.httpHeaders.keySet()) {
122+
if (!EXCLUDED_HEADERS.contains(headerName)) {
123+
List<String> value = this.httpHeaders.get(headerName);
124+
if (logger.isTraceEnabled()) {
125+
logger.trace("Adding header [" + headerName + "=" + value + "]");
126+
}
127+
headers.put(headerName, value);
128+
}
129+
}
130+
if (logger.isTraceEnabled()) {
131+
logger.trace("Handshake request headers: " + headers);
132+
}
133+
}
134+
@Override
135+
public void afterResponse(HandshakeResponse handshakeResponse) {
136+
if (logger.isTraceEnabled()) {
137+
logger.trace("Handshake response headers: " + handshakeResponse.getHeaders());
138+
}
139+
}
140+
}
141+
124142
}

spring-websocket/src/main/java/org/springframework/web/socket/server/endpoint/EndpointExporter.java renamed to spring-websocket/src/main/java/org/springframework/web/socket/server/endpoint/ServerEndpointExporter.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,13 @@
4848
* @author Rossen Stoyanchev
4949
* @since 4.0
5050
*/
51-
public class EndpointExporter implements InitializingBean, BeanPostProcessor, ApplicationContextAware {
51+
public class ServerEndpointExporter implements InitializingBean, BeanPostProcessor, ApplicationContextAware {
5252

5353
private static final boolean isServletApiPresent =
54-
ClassUtils.isPresent("javax.servlet.ServletContext", EndpointExporter.class.getClassLoader());
54+
ClassUtils.isPresent("javax.servlet.ServletContext", ServerEndpointExporter.class.getClassLoader());
55+
56+
private static Log logger = LogFactory.getLog(ServerEndpointExporter.class);
5557

56-
private static Log logger = LogFactory.getLog(EndpointExporter.class);
5758

5859
private final List<Class<?>> annotatedEndpointClasses = new ArrayList<Class<?>>();
5960

@@ -63,6 +64,7 @@ public class EndpointExporter implements InitializingBean, BeanPostProcessor, Ap
6364

6465
private ServerContainer serverContainer;
6566

67+
6668
/**
6769
* TODO
6870
* @param annotatedEndpointClasses

spring-websocket/src/main/java/org/springframework/web/socket/server/endpoint/EndpointRegistration.java renamed to spring-websocket/src/main/java/org/springframework/web/socket/server/endpoint/ServerEndpointRegistration.java

Lines changed: 54 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,14 @@
3838

3939
/**
4040
* An implementation of {@link javax.websocket.server.ServerEndpointConfig} that also
41-
* holds the target {@link javax.websocket.Endpoint} as a reference or a bean name.
42-
*
43-
* <p>
44-
* Beans of this type are detected by {@link EndpointExporter} and
45-
* registered with a Java WebSocket runtime at startup.
41+
* holds the target {@link javax.websocket.Endpoint} provided as a reference or as a bean
42+
* name. Beans of this type are detected by {@link ServerEndpointExporter} and registered
43+
* with a Java WebSocket runtime at startup.
4644
*
4745
* @author Rossen Stoyanchev
4846
* @since 4.0
4947
*/
50-
public class EndpointRegistration implements ServerEndpointConfig, BeanFactoryAware {
48+
public class ServerEndpointRegistration implements ServerEndpointConfig, BeanFactoryAware {
5149

5250
private final String path;
5351

@@ -65,7 +63,7 @@ public class EndpointRegistration implements ServerEndpointConfig, BeanFactoryAw
6563

6664
private final Map<String, Object> userProperties = new HashMap<String, Object>();
6765

68-
private Configurator configurator = new Configurator() {};
66+
private Configurator configurator = new EndpointRegistrationConfigurator();
6967

7068

7169
/**
@@ -74,15 +72,15 @@ public class EndpointRegistration implements ServerEndpointConfig, BeanFactoryAw
7472
* @param path
7573
* @param endpointClass
7674
*/
77-
public EndpointRegistration(String path, Class<? extends Endpoint> endpointClass) {
75+
public ServerEndpointRegistration(String path, Class<? extends Endpoint> endpointClass) {
7876
Assert.hasText(path, "path must not be empty");
7977
Assert.notNull(endpointClass, "endpointClass is required");
8078
this.path = path;
8179
this.endpointProvider = new BeanCreatingHandlerProvider<Endpoint>(endpointClass);
8280
this.endpoint = null;
8381
}
8482

85-
public EndpointRegistration(String path, Endpoint endpoint) {
83+
public ServerEndpointRegistration(String path, Endpoint endpoint) {
8684
Assert.hasText(path, "path must not be empty");
8785
Assert.notNull(endpoint, "endpoint is required");
8886
this.path = path;
@@ -152,38 +150,9 @@ public List<Class<? extends Decoder>> getDecoders() {
152150
return this.decoders;
153151
}
154152

155-
/**
156-
* The {@link Configurator#getEndpointInstance(Class)} method is always ignored.
157-
*/
158-
public void setConfigurator(Configurator configurator) {
159-
this.configurator = configurator;
160-
}
161-
162153
@Override
163154
public Configurator getConfigurator() {
164-
return new Configurator() {
165-
@SuppressWarnings("unchecked")
166-
@Override
167-
public <T> T getEndpointInstance(Class<T> clazz) throws InstantiationException {
168-
return (T) EndpointRegistration.this.getEndpoint();
169-
}
170-
@Override
171-
public void modifyHandshake(ServerEndpointConfig sec, HandshakeRequest request, HandshakeResponse response) {
172-
EndpointRegistration.this.configurator.modifyHandshake(sec, request, response);
173-
}
174-
@Override
175-
public boolean checkOrigin(String originHeaderValue) {
176-
return EndpointRegistration.this.configurator.checkOrigin(originHeaderValue);
177-
}
178-
@Override
179-
public String getNegotiatedSubprotocol(List<String> supported, List<String> requested) {
180-
return EndpointRegistration.this.configurator.getNegotiatedSubprotocol(supported, requested);
181-
}
182-
@Override
183-
public List<Extension> getNegotiatedExtensions(List<Extension> installed, List<Extension> requested) {
184-
return EndpointRegistration.this.configurator.getNegotiatedExtensions(installed, requested);
185-
}
186-
};
155+
return this.configurator;
187156
}
188157

189158
@Override
@@ -193,4 +162,50 @@ public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
193162
}
194163
}
195164

165+
protected void modifyHandshake(HandshakeRequest request, HandshakeResponse response) {
166+
this.configurator.modifyHandshake(this, request, response);
167+
}
168+
169+
protected boolean checkOrigin(String originHeaderValue) {
170+
return this.configurator.checkOrigin(originHeaderValue);
171+
}
172+
173+
protected String getNegotiatedSubprotocol(List<String> supported, List<String> requested) {
174+
return this.configurator.getNegotiatedSubprotocol(supported, requested);
175+
}
176+
177+
protected List<Extension> getNegotiatedExtensions(List<Extension> installed, List<Extension> requested) {
178+
return this.configurator.getNegotiatedExtensions(installed, requested);
179+
}
180+
181+
182+
private class EndpointRegistrationConfigurator extends Configurator {
183+
184+
@SuppressWarnings("unchecked")
185+
@Override
186+
public <T> T getEndpointInstance(Class<T> clazz) throws InstantiationException {
187+
return (T) ServerEndpointRegistration.this.getEndpoint();
188+
}
189+
190+
@Override
191+
public void modifyHandshake(ServerEndpointConfig sec, HandshakeRequest request, HandshakeResponse response) {
192+
super.modifyHandshake(sec, request, response);
193+
}
194+
195+
@Override
196+
public boolean checkOrigin(String originHeaderValue) {
197+
return super.checkOrigin(originHeaderValue);
198+
}
199+
200+
@Override
201+
public String getNegotiatedSubprotocol(List<String> supported, List<String> requested) {
202+
return super.getNegotiatedSubprotocol(supported, requested);
203+
}
204+
205+
@Override
206+
public List<Extension> getNegotiatedExtensions(List<Extension> installed, List<Extension> requested) {
207+
return super.getNegotiatedExtensions(installed, requested);
208+
}
209+
}
210+
196211
}

spring-websocket/src/main/java/org/springframework/web/socket/server/endpoint/ServletServerContainerFactoryBean.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
* using its setters allows configuring the {@code ServerContainer} through Spring
3535
* configuration. This is useful even if the ServerContainer is not injected into any
3636
* other bean. For example, an application can configure a {@link DefaultHandshakeHandler}
37-
* , a {@link SockJsService}, or {@link EndpointExporter}, and separately declare this
37+
* , a {@link SockJsService}, or {@link ServerEndpointExporter}, and separately declare this
3838
* FactoryBean in order to customize the properties of the (one and only)
3939
* {@code ServerContainer} instance.
4040
*
@@ -44,9 +44,6 @@
4444
public class ServletServerContainerFactoryBean
4545
implements FactoryBean<WebSocketContainer>, InitializingBean, ServletContextAware {
4646

47-
private static final String SERVER_CONTAINER_ATTR_NAME = "javax.websocket.server.ServerContainer";
48-
49-
5047
private Long asyncSendTimeout;
5148

5249
private Long maxSessionIdleTimeout;
@@ -92,7 +89,7 @@ public Integer getMaxBinaryMessageBufferSize() {
9289

9390
@Override
9491
public void setServletContext(ServletContext servletContext) {
95-
this.serverContainer = (ServerContainer) servletContext.getAttribute(SERVER_CONTAINER_ATTR_NAME);
92+
this.serverContainer = (ServerContainer) servletContext.getAttribute("javax.websocket.server.ServerContainer");
9693
}
9794

9895
@Override

spring-websocket/src/main/java/org/springframework/web/socket/server/endpoint/SpringConfigurator.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@
2727
import org.springframework.web.context.WebApplicationContext;
2828

2929
/**
30-
* This should be used in conjuction with {@link ServerEndpoint @ServerEndpoint} classes.
30+
* This should be used in conjunction with {@link ServerEndpoint @ServerEndpoint} classes.
3131
*
32-
* <p>For {@link javax.websocket.Endpoint}, see {@link EndpointExporter}.
32+
* <p>For {@link javax.websocket.Endpoint}, see {@link ServerEndpointExporter}.
3333
*
3434
* @author Rossen Stoyanchev
3535
* @since 4.0
@@ -56,7 +56,7 @@ public <T> T getEndpointInstance(Class<T> endpointClass) throws InstantiationExc
5656
}
5757
return wac.getAutowireCapableBeanFactory().createBean(endpointClass);
5858
}
59-
if (beans.size() == 1) {
59+
else if (beans.size() == 1) {
6060
if (logger.isTraceEnabled()) {
6161
logger.trace("Using @ServerEndpoint singleton " + beans.keySet().iterator().next());
6262
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package org.springframework.web.socket.server.endpoint;
2+
3+
4+
public class Test {
5+
6+
}

0 commit comments

Comments
 (0)