From 190408d1dc36e6566bcf90a0eac1fb532522666c Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Tue, 11 Apr 2017 15:08:39 -0400 Subject: [PATCH] Polish --- .../ServletWebSocketHandlerRegistration.java | 7 +++-- .../ServletWebSocketHandlerRegistry.java | 7 ++--- .../annotation/SockJsServiceRegistration.java | 30 +++++++++--------- ...MvcStompWebSocketEndpointRegistration.java | 31 +++++++------------ .../WebSocketConfigurationSupport.java | 2 +- 5 files changed, 35 insertions(+), 42 deletions(-) diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/config/annotation/ServletWebSocketHandlerRegistration.java b/spring-websocket/src/main/java/org/springframework/web/socket/config/annotation/ServletWebSocketHandlerRegistration.java index 1eac014c8e..609e07ab1d 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/config/annotation/ServletWebSocketHandlerRegistration.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/config/annotation/ServletWebSocketHandlerRegistration.java @@ -40,6 +40,7 @@ import org.springframework.web.socket.sockjs.support.SockJsHttpRequestHandler; public class ServletWebSocketHandlerRegistration extends AbstractWebSocketHandlerRegistration> { + public ServletWebSocketHandlerRegistration(TaskScheduler sockJsTaskScheduler) { super(sockJsTaskScheduler); } @@ -60,10 +61,12 @@ public class ServletWebSocketHandlerRegistration @Override protected void addWebSocketHandlerMapping(MultiValueMap mappings, - WebSocketHandler wsHandler, HandshakeHandler handshakeHandler, + WebSocketHandler webSocketHandler, HandshakeHandler handshakeHandler, HandshakeInterceptor[] interceptors, String path) { - WebSocketHttpRequestHandler httpHandler = new WebSocketHttpRequestHandler(wsHandler, handshakeHandler); + WebSocketHttpRequestHandler httpHandler = + new WebSocketHttpRequestHandler(webSocketHandler, handshakeHandler); + if (!ObjectUtils.isEmpty(interceptors)) { httpHandler.setHandshakeInterceptors(Arrays.asList(interceptors)); } diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/config/annotation/ServletWebSocketHandlerRegistry.java b/spring-websocket/src/main/java/org/springframework/web/socket/config/annotation/ServletWebSocketHandlerRegistry.java index 6f81252d70..8890fb8072 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/config/annotation/ServletWebSocketHandlerRegistry.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/config/annotation/ServletWebSocketHandlerRegistry.java @@ -41,8 +41,7 @@ import org.springframework.web.util.UrlPathHelper; */ public class ServletWebSocketHandlerRegistry implements WebSocketHandlerRegistry { - private final List registrations = - new ArrayList<>(); + private final List registrations = new ArrayList<>(4); private TaskScheduler sockJsTaskScheduler; @@ -56,10 +55,10 @@ public class ServletWebSocketHandlerRegistry implements WebSocketHandlerRegistry } @Override - public WebSocketHandlerRegistration addHandler(WebSocketHandler webSocketHandler, String... paths) { + public WebSocketHandlerRegistration addHandler(WebSocketHandler handler, String... paths) { ServletWebSocketHandlerRegistration registration = new ServletWebSocketHandlerRegistration(this.sockJsTaskScheduler); - registration.addHandler(webSocketHandler, paths); + registration.addHandler(handler, paths); this.registrations.add(registration); return registration; } diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/config/annotation/SockJsServiceRegistration.java b/spring-websocket/src/main/java/org/springframework/web/socket/config/annotation/SockJsServiceRegistration.java index c697a4249b..2e832fd782 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/config/annotation/SockJsServiceRegistration.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/config/annotation/SockJsServiceRegistration.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2017 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. @@ -30,17 +30,18 @@ import org.springframework.web.socket.sockjs.transport.TransportHandler; import org.springframework.web.socket.sockjs.transport.TransportHandlingSockJsService; import org.springframework.web.socket.sockjs.transport.handler.DefaultSockJsService; + /** - * A helper class for configuring SockJS fallback options, typically used indirectly, in - * conjunction with {@link org.springframework.web.socket.config.annotation.EnableWebSocket @EnableWebSocket} and - * {@link WebSocketConfigurer}. + * A helper class for configuring SockJS fallback options for use with an + * {@link org.springframework.web.socket.config.annotation.EnableWebSocket} and + * {@link WebSocketConfigurer} setup. * * @author Rossen Stoyanchev * @since 4.0 */ public class SockJsServiceRegistration { - private TaskScheduler taskScheduler; + private TaskScheduler scheduler; private String clientLibraryUrl; @@ -70,12 +71,12 @@ public class SockJsServiceRegistration { public SockJsServiceRegistration(TaskScheduler defaultTaskScheduler) { - this.taskScheduler = defaultTaskScheduler; + this.scheduler = defaultTaskScheduler; } public SockJsServiceRegistration setTaskScheduler(TaskScheduler taskScheduler) { - this.taskScheduler = taskScheduler; + this.scheduler = taskScheduler; return this; } @@ -277,14 +278,13 @@ public class SockJsServiceRegistration { } private TransportHandlingSockJsService createSockJsService() { - if (!this.transportHandlers.isEmpty()) { - Assert.state(this.transportHandlerOverrides.isEmpty(), - "Specify either TransportHandlers or TransportHandler overrides, not both"); - return new TransportHandlingSockJsService(this.taskScheduler, this.transportHandlers); - } - else { - return new DefaultSockJsService(this.taskScheduler, this.transportHandlerOverrides); - } + + Assert.state(this.transportHandlers.isEmpty() || this.transportHandlerOverrides.isEmpty(), + "Specify either TransportHandlers or TransportHandler overrides, not both"); + + return !this.transportHandlers.isEmpty() ? + new TransportHandlingSockJsService(this.scheduler, this.transportHandlers) : + new DefaultSockJsService(this.scheduler, this.transportHandlerOverrides); } } diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/config/annotation/WebMvcStompWebSocketEndpointRegistration.java b/spring-websocket/src/main/java/org/springframework/web/socket/config/annotation/WebMvcStompWebSocketEndpointRegistration.java index b156442888..b1fc5535ab 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/config/annotation/WebMvcStompWebSocketEndpointRegistration.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/config/annotation/WebMvcStompWebSocketEndpointRegistration.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2017 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. @@ -16,7 +16,9 @@ package org.springframework.web.socket.config.annotation; +import java.util.ArrayList; import java.util.Arrays; +import java.util.List; import org.springframework.scheduling.TaskScheduler; import org.springframework.util.Assert; @@ -33,8 +35,7 @@ import org.springframework.web.socket.sockjs.SockJsService; import org.springframework.web.socket.sockjs.support.SockJsHttpRequestHandler; import org.springframework.web.socket.sockjs.transport.handler.WebSocketTransportHandler; -import java.util.ArrayList; -import java.util.List; + /** * An abstract base class for configuring STOMP over WebSocket/SockJS endpoints. * @@ -55,7 +56,7 @@ public class WebMvcStompWebSocketEndpointRegistration implements StompWebSocketE private final List allowedOrigins = new ArrayList<>(); - private StompSockJsServiceRegistration registration; + private SockJsServiceRegistration registration; public WebMvcStompWebSocketEndpointRegistration(String[] paths, WebSocketHandler webSocketHandler, @@ -69,6 +70,7 @@ public class WebMvcStompWebSocketEndpointRegistration implements StompWebSocketE this.sockJsTaskScheduler = sockJsTaskScheduler; } + @Override public StompWebSocketEndpointRegistration setHandshakeHandler(HandshakeHandler handshakeHandler) { Assert.notNull(handshakeHandler, "'handshakeHandler' must not be null"); @@ -95,17 +97,18 @@ public class WebMvcStompWebSocketEndpointRegistration implements StompWebSocketE @Override public SockJsServiceRegistration withSockJS() { - this.registration = new StompSockJsServiceRegistration(this.sockJsTaskScheduler); + this.registration = new SockJsServiceRegistration(this.sockJsTaskScheduler); HandshakeInterceptor[] interceptors = getInterceptors(); if (interceptors.length > 0) { this.registration.setInterceptors(interceptors); } if (this.handshakeHandler != null) { - WebSocketTransportHandler transportHandler = new WebSocketTransportHandler(this.handshakeHandler); - this.registration.setTransportHandlerOverrides(transportHandler); + WebSocketTransportHandler handler = new WebSocketTransportHandler(this.handshakeHandler); + this.registration.setTransportHandlerOverrides(handler); } if (!this.allowedOrigins.isEmpty()) { - this.registration.setAllowedOrigins(this.allowedOrigins.toArray(new String[this.allowedOrigins.size()])); + this.registration.setAllowedOrigins( + this.allowedOrigins.toArray(new String[this.allowedOrigins.size()])); } return this.registration; } @@ -146,16 +149,4 @@ public class WebMvcStompWebSocketEndpointRegistration implements StompWebSocketE return mappings; } - - private static class StompSockJsServiceRegistration extends SockJsServiceRegistration { - - public StompSockJsServiceRegistration(TaskScheduler defaultTaskScheduler) { - super(defaultTaskScheduler); - } - - protected SockJsService getSockJsService() { - return super.getSockJsService(); - } - } - } diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/config/annotation/WebSocketConfigurationSupport.java b/spring-websocket/src/main/java/org/springframework/web/socket/config/annotation/WebSocketConfigurationSupport.java index 35cd156522..1b8de76bf3 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/config/annotation/WebSocketConfigurationSupport.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/config/annotation/WebSocketConfigurationSupport.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2017 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.