Merge pull request #21081 from aartiguptaa

* pr/21081:
  Polish "Add support for customizing RSocketMessageHandler"
  Add support for customizing RSocketMessageHandler

Closes gh-21081
This commit is contained in:
Madhura Bhave 2020-04-29 17:16:29 -07:00
commit d5246f1873
3 changed files with 63 additions and 3 deletions

View File

@ -0,0 +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.springframework.messaging.rsocket.annotation.support.RSocketMessageHandler;
/**
* Callback interface that can be used to customize a {@link RSocketMessageHandler}.
*
* @author Aarti Gupta
* @author Madhura Bhave
* @since 2.3.0
*/
@FunctionalInterface
public interface RSocketMessageHandlerCustomizer {
/**
* Customize the {@link RSocketMessageHandler}.
* @param messageHandler the message handler to customize
*/
void customize(RSocketMessageHandler messageHandler);
}

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -19,6 +19,7 @@ package org.springframework.boot.autoconfigure.rsocket;
import io.rsocket.RSocketFactory; import io.rsocket.RSocketFactory;
import io.rsocket.transport.netty.server.TcpServerTransport; import io.rsocket.transport.netty.server.TcpServerTransport;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
@ -43,9 +44,11 @@ public class RSocketMessagingAutoConfiguration {
@Bean @Bean
@ConditionalOnMissingBean @ConditionalOnMissingBean
public RSocketMessageHandler messageHandler(RSocketStrategies rSocketStrategies) { public RSocketMessageHandler messageHandler(RSocketStrategies rSocketStrategies,
ObjectProvider<RSocketMessageHandlerCustomizer> customizers) {
RSocketMessageHandler messageHandler = new RSocketMessageHandler(); RSocketMessageHandler messageHandler = new RSocketMessageHandler();
messageHandler.setRSocketStrategies(rSocketStrategies); messageHandler.setRSocketStrategies(rSocketStrategies);
customizers.orderedStream().forEach((customizer) -> customizer.customize(messageHandler));
return messageHandler; return messageHandler;
} }

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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.core.codec.StringDecoder;
import org.springframework.messaging.rsocket.RSocketStrategies; import org.springframework.messaging.rsocket.RSocketStrategies;
import org.springframework.messaging.rsocket.annotation.support.RSocketMessageHandler; import org.springframework.messaging.rsocket.annotation.support.RSocketMessageHandler;
import org.springframework.util.MimeType;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
@ -33,6 +34,7 @@ import static org.assertj.core.api.Assertions.assertThat;
* Tests for {@link RSocketMessagingAutoConfiguration}. * Tests for {@link RSocketMessagingAutoConfiguration}.
* *
* @author Brian Clozel * @author Brian Clozel
* @author Madhura Bhave
*/ */
class RSocketMessagingAutoConfigurationTests { class RSocketMessagingAutoConfigurationTests {
@ -61,6 +63,14 @@ class RSocketMessagingAutoConfigurationTests {
.getBeanNames(RSocketMessageHandler.class).containsOnly("customMessageHandler")); .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) @Configuration(proxyBeanMethods = false)
static class BaseConfiguration { 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"));
}
}
} }