Support Jetty WebSocket server parameters
Closes gh-30344
This commit is contained in:
parent
29e3acc887
commit
1f8913a96c
|
|
@ -280,9 +280,7 @@ The following example shows the XML configuration equivalent of the preceding ex
|
||||||
NOTE: For client-side WebSocket configuration, you should use `WebSocketContainerFactoryBean`
|
NOTE: For client-side WebSocket configuration, you should use `WebSocketContainerFactoryBean`
|
||||||
(XML) or `ContainerProvider.getWebSocketContainer()` (Java configuration).
|
(XML) or `ContainerProvider.getWebSocketContainer()` (Java configuration).
|
||||||
|
|
||||||
For Jetty, you need to supply a pre-configured Jetty `WebSocketServerFactory` and plug
|
For Jetty, you need to supply a `Consumer` callback to configure the WebSocket server. For example:
|
||||||
that into Spring's `DefaultHandshakeHandler` through your WebSocket Java config.
|
|
||||||
The following example shows how to do so:
|
|
||||||
|
|
||||||
[source,java,indent=0,subs="verbatim,quotes"]
|
[source,java,indent=0,subs="verbatim,quotes"]
|
||||||
----
|
----
|
||||||
|
|
@ -292,62 +290,20 @@ The following example shows how to do so:
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
|
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
|
||||||
|
|
||||||
|
JettyRequestUpgradeStrategy upgradeStrategy = new JettyRequestUpgradeStrategy();
|
||||||
|
upgradeStrategy.addWebSocketConfigurer(configurable -> {
|
||||||
|
policy.setInputBufferSize(8192);
|
||||||
|
policy.setIdleTimeout(600000);
|
||||||
|
});
|
||||||
|
|
||||||
registry.addHandler(echoWebSocketHandler(),
|
registry.addHandler(echoWebSocketHandler(),
|
||||||
"/echo").setHandshakeHandler(handshakeHandler());
|
"/echo").setHandshakeHandler(new DefaultHandshakeHandler(upgradeStrategy));
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
public DefaultHandshakeHandler handshakeHandler() {
|
|
||||||
|
|
||||||
WebSocketPolicy policy = new WebSocketPolicy(WebSocketBehavior.SERVER);
|
|
||||||
policy.setInputBufferSize(8192);
|
|
||||||
policy.setIdleTimeout(600000);
|
|
||||||
|
|
||||||
return new DefaultHandshakeHandler(
|
|
||||||
new JettyRequestUpgradeStrategy(new WebSocketServerFactory(policy)));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
----
|
----
|
||||||
|
|
||||||
The following example shows the XML configuration equivalent of the preceding example:
|
|
||||||
|
|
||||||
[source,xml,indent=0,subs="verbatim,quotes,attributes"]
|
|
||||||
----
|
|
||||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
|
||||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
|
||||||
xmlns:websocket="http://www.springframework.org/schema/websocket"
|
|
||||||
xsi:schemaLocation="
|
|
||||||
http://www.springframework.org/schema/beans
|
|
||||||
https://www.springframework.org/schema/beans/spring-beans.xsd
|
|
||||||
http://www.springframework.org/schema/websocket
|
|
||||||
https://www.springframework.org/schema/websocket/spring-websocket.xsd">
|
|
||||||
|
|
||||||
<websocket:handlers>
|
|
||||||
<websocket:mapping path="/echo" handler="echoHandler"/>
|
|
||||||
<websocket:handshake-handler ref="handshakeHandler"/>
|
|
||||||
</websocket:handlers>
|
|
||||||
|
|
||||||
<bean id="handshakeHandler" class="org.springframework...DefaultHandshakeHandler">
|
|
||||||
<constructor-arg ref="upgradeStrategy"/>
|
|
||||||
</bean>
|
|
||||||
|
|
||||||
<bean id="upgradeStrategy" class="org.springframework...JettyRequestUpgradeStrategy">
|
|
||||||
<constructor-arg ref="serverFactory"/>
|
|
||||||
</bean>
|
|
||||||
|
|
||||||
<bean id="serverFactory" class="org.eclipse.jetty...WebSocketServerFactory">
|
|
||||||
<constructor-arg>
|
|
||||||
<bean class="org.eclipse.jetty...WebSocketPolicy">
|
|
||||||
<constructor-arg value="SERVER"/>
|
|
||||||
<property name="inputBufferSize" value="8092"/>
|
|
||||||
<property name="idleTimeout" value="600000"/>
|
|
||||||
</bean>
|
|
||||||
</constructor-arg>
|
|
||||||
</bean>
|
|
||||||
|
|
||||||
</beans>
|
|
||||||
----
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2022 the original author or authors.
|
* Copyright 2002-2023 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|
@ -25,6 +25,7 @@ import java.util.Map;
|
||||||
import java.util.function.Predicate;
|
import java.util.function.Predicate;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
import jakarta.servlet.ServletContext;
|
||||||
import org.apache.commons.logging.Log;
|
import org.apache.commons.logging.Log;
|
||||||
import org.apache.commons.logging.LogFactory;
|
import org.apache.commons.logging.LogFactory;
|
||||||
import reactor.core.publisher.Mono;
|
import reactor.core.publisher.Mono;
|
||||||
|
|
@ -39,6 +40,7 @@ import org.springframework.util.Assert;
|
||||||
import org.springframework.util.ClassUtils;
|
import org.springframework.util.ClassUtils;
|
||||||
import org.springframework.util.MultiValueMap;
|
import org.springframework.util.MultiValueMap;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
|
import org.springframework.web.context.ServletContextAware;
|
||||||
import org.springframework.web.reactive.socket.HandshakeInfo;
|
import org.springframework.web.reactive.socket.HandshakeInfo;
|
||||||
import org.springframework.web.reactive.socket.WebSocketHandler;
|
import org.springframework.web.reactive.socket.WebSocketHandler;
|
||||||
import org.springframework.web.reactive.socket.server.RequestUpgradeStrategy;
|
import org.springframework.web.reactive.socket.server.RequestUpgradeStrategy;
|
||||||
|
|
@ -63,7 +65,7 @@ import org.springframework.web.server.ServerWebInputException;
|
||||||
* @author Juergen Hoeller
|
* @author Juergen Hoeller
|
||||||
* @since 5.0
|
* @since 5.0
|
||||||
*/
|
*/
|
||||||
public class HandshakeWebSocketService implements WebSocketService, Lifecycle {
|
public class HandshakeWebSocketService implements WebSocketService, ServletContextAware, Lifecycle {
|
||||||
|
|
||||||
private static final String SEC_WEBSOCKET_KEY = "Sec-WebSocket-Key";
|
private static final String SEC_WEBSOCKET_KEY = "Sec-WebSocket-Key";
|
||||||
|
|
||||||
|
|
@ -99,6 +101,7 @@ public class HandshakeWebSocketService implements WebSocketService, Lifecycle {
|
||||||
|
|
||||||
private static final Log logger = LogFactory.getLog(HandshakeWebSocketService.class);
|
private static final Log logger = LogFactory.getLog(HandshakeWebSocketService.class);
|
||||||
|
|
||||||
|
|
||||||
private final RequestUpgradeStrategy upgradeStrategy;
|
private final RequestUpgradeStrategy upgradeStrategy;
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
|
|
@ -154,6 +157,13 @@ public class HandshakeWebSocketService implements WebSocketService, Lifecycle {
|
||||||
return this.sessionAttributePredicate;
|
return this.sessionAttributePredicate;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setServletContext(ServletContext servletContext) {
|
||||||
|
if (getUpgradeStrategy() instanceof ServletContextAware servletContextAware) {
|
||||||
|
servletContextAware.setServletContext(servletContext);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void start() {
|
public void start() {
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2022 the original author or authors.
|
* Copyright 2002-2023 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|
@ -16,11 +16,13 @@
|
||||||
|
|
||||||
package org.springframework.web.reactive.socket.server.support;
|
package org.springframework.web.reactive.socket.server.support;
|
||||||
|
|
||||||
|
import jakarta.servlet.ServletContext;
|
||||||
import reactor.core.publisher.Mono;
|
import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
import org.springframework.context.annotation.ImportRuntimeHints;
|
import org.springframework.context.annotation.ImportRuntimeHints;
|
||||||
import org.springframework.core.Ordered;
|
import org.springframework.core.Ordered;
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
|
import org.springframework.web.context.ServletContextAware;
|
||||||
import org.springframework.web.reactive.HandlerAdapter;
|
import org.springframework.web.reactive.HandlerAdapter;
|
||||||
import org.springframework.web.reactive.HandlerResult;
|
import org.springframework.web.reactive.HandlerResult;
|
||||||
import org.springframework.web.reactive.socket.WebSocketHandler;
|
import org.springframework.web.reactive.socket.WebSocketHandler;
|
||||||
|
|
@ -47,7 +49,7 @@ import org.springframework.web.server.ServerWebExchange;
|
||||||
* @since 5.0
|
* @since 5.0
|
||||||
*/
|
*/
|
||||||
@ImportRuntimeHints(HandshakeWebSocketServiceRuntimeHints.class)
|
@ImportRuntimeHints(HandshakeWebSocketServiceRuntimeHints.class)
|
||||||
public class WebSocketHandlerAdapter implements HandlerAdapter, Ordered {
|
public class WebSocketHandlerAdapter implements HandlerAdapter, ServletContextAware, Ordered {
|
||||||
|
|
||||||
private final WebSocketService webSocketService;
|
private final WebSocketService webSocketService;
|
||||||
|
|
||||||
|
|
@ -97,6 +99,13 @@ public class WebSocketHandlerAdapter implements HandlerAdapter, Ordered {
|
||||||
return this.webSocketService;
|
return this.webSocketService;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setServletContext(ServletContext servletContext) {
|
||||||
|
if (this.webSocketService instanceof ServletContextAware servletContextAware) {
|
||||||
|
servletContextAware.setServletContext(servletContext);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean supports(Object handler) {
|
public boolean supports(Object handler) {
|
||||||
|
|
|
||||||
|
|
@ -16,6 +16,7 @@
|
||||||
|
|
||||||
package org.springframework.web.reactive.socket.server.upgrade;
|
package org.springframework.web.reactive.socket.server.upgrade;
|
||||||
|
|
||||||
|
import java.util.function.Consumer;
|
||||||
import java.util.function.Supplier;
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
import jakarta.servlet.ServletContext;
|
import jakarta.servlet.ServletContext;
|
||||||
|
|
@ -23,6 +24,7 @@ import jakarta.servlet.http.HttpServletRequest;
|
||||||
import jakarta.servlet.http.HttpServletResponse;
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
import org.eclipse.jetty.ee10.websocket.server.JettyWebSocketCreator;
|
import org.eclipse.jetty.ee10.websocket.server.JettyWebSocketCreator;
|
||||||
import org.eclipse.jetty.ee10.websocket.server.JettyWebSocketServerContainer;
|
import org.eclipse.jetty.ee10.websocket.server.JettyWebSocketServerContainer;
|
||||||
|
import org.eclipse.jetty.websocket.api.Configurable;
|
||||||
import reactor.core.publisher.Mono;
|
import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
import org.springframework.core.io.buffer.DataBufferFactory;
|
import org.springframework.core.io.buffer.DataBufferFactory;
|
||||||
|
|
@ -31,6 +33,7 @@ import org.springframework.http.server.reactive.ServerHttpRequestDecorator;
|
||||||
import org.springframework.http.server.reactive.ServerHttpResponse;
|
import org.springframework.http.server.reactive.ServerHttpResponse;
|
||||||
import org.springframework.http.server.reactive.ServerHttpResponseDecorator;
|
import org.springframework.http.server.reactive.ServerHttpResponseDecorator;
|
||||||
import org.springframework.lang.Nullable;
|
import org.springframework.lang.Nullable;
|
||||||
|
import org.springframework.web.context.ServletContextAware;
|
||||||
import org.springframework.web.reactive.socket.HandshakeInfo;
|
import org.springframework.web.reactive.socket.HandshakeInfo;
|
||||||
import org.springframework.web.reactive.socket.WebSocketHandler;
|
import org.springframework.web.reactive.socket.WebSocketHandler;
|
||||||
import org.springframework.web.reactive.socket.adapter.ContextWebSocketHandler;
|
import org.springframework.web.reactive.socket.adapter.ContextWebSocketHandler;
|
||||||
|
|
@ -45,7 +48,30 @@ import org.springframework.web.server.ServerWebExchange;
|
||||||
* @author Rossen Stoyanchev
|
* @author Rossen Stoyanchev
|
||||||
* @since 5.3.4
|
* @since 5.3.4
|
||||||
*/
|
*/
|
||||||
public class JettyRequestUpgradeStrategy implements RequestUpgradeStrategy {
|
public class JettyRequestUpgradeStrategy implements RequestUpgradeStrategy, ServletContextAware {
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
private Consumer<Configurable> webSocketConfigurer;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add a callback to configure WebSocket server parameters on
|
||||||
|
* {@link JettyWebSocketServerContainer}.
|
||||||
|
* @since 6.1.0
|
||||||
|
*/
|
||||||
|
public void addWebSocketConfigurer(Consumer<Configurable> webSocketConfigurer) {
|
||||||
|
this.webSocketConfigurer = (this.webSocketConfigurer != null ?
|
||||||
|
this.webSocketConfigurer.andThen(webSocketConfigurer) : webSocketConfigurer);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setServletContext(ServletContext servletContext) {
|
||||||
|
JettyWebSocketServerContainer container = JettyWebSocketServerContainer.getContainer(servletContext);
|
||||||
|
if (container != null && this.webSocketConfigurer != null) {
|
||||||
|
this.webSocketConfigurer.accept(container);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Mono<Void> upgrade(
|
public Mono<Void> upgrade(
|
||||||
|
|
|
||||||
|
|
@ -21,12 +21,14 @@ import java.security.Principal;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
import jakarta.servlet.ServletContext;
|
import jakarta.servlet.ServletContext;
|
||||||
import jakarta.servlet.http.HttpServletRequest;
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
import jakarta.servlet.http.HttpServletResponse;
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
import org.eclipse.jetty.ee10.websocket.server.JettyWebSocketCreator;
|
import org.eclipse.jetty.ee10.websocket.server.JettyWebSocketCreator;
|
||||||
import org.eclipse.jetty.ee10.websocket.server.JettyWebSocketServerContainer;
|
import org.eclipse.jetty.ee10.websocket.server.JettyWebSocketServerContainer;
|
||||||
|
import org.eclipse.jetty.websocket.api.Configurable;
|
||||||
|
|
||||||
import org.springframework.http.server.ServerHttpRequest;
|
import org.springframework.http.server.ServerHttpRequest;
|
||||||
import org.springframework.http.server.ServerHttpResponse;
|
import org.springframework.http.server.ServerHttpResponse;
|
||||||
|
|
@ -34,6 +36,7 @@ import org.springframework.http.server.ServletServerHttpRequest;
|
||||||
import org.springframework.http.server.ServletServerHttpResponse;
|
import org.springframework.http.server.ServletServerHttpResponse;
|
||||||
import org.springframework.lang.Nullable;
|
import org.springframework.lang.Nullable;
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
|
import org.springframework.web.context.ServletContextAware;
|
||||||
import org.springframework.web.socket.WebSocketExtension;
|
import org.springframework.web.socket.WebSocketExtension;
|
||||||
import org.springframework.web.socket.WebSocketHandler;
|
import org.springframework.web.socket.WebSocketHandler;
|
||||||
import org.springframework.web.socket.adapter.jetty.JettyWebSocketHandlerAdapter;
|
import org.springframework.web.socket.adapter.jetty.JettyWebSocketHandlerAdapter;
|
||||||
|
|
@ -47,11 +50,15 @@ import org.springframework.web.socket.server.RequestUpgradeStrategy;
|
||||||
* @author Rossen Stoyanchev
|
* @author Rossen Stoyanchev
|
||||||
* @since 5.3.4
|
* @since 5.3.4
|
||||||
*/
|
*/
|
||||||
public class JettyRequestUpgradeStrategy implements RequestUpgradeStrategy {
|
public class JettyRequestUpgradeStrategy implements RequestUpgradeStrategy, ServletContextAware {
|
||||||
|
|
||||||
private static final String[] SUPPORTED_VERSIONS = new String[] {"13"};
|
private static final String[] SUPPORTED_VERSIONS = new String[] {"13"};
|
||||||
|
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
private Consumer<Configurable> webSocketConfigurer;
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String[] getSupportedVersions() {
|
public String[] getSupportedVersions() {
|
||||||
return SUPPORTED_VERSIONS;
|
return SUPPORTED_VERSIONS;
|
||||||
|
|
@ -62,6 +69,24 @@ public class JettyRequestUpgradeStrategy implements RequestUpgradeStrategy {
|
||||||
return Collections.emptyList();
|
return Collections.emptyList();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add a callback to configure WebSocket server parameters on
|
||||||
|
* {@link JettyWebSocketServerContainer}.
|
||||||
|
* @since 6.1.0
|
||||||
|
*/
|
||||||
|
public void addWebSocketConfigurer(Consumer<Configurable> webSocketConfigurer) {
|
||||||
|
this.webSocketConfigurer = (this.webSocketConfigurer != null ?
|
||||||
|
this.webSocketConfigurer.andThen(webSocketConfigurer) : webSocketConfigurer);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setServletContext(ServletContext servletContext) {
|
||||||
|
JettyWebSocketServerContainer container = JettyWebSocketServerContainer.getContainer(servletContext);
|
||||||
|
if (container != null && this.webSocketConfigurer != null) {
|
||||||
|
this.webSocketConfigurer.accept(container);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void upgrade(ServerHttpRequest request, ServerHttpResponse response,
|
public void upgrade(ServerHttpRequest request, ServerHttpResponse response,
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue