From 456d6e78fe07968a8157e639a3ab5fff70487148 Mon Sep 17 00:00:00 2001 From: Aarti Gupta Date: Fri, 17 Apr 2020 16:03:28 -0700 Subject: [PATCH 1/2] Add support for customizing RSocketMessageHandler See gh-21081 --- .../RSocketMessageHandlerCustomizer.java | 18 ++++++++++++++++++ .../RSocketMessagingAutoConfiguration.java | 14 +++++++++++--- 2 files changed, 29 insertions(+), 3 deletions(-) create mode 100644 spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/rsocket/RSocketMessageHandlerCustomizer.java diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/rsocket/RSocketMessageHandlerCustomizer.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/rsocket/RSocketMessageHandlerCustomizer.java new file mode 100644 index 00000000000..978dffd5dbe --- /dev/null +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/rsocket/RSocketMessageHandlerCustomizer.java @@ -0,0 +1,18 @@ +package org.springframework.boot.autoconfigure.rsocket; + +import org.apache.catalina.connector.Connector; +import org.springframework.boot.web.embedded.tomcat.ConfigurableTomcatWebServerFactory; +import org.springframework.messaging.rsocket.RSocketStrategies; +import org.springframework.messaging.rsocket.annotation.support.RSocketMessageHandler; +import org.springframework.util.RouteMatcher; + +/** + * @author Aarti Gupta + * Callback interface that can be used to customize a RSocketMessageHandler {@link Connector}. + */ +@FunctionalInterface +public interface RSocketMessageHandlerCustomizer { + + RSocketMessageHandler setRouteMatcher(RouteMatcher handler); + +} diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/rsocket/RSocketMessagingAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/rsocket/RSocketMessagingAutoConfiguration.java index 09a3898180b..d73fc35bf73 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/rsocket/RSocketMessagingAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/rsocket/RSocketMessagingAutoConfiguration.java @@ -19,6 +19,9 @@ package org.springframework.boot.autoconfigure.rsocket; import io.rsocket.RSocketFactory; import io.rsocket.transport.netty.server.TcpServerTransport; +import java.util.Objects; +import java.util.stream.Collectors; +import org.springframework.beans.factory.ObjectProvider; import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; @@ -28,6 +31,7 @@ import org.springframework.context.annotation.Configuration; import org.springframework.messaging.rsocket.RSocketRequester; import org.springframework.messaging.rsocket.RSocketStrategies; import org.springframework.messaging.rsocket.annotation.support.RSocketMessageHandler; +import org.springframework.util.RouteMatcher; /** * {@link EnableAutoConfiguration Auto-configuration} for Spring RSocket support in Spring @@ -37,16 +41,20 @@ import org.springframework.messaging.rsocket.annotation.support.RSocketMessageHa * @since 2.2.0 */ @Configuration(proxyBeanMethods = false) -@ConditionalOnClass({ RSocketRequester.class, RSocketFactory.class, TcpServerTransport.class }) +@ConditionalOnClass({RSocketRequester.class, RSocketFactory.class, TcpServerTransport.class}) @AutoConfigureAfter(RSocketStrategiesAutoConfiguration.class) public class RSocketMessagingAutoConfiguration { + @Bean @ConditionalOnMissingBean - public RSocketMessageHandler messageHandler(RSocketStrategies rSocketStrategies) { + public RSocketMessageHandler messageHandler(RSocketStrategies rSocketStrategies, ObjectProvider customizers) { RSocketMessageHandler messageHandler = new RSocketMessageHandler(); messageHandler.setRSocketStrategies(rSocketStrategies); - return messageHandler; + RSocketMessageHandlerCustomizer rSocketMessageHandlerCustomizer = customizers.getIfAvailable(); + return rSocketMessageHandlerCustomizer.setRouteMatcher(rSocketStrategies.routeMatcher()); } + + } From 6007a71b9cb37cd2e2cd5b1e2e35e2ecfdb0ba1b Mon Sep 17 00:00:00 2001 From: Madhura Bhave Date: Wed, 29 Apr 2020 17:15:09 -0700 Subject: [PATCH 2/2] Polish "Add support for customizing RSocketMessageHandler" See gh-21081 --- .../RSocketMessageHandlerCustomizer.java | 31 +++++++++++++++---- .../RSocketMessagingAutoConfiguration.java | 17 ++++------ ...SocketMessagingAutoConfigurationTests.java | 22 ++++++++++++- 3 files changed, 52 insertions(+), 18 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/rsocket/RSocketMessageHandlerCustomizer.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/rsocket/RSocketMessageHandlerCustomizer.java index 978dffd5dbe..c1f7e2f1d62 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/rsocket/RSocketMessageHandlerCustomizer.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/rsocket/RSocketMessageHandlerCustomizer.java @@ -1,18 +1,37 @@ +/* + * Copyright 2012-2020 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. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package org.springframework.boot.autoconfigure.rsocket; -import org.apache.catalina.connector.Connector; -import org.springframework.boot.web.embedded.tomcat.ConfigurableTomcatWebServerFactory; -import org.springframework.messaging.rsocket.RSocketStrategies; import org.springframework.messaging.rsocket.annotation.support.RSocketMessageHandler; -import org.springframework.util.RouteMatcher; /** + * Callback interface that can be used to customize a {@link RSocketMessageHandler}. + * * @author Aarti Gupta - * Callback interface that can be used to customize a RSocketMessageHandler {@link Connector}. + * @author Madhura Bhave + * @since 2.3.0 */ @FunctionalInterface public interface RSocketMessageHandlerCustomizer { - RSocketMessageHandler setRouteMatcher(RouteMatcher handler); + /** + * Customize the {@link RSocketMessageHandler}. + * @param messageHandler the message handler to customize + */ + void customize(RSocketMessageHandler messageHandler); } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/rsocket/RSocketMessagingAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/rsocket/RSocketMessagingAutoConfiguration.java index d73fc35bf73..a00cf4efa26 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/rsocket/RSocketMessagingAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/rsocket/RSocketMessagingAutoConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2020 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. @@ -19,8 +19,6 @@ package org.springframework.boot.autoconfigure.rsocket; import io.rsocket.RSocketFactory; import io.rsocket.transport.netty.server.TcpServerTransport; -import java.util.Objects; -import java.util.stream.Collectors; import org.springframework.beans.factory.ObjectProvider; import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; @@ -31,7 +29,6 @@ import org.springframework.context.annotation.Configuration; import org.springframework.messaging.rsocket.RSocketRequester; import org.springframework.messaging.rsocket.RSocketStrategies; import org.springframework.messaging.rsocket.annotation.support.RSocketMessageHandler; -import org.springframework.util.RouteMatcher; /** * {@link EnableAutoConfiguration Auto-configuration} for Spring RSocket support in Spring @@ -41,20 +38,18 @@ import org.springframework.util.RouteMatcher; * @since 2.2.0 */ @Configuration(proxyBeanMethods = false) -@ConditionalOnClass({RSocketRequester.class, RSocketFactory.class, TcpServerTransport.class}) +@ConditionalOnClass({ RSocketRequester.class, RSocketFactory.class, TcpServerTransport.class }) @AutoConfigureAfter(RSocketStrategiesAutoConfiguration.class) public class RSocketMessagingAutoConfiguration { - @Bean @ConditionalOnMissingBean - public RSocketMessageHandler messageHandler(RSocketStrategies rSocketStrategies, ObjectProvider customizers) { + public RSocketMessageHandler messageHandler(RSocketStrategies rSocketStrategies, + ObjectProvider customizers) { RSocketMessageHandler messageHandler = new RSocketMessageHandler(); messageHandler.setRSocketStrategies(rSocketStrategies); - RSocketMessageHandlerCustomizer rSocketMessageHandlerCustomizer = customizers.getIfAvailable(); - return rSocketMessageHandlerCustomizer.setRouteMatcher(rSocketStrategies.routeMatcher()); + customizers.orderedStream().forEach((customizer) -> customizer.customize(messageHandler)); + return messageHandler; } - - } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/rsocket/RSocketMessagingAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/rsocket/RSocketMessagingAutoConfigurationTests.java index 41b7bc24a0a..aee5e0f7058 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/rsocket/RSocketMessagingAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/rsocket/RSocketMessagingAutoConfigurationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2020 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. @@ -26,6 +26,7 @@ import org.springframework.core.codec.CharSequenceEncoder; import org.springframework.core.codec.StringDecoder; import org.springframework.messaging.rsocket.RSocketStrategies; import org.springframework.messaging.rsocket.annotation.support.RSocketMessageHandler; +import org.springframework.util.MimeType; import static org.assertj.core.api.Assertions.assertThat; @@ -33,6 +34,7 @@ import static org.assertj.core.api.Assertions.assertThat; * Tests for {@link RSocketMessagingAutoConfiguration}. * * @author Brian Clozel + * @author Madhura Bhave */ class RSocketMessagingAutoConfigurationTests { @@ -61,6 +63,14 @@ class RSocketMessagingAutoConfigurationTests { .getBeanNames(RSocketMessageHandler.class).containsOnly("customMessageHandler")); } + @Test + void shouldApplyMessageHandlerCustomizers() { + this.contextRunner.withUserConfiguration(CustomizerConfiguration.class).run((context) -> { + RSocketMessageHandler handler = context.getBean(RSocketMessageHandler.class); + assertThat(handler.getDefaultDataMimeType()).isEqualTo(MimeType.valueOf("application/json")); + }); + } + @Configuration(proxyBeanMethods = false) static class BaseConfiguration { @@ -86,4 +96,14 @@ class RSocketMessagingAutoConfigurationTests { } + @Configuration(proxyBeanMethods = false) + static class CustomizerConfiguration { + + @Bean + RSocketMessageHandlerCustomizer customizer() { + return (messageHandler) -> messageHandler.setDefaultDataMimeType(MimeType.valueOf("application/json")); + } + + } + }