From f840141652a4ecf6455ebd3805b6262ddd06ebf8 Mon Sep 17 00:00:00 2001 From: Leo Li <269739606@qq.com> Date: Wed, 26 Jan 2022 10:56:50 +0800 Subject: [PATCH] Allow custom RSocket WebsocketServerSpecs to be defined See gh-29567 --- .../rsocket/RSocketProperties.java | 68 +++++++++++++++++++ .../RSocketServerAutoConfiguration.java | 2 +- .../RSocketWebSocketNettyRouteProvider.java | 14 +++- 3 files changed, 81 insertions(+), 3 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/rsocket/RSocketProperties.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/rsocket/RSocketProperties.java index f1921f23885..c43895c47ab 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/rsocket/RSocketProperties.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/rsocket/RSocketProperties.java @@ -73,6 +73,8 @@ public class RSocketProperties { @NestedConfigurationProperty private Ssl ssl; + private Spec spec = new Spec(); + public Integer getPort() { return this.port; } @@ -121,6 +123,72 @@ public class RSocketProperties { this.ssl = ssl; } + public Spec getSpec() { + return this.spec; + } + + public void setSpec(Spec spec) { + this.spec = spec; + } + + public static class Spec { + + /** + * Sub-protocol to use in websocket handshake signature. + */ + private String protocols; + + /** + * Specifies a custom maximum allowable frame payload length. 65536 by + * default. + */ + private int maxFramePayloadLength = 65536; + + /** + * Flag whether to proxy websocket ping frames or respond to them. + */ + private boolean handlePing; + + /** + * Flag whether the websocket compression extension is enabled if the client + * request presents websocket extensions headers. + */ + private boolean compress; + + public String getProtocols() { + return this.protocols; + } + + public void setProtocols(String protocols) { + this.protocols = protocols; + } + + public int getMaxFramePayloadLength() { + return this.maxFramePayloadLength; + } + + public void setMaxFramePayloadLength(int maxFramePayloadLength) { + this.maxFramePayloadLength = maxFramePayloadLength; + } + + public boolean isHandlePing() { + return this.handlePing; + } + + public void setHandlePing(boolean handlePing) { + this.handlePing = handlePing; + } + + public boolean isCompress() { + return this.compress; + } + + public void setCompress(boolean compress) { + this.compress = compress; + } + + } + } } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/rsocket/RSocketServerAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/rsocket/RSocketServerAutoConfiguration.java index 96bbbc454ba..6c5394d5904 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/rsocket/RSocketServerAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/rsocket/RSocketServerAutoConfiguration.java @@ -73,7 +73,7 @@ public class RSocketServerAutoConfiguration { RSocketWebSocketNettyRouteProvider rSocketWebsocketRouteProvider(RSocketProperties properties, RSocketMessageHandler messageHandler, ObjectProvider customizers) { return new RSocketWebSocketNettyRouteProvider(properties.getServer().getMappingPath(), - messageHandler.responder(), customizers.orderedStream()); + properties.getServer().getSpec(), messageHandler.responder(), customizers.orderedStream()); } } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/rsocket/RSocketWebSocketNettyRouteProvider.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/rsocket/RSocketWebSocketNettyRouteProvider.java index 5cb8d7f374f..9d9ddc91dd5 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/rsocket/RSocketWebSocketNettyRouteProvider.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/rsocket/RSocketWebSocketNettyRouteProvider.java @@ -24,7 +24,9 @@ import io.rsocket.core.RSocketServer; import io.rsocket.transport.ServerTransport; import io.rsocket.transport.netty.server.WebsocketRouteTransport; import reactor.netty.http.server.HttpServerRoutes; +import reactor.netty.http.server.WebsocketServerSpec; +import org.springframework.boot.autoconfigure.rsocket.RSocketProperties.Server.Spec; import org.springframework.boot.rsocket.server.RSocketServerCustomizer; import org.springframework.boot.web.embedded.netty.NettyRouteProvider; @@ -32,6 +34,7 @@ import org.springframework.boot.web.embedded.netty.NettyRouteProvider; * {@link NettyRouteProvider} that configures an RSocket Websocket endpoint. * * @author Brian Clozel + * @author Leo Li */ class RSocketWebSocketNettyRouteProvider implements NettyRouteProvider { @@ -41,11 +44,14 @@ class RSocketWebSocketNettyRouteProvider implements NettyRouteProvider { private final List customizers; - RSocketWebSocketNettyRouteProvider(String mappingPath, SocketAcceptor socketAcceptor, + private final Spec spec; + + RSocketWebSocketNettyRouteProvider(String mappingPath, Spec spec, SocketAcceptor socketAcceptor, Stream customizers) { this.mappingPath = mappingPath; this.socketAcceptor = socketAcceptor; this.customizers = customizers.toList(); + this.spec = spec; } @Override @@ -53,7 +59,11 @@ class RSocketWebSocketNettyRouteProvider implements NettyRouteProvider { RSocketServer server = RSocketServer.create(this.socketAcceptor); this.customizers.forEach((customizer) -> customizer.customize(server)); ServerTransport.ConnectionAcceptor connectionAcceptor = server.asConnectionAcceptor(); - return httpServerRoutes.ws(this.mappingPath, WebsocketRouteTransport.newHandler(connectionAcceptor)); + WebsocketServerSpec.Builder build = (this.spec.getProtocols() == null) ? WebsocketServerSpec.builder() + : WebsocketServerSpec.builder().protocols(this.spec.getProtocols()); + return httpServerRoutes.ws(this.mappingPath, WebsocketRouteTransport.newHandler(connectionAcceptor), + build.maxFramePayloadLength(this.spec.getMaxFramePayloadLength()).handlePing(this.spec.isHandlePing()) + .compress(this.spec.isCompress()).build()); } }