From bb150c47cf50ac946197354b199f5081e8a7fb85 Mon Sep 17 00:00:00 2001 From: Brian Clozel Date: Wed, 19 Nov 2014 11:30:07 +0100 Subject: [PATCH] Add undertow 1.1.0.Final support Upgrade undertow dependency to 1.1.0.Final. Add support for undertow 1.1.0.Final in the UndertowRequestUpgradeStrategy, after a breaking change in the `io.undertow.websockets.jsr.ConfiguredServerEndpoint` constructor. Issue: SPR-12302 --- build.gradle | 2 +- .../UndertowRequestUpgradeStrategy.java | 54 ++++++++++++++----- 2 files changed, 43 insertions(+), 13 deletions(-) diff --git a/build.gradle b/build.gradle index a38bd77967..392f1e08e1 100644 --- a/build.gradle +++ b/build.gradle @@ -54,7 +54,7 @@ configure(allprojects) { project -> ext.tiles3Version = "3.0.5" ext.tomcatVersion = "8.0.15" ext.tyrusVersion = "1.3.5" - ext.undertowVersion = "1.0.17.Final" + ext.undertowVersion = "1.1.0.Final" ext.woodstoxVersion = "4.4.1" ext.xstreamVersion = "1.4.7" diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/server/standard/UndertowRequestUpgradeStrategy.java b/spring-websocket/src/main/java/org/springframework/web/socket/server/standard/UndertowRequestUpgradeStrategy.java index 5a38b9e988..cb7000e50d 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/server/standard/UndertowRequestUpgradeStrategy.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/server/standard/UndertowRequestUpgradeStrategy.java @@ -28,12 +28,14 @@ import javax.websocket.Decoder; import javax.websocket.Encoder; import javax.websocket.Endpoint; import javax.websocket.Extension; +import javax.websocket.server.ServerEndpointConfig; import io.undertow.server.HttpServerExchange; import io.undertow.server.HttpUpgradeListener; import io.undertow.servlet.api.InstanceFactory; import io.undertow.servlet.api.InstanceHandle; import io.undertow.servlet.websockets.ServletWebSocketHttpExchange; +import io.undertow.util.PathTemplate; import io.undertow.websockets.core.WebSocketChannel; import io.undertow.websockets.core.WebSocketVersion; import io.undertow.websockets.core.protocol.Handshake; @@ -41,6 +43,7 @@ import io.undertow.websockets.jsr.ConfiguredServerEndpoint; import io.undertow.websockets.jsr.EncodingFactory; import io.undertow.websockets.jsr.EndpointSessionHandler; import io.undertow.websockets.jsr.ServerWebSocketContainer; +import io.undertow.websockets.jsr.annotated.AnnotatedEndpointFactory; import io.undertow.websockets.jsr.handshake.HandshakeUtil; import io.undertow.websockets.jsr.handshake.JsrHybi07Handshake; import io.undertow.websockets.jsr.handshake.JsrHybi08Handshake; @@ -64,20 +67,38 @@ public class UndertowRequestUpgradeStrategy extends AbstractStandardUpgradeStrat private static final Constructor exchangeConstructor; + private static final Constructor endpointConstructor; + private static final boolean undertow10Present; + private static final boolean undertow11Present; + static { - Class type = ServletWebSocketHttpExchange.class; - Class[] paramTypes = new Class[] {HttpServletRequest.class, HttpServletResponse.class, Set.class}; - if (ClassUtils.hasConstructor(type, paramTypes)) { - exchangeConstructor = ClassUtils.getConstructorIfAvailable(type, paramTypes); + Class exchangeType = ServletWebSocketHttpExchange.class; + Class[] exchangeParamTypes = new Class[] {HttpServletRequest.class, HttpServletResponse.class, Set.class}; + if (ClassUtils.hasConstructor(exchangeType, exchangeParamTypes)) { + exchangeConstructor = ClassUtils.getConstructorIfAvailable(exchangeType, exchangeParamTypes); undertow10Present = false; } else { - paramTypes = new Class[] {HttpServletRequest.class, HttpServletResponse.class}; - exchangeConstructor = ClassUtils.getConstructorIfAvailable(type, paramTypes); + exchangeParamTypes = new Class[] {HttpServletRequest.class, HttpServletResponse.class}; + exchangeConstructor = ClassUtils.getConstructorIfAvailable(exchangeType, exchangeParamTypes); undertow10Present = true; } + + Class endpointType = ConfiguredServerEndpoint.class; + Class[] endpointParamTypes = new Class[] {ServerEndpointConfig.class, InstanceFactory.class, + PathTemplate.class, EncodingFactory.class, AnnotatedEndpointFactory.class}; + if (ClassUtils.hasConstructor(endpointType, endpointParamTypes)) { + endpointConstructor = ClassUtils.getConstructorIfAvailable(endpointType, endpointParamTypes); + undertow11Present = true; + } + else { + endpointParamTypes = new Class[] {ServerEndpointConfig.class, InstanceFactory.class, + PathTemplate.class, EncodingFactory.class}; + endpointConstructor = ClassUtils.getConstructorIfAvailable(endpointType, endpointParamTypes); + undertow11Present = false; + } } private static final String[] supportedVersions = new String[] { @@ -174,12 +195,21 @@ public class UndertowRequestUpgradeStrategy extends AbstractStandardUpgradeStrat endpointRegistration.setSubprotocols(Arrays.asList(selectedProtocol)); endpointRegistration.setExtensions(selectedExtensions); - return new ConfiguredServerEndpoint(endpointRegistration, new EndpointInstanceFactory(endpoint), null, - new EncodingFactory( - Collections., List>>emptyMap(), - Collections., List>>emptyMap(), - Collections., List>>emptyMap(), - Collections., List>>emptyMap())); + EncodingFactory encodingFactory = new EncodingFactory( + Collections., List>>emptyMap(), + Collections., List>>emptyMap(), + Collections., List>>emptyMap(), + Collections., List>>emptyMap()); + try { + return undertow11Present ? + endpointConstructor.newInstance(endpointRegistration, + new EndpointInstanceFactory(endpoint), null, encodingFactory, null) : + endpointConstructor.newInstance(endpointRegistration, + new EndpointInstanceFactory(endpoint), null, encodingFactory); + } + catch (Exception ex) { + throw new HandshakeFailureException("Failed to instantiate ConfiguredServerEndpoint", ex); + } }