Skip to content

Commit 1f8913a

Browse files
committed
Support Jetty WebSocket server parameters
Closes gh-30344
1 parent 29e3acc commit 1f8913a

File tree

5 files changed

+84
-58
lines changed

5 files changed

+84
-58
lines changed

framework-docs/modules/ROOT/pages/web/websocket/server.adoc

Lines changed: 8 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -280,9 +280,7 @@ The following example shows the XML configuration equivalent of the preceding ex
280280
NOTE: For client-side WebSocket configuration, you should use `WebSocketContainerFactoryBean`
281281
(XML) or `ContainerProvider.getWebSocketContainer()` (Java configuration).
282282

283-
For Jetty, you need to supply a pre-configured Jetty `WebSocketServerFactory` and plug
284-
that into Spring's `DefaultHandshakeHandler` through your WebSocket Java config.
285-
The following example shows how to do so:
283+
For Jetty, you need to supply a `Consumer` callback to configure the WebSocket server. For example:
286284

287285
[source,java,indent=0,subs="verbatim,quotes"]
288286
----
@@ -292,62 +290,20 @@ The following example shows how to do so:
292290
293291
@Override
294292
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
295-
registry.addHandler(echoWebSocketHandler(),
296-
"/echo").setHandshakeHandler(handshakeHandler());
297-
}
298-
299-
@Bean
300-
public DefaultHandshakeHandler handshakeHandler() {
301293
302-
WebSocketPolicy policy = new WebSocketPolicy(WebSocketBehavior.SERVER);
303-
policy.setInputBufferSize(8192);
304-
policy.setIdleTimeout(600000);
294+
JettyRequestUpgradeStrategy upgradeStrategy = new JettyRequestUpgradeStrategy();
295+
upgradeStrategy.addWebSocketConfigurer(configurable -> {
296+
policy.setInputBufferSize(8192);
297+
policy.setIdleTimeout(600000);
298+
});
305299
306-
return new DefaultHandshakeHandler(
307-
new JettyRequestUpgradeStrategy(new WebSocketServerFactory(policy)));
300+
registry.addHandler(echoWebSocketHandler(),
301+
"/echo").setHandshakeHandler(new DefaultHandshakeHandler(upgradeStrategy));
308302
}
309303
310304
}
311305
----
312306

313-
The following example shows the XML configuration equivalent of the preceding example:
314-
315-
[source,xml,indent=0,subs="verbatim,quotes,attributes"]
316-
----
317-
<beans xmlns="http://www.springframework.org/schema/beans"
318-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
319-
xmlns:websocket="http://www.springframework.org/schema/websocket"
320-
xsi:schemaLocation="
321-
http://www.springframework.org/schema/beans
322-
https://www.springframework.org/schema/beans/spring-beans.xsd
323-
http://www.springframework.org/schema/websocket
324-
https://www.springframework.org/schema/websocket/spring-websocket.xsd">
325-
326-
<websocket:handlers>
327-
<websocket:mapping path="/echo" handler="echoHandler"/>
328-
<websocket:handshake-handler ref="handshakeHandler"/>
329-
</websocket:handlers>
330-
331-
<bean id="handshakeHandler" class="org.springframework...DefaultHandshakeHandler">
332-
<constructor-arg ref="upgradeStrategy"/>
333-
</bean>
334-
335-
<bean id="upgradeStrategy" class="org.springframework...JettyRequestUpgradeStrategy">
336-
<constructor-arg ref="serverFactory"/>
337-
</bean>
338-
339-
<bean id="serverFactory" class="org.eclipse.jetty...WebSocketServerFactory">
340-
<constructor-arg>
341-
<bean class="org.eclipse.jetty...WebSocketPolicy">
342-
<constructor-arg value="SERVER"/>
343-
<property name="inputBufferSize" value="8092"/>
344-
<property name="idleTimeout" value="600000"/>
345-
</bean>
346-
</constructor-arg>
347-
</bean>
348-
349-
</beans>
350-
----
351307

352308

353309

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 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.
@@ -25,6 +25,7 @@
2525
import java.util.function.Predicate;
2626
import java.util.stream.Collectors;
2727

28+
import jakarta.servlet.ServletContext;
2829
import org.apache.commons.logging.Log;
2930
import org.apache.commons.logging.LogFactory;
3031
import reactor.core.publisher.Mono;
@@ -39,6 +40,7 @@
3940
import org.springframework.util.ClassUtils;
4041
import org.springframework.util.MultiValueMap;
4142
import org.springframework.util.StringUtils;
43+
import org.springframework.web.context.ServletContextAware;
4244
import org.springframework.web.reactive.socket.HandshakeInfo;
4345
import org.springframework.web.reactive.socket.WebSocketHandler;
4446
import org.springframework.web.reactive.socket.server.RequestUpgradeStrategy;
@@ -63,7 +65,7 @@
6365
* @author Juergen Hoeller
6466
* @since 5.0
6567
*/
66-
public class HandshakeWebSocketService implements WebSocketService, Lifecycle {
68+
public class HandshakeWebSocketService implements WebSocketService, ServletContextAware, Lifecycle {
6769

6870
private static final String SEC_WEBSOCKET_KEY = "Sec-WebSocket-Key";
6971

@@ -99,6 +101,7 @@ public class HandshakeWebSocketService implements WebSocketService, Lifecycle {
99101

100102
private static final Log logger = LogFactory.getLog(HandshakeWebSocketService.class);
101103

104+
102105
private final RequestUpgradeStrategy upgradeStrategy;
103106

104107
@Nullable
@@ -154,6 +157,13 @@ public Predicate<String> getSessionAttributePredicate() {
154157
return this.sessionAttributePredicate;
155158
}
156159

160+
@Override
161+
public void setServletContext(ServletContext servletContext) {
162+
if (getUpgradeStrategy() instanceof ServletContextAware servletContextAware) {
163+
servletContextAware.setServletContext(servletContext);
164+
}
165+
}
166+
157167

158168
@Override
159169
public void start() {

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 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.
@@ -16,11 +16,13 @@
1616

1717
package org.springframework.web.reactive.socket.server.support;
1818

19+
import jakarta.servlet.ServletContext;
1920
import reactor.core.publisher.Mono;
2021

2122
import org.springframework.context.annotation.ImportRuntimeHints;
2223
import org.springframework.core.Ordered;
2324
import org.springframework.util.Assert;
25+
import org.springframework.web.context.ServletContextAware;
2426
import org.springframework.web.reactive.HandlerAdapter;
2527
import org.springframework.web.reactive.HandlerResult;
2628
import org.springframework.web.reactive.socket.WebSocketHandler;
@@ -47,7 +49,7 @@
4749
* @since 5.0
4850
*/
4951
@ImportRuntimeHints(HandshakeWebSocketServiceRuntimeHints.class)
50-
public class WebSocketHandlerAdapter implements HandlerAdapter, Ordered {
52+
public class WebSocketHandlerAdapter implements HandlerAdapter, ServletContextAware, Ordered {
5153

5254
private final WebSocketService webSocketService;
5355

@@ -97,6 +99,13 @@ public WebSocketService getWebSocketService() {
9799
return this.webSocketService;
98100
}
99101

102+
@Override
103+
public void setServletContext(ServletContext servletContext) {
104+
if (this.webSocketService instanceof ServletContextAware servletContextAware) {
105+
servletContextAware.setServletContext(servletContext);
106+
}
107+
}
108+
100109

101110
@Override
102111
public boolean supports(Object handler) {

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

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@
1616

1717
package org.springframework.web.reactive.socket.server.upgrade;
1818

19+
import java.util.function.Consumer;
1920
import java.util.function.Supplier;
2021

2122
import jakarta.servlet.ServletContext;
2223
import jakarta.servlet.http.HttpServletRequest;
2324
import jakarta.servlet.http.HttpServletResponse;
2425
import org.eclipse.jetty.ee10.websocket.server.JettyWebSocketCreator;
2526
import org.eclipse.jetty.ee10.websocket.server.JettyWebSocketServerContainer;
27+
import org.eclipse.jetty.websocket.api.Configurable;
2628
import reactor.core.publisher.Mono;
2729

2830
import org.springframework.core.io.buffer.DataBufferFactory;
@@ -31,6 +33,7 @@
3133
import org.springframework.http.server.reactive.ServerHttpResponse;
3234
import org.springframework.http.server.reactive.ServerHttpResponseDecorator;
3335
import org.springframework.lang.Nullable;
36+
import org.springframework.web.context.ServletContextAware;
3437
import org.springframework.web.reactive.socket.HandshakeInfo;
3538
import org.springframework.web.reactive.socket.WebSocketHandler;
3639
import org.springframework.web.reactive.socket.adapter.ContextWebSocketHandler;
@@ -45,7 +48,30 @@
4548
* @author Rossen Stoyanchev
4649
* @since 5.3.4
4750
*/
48-
public class JettyRequestUpgradeStrategy implements RequestUpgradeStrategy {
51+
public class JettyRequestUpgradeStrategy implements RequestUpgradeStrategy, ServletContextAware {
52+
53+
@Nullable
54+
private Consumer<Configurable> webSocketConfigurer;
55+
56+
57+
/**
58+
* Add a callback to configure WebSocket server parameters on
59+
* {@link JettyWebSocketServerContainer}.
60+
* @since 6.1.0
61+
*/
62+
public void addWebSocketConfigurer(Consumer<Configurable> webSocketConfigurer) {
63+
this.webSocketConfigurer = (this.webSocketConfigurer != null ?
64+
this.webSocketConfigurer.andThen(webSocketConfigurer) : webSocketConfigurer);
65+
}
66+
67+
@Override
68+
public void setServletContext(ServletContext servletContext) {
69+
JettyWebSocketServerContainer container = JettyWebSocketServerContainer.getContainer(servletContext);
70+
if (container != null && this.webSocketConfigurer != null) {
71+
this.webSocketConfigurer.accept(container);
72+
}
73+
}
74+
4975

5076
@Override
5177
public Mono<Void> upgrade(

spring-websocket/src/main/java/org/springframework/web/socket/server/jetty/JettyRequestUpgradeStrategy.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,22 @@
2121
import java.util.Collections;
2222
import java.util.List;
2323
import java.util.Map;
24+
import java.util.function.Consumer;
2425

2526
import jakarta.servlet.ServletContext;
2627
import jakarta.servlet.http.HttpServletRequest;
2728
import jakarta.servlet.http.HttpServletResponse;
2829
import org.eclipse.jetty.ee10.websocket.server.JettyWebSocketCreator;
2930
import org.eclipse.jetty.ee10.websocket.server.JettyWebSocketServerContainer;
31+
import org.eclipse.jetty.websocket.api.Configurable;
3032

3133
import org.springframework.http.server.ServerHttpRequest;
3234
import org.springframework.http.server.ServerHttpResponse;
3335
import org.springframework.http.server.ServletServerHttpRequest;
3436
import org.springframework.http.server.ServletServerHttpResponse;
3537
import org.springframework.lang.Nullable;
3638
import org.springframework.util.Assert;
39+
import org.springframework.web.context.ServletContextAware;
3740
import org.springframework.web.socket.WebSocketExtension;
3841
import org.springframework.web.socket.WebSocketHandler;
3942
import org.springframework.web.socket.adapter.jetty.JettyWebSocketHandlerAdapter;
@@ -47,11 +50,15 @@
4750
* @author Rossen Stoyanchev
4851
* @since 5.3.4
4952
*/
50-
public class JettyRequestUpgradeStrategy implements RequestUpgradeStrategy {
53+
public class JettyRequestUpgradeStrategy implements RequestUpgradeStrategy, ServletContextAware {
5154

5255
private static final String[] SUPPORTED_VERSIONS = new String[] {"13"};
5356

5457

58+
@Nullable
59+
private Consumer<Configurable> webSocketConfigurer;
60+
61+
5562
@Override
5663
public String[] getSupportedVersions() {
5764
return SUPPORTED_VERSIONS;
@@ -62,6 +69,24 @@ public List<WebSocketExtension> getSupportedExtensions(ServerHttpRequest request
6269
return Collections.emptyList();
6370
}
6471

72+
/**
73+
* Add a callback to configure WebSocket server parameters on
74+
* {@link JettyWebSocketServerContainer}.
75+
* @since 6.1.0
76+
*/
77+
public void addWebSocketConfigurer(Consumer<Configurable> webSocketConfigurer) {
78+
this.webSocketConfigurer = (this.webSocketConfigurer != null ?
79+
this.webSocketConfigurer.andThen(webSocketConfigurer) : webSocketConfigurer);
80+
}
81+
82+
@Override
83+
public void setServletContext(ServletContext servletContext) {
84+
JettyWebSocketServerContainer container = JettyWebSocketServerContainer.getContainer(servletContext);
85+
if (container != null && this.webSocketConfigurer != null) {
86+
this.webSocketConfigurer.accept(container);
87+
}
88+
}
89+
6590

6691
@Override
6792
public void upgrade(ServerHttpRequest request, ServerHttpResponse response,

0 commit comments

Comments
 (0)