Skip to content

Commit d5246f1

Browse files
committed
Merge pull request #21081 from aartiguptaa
* pr/21081: Polish "Add support for customizing RSocketMessageHandler" Add support for customizing RSocketMessageHandler Closes gh-21081
2 parents 0cb0907 + 6007a71 commit d5246f1

File tree

3 files changed

+63
-3
lines changed

3 files changed

+63
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +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+
17+
package org.springframework.boot.autoconfigure.rsocket;
18+
19+
import org.springframework.messaging.rsocket.annotation.support.RSocketMessageHandler;
20+
21+
/**
22+
* Callback interface that can be used to customize a {@link RSocketMessageHandler}.
23+
*
24+
* @author Aarti Gupta
25+
* @author Madhura Bhave
26+
* @since 2.3.0
27+
*/
28+
@FunctionalInterface
29+
public interface RSocketMessageHandlerCustomizer {
30+
31+
/**
32+
* Customize the {@link RSocketMessageHandler}.
33+
* @param messageHandler the message handler to customize
34+
*/
35+
void customize(RSocketMessageHandler messageHandler);
36+
37+
}

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

Lines changed: 5 additions & 2 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,6 +19,7 @@
1919
import io.rsocket.RSocketFactory;
2020
import io.rsocket.transport.netty.server.TcpServerTransport;
2121

22+
import org.springframework.beans.factory.ObjectProvider;
2223
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
2324
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
2425
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
@@ -43,9 +44,11 @@ public class RSocketMessagingAutoConfiguration {
4344

4445
@Bean
4546
@ConditionalOnMissingBean
46-
public RSocketMessageHandler messageHandler(RSocketStrategies rSocketStrategies) {
47+
public RSocketMessageHandler messageHandler(RSocketStrategies rSocketStrategies,
48+
ObjectProvider<RSocketMessageHandlerCustomizer> customizers) {
4749
RSocketMessageHandler messageHandler = new RSocketMessageHandler();
4850
messageHandler.setRSocketStrategies(rSocketStrategies);
51+
customizers.orderedStream().forEach((customizer) -> customizer.customize(messageHandler));
4952
return messageHandler;
5053
}
5154

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)