Configure String codecs in RSocket strategies

Closes gh-16924
pull/16971/head
Brian Clozel 6 years ago
parent b2e395db7a
commit 45507c475b

@ -33,6 +33,8 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.core.ReactiveAdapterRegistry; import org.springframework.core.ReactiveAdapterRegistry;
import org.springframework.core.annotation.Order; import org.springframework.core.annotation.Order;
import org.springframework.core.codec.CharSequenceEncoder;
import org.springframework.core.codec.StringDecoder;
import org.springframework.core.io.buffer.NettyDataBufferFactory; import org.springframework.core.io.buffer.NettyDataBufferFactory;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.http.codec.cbor.Jackson2CborDecoder; import org.springframework.http.codec.cbor.Jackson2CborDecoder;
@ -62,6 +64,8 @@ public class RSocketStrategiesAutoConfiguration {
builder.reactiveAdapterStrategy(ReactiveAdapterRegistry.getSharedInstance()); builder.reactiveAdapterStrategy(ReactiveAdapterRegistry.getSharedInstance());
customizers.orderedStream() customizers.orderedStream()
.forEach((customizer) -> customizer.customize(builder)); .forEach((customizer) -> customizer.customize(builder));
builder.decoder(StringDecoder.textPlainOnly());
builder.encoder(CharSequenceEncoder.textPlainOnly());
builder.dataBufferFactory( builder.dataBufferFactory(
new NettyDataBufferFactory(PooledByteBufAllocator.DEFAULT)); new NettyDataBufferFactory(PooledByteBufAllocator.DEFAULT));
return builder.build(); return builder.build();

@ -24,6 +24,8 @@ import org.springframework.boot.rsocket.messaging.RSocketStrategiesCustomizer;
import org.springframework.boot.test.context.runner.ApplicationContextRunner; import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.core.codec.ByteArrayDecoder;
import org.springframework.core.codec.ByteArrayEncoder;
import org.springframework.core.codec.CharSequenceEncoder; import org.springframework.core.codec.CharSequenceEncoder;
import org.springframework.core.codec.StringDecoder; import org.springframework.core.codec.StringDecoder;
import org.springframework.http.codec.cbor.Jackson2CborDecoder; import org.springframework.http.codec.cbor.Jackson2CborDecoder;
@ -51,16 +53,19 @@ public class RSocketStrategiesAutoConfigurationTests {
this.contextRunner.run((context) -> { this.contextRunner.run((context) -> {
assertThat(context).getBeans(RSocketStrategies.class).hasSize(1); assertThat(context).getBeans(RSocketStrategies.class).hasSize(1);
RSocketStrategies strategies = context.getBean(RSocketStrategies.class); RSocketStrategies strategies = context.getBean(RSocketStrategies.class);
assertThat(strategies.decoders()).hasSize(2); assertThat(strategies.decoders()).hasSize(3);
assertThat(strategies.decoders().get(0)) assertThat(strategies.decoders().get(0))
.isInstanceOf(Jackson2CborDecoder.class); .isInstanceOf(Jackson2CborDecoder.class);
assertThat(strategies.decoders().get(1)) assertThat(strategies.decoders().get(1))
.isInstanceOf(Jackson2JsonDecoder.class); .isInstanceOf(Jackson2JsonDecoder.class);
assertThat(strategies.encoders()).hasSize(2); assertThat(strategies.decoders().get(2)).isInstanceOf(StringDecoder.class);
assertThat(strategies.encoders()).hasSize(3);
assertThat(strategies.encoders().get(0)) assertThat(strategies.encoders().get(0))
.isInstanceOf(Jackson2CborEncoder.class); .isInstanceOf(Jackson2CborEncoder.class);
assertThat(strategies.encoders().get(1)) assertThat(strategies.encoders().get(1))
.isInstanceOf(Jackson2JsonEncoder.class); .isInstanceOf(Jackson2JsonEncoder.class);
assertThat(strategies.encoders().get(2))
.isInstanceOf(CharSequenceEncoder.class);
}); });
} }
@ -80,10 +85,10 @@ public class RSocketStrategiesAutoConfigurationTests {
assertThat(context).getBeans(RSocketStrategies.class).hasSize(1); assertThat(context).getBeans(RSocketStrategies.class).hasSize(1);
RSocketStrategies strategies = context RSocketStrategies strategies = context
.getBean(RSocketStrategies.class); .getBean(RSocketStrategies.class);
assertThat(strategies.decoders()).hasSize(3) assertThat(strategies.decoders()).hasSize(4)
.hasAtLeastOneElementOfType(StringDecoder.class); .hasAtLeastOneElementOfType(ByteArrayDecoder.class);
assertThat(strategies.encoders()).hasSize(3) assertThat(strategies.encoders()).hasSize(4)
.hasAtLeastOneElementOfType(CharSequenceEncoder.class); .hasAtLeastOneElementOfType(ByteArrayEncoder.class);
}); });
} }
@ -119,8 +124,8 @@ public class RSocketStrategiesAutoConfigurationTests {
@Bean @Bean
public RSocketStrategiesCustomizer myCustomizer() { public RSocketStrategiesCustomizer myCustomizer() {
return (strategies) -> strategies.encoder(CharSequenceEncoder.textPlainOnly()) return (strategies) -> strategies.encoder(new ByteArrayEncoder())
.decoder(StringDecoder.textPlainOnly()); .decoder(new ByteArrayDecoder());
} }
} }

Loading…
Cancel
Save