From 71f8347c819582af61653d88dc53a1ec4a655d3f Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Wed, 2 Oct 2019 10:41:53 -0700 Subject: [PATCH] Rename ServerRSocketFactoryCustomizer Rename `ServerRSocketFactoryCustomizer` to `ServerRSocketFactoryProcessor` to better reflect that it is not a traditional customizer. Closes gh-18390 --- .../RSocketServerAutoConfiguration.java | 17 +++++------ .../RSocketWebSocketNettyRouteProvider.java | 19 ++++++------- .../RSocketServerAutoConfigurationTests.java | 6 ++-- ...ocketWebSocketNettyRouteProviderTests.java | 11 ++++---- .../netty/NettyRSocketServerFactory.java | 28 +++++++++---------- ...ava => ServerRSocketFactoryProcessor.java} | 18 ++++++++---- .../netty/NettyRSocketServerFactoryTests.java | 20 ++++++------- 7 files changed, 60 insertions(+), 59 deletions(-) rename spring-boot-project/spring-boot/src/main/java/org/springframework/boot/rsocket/server/{ServerRSocketFactoryCustomizer.java => ServerRSocketFactoryProcessor.java} (60%) 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 b6e3baca86..8d7bdec50c 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 @@ -37,7 +37,7 @@ import org.springframework.boot.context.properties.PropertyMapper; import org.springframework.boot.rsocket.context.RSocketServerBootstrap; import org.springframework.boot.rsocket.netty.NettyRSocketServerFactory; import org.springframework.boot.rsocket.server.RSocketServerFactory; -import org.springframework.boot.rsocket.server.ServerRSocketFactoryCustomizer; +import org.springframework.boot.rsocket.server.ServerRSocketFactoryProcessor; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Conditional; import org.springframework.context.annotation.Configuration; @@ -70,11 +70,9 @@ public class RSocketServerAutoConfiguration { @Bean @ConditionalOnMissingBean RSocketWebSocketNettyRouteProvider rSocketWebsocketRouteProvider(RSocketProperties properties, - RSocketMessageHandler messageHandler, ObjectProvider customizers) { - RSocketWebSocketNettyRouteProvider routeProvider = new RSocketWebSocketNettyRouteProvider( - properties.getServer().getMappingPath(), messageHandler.responder()); - routeProvider.setCustomizers(customizers.orderedStream().collect(Collectors.toList())); - return routeProvider; + RSocketMessageHandler messageHandler, ObjectProvider processors) { + return new RSocketWebSocketNettyRouteProvider(properties.getServer().getMappingPath(), + messageHandler.responder(), processors.orderedStream()); } } @@ -92,14 +90,14 @@ public class RSocketServerAutoConfiguration { @Bean @ConditionalOnMissingBean RSocketServerFactory rSocketServerFactory(RSocketProperties properties, ReactorResourceFactory resourceFactory, - ObjectProvider customizers) { + ObjectProvider processors) { NettyRSocketServerFactory factory = new NettyRSocketServerFactory(); factory.setResourceFactory(resourceFactory); factory.setTransport(properties.getServer().getTransport()); PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull(); map.from(properties.getServer().getAddress()).to(factory::setAddress); map.from(properties.getServer().getPort()).to(factory::setPort); - factory.setServerCustomizers(customizers.orderedStream().collect(Collectors.toList())); + factory.setServerProcessors(processors.orderedStream().collect(Collectors.toList())); return factory; } @@ -111,8 +109,7 @@ public class RSocketServerAutoConfiguration { } @Bean - ServerRSocketFactoryCustomizer frameDecoderServerFactoryCustomizer( - RSocketMessageHandler rSocketMessageHandler) { + ServerRSocketFactoryProcessor frameDecoderServerFactoryCustomizer(RSocketMessageHandler rSocketMessageHandler) { return (serverRSocketFactory) -> { if (rSocketMessageHandler.getRSocketStrategies() .dataBufferFactory() instanceof NettyDataBufferFactory) { 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 3f7ada8b85..a15a82cc09 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 @@ -16,8 +16,9 @@ package org.springframework.boot.autoconfigure.rsocket; -import java.util.Collections; import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; import io.rsocket.RSocketFactory; import io.rsocket.SocketAcceptor; @@ -25,7 +26,7 @@ import io.rsocket.transport.ServerTransport; import io.rsocket.transport.netty.server.WebsocketRouteTransport; import reactor.netty.http.server.HttpServerRoutes; -import org.springframework.boot.rsocket.server.ServerRSocketFactoryCustomizer; +import org.springframework.boot.rsocket.server.ServerRSocketFactoryProcessor; import org.springframework.boot.web.embedded.netty.NettyRouteProvider; /** @@ -39,22 +40,20 @@ class RSocketWebSocketNettyRouteProvider implements NettyRouteProvider { private final SocketAcceptor socketAcceptor; - private List customizers = Collections.emptyList(); + private final List processors; - RSocketWebSocketNettyRouteProvider(String mappingPath, SocketAcceptor socketAcceptor) { + RSocketWebSocketNettyRouteProvider(String mappingPath, SocketAcceptor socketAcceptor, + Stream processors) { this.mappingPath = mappingPath; this.socketAcceptor = socketAcceptor; - } - - void setCustomizers(List customizers) { - this.customizers = customizers; + this.processors = processors.collect(Collectors.toList()); } @Override public HttpServerRoutes apply(HttpServerRoutes httpServerRoutes) { RSocketFactory.ServerRSocketFactory server = RSocketFactory.receive(); - for (ServerRSocketFactoryCustomizer customizer : this.customizers) { - server = customizer.apply(server); + for (ServerRSocketFactoryProcessor processor : this.processors) { + server = processor.process(server); } ServerTransport.ConnectionAcceptor acceptor = server.acceptor(this.socketAcceptor).toConnectionAcceptor(); return httpServerRoutes.ws(this.mappingPath, WebsocketRouteTransport.newHandler(acceptor)); diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/rsocket/RSocketServerAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/rsocket/RSocketServerAutoConfigurationTests.java index 79b6619c23..f03c09c9fa 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/rsocket/RSocketServerAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/rsocket/RSocketServerAutoConfigurationTests.java @@ -22,7 +22,7 @@ import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.rsocket.context.RSocketPortInfoApplicationContextInitializer; import org.springframework.boot.rsocket.context.RSocketServerBootstrap; import org.springframework.boot.rsocket.server.RSocketServerFactory; -import org.springframework.boot.rsocket.server.ServerRSocketFactoryCustomizer; +import org.springframework.boot.rsocket.server.ServerRSocketFactoryProcessor; import org.springframework.boot.test.context.runner.ApplicationContextRunner; import org.springframework.boot.test.context.runner.ReactiveWebApplicationContextRunner; import org.springframework.boot.web.server.WebServerFactoryCustomizer; @@ -79,7 +79,7 @@ class RSocketServerAutoConfigurationTests { reactiveWebContextRunner().withPropertyValues("spring.rsocket.server.port=0") .run((context) -> assertThat(context).hasSingleBean(RSocketServerFactory.class) .hasSingleBean(RSocketServerBootstrap.class) - .hasSingleBean(ServerRSocketFactoryCustomizer.class)); + .hasSingleBean(ServerRSocketFactoryProcessor.class)); } @Test @@ -88,7 +88,7 @@ class RSocketServerAutoConfigurationTests { .withInitializer(new RSocketPortInfoApplicationContextInitializer()).run((context) -> { assertThat(context).hasSingleBean(RSocketServerFactory.class) .hasSingleBean(RSocketServerBootstrap.class) - .hasSingleBean(ServerRSocketFactoryCustomizer.class); + .hasSingleBean(ServerRSocketFactoryProcessor.class); assertThat(context.getEnvironment().getProperty("local.rsocket.server.port")).isNotNull(); }); } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/rsocket/RSocketWebSocketNettyRouteProviderTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/rsocket/RSocketWebSocketNettyRouteProviderTests.java index 5cbf3a3bd6..b6ea6c9bb6 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/rsocket/RSocketWebSocketNettyRouteProviderTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/rsocket/RSocketWebSocketNettyRouteProviderTests.java @@ -28,7 +28,7 @@ import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration; import org.springframework.boot.autoconfigure.web.reactive.HttpHandlerAutoConfiguration; import org.springframework.boot.autoconfigure.web.reactive.WebFluxAutoConfiguration; import org.springframework.boot.autoconfigure.web.reactive.error.ErrorWebFluxAutoConfiguration; -import org.springframework.boot.rsocket.server.ServerRSocketFactoryCustomizer; +import org.springframework.boot.rsocket.server.ServerRSocketFactoryProcessor; import org.springframework.boot.test.context.runner.ReactiveWebApplicationContextRunner; import org.springframework.boot.web.embedded.netty.NettyReactiveWebServerFactory; import org.springframework.boot.web.reactive.context.AnnotationConfigReactiveWebServerApplicationContext; @@ -52,7 +52,6 @@ import static org.assertj.core.api.Assertions.assertThat; * * @author Brian Clozel */ - class RSocketWebSocketNettyRouteProviderTests { @Test @@ -77,7 +76,7 @@ class RSocketWebSocketNettyRouteProviderTests { WebTestClient client = createWebTestClient(serverContext.getWebServer()); client.get().uri("/protocol").exchange().expectStatus().isOk().expectBody().jsonPath("name", "http"); - assertThat(WebConfiguration.customizerCallCount).isEqualTo(1); + assertThat(WebConfiguration.processorCallCount).isEqualTo(1); }); } @@ -95,7 +94,7 @@ class RSocketWebSocketNettyRouteProviderTests { @Configuration(proxyBeanMethods = false) static class WebConfiguration { - static int customizerCallCount = 0; + static int processorCallCount = 0; @Bean WebController webController() { @@ -110,9 +109,9 @@ class RSocketWebSocketNettyRouteProviderTests { } @Bean - ServerRSocketFactoryCustomizer myRSocketFactoryCustomizer() { + ServerRSocketFactoryProcessor myRSocketFactoryProcessor() { return (server) -> { - customizerCallCount++; + processorCallCount++; return server; }; } diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/rsocket/netty/NettyRSocketServerFactory.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/rsocket/netty/NettyRSocketServerFactory.java index a0027895ad..83a51b2584 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/rsocket/netty/NettyRSocketServerFactory.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/rsocket/netty/NettyRSocketServerFactory.java @@ -37,7 +37,7 @@ import reactor.netty.tcp.TcpServer; import org.springframework.boot.rsocket.server.ConfigurableRSocketServerFactory; import org.springframework.boot.rsocket.server.RSocketServer; import org.springframework.boot.rsocket.server.RSocketServerFactory; -import org.springframework.boot.rsocket.server.ServerRSocketFactoryCustomizer; +import org.springframework.boot.rsocket.server.ServerRSocketFactoryProcessor; import org.springframework.http.client.reactive.ReactorResourceFactory; import org.springframework.util.Assert; @@ -60,7 +60,7 @@ public class NettyRSocketServerFactory implements RSocketServerFactory, Configur private Duration lifecycleTimeout; - private List serverCustomizers = new ArrayList<>(); + private List serverProcessors = new ArrayList<>(); @Override public void setPort(int port) { @@ -86,23 +86,23 @@ public class NettyRSocketServerFactory implements RSocketServerFactory, Configur } /** - * Set {@link ServerRSocketFactoryCustomizer}s that should be applied to the RSocket + * Set {@link ServerRSocketFactoryProcessor}s that should be applied to the RSocket * server builder. Calling this method will replace any existing customizers. - * @param serverCustomizers the customizers to set + * @param serverProcessors server processors to apply before the server starts */ - public void setServerCustomizers(Collection serverCustomizers) { - Assert.notNull(serverCustomizers, "ServerCustomizers must not be null"); - this.serverCustomizers = new ArrayList<>(serverCustomizers); + public void setServerProcessors(Collection serverProcessors) { + Assert.notNull(serverProcessors, "ServerProcessors must not be null"); + this.serverProcessors = new ArrayList<>(serverProcessors); } /** - * Add {@link ServerRSocketFactoryCustomizer}s that should applied while building the + * Add {@link ServerRSocketFactoryProcessor}s that should applied while building the * server. - * @param serverCustomizers the customizers to add + * @param serverProcessors server processors to apply before the server starts */ - public void addServerCustomizers(ServerRSocketFactoryCustomizer... serverCustomizers) { - Assert.notNull(serverCustomizers, "ServerCustomizer must not be null"); - this.serverCustomizers.addAll(Arrays.asList(serverCustomizers)); + public void addServerProcessors(ServerRSocketFactoryProcessor... serverProcessors) { + Assert.notNull(serverProcessors, "ServerProcessors must not be null"); + this.serverProcessors.addAll(Arrays.asList(serverProcessors)); } /** @@ -118,8 +118,8 @@ public class NettyRSocketServerFactory implements RSocketServerFactory, Configur public NettyRSocketServer create(SocketAcceptor socketAcceptor) { ServerTransport transport = createTransport(); RSocketFactory.ServerRSocketFactory factory = RSocketFactory.receive(); - for (ServerRSocketFactoryCustomizer customizer : this.serverCustomizers) { - factory = customizer.apply(factory); + for (ServerRSocketFactoryProcessor processor : this.serverProcessors) { + factory = processor.process(factory); } Mono starter = factory.acceptor(socketAcceptor).transport(transport).start(); return new NettyRSocketServer(starter, this.lifecycleTimeout); diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/rsocket/server/ServerRSocketFactoryCustomizer.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/rsocket/server/ServerRSocketFactoryProcessor.java similarity index 60% rename from spring-boot-project/spring-boot/src/main/java/org/springframework/boot/rsocket/server/ServerRSocketFactoryCustomizer.java rename to spring-boot-project/spring-boot/src/main/java/org/springframework/boot/rsocket/server/ServerRSocketFactoryProcessor.java index 26ebeb35c0..46242cc771 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/rsocket/server/ServerRSocketFactoryCustomizer.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/rsocket/server/ServerRSocketFactoryProcessor.java @@ -16,19 +16,25 @@ package org.springframework.boot.rsocket.server; -import java.util.function.Function; - -import io.rsocket.RSocketFactory; +import io.rsocket.RSocketFactory.ServerRSocketFactory; /** - * Mapping function that can be used to customize an RSocket server factory. + * Processor that allows for custom modification of a {@link ServerRSocketFactory + * RSocketFactory.ServerRSocketFactory} before it is used. * * @author Brian Clozel * @see RSocketServerFactory * @since 2.2.0 */ @FunctionalInterface -public interface ServerRSocketFactoryCustomizer - extends Function { +public interface ServerRSocketFactoryProcessor { + + /** + * Apply this {@code ServerRSocketFactoryProcessor} to the given factory instance + * before it's used. + * @param factory the factory to process + * @return the processed factory instance + */ + ServerRSocketFactory process(ServerRSocketFactory factory); } diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/rsocket/netty/NettyRSocketServerFactoryTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/rsocket/netty/NettyRSocketServerFactoryTests.java index 0178c64772..48f02414ed 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/rsocket/netty/NettyRSocketServerFactoryTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/rsocket/netty/NettyRSocketServerFactoryTests.java @@ -36,7 +36,7 @@ import org.mockito.InOrder; import reactor.core.publisher.Mono; import org.springframework.boot.rsocket.server.RSocketServer; -import org.springframework.boot.rsocket.server.ServerRSocketFactoryCustomizer; +import org.springframework.boot.rsocket.server.ServerRSocketFactoryProcessor; import org.springframework.core.codec.CharSequenceEncoder; import org.springframework.core.codec.StringDecoder; import org.springframework.core.io.buffer.NettyDataBufferFactory; @@ -130,19 +130,19 @@ class NettyRSocketServerFactoryTests { } @Test - void serverCustomizers() { + void serverProcessors() { NettyRSocketServerFactory factory = getFactory(); - ServerRSocketFactoryCustomizer[] customizers = new ServerRSocketFactoryCustomizer[2]; - for (int i = 0; i < customizers.length; i++) { - customizers[i] = mock(ServerRSocketFactoryCustomizer.class); - given(customizers[i].apply(any(RSocketFactory.ServerRSocketFactory.class))) + ServerRSocketFactoryProcessor[] processors = new ServerRSocketFactoryProcessor[2]; + for (int i = 0; i < processors.length; i++) { + processors[i] = mock(ServerRSocketFactoryProcessor.class); + given(processors[i].process(any(RSocketFactory.ServerRSocketFactory.class))) .will((invocation) -> invocation.getArgument(0)); } - factory.setServerCustomizers(Arrays.asList(customizers[0], customizers[1])); + factory.setServerProcessors(Arrays.asList(processors)); this.server = factory.create(new EchoRequestResponseAcceptor()); - InOrder ordered = inOrder((Object[]) customizers); - for (ServerRSocketFactoryCustomizer customizer : customizers) { - ordered.verify(customizer).apply(any(RSocketFactory.ServerRSocketFactory.class)); + InOrder ordered = inOrder((Object[]) processors); + for (ServerRSocketFactoryProcessor processor : processors) { + ordered.verify(processor).process(any(RSocketFactory.ServerRSocketFactory.class)); } }