Skip to content

Commit a2ac764

Browse files
committed
Reuse StandardWebSocketUpgradeStrategy as a base class for Tomcat etc
Includes non-reflective instantiation of well-known strategy classes. See gh-29436
1 parent 465575f commit a2ac764

14 files changed

+379
-382
lines changed

spring-webflux/src/main/java/org/springframework/web/reactive/socket/server/support/HandshakeWebSocketService.java

Lines changed: 62 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,17 @@
3838
import org.springframework.util.Assert;
3939
import org.springframework.util.ClassUtils;
4040
import org.springframework.util.MultiValueMap;
41-
import org.springframework.util.ReflectionUtils;
4241
import org.springframework.util.StringUtils;
4342
import org.springframework.web.reactive.socket.HandshakeInfo;
4443
import org.springframework.web.reactive.socket.WebSocketHandler;
4544
import org.springframework.web.reactive.socket.server.RequestUpgradeStrategy;
4645
import org.springframework.web.reactive.socket.server.WebSocketService;
46+
import org.springframework.web.reactive.socket.server.upgrade.JettyRequestUpgradeStrategy;
47+
import org.springframework.web.reactive.socket.server.upgrade.ReactorNetty2RequestUpgradeStrategy;
48+
import org.springframework.web.reactive.socket.server.upgrade.ReactorNettyRequestUpgradeStrategy;
49+
import org.springframework.web.reactive.socket.server.upgrade.StandardWebSocketUpgradeStrategy;
50+
import org.springframework.web.reactive.socket.server.upgrade.TomcatRequestUpgradeStrategy;
51+
import org.springframework.web.reactive.socket.server.upgrade.UndertowRequestUpgradeStrategy;
4752
import org.springframework.web.server.MethodNotAllowedException;
4853
import org.springframework.web.server.ServerWebExchange;
4954
import org.springframework.web.server.ServerWebInputException;
@@ -55,6 +60,7 @@
5560
* also be explicitly configured.
5661
*
5762
* @author Rossen Stoyanchev
63+
* @author Juergen Hoeller
5864
* @since 5.0
5965
*/
6066
public class HandshakeWebSocketService implements WebSocketService, Lifecycle {
@@ -66,28 +72,32 @@ public class HandshakeWebSocketService implements WebSocketService, Lifecycle {
6672
private static final Mono<Map<String, Object>> EMPTY_ATTRIBUTES = Mono.just(Collections.emptyMap());
6773

6874

69-
private static final boolean tomcatPresent;
75+
private static final boolean tomcatWsPresent;
7076

71-
private static final boolean jettyPresent;
77+
private static final boolean jettyWsPresent;
7278

73-
private static final boolean undertowPresent;
79+
private static final boolean undertowWsPresent;
7480

7581
private static final boolean reactorNettyPresent;
7682

7783
private static final boolean reactorNetty2Present;
7884

7985
static {
80-
ClassLoader loader = HandshakeWebSocketService.class.getClassLoader();
81-
tomcatPresent = ClassUtils.isPresent("org.apache.tomcat.websocket.server.WsHttpUpgradeHandler", loader);
82-
jettyPresent = ClassUtils.isPresent("org.eclipse.jetty.websocket.server.JettyWebSocketServerContainer", loader);
83-
undertowPresent = ClassUtils.isPresent("io.undertow.websockets.WebSocketProtocolHandshakeHandler", loader);
84-
reactorNettyPresent = ClassUtils.isPresent("reactor.netty.http.server.HttpServerResponse", loader);
85-
reactorNetty2Present = ClassUtils.isPresent("reactor.netty5.http.server.HttpServerResponse", loader);
86+
ClassLoader classLoader = HandshakeWebSocketService.class.getClassLoader();
87+
tomcatWsPresent = ClassUtils.isPresent(
88+
"org.apache.tomcat.websocket.server.WsHttpUpgradeHandler", classLoader);
89+
jettyWsPresent = ClassUtils.isPresent(
90+
"org.eclipse.jetty.websocket.server.JettyWebSocketServerContainer", classLoader);
91+
undertowWsPresent = ClassUtils.isPresent(
92+
"io.undertow.websockets.WebSocketProtocolHandshakeHandler", classLoader);
93+
reactorNettyPresent = ClassUtils.isPresent(
94+
"reactor.netty.http.server.HttpServerResponse", classLoader);
95+
reactorNetty2Present = ClassUtils.isPresent(
96+
"reactor.netty5.http.server.HttpServerResponse", classLoader);
8697
}
8798

8899

89-
protected static final Log logger = LogFactory.getLog(HandshakeWebSocketService.class);
90-
100+
private static final Log logger = LogFactory.getLog(HandshakeWebSocketService.class);
91101

92102
private final RequestUpgradeStrategy upgradeStrategy;
93103

@@ -114,40 +124,6 @@ public HandshakeWebSocketService(RequestUpgradeStrategy upgradeStrategy) {
114124
this.upgradeStrategy = upgradeStrategy;
115125
}
116126

117-
static RequestUpgradeStrategy initUpgradeStrategy() {
118-
String className;
119-
if (tomcatPresent) {
120-
className = "TomcatRequestUpgradeStrategy";
121-
}
122-
else if (jettyPresent) {
123-
className = "JettyRequestUpgradeStrategy";
124-
}
125-
else if (undertowPresent) {
126-
className = "UndertowRequestUpgradeStrategy";
127-
}
128-
else if (reactorNettyPresent) {
129-
// As late as possible (Reactor Netty commonly used for WebClient)
130-
className = "ReactorNettyRequestUpgradeStrategy";
131-
}
132-
else if (reactorNetty2Present) {
133-
// As late as possible (Reactor Netty commonly used for WebClient)
134-
className = "ReactorNetty2RequestUpgradeStrategy";
135-
}
136-
else {
137-
throw new IllegalStateException("No suitable default RequestUpgradeStrategy found");
138-
}
139-
140-
try {
141-
className = "org.springframework.web.reactive.socket.server.upgrade." + className;
142-
Class<?> clazz = ClassUtils.forName(className, HandshakeWebSocketService.class.getClassLoader());
143-
return (RequestUpgradeStrategy) ReflectionUtils.accessibleConstructor(clazz).newInstance();
144-
}
145-
catch (Throwable ex) {
146-
throw new IllegalStateException(
147-
"Failed to instantiate RequestUpgradeStrategy: " + className, ex);
148-
}
149-
}
150-
151127

152128
/**
153129
* Return the {@link RequestUpgradeStrategy} for WebSocket requests.
@@ -292,4 +268,44 @@ private HandshakeInfo createHandshakeInfo(ServerWebExchange exchange, ServerHttp
292268
return new HandshakeInfo(uri, headers, cookies, principal, protocol, remoteAddress, attributes, logPrefix);
293269
}
294270

271+
272+
static RequestUpgradeStrategy initUpgradeStrategy() {
273+
if (tomcatWsPresent) {
274+
return new TomcatRequestUpgradeStrategy();
275+
}
276+
else if (jettyWsPresent) {
277+
return new JettyRequestUpgradeStrategy();
278+
}
279+
else if (undertowWsPresent) {
280+
return new UndertowRequestUpgradeStrategy();
281+
}
282+
else if (reactorNettyPresent) {
283+
// As late as possible (Reactor Netty commonly used for WebClient)
284+
return ReactorNettyStrategyDelegate.forReactorNetty1();
285+
}
286+
else if (reactorNetty2Present) {
287+
// As late as possible (Reactor Netty commonly used for WebClient)
288+
return ReactorNettyStrategyDelegate.forReactorNetty2();
289+
}
290+
else {
291+
// Let's assume Jakarta WebSocket API 2.1+
292+
return new StandardWebSocketUpgradeStrategy();
293+
}
294+
}
295+
296+
297+
/**
298+
* Inner class to avoid a reachable dependency on Reactor Netty API.
299+
*/
300+
private static class ReactorNettyStrategyDelegate {
301+
302+
public static RequestUpgradeStrategy forReactorNetty1() {
303+
return new ReactorNettyRequestUpgradeStrategy();
304+
}
305+
306+
public static RequestUpgradeStrategy forReactorNetty2() {
307+
return new ReactorNetty2RequestUpgradeStrategy();
308+
}
309+
}
310+
295311
}

spring-webflux/src/main/java/org/springframework/web/reactive/socket/server/upgrade/JettyRequestUpgradeStrategy.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -40,7 +40,7 @@
4040
import org.springframework.web.server.ServerWebExchange;
4141

4242
/**
43-
* A {@link RequestUpgradeStrategy} for Jetty 11.
43+
* A WebSocket {@code RequestUpgradeStrategy} for Jetty 11.
4444
*
4545
* @author Rossen Stoyanchev
4646
* @since 5.3.4

spring-webflux/src/main/java/org/springframework/web/reactive/socket/server/upgrade/ReactorNetty2RequestUpgradeStrategy.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
import org.springframework.web.server.ServerWebExchange;
3636

3737
/**
38-
* A {@link RequestUpgradeStrategy} for use with Reactor Netty for Netty 5.
38+
* A WebSocket {@code RequestUpgradeStrategy} for Reactor Netty for Netty 5.
3939
*
4040
* <p>This class is based on {@link ReactorNettyRequestUpgradeStrategy}.
4141
*\

spring-webflux/src/main/java/org/springframework/web/reactive/socket/server/upgrade/ReactorNettyRequestUpgradeStrategy.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -35,7 +35,7 @@
3535
import org.springframework.web.server.ServerWebExchange;
3636

3737
/**
38-
* A {@link RequestUpgradeStrategy} for use with Reactor Netty.
38+
* A WebSocket {@code RequestUpgradeStrategy} for Reactor Netty.
3939
*
4040
* @author Rossen Stoyanchev
4141
* @since 5.0
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
/*
2+
* Copyright 2002-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.web.reactive.socket.server.upgrade;
18+
19+
import java.util.Collections;
20+
import java.util.Map;
21+
import java.util.function.Supplier;
22+
23+
import jakarta.servlet.http.HttpServletRequest;
24+
import jakarta.servlet.http.HttpServletResponse;
25+
import jakarta.websocket.Endpoint;
26+
import jakarta.websocket.server.ServerContainer;
27+
import jakarta.websocket.server.ServerEndpointConfig;
28+
import reactor.core.publisher.Mono;
29+
30+
import org.springframework.core.io.buffer.DataBufferFactory;
31+
import org.springframework.http.server.reactive.ServerHttpRequest;
32+
import org.springframework.http.server.reactive.ServerHttpRequestDecorator;
33+
import org.springframework.http.server.reactive.ServerHttpResponse;
34+
import org.springframework.http.server.reactive.ServerHttpResponseDecorator;
35+
import org.springframework.lang.Nullable;
36+
import org.springframework.util.Assert;
37+
import org.springframework.web.reactive.socket.HandshakeInfo;
38+
import org.springframework.web.reactive.socket.WebSocketHandler;
39+
import org.springframework.web.reactive.socket.adapter.ContextWebSocketHandler;
40+
import org.springframework.web.reactive.socket.adapter.StandardWebSocketHandlerAdapter;
41+
import org.springframework.web.reactive.socket.adapter.TomcatWebSocketSession;
42+
import org.springframework.web.reactive.socket.server.RequestUpgradeStrategy;
43+
import org.springframework.web.server.ServerWebExchange;
44+
45+
/**
46+
* A WebSocket {@code RequestUpgradeStrategy} for the Jakarta WebSocket API 2.1+.
47+
*
48+
* <p>This strategy serves as a fallback if no specific server has been detected.
49+
* It can also be used with Jakarta EE 10 level servers such as Tomcat 10.1 and
50+
* Undertow 2.3 directly, relying on their built-in Jakarta WebSocket 2.1 support.
51+
*
52+
* @author Juergen Hoeller
53+
* @author Violeta Georgieva
54+
* @author Rossen Stoyanchev
55+
* @since 6.0
56+
* @see jakarta.websocket.server.ServerContainer#upgradeHttpToWebSocket
57+
*/
58+
public class StandardWebSocketUpgradeStrategy implements RequestUpgradeStrategy {
59+
60+
private static final String SERVER_CONTAINER_ATTR = "jakarta.websocket.server.ServerContainer";
61+
62+
63+
@Nullable
64+
private Long asyncSendTimeout;
65+
66+
@Nullable
67+
private Long maxSessionIdleTimeout;
68+
69+
@Nullable
70+
private Integer maxTextMessageBufferSize;
71+
72+
@Nullable
73+
private Integer maxBinaryMessageBufferSize;
74+
75+
@Nullable
76+
private ServerContainer serverContainer;
77+
78+
79+
/**
80+
* Exposes the underlying config option on
81+
* {@link ServerContainer#setAsyncSendTimeout(long)}.
82+
*/
83+
public void setAsyncSendTimeout(Long timeoutInMillis) {
84+
this.asyncSendTimeout = timeoutInMillis;
85+
}
86+
87+
@Nullable
88+
public Long getAsyncSendTimeout() {
89+
return this.asyncSendTimeout;
90+
}
91+
92+
/**
93+
* Exposes the underlying config option on
94+
* {@link ServerContainer#setDefaultMaxSessionIdleTimeout(long)}.
95+
*/
96+
public void setMaxSessionIdleTimeout(Long timeoutInMillis) {
97+
this.maxSessionIdleTimeout = timeoutInMillis;
98+
}
99+
100+
@Nullable
101+
public Long getMaxSessionIdleTimeout() {
102+
return this.maxSessionIdleTimeout;
103+
}
104+
105+
/**
106+
* Exposes the underlying config option on
107+
* {@link ServerContainer#setDefaultMaxTextMessageBufferSize(int)}.
108+
*/
109+
public void setMaxTextMessageBufferSize(Integer bufferSize) {
110+
this.maxTextMessageBufferSize = bufferSize;
111+
}
112+
113+
@Nullable
114+
public Integer getMaxTextMessageBufferSize() {
115+
return this.maxTextMessageBufferSize;
116+
}
117+
118+
/**
119+
* Exposes the underlying config option on
120+
* {@link ServerContainer#setDefaultMaxBinaryMessageBufferSize(int)}.
121+
*/
122+
public void setMaxBinaryMessageBufferSize(Integer bufferSize) {
123+
this.maxBinaryMessageBufferSize = bufferSize;
124+
}
125+
126+
@Nullable
127+
public Integer getMaxBinaryMessageBufferSize() {
128+
return this.maxBinaryMessageBufferSize;
129+
}
130+
131+
@Override
132+
public Mono<Void> upgrade(ServerWebExchange exchange, WebSocketHandler handler,
133+
@Nullable String subProtocol, Supplier<HandshakeInfo> handshakeInfoFactory){
134+
135+
ServerHttpRequest request = exchange.getRequest();
136+
ServerHttpResponse response = exchange.getResponse();
137+
138+
HttpServletRequest servletRequest = ServerHttpRequestDecorator.getNativeRequest(request);
139+
HttpServletResponse servletResponse = ServerHttpResponseDecorator.getNativeResponse(response);
140+
141+
HandshakeInfo handshakeInfo = handshakeInfoFactory.get();
142+
DataBufferFactory bufferFactory = response.bufferFactory();
143+
144+
// Trigger WebFlux preCommit actions and upgrade
145+
return exchange.getResponse().setComplete()
146+
.then(Mono.deferContextual(contextView -> {
147+
Endpoint endpoint = new StandardWebSocketHandlerAdapter(
148+
ContextWebSocketHandler.decorate(handler, contextView),
149+
session -> new TomcatWebSocketSession(session, handshakeInfo, bufferFactory));
150+
151+
String requestURI = servletRequest.getRequestURI();
152+
DefaultServerEndpointConfig config = new DefaultServerEndpointConfig(requestURI, endpoint);
153+
config.setSubprotocols(subProtocol != null ?
154+
Collections.singletonList(subProtocol) : Collections.emptyList());
155+
156+
try {
157+
upgradeHttpToWebSocket(servletRequest, servletResponse, config, Collections.emptyMap());
158+
}
159+
catch (Exception ex) {
160+
return Mono.error(ex);
161+
}
162+
return Mono.empty();
163+
}));
164+
}
165+
166+
167+
protected void upgradeHttpToWebSocket(HttpServletRequest request, HttpServletResponse response,
168+
ServerEndpointConfig endpointConfig, Map<String,String> pathParams) throws Exception {
169+
170+
getContainer(request).upgradeHttpToWebSocket(request, response, endpointConfig, pathParams);
171+
}
172+
173+
protected ServerContainer getContainer(HttpServletRequest request) {
174+
if (this.serverContainer == null) {
175+
Object container = request.getServletContext().getAttribute(SERVER_CONTAINER_ATTR);
176+
Assert.state(container instanceof ServerContainer,
177+
"ServletContext attribute 'jakarta.websocket.server.ServerContainer' not found.");
178+
this.serverContainer = (ServerContainer) container;
179+
initServerContainer(this.serverContainer);
180+
}
181+
return this.serverContainer;
182+
}
183+
184+
private void initServerContainer(ServerContainer serverContainer) {
185+
if (this.asyncSendTimeout != null) {
186+
serverContainer.setAsyncSendTimeout(this.asyncSendTimeout);
187+
}
188+
if (this.maxSessionIdleTimeout != null) {
189+
serverContainer.setDefaultMaxSessionIdleTimeout(this.maxSessionIdleTimeout);
190+
}
191+
if (this.maxTextMessageBufferSize != null) {
192+
serverContainer.setDefaultMaxTextMessageBufferSize(this.maxTextMessageBufferSize);
193+
}
194+
if (this.maxBinaryMessageBufferSize != null) {
195+
serverContainer.setDefaultMaxBinaryMessageBufferSize(this.maxBinaryMessageBufferSize);
196+
}
197+
}
198+
199+
}

0 commit comments

Comments
 (0)