Skip to content

Commit 6007a71

Browse files
committed
Polish "Add support for customizing RSocketMessageHandler"
See gh-21081
1 parent 456d6e7 commit 6007a71

File tree

3 files changed

+52
-18
lines changed

3 files changed

+52
-18
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,37 @@
1+
/*
2+
* Copyright 2012-2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
117
package org.springframework.boot.autoconfigure.rsocket;
218

3-
import org.apache.catalina.connector.Connector;
4-
import org.springframework.boot.web.embedded.tomcat.ConfigurableTomcatWebServerFactory;
5-
import org.springframework.messaging.rsocket.RSocketStrategies;
619
import org.springframework.messaging.rsocket.annotation.support.RSocketMessageHandler;
7-
import org.springframework.util.RouteMatcher;
820

921
/**
22+
* Callback interface that can be used to customize a {@link RSocketMessageHandler}.
23+
*
1024
* @author Aarti Gupta
11-
* Callback interface that can be used to customize a RSocketMessageHandler {@link Connector}.
25+
* @author Madhura Bhave
26+
* @since 2.3.0
1227
*/
1328
@FunctionalInterface
1429
public interface RSocketMessageHandlerCustomizer {
1530

16-
RSocketMessageHandler setRouteMatcher(RouteMatcher handler);
31+
/**
32+
* Customize the {@link RSocketMessageHandler}.
33+
* @param messageHandler the message handler to customize
34+
*/
35+
void customize(RSocketMessageHandler messageHandler);
1736

1837
}

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/rsocket/RSocketMessagingAutoConfiguration.java

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,8 +19,6 @@
1919
import io.rsocket.RSocketFactory;
2020
import io.rsocket.transport.netty.server.TcpServerTransport;
2121

22-
import java.util.Objects;
23-
import java.util.stream.Collectors;
2422
import org.springframework.beans.factory.ObjectProvider;
2523
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
2624
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
@@ -31,7 +29,6 @@
3129
import org.springframework.messaging.rsocket.RSocketRequester;
3230
import org.springframework.messaging.rsocket.RSocketStrategies;
3331
import org.springframework.messaging.rsocket.annotation.support.RSocketMessageHandler;
34-
import org.springframework.util.RouteMatcher;
3532

3633
/**
3734
* {@link EnableAutoConfiguration Auto-configuration} for Spring RSocket support in Spring
@@ -41,20 +38,18 @@
4138
* @since 2.2.0
4239
*/
4340
@Configuration(proxyBeanMethods = false)
44-
@ConditionalOnClass({RSocketRequester.class, RSocketFactory.class, TcpServerTransport.class})
41+
@ConditionalOnClass({ RSocketRequester.class, RSocketFactory.class, TcpServerTransport.class })
4542
@AutoConfigureAfter(RSocketStrategiesAutoConfiguration.class)
4643
public class RSocketMessagingAutoConfiguration {
4744

48-
4945
@Bean
5046
@ConditionalOnMissingBean
51-
public RSocketMessageHandler messageHandler(RSocketStrategies rSocketStrategies, ObjectProvider<RSocketMessageHandlerCustomizer> customizers) {
47+
public RSocketMessageHandler messageHandler(RSocketStrategies rSocketStrategies,
48+
ObjectProvider<RSocketMessageHandlerCustomizer> customizers) {
5249
RSocketMessageHandler messageHandler = new RSocketMessageHandler();
5350
messageHandler.setRSocketStrategies(rSocketStrategies);
54-
RSocketMessageHandlerCustomizer rSocketMessageHandlerCustomizer = customizers.getIfAvailable();
55-
return rSocketMessageHandlerCustomizer.setRouteMatcher(rSocketStrategies.routeMatcher());
51+
customizers.orderedStream().forEach((customizer) -> customizer.customize(messageHandler));
52+
return messageHandler;
5653
}
5754

58-
59-
6055
}

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/rsocket/RSocketMessagingAutoConfigurationTests.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -26,13 +26,15 @@
2626
import org.springframework.core.codec.StringDecoder;
2727
import org.springframework.messaging.rsocket.RSocketStrategies;
2828
import org.springframework.messaging.rsocket.annotation.support.RSocketMessageHandler;
29+
import org.springframework.util.MimeType;
2930

3031
import static org.assertj.core.api.Assertions.assertThat;
3132

3233
/**
3334
* Tests for {@link RSocketMessagingAutoConfiguration}.
3435
*
3536
* @author Brian Clozel
37+
* @author Madhura Bhave
3638
*/
3739
class RSocketMessagingAutoConfigurationTests {
3840

@@ -61,6 +63,14 @@ void shouldUseCustomSocketAcceptor() {
6163
.getBeanNames(RSocketMessageHandler.class).containsOnly("customMessageHandler"));
6264
}
6365

66+
@Test
67+
void shouldApplyMessageHandlerCustomizers() {
68+
this.contextRunner.withUserConfiguration(CustomizerConfiguration.class).run((context) -> {
69+
RSocketMessageHandler handler = context.getBean(RSocketMessageHandler.class);
70+
assertThat(handler.getDefaultDataMimeType()).isEqualTo(MimeType.valueOf("application/json"));
71+
});
72+
}
73+
6474
@Configuration(proxyBeanMethods = false)
6575
static class BaseConfiguration {
6676

@@ -86,4 +96,14 @@ RSocketMessageHandler customMessageHandler() {
8696

8797
}
8898

99+
@Configuration(proxyBeanMethods = false)
100+
static class CustomizerConfiguration {
101+
102+
@Bean
103+
RSocketMessageHandlerCustomizer customizer() {
104+
return (messageHandler) -> messageHandler.setDefaultDataMimeType(MimeType.valueOf("application/json"));
105+
}
106+
107+
}
108+
89109
}

0 commit comments

Comments
 (0)