Polish 'Add `spring.rabbitmq.template.allowed-list-patterns` property'

See gh-40421
This commit is contained in:
Phillip Webb 2024-04-18 12:46:30 -07:00
parent c329c5fe5b
commit d243d7eb50
2 changed files with 51 additions and 10 deletions

View File

@ -24,7 +24,9 @@ import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.AllowedListDeserializingMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.boot.context.properties.PropertyMapper;
import org.springframework.boot.context.properties.source.InvalidConfigurationPropertyValueException;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
/**
* Configure {@link RabbitTemplate} with sensible defaults.
@ -104,12 +106,19 @@ public class RabbitTemplateConfigurer {
map.from(templateProperties::getRoutingKey).to(template::setRoutingKey);
map.from(templateProperties::getDefaultReceiveQueue).whenNonNull().to(template::setDefaultReceiveQueue);
map.from(templateProperties::isObservationEnabled).to(template::setObservationEnabled);
if (templateProperties.getAllowedListPatterns() != null) {
MessageConverter messageConverter = template.getMessageConverter();
if (messageConverter instanceof AllowedListDeserializingMessageConverter mc) {
mc.setAllowedListPatterns(templateProperties.getAllowedListPatterns());
}
map.from(templateProperties::getAllowedListPatterns)
.whenNot(CollectionUtils::isEmpty)
.to((allowListPatterns) -> setAllowedListPatterns(template.getMessageConverter(), allowListPatterns));
}
private void setAllowedListPatterns(MessageConverter messageConverter, List<String> allowListPatterns) {
if (messageConverter instanceof AllowedListDeserializingMessageConverter allowedListDeserializingMessageConverter) {
allowedListDeserializingMessageConverter.setAllowedListPatterns(allowListPatterns);
return;
}
throw new InvalidConfigurationPropertyValueException("spring.rabbitmq.template.allow-list-patterns",
allowListPatterns,
"Allow list patterns can only be applied to a AllowedListDeserializingMessageConverter");
}
private boolean determineMandatoryFlag() {

View File

@ -43,6 +43,7 @@ import org.mockito.InOrder;
import org.springframework.amqp.core.AcknowledgeMode;
import org.springframework.amqp.core.AmqpAdmin;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.config.AbstractRabbitListenerContainerFactory;
@ -62,12 +63,13 @@ import org.springframework.amqp.rabbit.listener.DirectMessageListenerContainer;
import org.springframework.amqp.rabbit.listener.RabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.amqp.rabbit.retry.MessageRecoverer;
import org.springframework.amqp.support.converter.AllowedListDeserializingMessageConverter;
import org.springframework.amqp.support.converter.MessageConversionException;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.amqp.support.converter.SerializerMessageConverter;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.autoconfigure.ssl.SslAutoConfiguration;
import org.springframework.boot.context.properties.source.InvalidConfigurationPropertyValueException;
import org.springframework.boot.test.context.FilteredClassLoader;
import org.springframework.boot.test.context.assertj.AssertableApplicationContext;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
@ -802,20 +804,27 @@ class RabbitAutoConfigurationTests {
});
}
@SuppressWarnings("unchecked")
@ParameterizedTest
@ValueSource(classes = { TestConfiguration.class, TestConfiguration6.class })
@SuppressWarnings("unchecked")
void customizeAllowedListPatterns(Class<?> configuration) {
this.contextRunner.withUserConfiguration(configuration)
.withPropertyValues("spring.rabbitmq.template.allowed-list-patterns:*")
.run((context) -> {
MessageConverter messageConverter = context.getBean(RabbitTemplate.class).getMessageConverter();
assertThat(messageConverter).isInstanceOfSatisfying(AllowedListDeserializingMessageConverter.class,
(mc) -> assertThat(mc).extracting("allowedListPatterns")
.isInstanceOfSatisfying(Collection.class, (set) -> assertThat(set).contains("*")));
assertThat(messageConverter).extracting("allowedListPatterns")
.isInstanceOfSatisfying(Collection.class, (set) -> assertThat(set).contains("*"));
});
}
@Test
void customizeAllowedListPatternsWhenHasNoAllowedListDeserializingMessageConverter() {
this.contextRunner.withUserConfiguration(CustomMessageConverterConfiguration.class)
.withPropertyValues("spring.rabbitmq.template.allowed-list-patterns:*")
.run((context) -> assertThat(context).getFailure()
.hasRootCauseInstanceOf(InvalidConfigurationPropertyValueException.class));
}
@Test
void noSslByDefault() {
this.contextRunner.withUserConfiguration(TestConfiguration.class).run((context) -> {
@ -1417,6 +1426,29 @@ class RabbitAutoConfigurationTests {
}
@Configuration
static class CustomMessageConverterConfiguration {
@Bean
MessageConverter messageConverter() {
return new MessageConverter() {
@Override
public Message toMessage(Object object, MessageProperties messageProperties)
throws MessageConversionException {
return new Message(object.toString().getBytes());
}
@Override
public Object fromMessage(Message message) throws MessageConversionException {
return new String(message.getBody());
}
};
}
}
static class TestListener {
@RabbitListener(queues = "test", autoStartup = "false")