Support Jetty WebSocket server parameters

Closes gh-30344
This commit is contained in:
rstoyanchev
2023-09-13 09:22:16 +01:00
parent 29e3acc887
commit 1f8913a96c
5 changed files with 85 additions and 59 deletions

View File

@@ -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");
* 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.stream.Collectors;
import jakarta.servlet.ServletContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import reactor.core.publisher.Mono;
@@ -39,6 +40,7 @@ import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils;
import org.springframework.web.context.ServletContextAware;
import org.springframework.web.reactive.socket.HandshakeInfo;
import org.springframework.web.reactive.socket.WebSocketHandler;
import org.springframework.web.reactive.socket.server.RequestUpgradeStrategy;
@@ -63,7 +65,7 @@ import org.springframework.web.server.ServerWebInputException;
* @author Juergen Hoeller
* @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";
@@ -99,6 +101,7 @@ public class HandshakeWebSocketService implements WebSocketService, Lifecycle {
private static final Log logger = LogFactory.getLog(HandshakeWebSocketService.class);
private final RequestUpgradeStrategy upgradeStrategy;
@Nullable
@@ -154,6 +157,13 @@ public class HandshakeWebSocketService implements WebSocketService, Lifecycle {
return this.sessionAttributePredicate;
}
@Override
public void setServletContext(ServletContext servletContext) {
if (getUpgradeStrategy() instanceof ServletContextAware servletContextAware) {
servletContextAware.setServletContext(servletContext);
}
}
@Override
public void start() {

View File

@@ -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");
* you may not use this file except in compliance with the License.
@@ -16,11 +16,13 @@
package org.springframework.web.reactive.socket.server.support;
import jakarta.servlet.ServletContext;
import reactor.core.publisher.Mono;
import org.springframework.context.annotation.ImportRuntimeHints;
import org.springframework.core.Ordered;
import org.springframework.util.Assert;
import org.springframework.web.context.ServletContextAware;
import org.springframework.web.reactive.HandlerAdapter;
import org.springframework.web.reactive.HandlerResult;
import org.springframework.web.reactive.socket.WebSocketHandler;
@@ -47,7 +49,7 @@ import org.springframework.web.server.ServerWebExchange;
* @since 5.0
*/
@ImportRuntimeHints(HandshakeWebSocketServiceRuntimeHints.class)
public class WebSocketHandlerAdapter implements HandlerAdapter, Ordered {
public class WebSocketHandlerAdapter implements HandlerAdapter, ServletContextAware, Ordered {
private final WebSocketService webSocketService;
@@ -97,6 +99,13 @@ public class WebSocketHandlerAdapter implements HandlerAdapter, Ordered {
return this.webSocketService;
}
@Override
public void setServletContext(ServletContext servletContext) {
if (this.webSocketService instanceof ServletContextAware servletContextAware) {
servletContextAware.setServletContext(servletContext);
}
}
@Override
public boolean supports(Object handler) {

View File

@@ -16,6 +16,7 @@
package org.springframework.web.reactive.socket.server.upgrade;
import java.util.function.Consumer;
import java.util.function.Supplier;
import jakarta.servlet.ServletContext;
@@ -23,6 +24,7 @@ import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.eclipse.jetty.ee10.websocket.server.JettyWebSocketCreator;
import org.eclipse.jetty.ee10.websocket.server.JettyWebSocketServerContainer;
import org.eclipse.jetty.websocket.api.Configurable;
import reactor.core.publisher.Mono;
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.ServerHttpResponseDecorator;
import org.springframework.lang.Nullable;
import org.springframework.web.context.ServletContextAware;
import org.springframework.web.reactive.socket.HandshakeInfo;
import org.springframework.web.reactive.socket.WebSocketHandler;
import org.springframework.web.reactive.socket.adapter.ContextWebSocketHandler;
@@ -45,7 +48,30 @@ import org.springframework.web.server.ServerWebExchange;
* @author Rossen Stoyanchev
* @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
public Mono<Void> upgrade(