Skip to content

Commit d243d7e

Browse files
committed
Polish 'Add spring.rabbitmq.template.allowed-list-patterns property'
See gh-40421
1 parent c329c5f commit d243d7e

File tree

2 files changed

+51
-10
lines changed

2 files changed

+51
-10
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitTemplateConfigurer.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@
2424
import org.springframework.amqp.support.converter.AllowedListDeserializingMessageConverter;
2525
import org.springframework.amqp.support.converter.MessageConverter;
2626
import org.springframework.boot.context.properties.PropertyMapper;
27+
import org.springframework.boot.context.properties.source.InvalidConfigurationPropertyValueException;
2728
import org.springframework.util.Assert;
29+
import org.springframework.util.CollectionUtils;
2830

2931
/**
3032
* Configure {@link RabbitTemplate} with sensible defaults.
@@ -104,12 +106,19 @@ public void configure(RabbitTemplate template, ConnectionFactory connectionFacto
104106
map.from(templateProperties::getRoutingKey).to(template::setRoutingKey);
105107
map.from(templateProperties::getDefaultReceiveQueue).whenNonNull().to(template::setDefaultReceiveQueue);
106108
map.from(templateProperties::isObservationEnabled).to(template::setObservationEnabled);
107-
if (templateProperties.getAllowedListPatterns() != null) {
108-
MessageConverter messageConverter = template.getMessageConverter();
109-
if (messageConverter instanceof AllowedListDeserializingMessageConverter mc) {
110-
mc.setAllowedListPatterns(templateProperties.getAllowedListPatterns());
111-
}
109+
map.from(templateProperties::getAllowedListPatterns)
110+
.whenNot(CollectionUtils::isEmpty)
111+
.to((allowListPatterns) -> setAllowedListPatterns(template.getMessageConverter(), allowListPatterns));
112+
}
113+
114+
private void setAllowedListPatterns(MessageConverter messageConverter, List<String> allowListPatterns) {
115+
if (messageConverter instanceof AllowedListDeserializingMessageConverter allowedListDeserializingMessageConverter) {
116+
allowedListDeserializingMessageConverter.setAllowedListPatterns(allowListPatterns);
117+
return;
112118
}
119+
throw new InvalidConfigurationPropertyValueException("spring.rabbitmq.template.allow-list-patterns",
120+
allowListPatterns,
121+
"Allow list patterns can only be applied to a AllowedListDeserializingMessageConverter");
113122
}
114123

115124
private boolean determineMandatoryFlag() {

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/amqp/RabbitAutoConfigurationTests.java

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import org.springframework.amqp.core.AcknowledgeMode;
4444
import org.springframework.amqp.core.AmqpAdmin;
4545
import org.springframework.amqp.core.Message;
46+
import org.springframework.amqp.core.MessageProperties;
4647
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
4748
import org.springframework.amqp.rabbit.annotation.RabbitListener;
4849
import org.springframework.amqp.rabbit.config.AbstractRabbitListenerContainerFactory;
@@ -62,12 +63,13 @@
6263
import org.springframework.amqp.rabbit.listener.RabbitListenerContainerFactory;
6364
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
6465
import org.springframework.amqp.rabbit.retry.MessageRecoverer;
65-
import org.springframework.amqp.support.converter.AllowedListDeserializingMessageConverter;
66+
import org.springframework.amqp.support.converter.MessageConversionException;
6667
import org.springframework.amqp.support.converter.MessageConverter;
6768
import org.springframework.amqp.support.converter.SerializerMessageConverter;
6869
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
6970
import org.springframework.boot.autoconfigure.AutoConfigurations;
7071
import org.springframework.boot.autoconfigure.ssl.SslAutoConfiguration;
72+
import org.springframework.boot.context.properties.source.InvalidConfigurationPropertyValueException;
7173
import org.springframework.boot.test.context.FilteredClassLoader;
7274
import org.springframework.boot.test.context.assertj.AssertableApplicationContext;
7375
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
@@ -802,20 +804,27 @@ void customizeRequestedChannelMax() {
802804
});
803805
}
804806

805-
@SuppressWarnings("unchecked")
806807
@ParameterizedTest
807808
@ValueSource(classes = { TestConfiguration.class, TestConfiguration6.class })
809+
@SuppressWarnings("unchecked")
808810
void customizeAllowedListPatterns(Class<?> configuration) {
809811
this.contextRunner.withUserConfiguration(configuration)
810812
.withPropertyValues("spring.rabbitmq.template.allowed-list-patterns:*")
811813
.run((context) -> {
812814
MessageConverter messageConverter = context.getBean(RabbitTemplate.class).getMessageConverter();
813-
assertThat(messageConverter).isInstanceOfSatisfying(AllowedListDeserializingMessageConverter.class,
814-
(mc) -> assertThat(mc).extracting("allowedListPatterns")
815-
.isInstanceOfSatisfying(Collection.class, (set) -> assertThat(set).contains("*")));
815+
assertThat(messageConverter).extracting("allowedListPatterns")
816+
.isInstanceOfSatisfying(Collection.class, (set) -> assertThat(set).contains("*"));
816817
});
817818
}
818819

820+
@Test
821+
void customizeAllowedListPatternsWhenHasNoAllowedListDeserializingMessageConverter() {
822+
this.contextRunner.withUserConfiguration(CustomMessageConverterConfiguration.class)
823+
.withPropertyValues("spring.rabbitmq.template.allowed-list-patterns:*")
824+
.run((context) -> assertThat(context).getFailure()
825+
.hasRootCauseInstanceOf(InvalidConfigurationPropertyValueException.class));
826+
}
827+
819828
@Test
820829
void noSslByDefault() {
821830
this.contextRunner.withUserConfiguration(TestConfiguration.class).run((context) -> {
@@ -1417,6 +1426,29 @@ public List<Address> getAddresses() {
14171426

14181427
}
14191428

1429+
@Configuration
1430+
static class CustomMessageConverterConfiguration {
1431+
1432+
@Bean
1433+
MessageConverter messageConverter() {
1434+
return new MessageConverter() {
1435+
1436+
@Override
1437+
public Message toMessage(Object object, MessageProperties messageProperties)
1438+
throws MessageConversionException {
1439+
return new Message(object.toString().getBytes());
1440+
}
1441+
1442+
@Override
1443+
public Object fromMessage(Message message) throws MessageConversionException {
1444+
return new String(message.getBody());
1445+
}
1446+
1447+
};
1448+
}
1449+
1450+
}
1451+
14201452
static class TestListener {
14211453

14221454
@RabbitListener(queues = "test", autoStartup = "false")

0 commit comments

Comments
 (0)