Skip to content

Commit bb150c4

Browse files
committed
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
1 parent 161d3e3 commit bb150c4

File tree

2 files changed

+43
-13
lines changed

2 files changed

+43
-13
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ configure(allprojects) { project ->
5454
ext.tiles3Version = "3.0.5"
5555
ext.tomcatVersion = "8.0.15"
5656
ext.tyrusVersion = "1.3.5"
57-
ext.undertowVersion = "1.0.17.Final"
57+
ext.undertowVersion = "1.1.0.Final"
5858
ext.woodstoxVersion = "4.4.1"
5959
ext.xstreamVersion = "1.4.7"
6060

spring-websocket/src/main/java/org/springframework/web/socket/server/standard/UndertowRequestUpgradeStrategy.java

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,22 @@
2828
import javax.websocket.Encoder;
2929
import javax.websocket.Endpoint;
3030
import javax.websocket.Extension;
31+
import javax.websocket.server.ServerEndpointConfig;
3132

3233
import io.undertow.server.HttpServerExchange;
3334
import io.undertow.server.HttpUpgradeListener;
3435
import io.undertow.servlet.api.InstanceFactory;
3536
import io.undertow.servlet.api.InstanceHandle;
3637
import io.undertow.servlet.websockets.ServletWebSocketHttpExchange;
38+
import io.undertow.util.PathTemplate;
3739
import io.undertow.websockets.core.WebSocketChannel;
3840
import io.undertow.websockets.core.WebSocketVersion;
3941
import io.undertow.websockets.core.protocol.Handshake;
4042
import io.undertow.websockets.jsr.ConfiguredServerEndpoint;
4143
import io.undertow.websockets.jsr.EncodingFactory;
4244
import io.undertow.websockets.jsr.EndpointSessionHandler;
4345
import io.undertow.websockets.jsr.ServerWebSocketContainer;
46+
import io.undertow.websockets.jsr.annotated.AnnotatedEndpointFactory;
4447
import io.undertow.websockets.jsr.handshake.HandshakeUtil;
4548
import io.undertow.websockets.jsr.handshake.JsrHybi07Handshake;
4649
import io.undertow.websockets.jsr.handshake.JsrHybi08Handshake;
@@ -64,20 +67,38 @@ public class UndertowRequestUpgradeStrategy extends AbstractStandardUpgradeStrat
6467

6568
private static final Constructor<ServletWebSocketHttpExchange> exchangeConstructor;
6669

70+
private static final Constructor<ConfiguredServerEndpoint> endpointConstructor;
71+
6772
private static final boolean undertow10Present;
6873

74+
private static final boolean undertow11Present;
75+
6976
static {
70-
Class<ServletWebSocketHttpExchange> type = ServletWebSocketHttpExchange.class;
71-
Class<?>[] paramTypes = new Class<?>[] {HttpServletRequest.class, HttpServletResponse.class, Set.class};
72-
if (ClassUtils.hasConstructor(type, paramTypes)) {
73-
exchangeConstructor = ClassUtils.getConstructorIfAvailable(type, paramTypes);
77+
Class<ServletWebSocketHttpExchange> exchangeType = ServletWebSocketHttpExchange.class;
78+
Class<?>[] exchangeParamTypes = new Class<?>[] {HttpServletRequest.class, HttpServletResponse.class, Set.class};
79+
if (ClassUtils.hasConstructor(exchangeType, exchangeParamTypes)) {
80+
exchangeConstructor = ClassUtils.getConstructorIfAvailable(exchangeType, exchangeParamTypes);
7481
undertow10Present = false;
7582
}
7683
else {
77-
paramTypes = new Class<?>[] {HttpServletRequest.class, HttpServletResponse.class};
78-
exchangeConstructor = ClassUtils.getConstructorIfAvailable(type, paramTypes);
84+
exchangeParamTypes = new Class<?>[] {HttpServletRequest.class, HttpServletResponse.class};
85+
exchangeConstructor = ClassUtils.getConstructorIfAvailable(exchangeType, exchangeParamTypes);
7986
undertow10Present = true;
8087
}
88+
89+
Class<ConfiguredServerEndpoint> endpointType = ConfiguredServerEndpoint.class;
90+
Class<?>[] endpointParamTypes = new Class<?>[] {ServerEndpointConfig.class, InstanceFactory.class,
91+
PathTemplate.class, EncodingFactory.class, AnnotatedEndpointFactory.class};
92+
if (ClassUtils.hasConstructor(endpointType, endpointParamTypes)) {
93+
endpointConstructor = ClassUtils.getConstructorIfAvailable(endpointType, endpointParamTypes);
94+
undertow11Present = true;
95+
}
96+
else {
97+
endpointParamTypes = new Class<?>[] {ServerEndpointConfig.class, InstanceFactory.class,
98+
PathTemplate.class, EncodingFactory.class};
99+
endpointConstructor = ClassUtils.getConstructorIfAvailable(endpointType, endpointParamTypes);
100+
undertow11Present = false;
101+
}
81102
}
82103

83104
private static final String[] supportedVersions = new String[] {
@@ -174,12 +195,21 @@ private ConfiguredServerEndpoint createConfiguredServerEndpoint(String selectedP
174195
endpointRegistration.setSubprotocols(Arrays.asList(selectedProtocol));
175196
endpointRegistration.setExtensions(selectedExtensions);
176197

177-
return new ConfiguredServerEndpoint(endpointRegistration, new EndpointInstanceFactory(endpoint), null,
178-
new EncodingFactory(
179-
Collections.<Class<?>, List<InstanceFactory<? extends Encoder>>>emptyMap(),
180-
Collections.<Class<?>, List<InstanceFactory<? extends Decoder>>>emptyMap(),
181-
Collections.<Class<?>, List<InstanceFactory<? extends Encoder>>>emptyMap(),
182-
Collections.<Class<?>, List<InstanceFactory<? extends Decoder>>>emptyMap()));
198+
EncodingFactory encodingFactory = new EncodingFactory(
199+
Collections.<Class<?>, List<InstanceFactory<? extends Encoder>>>emptyMap(),
200+
Collections.<Class<?>, List<InstanceFactory<? extends Decoder>>>emptyMap(),
201+
Collections.<Class<?>, List<InstanceFactory<? extends Encoder>>>emptyMap(),
202+
Collections.<Class<?>, List<InstanceFactory<? extends Decoder>>>emptyMap());
203+
try {
204+
return undertow11Present ?
205+
endpointConstructor.newInstance(endpointRegistration,
206+
new EndpointInstanceFactory(endpoint), null, encodingFactory, null) :
207+
endpointConstructor.newInstance(endpointRegistration,
208+
new EndpointInstanceFactory(endpoint), null, encodingFactory);
209+
}
210+
catch (Exception ex) {
211+
throw new HandshakeFailureException("Failed to instantiate ConfiguredServerEndpoint", ex);
212+
}
183213
}
184214

185215

0 commit comments

Comments
 (0)