Add Java config support for WebSocket and STOMP

Issue: SPR-10835
This commit is contained in:
Rossen Stoyanchev
2013-08-26 17:37:31 -04:00
parent 4b6a9ac180
commit 4c0da5867a
31 changed files with 2241 additions and 295 deletions

View File

@@ -0,0 +1,57 @@
/*
* Copyright 2002-2013 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.web.socket.server.config;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.CollectionUtils;
/**
* A variation of {@link WebSocketConfigurationSupport} that detects implementations of
* {@link WebSocketConfigurer} in Spring configuration and invokes them in order to
* configure WebSocket request handling.
*
* @author Rossen Stoyanchev
* @since 4.0
*/
@Configuration
public class DelegatingWebSocketConfiguration extends WebSocketConfigurationSupport {
private final List<WebSocketConfigurer> configurers = new ArrayList<WebSocketConfigurer>();
@Autowired(required = false)
public void setConfigurers(List<WebSocketConfigurer> configurers) {
if (CollectionUtils.isEmpty(configurers)) {
return;
}
this.configurers.addAll(configurers);
}
@Override
protected void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
for (WebSocketConfigurer configurer : this.configurers) {
configurer.registerWebSocketHandlers(registry);
}
}
}

View File

@@ -0,0 +1,63 @@
/*
* Copyright 2002-2013 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. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package org.springframework.web.socket.server.config;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.context.annotation.Import;
/**
* Add this annotation to an {@code @Configuration} class to configure
* processing WebSocket requests:
*
* <pre class="code">
* &#064;Configuration
* &#064;EnableWebSocket
* public class MyWebSocketConfig {
*
* }
* </pre>
* <p>Customize the imported configuration by implementing the
* {@link WebSocketConfigurer} interface:
*
* <pre class="code">
* &#064;Configuration
* &#064;EnableWebSocket
* public class MyConfiguration implements WebSocketConfigurer {
*
* &#064;Override
* public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
* registry.addHandler(echoWebSocketHandler(), "/echo").withSockJS();
* }
*
* &#064;Bean
* public WebSocketHandler echoWebSocketHandler() {
* return new EchoWebSocketHandler();
* }
* }
* </pre>
*
* @author Rossen Stoyanchev
* @since 4.0
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@Import(DelegatingWebSocketConfiguration.class)
public @interface EnableWebSocket {
}

View File

@@ -0,0 +1,260 @@
/*
* Copyright 2002-2013 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.web.socket.server.config;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.util.ObjectUtils;
import org.springframework.web.socket.server.HandshakeInterceptor;
import org.springframework.web.socket.sockjs.SockJsService;
import org.springframework.web.socket.sockjs.transport.handler.DefaultSockJsService;
/**
* A helper class for configuring SockJS fallback options, typically used indirectly, in
* conjunction with {@link EnableWebSocket @EnableWebSocket} and
* {@link WebSocketConfigurer}.
*
* @author Rossen Stoyanchev
* @since 4.0
*/
public class SockJsServiceRegistration {
private TaskScheduler taskScheduler;
private String clientLibraryUrl;
private Integer streamBytesLimit;
private Boolean sessionCookieEnabled;
private Long heartbeatTime;
private Long disconnectDelay;
private Integer httpMessageCacheSize;
private Boolean webSocketEnabled;
private final List<HandshakeInterceptor> handshakeInterceptors = new ArrayList<HandshakeInterceptor>();
public SockJsServiceRegistration(TaskScheduler defaultTaskScheduler) {
this.taskScheduler = defaultTaskScheduler;
}
public SockJsServiceRegistration setTaskScheduler(TaskScheduler taskScheduler) {
this.taskScheduler = taskScheduler;
return this;
}
protected TaskScheduler getTaskScheduler() {
return this.taskScheduler;
}
/**
* Transports which don't support cross-domain communication natively (e.g.
* "eventsource", "htmlfile") rely on serving a simple page (using the
* "foreign" domain) from an invisible iframe. Code run from this iframe
* doesn't need to worry about cross-domain issues since it is running from
* a domain local to the SockJS server. The iframe does need to load the
* SockJS javascript client library and this option allows configuring its
* url.
*
* <p>By default this is set to point to
* "https://d1fxtkz8shb9d2.cloudfront.net/sockjs-0.3.4.min.js".
*/
public SockJsServiceRegistration setClientLibraryUrl(String clientLibraryUrl) {
this.clientLibraryUrl = clientLibraryUrl;
return this;
}
/**
* The URL to the SockJS JavaScript client library.
* @see #setSockJsClientLibraryUrl(String)
*/
protected String getClientLibraryUrl() {
return this.clientLibraryUrl;
}
/**
* Streaming transports save responses on the client side and don't free
* memory used by delivered messages. Such transports need to recycle the
* connection once in a while. This property sets a minimum number of bytes
* that can be send over a single HTTP streaming request before it will be
* closed. After that client will open a new request. Setting this value to
* one effectively disables streaming and will make streaming transports to
* behave like polling transports.
*
* <p>The default value is 128K (i.e. 128 * 1024).
*/
public SockJsServiceRegistration setStreamBytesLimit(int streamBytesLimit) {
this.streamBytesLimit = streamBytesLimit;
return this;
}
protected Integer getStreamBytesLimit() {
return this.streamBytesLimit;
}
/**
* Some load balancers do sticky sessions, but only if there is a "JSESSIONID"
* cookie. Even if it is set to a dummy value, it doesn't matter since
* session information is added by the load balancer.
*
* <p>The default value is "false" since Java servers set the session cookie.
*/
public SockJsServiceRegistration setDummySessionCookieEnabled(boolean sessionCookieEnabled) {
this.sessionCookieEnabled = sessionCookieEnabled;
return this;
}
/**
* Whether setting JSESSIONID cookie is necessary.
* @see #setDummySessionCookieEnabled(boolean)
*/
protected Boolean getDummySessionCookieEnabled() {
return this.sessionCookieEnabled;
}
/**
* The amount of time in milliseconds when the server has not sent any
* messages and after which the server should send a heartbeat frame to the
* client in order to keep the connection from breaking.
*
* <p>The default value is 25,000 (25 seconds).
*/
public SockJsServiceRegistration setHeartbeatTime(long heartbeatTime) {
this.heartbeatTime = heartbeatTime;
return this;
}
protected Long getHeartbeatTime() {
return this.heartbeatTime;
}
/**
* The amount of time in milliseconds before a client is considered
* disconnected after not having a receiving connection, i.e. an active
* connection over which the server can send data to the client.
*
* <p>The default value is 5000.
*/
public SockJsServiceRegistration setDisconnectDelay(long disconnectDelay) {
this.disconnectDelay = disconnectDelay;
return this;
}
/**
* Return the amount of time in milliseconds before a client is considered disconnected.
*/
protected Long getDisconnectDelay() {
return this.disconnectDelay;
}
/**
* The number of server-to-client messages that a session can cache while waiting for
* the next HTTP polling request from the client. All HTTP transports use this
* property since even streaming transports recycle HTTP requests periodically.
* <p>
* The amount of time between HTTP requests should be relatively brief and will not
* exceed the allows disconnect delay (see
* {@link #setDisconnectDelay(long)}), 5 seconds by default.
* <p>
* The default size is 100.
*/
public SockJsServiceRegistration setHttpMessageCacheSize(int httpMessageCacheSize) {
this.httpMessageCacheSize = httpMessageCacheSize;
return this;
}
/**
* Return the size of the HTTP message cache.
*/
protected Integer getHttpMessageCacheSize() {
return this.httpMessageCacheSize;
}
/**
* Some load balancers don't support WebSocket. This option can be used to
* disable the WebSocket transport on the server side.
*
* <p>The default value is "true".
*/
public SockJsServiceRegistration setWebSocketEnabled(boolean webSocketEnabled) {
this.webSocketEnabled = webSocketEnabled;
return this;
}
/**
* Whether WebSocket transport is enabled.
* @see #setWebSocketsEnabled(boolean)
*/
protected Boolean getWebSocketEnabled() {
return this.webSocketEnabled;
}
public SockJsServiceRegistration setInterceptors(HandshakeInterceptor... interceptors) {
if (!ObjectUtils.isEmpty(interceptors)) {
this.handshakeInterceptors.addAll(Arrays.asList(interceptors));
}
return this;
}
protected List<HandshakeInterceptor> getInterceptors() {
return this.handshakeInterceptors;
}
protected SockJsService getSockJsService(String[] sockJsPrefixes) {
DefaultSockJsService service = createSockJsService();
if (sockJsPrefixes != null) {
service.setValidSockJsPrefixes(sockJsPrefixes);
}
if (getClientLibraryUrl() != null) {
service.setSockJsClientLibraryUrl(getClientLibraryUrl());
}
if (getStreamBytesLimit() != null) {
service.setStreamBytesLimit(getStreamBytesLimit());
}
if (getDummySessionCookieEnabled() != null) {
service.setDummySessionCookieEnabled(getDummySessionCookieEnabled());
}
if (getHeartbeatTime() != null) {
service.setHeartbeatTime(getHeartbeatTime());
}
if (getDisconnectDelay() != null) {
service.setDisconnectDelay(getDisconnectDelay());
}
if (getHttpMessageCacheSize() != null) {
service.setHttpMessageCacheSize(getHttpMessageCacheSize());
}
if (getWebSocketEnabled() != null) {
service.setWebSocketsEnabled(getWebSocketEnabled());
}
service.setHandshakeInterceptors(getInterceptors());
return service;
}
protected DefaultSockJsService createSockJsService() {
return new DefaultSockJsService(getTaskScheduler());
}
}

View File

@@ -0,0 +1,52 @@
/*
* Copyright 2002-2013 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.web.socket.server.config;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.web.servlet.HandlerMapping;
/**
* Configuration support for WebSocket request handling.
*
* @author Rossen Stoyanchev
* @since 4.0
*/
public class WebSocketConfigurationSupport {
@Bean
public HandlerMapping webSocketHandlerMapping() {
WebSocketHandlerRegistry registry = new WebSocketHandlerRegistry();
registry.setDefaultTaskScheduler(sockJsTaskScheduler());
registerWebSocketHandlers(registry);
return registry.getHandlerMapping();
}
protected void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
}
@Bean
public ThreadPoolTaskScheduler sockJsTaskScheduler() {
ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
scheduler.setThreadNamePrefix("SockJS-");
scheduler.setPoolSize(10);
return scheduler;
}
}

View File

@@ -0,0 +1,38 @@
/*
* Copyright 2002-2013 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.web.socket.server.config;
import org.eclipse.jetty.websocket.server.WebSocketHandler;
/**
* Defines callback methods to configure the WebSocket request handling
* via {@link EnableWebSocket @EnableWebSocket}.
*
* @author Rossen Stoyanchev
* @since 4.0
*/
public interface WebSocketConfigurer {
/**
* Register {@link WebSocketHandler}s including SockJS fallback options if desired.
*/
void registerWebSocketHandlers(WebSocketHandlerRegistry registry);
}

View File

@@ -0,0 +1,131 @@
/*
* Copyright 2002-2013 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.web.socket.server.config;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.util.Assert;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.HttpRequestHandler;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.server.DefaultHandshakeHandler;
import org.springframework.web.socket.server.HandshakeHandler;
import org.springframework.web.socket.server.HandshakeInterceptor;
import org.springframework.web.socket.server.support.WebSocketHttpRequestHandler;
import org.springframework.web.socket.sockjs.SockJsHttpRequestHandler;
import org.springframework.web.socket.sockjs.SockJsService;
/**
* A helper class for configuring {@link WebSocketHandler} request handling
* including SockJS fallback options.
*
* @author Rossen Stoyanchev
* @since 4.0
*/
public class WebSocketHandlerRegistration {
private MultiValueMap<WebSocketHandler, String> handlerMap =
new LinkedMultiValueMap<WebSocketHandler, String>();
private final List<HandshakeInterceptor> interceptors = new ArrayList<HandshakeInterceptor>();
private SockJsServiceRegistration sockJsServiceRegistration;
private TaskScheduler defaultTaskScheduler;
public WebSocketHandlerRegistration addHandler(WebSocketHandler handler, String... paths) {
Assert.notNull(handler);
Assert.notEmpty(paths);
this.handlerMap.put(handler, Arrays.asList(paths));
return this;
}
protected MultiValueMap<WebSocketHandler, String> getHandlerMap() {
return this.handlerMap;
}
public void addInterceptors(HandshakeInterceptor... interceptors) {
this.interceptors.addAll(Arrays.asList(interceptors));
}
protected List<HandshakeInterceptor> getInterceptors() {
return this.interceptors;
}
public SockJsServiceRegistration withSockJS() {
this.sockJsServiceRegistration = new SockJsServiceRegistration(this.defaultTaskScheduler);
this.sockJsServiceRegistration.setInterceptors(
getInterceptors().toArray(new HandshakeInterceptor[getInterceptors().size()]));
return this.sockJsServiceRegistration;
}
protected SockJsServiceRegistration getSockJsServiceRegistration() {
return this.sockJsServiceRegistration;
}
protected void setDefaultTaskScheduler(TaskScheduler defaultTaskScheduler) {
this.defaultTaskScheduler = defaultTaskScheduler;
}
protected TaskScheduler getDefaultTaskScheduler() {
return this.defaultTaskScheduler;
}
protected MultiValueMap<HttpRequestHandler, String> getMappings() {
MultiValueMap<HttpRequestHandler, String> mappings = new LinkedMultiValueMap<HttpRequestHandler, String>();
if (getSockJsServiceRegistration() == null) {
HandshakeHandler handshakeHandler = createHandshakeHandler();
for (WebSocketHandler handler : getHandlerMap().keySet()) {
for (String path : getHandlerMap().get(handler)) {
WebSocketHttpRequestHandler httpHandler = new WebSocketHttpRequestHandler(handler, handshakeHandler);
httpHandler.setHandshakeInterceptors(getInterceptors());
mappings.add(httpHandler, path);
}
}
}
else {
SockJsService sockJsService = getSockJsServiceRegistration().getSockJsService(getAllPrefixes());
for (WebSocketHandler handler : getHandlerMap().keySet()) {
for (String path : getHandlerMap().get(handler)) {
SockJsHttpRequestHandler httpHandler = new SockJsHttpRequestHandler(sockJsService, handler);
mappings.add(httpHandler, path.endsWith("/") ? path + "**" : path + "/**");
}
}
}
return mappings;
}
protected DefaultHandshakeHandler createHandshakeHandler() {
return new DefaultHandshakeHandler();
}
protected final String[] getAllPrefixes() {
List<String> all = new ArrayList<String>();
for (List<String> prefixes: this.handlerMap.values()) {
all.addAll(prefixes);
}
return all.toArray(new String[all.size()]);
}
}

View File

@@ -0,0 +1,100 @@
/*
* Copyright 2002-2013 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.web.socket.server.config;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.util.MultiValueMap;
import org.springframework.web.HttpRequestHandler;
import org.springframework.web.servlet.HandlerMapping;
import org.springframework.web.servlet.handler.AbstractHandlerMapping;
import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping;
import org.springframework.web.socket.WebSocketHandler;
/**
* A helper class for configuring {@link WebSocketHandler} request handling.
*
* @author Rossen Stoyanchev
* @since 4.0
*/
public class WebSocketHandlerRegistry {
private final List<WebSocketHandlerRegistration> registrations = new ArrayList<WebSocketHandlerRegistration>();
private int order = 1;
private TaskScheduler defaultTaskScheduler;
public WebSocketHandlerRegistration addHandler(WebSocketHandler wsHandler, String... paths) {
WebSocketHandlerRegistration r = new WebSocketHandlerRegistration();
r.addHandler(wsHandler, paths);
r.setDefaultTaskScheduler(this.defaultTaskScheduler);
this.registrations.add(r);
return r;
}
protected List<WebSocketHandlerRegistration> getRegistrations() {
return this.registrations;
}
/**
* Specify the order to use for WebSocket {@link HandlerMapping} relative to other
* handler mappings configured in the Spring MVC configuration. The default value is
* 1.
*/
public void setOrder(int order) {
this.order = order;
}
protected int getOrder() {
return this.order;
}
protected void setDefaultTaskScheduler(TaskScheduler defaultTaskScheduler) {
this.defaultTaskScheduler = defaultTaskScheduler;
}
protected TaskScheduler getDefaultTaskScheduler() {
return this.defaultTaskScheduler;
}
/**
* Returns a handler mapping with the mapped ViewControllers; or {@code null} in case of no registrations.
*/
protected AbstractHandlerMapping getHandlerMapping() {
Map<String, Object> urlMap = new LinkedHashMap<String, Object>();
for (WebSocketHandlerRegistration registration : this.registrations) {
MultiValueMap<HttpRequestHandler, String> mappings = registration.getMappings();
for (HttpRequestHandler httpHandler : mappings.keySet()) {
for (String pattern : mappings.get(httpHandler)) {
urlMap.put(pattern, httpHandler);
}
}
}
SimpleUrlHandlerMapping hm = new SimpleUrlHandlerMapping();
hm.setOrder(this.order);
hm.setUrlMap(urlMap);
return hm;
}
}

View File

@@ -0,0 +1,21 @@
/*
* Copyright 2002-2013 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Configuration support for WebSocket request handling.
*/
package org.springframework.web.socket.server.config;

View File

@@ -77,6 +77,13 @@ public class WebSocketHttpRequestHandler implements HttpRequestHandler {
}
/**
* Return the WebSocketHandler.
*/
public WebSocketHandler getWebSocketHandler() {
return this.wsHandler;
}
/**
* Configure one or more WebSocket handshake request interceptors.
*/

View File

@@ -59,6 +59,20 @@ public class SockJsHttpRequestHandler implements HttpRequestHandler {
}
/**
* Return the {@link SockJsService}.
*/
public SockJsService getSockJsService() {
return this.sockJsService;
}
/**
* Return the {@link WebSocketHandler}.
*/
public WebSocketHandler getWebSocketHandler() {
return this.wsHandler;
}
@Override
public void handleRequest(HttpServletRequest servletRequest, HttpServletResponse servletResponse)
throws ServletException, IOException {

View File

@@ -0,0 +1,98 @@
/*
* Copyright 2002-2013 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.web.socket.server.config;
import java.util.Arrays;
import org.junit.Before;
import org.junit.Test;
import org.springframework.web.context.support.GenericWebApplicationContext;
import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.adapter.TextWebSocketHandlerAdapter;
import org.springframework.web.socket.server.support.WebSocketHttpRequestHandler;
import org.springframework.web.socket.sockjs.SockJsHttpRequestHandler;
import static org.junit.Assert.*;
/**
* Test fixture for {@link WebSocketConfigurationSupport}.
*
* @author Rossen Stoyanchev
*/
public class WebSocketConfigurationTests {
private DelegatingWebSocketConfiguration config;
private GenericWebApplicationContext context;
@Before
public void setup() {
this.config = new DelegatingWebSocketConfiguration();
this.context = new GenericWebApplicationContext();
this.context.refresh();
}
@Test
public void webSocket() throws Exception {
final WebSocketHandler handler = new TextWebSocketHandlerAdapter();
WebSocketConfigurer configurer = new WebSocketConfigurer() {
@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
registry.addHandler(handler, "/h1");
}
};
this.config.setConfigurers(Arrays.asList(configurer));
SimpleUrlHandlerMapping hm = (SimpleUrlHandlerMapping) this.config.webSocketHandlerMapping();
hm.setApplicationContext(this.context);
Object actual = hm.getUrlMap().get("/h1");
assertNotNull(actual);
assertEquals(WebSocketHttpRequestHandler.class, actual.getClass());
assertEquals(1, hm.getUrlMap().size());
}
@Test
public void webSocketWithSockJS() throws Exception {
final WebSocketHandler handler = new TextWebSocketHandlerAdapter();
WebSocketConfigurer configurer = new WebSocketConfigurer() {
@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
registry.addHandler(handler, "/h1").withSockJS();
}
};
this.config.setConfigurers(Arrays.asList(configurer));
SimpleUrlHandlerMapping hm = (SimpleUrlHandlerMapping) this.config.webSocketHandlerMapping();
hm.setApplicationContext(this.context);
Object actual = hm.getUrlMap().get("/h1/**");
assertNotNull(actual);
assertEquals(SockJsHttpRequestHandler.class, actual.getClass());
assertEquals(1, hm.getUrlMap().size());
}
}