Browse Source

Revise solution for WebFlux JettyRequestUpgradeStrategy

See gh-30344
pull/31226/head
rstoyanchev 1 year ago
parent
commit
bd4b3866fc
  1. 34
      spring-webflux/src/main/java/org/springframework/web/reactive/socket/server/upgrade/JettyRequestUpgradeStrategy.java

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

@ -16,6 +16,7 @@ @@ -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; @@ -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;
@ -47,6 +49,24 @@ import org.springframework.web.server.ServerWebExchange; @@ -47,6 +49,24 @@ import org.springframework.web.server.ServerWebExchange;
*/
public class JettyRequestUpgradeStrategy implements RequestUpgradeStrategy {
@Nullable
private Consumer<Configurable> webSocketConfigurer;
@Nullable
private JettyWebSocketServerContainer serverContainer;
/**
* 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 Mono<Void> upgrade(
ServerWebExchange exchange, WebSocketHandler handler,
@ -76,8 +96,7 @@ public class JettyRequestUpgradeStrategy implements RequestUpgradeStrategy { @@ -76,8 +96,7 @@ public class JettyRequestUpgradeStrategy implements RequestUpgradeStrategy {
return adapter;
};
JettyWebSocketServerContainer container = JettyWebSocketServerContainer.getContainer(servletContext);
JettyWebSocketServerContainer container = getWebSocketServerContainer(servletContext);
try {
container.upgrade(webSocketCreator, servletRequest, servletResponse);
}
@ -89,4 +108,15 @@ public class JettyRequestUpgradeStrategy implements RequestUpgradeStrategy { @@ -89,4 +108,15 @@ public class JettyRequestUpgradeStrategy implements RequestUpgradeStrategy {
}));
}
private JettyWebSocketServerContainer getWebSocketServerContainer(ServletContext servletContext) {
if (this.serverContainer == null) {
JettyWebSocketServerContainer container = JettyWebSocketServerContainer.getContainer(servletContext);
if (this.webSocketConfigurer != null) {
this.webSocketConfigurer.accept(container);
}
this.serverContainer = container;
}
return this.serverContainer;
}
}

Loading…
Cancel
Save