Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
import java.util.stream.Stream;
Expand Down Expand Up @@ -125,6 +126,7 @@ protected void detectAnnotationsAndRegisterEndpoints(Object bean, Class<?> targe
}
annotatedMethods.entrySet().stream()
.map(entry -> createAndConfigureEndpoint(bean, entry.getKey(), entry.getValue()))
.filter(Objects::nonNull)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not filter the endpoints here, before the map method?

.forEach(this.endpointRegistrar::registerEndpoint);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

import org.springframework.messaging.converter.MessageConverter;
import org.springframework.messaging.handler.invocation.HandlerMethodArgumentResolver;

Expand All @@ -44,12 +47,23 @@ public class SqsListenerAnnotationBeanPostProcessor extends AbstractListenerAnno

private static final String GENERATED_ID_PREFIX = "io.awspring.cloud.sqs.sqsListenerEndpointContainer#";

private final List<SqsListenerFilter> filters;

public SqsListenerAnnotationBeanPostProcessor(Optional<List<SqsListenerFilter>> filters) {
this.filters = filters.orElseGet(Collections::emptyList);
}

@Override
protected Class<SqsListener> getAnnotationClass() {
return SqsListener.class;
}

@Override
protected Endpoint createEndpoint(SqsListener sqsListenerAnnotation) {
if (filters.stream().anyMatch(f -> !f.createEndpoint(sqsListenerAnnotation))) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this logic would be better placed in the AbstractListenerAnnotationBeanPostProcessor, since it's not an SQS-specific logic.

return null;
}

return SqsEndpoint.builder().queueNames(resolveEndpointNames(sqsListenerAnnotation.value()))
.factoryBeanName(resolveAsString(sqsListenerAnnotation.factory(), "factory"))
.id(getEndpointId(sqsListenerAnnotation.id()))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package io.awspring.cloud.sqs.annotation;

/**
* Predicate interface to filter {@link SqsListener} annotations during bean post-processing.
*/
@FunctionalInterface
public interface SqsListenerFilter {

boolean createEndpoint(SqsListener annotation);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps shouldCreateEndpoint?

}
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,9 @@ public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
* @param endpoint the endpoint.
*/
public void registerEndpoint(Endpoint endpoint) {
this.endpoints.add(endpoint);
if (endpoint != null) {
this.endpoints.add(endpoint);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class SqsBootstrapConfiguration implements ImportBeanDefinitionRegistrar
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
if (!registry.containsBeanDefinition(SqsBeanNames.SQS_LISTENER_ANNOTATION_BEAN_POST_PROCESSOR_BEAN_NAME)) {
registry.registerBeanDefinition(SqsBeanNames.SQS_LISTENER_ANNOTATION_BEAN_POST_PROCESSOR_BEAN_NAME,
new RootBeanDefinition(SqsListenerAnnotationBeanPostProcessor.class));
new RootBeanDefinition(SqsListenerAnnotationBeanPostProcessor.class, RootBeanDefinition.AUTOWIRE_BY_TYPE, true));
}

if (!registry.containsBeanDefinition(SqsBeanNames.ENDPOINT_REGISTRY_BEAN_NAME)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.InstanceOfAssertFactories.list;
import static org.assertj.core.api.InstanceOfAssertFactories.type;
import static org.mockito.ArgumentMatchers.isNotNull;
import static org.mockito.BDDMockito.given;
import static org.mockito.BDDMockito.then;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.when;

import com.fasterxml.jackson.databind.ObjectMapper;
Expand All @@ -37,6 +40,8 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.springframework.beans.factory.ListableBeanFactory;
Expand Down Expand Up @@ -100,7 +105,7 @@ public void registerEndpoint(Endpoint endpoint) {
super.registerEndpoint(endpoint);
}
};
SqsListenerAnnotationBeanPostProcessor processor = new SqsListenerAnnotationBeanPostProcessor() {
SqsListenerAnnotationBeanPostProcessor processor = new SqsListenerAnnotationBeanPostProcessor(Optional.empty()) {
@Override
protected EndpointRegistrar createEndpointRegistrar() {
return registrar;
Expand Down Expand Up @@ -150,7 +155,7 @@ void shouldChangeListenerRegistryBeanName() {

EndpointRegistrar registrar = new EndpointRegistrar();

SqsListenerAnnotationBeanPostProcessor processor = new SqsListenerAnnotationBeanPostProcessor() {
SqsListenerAnnotationBeanPostProcessor processor = new SqsListenerAnnotationBeanPostProcessor(Optional.empty()) {
@Override
protected EndpointRegistrar createEndpointRegistrar() {
return registrar;
Expand Down Expand Up @@ -183,7 +188,7 @@ void shouldThrowIfFactoryBeanNotFound() {
when(beanFactory.containsBean(EndpointRegistrar.DEFAULT_LISTENER_CONTAINER_FACTORY_BEAN_NAME))
.thenReturn(false);

SqsListenerAnnotationBeanPostProcessor processor = new SqsListenerAnnotationBeanPostProcessor();
SqsListenerAnnotationBeanPostProcessor processor = new SqsListenerAnnotationBeanPostProcessor(Optional.empty());

Listener bean = new Listener();
StringValueResolver valueResolver = mock(StringValueResolver.class);
Expand All @@ -206,7 +211,7 @@ void shouldResolveListOfQueuesFromSPEL() {
SqsQueueNameReader sqsQueueNameReader = new SqsQueueNameReader();
beanFactory.registerSingleton("sqsQueueNameReader", sqsQueueNameReader);

SqsListenerAnnotationBeanPostProcessor processor = new SqsListenerAnnotationBeanPostProcessor();
SqsListenerAnnotationBeanPostProcessor processor = new SqsListenerAnnotationBeanPostProcessor(Optional.empty());

ManyQueuesListener bean = new ManyQueuesListener(sqsQueueNameReader);
processor.setBeanFactory(beanFactory);
Expand All @@ -222,6 +227,45 @@ void shouldResolveListOfQueuesFromSPEL() {

}

@Test
void shouldApplyFiltersThatPrevent() {
EndpointRegistrar registrar = mock(EndpointRegistrar.class);
List<SqsListenerFilter> filters = List.of(
annotation -> true, // Passes all annotations
annotation -> false // Denies all annotations
);

SqsListenerAnnotationBeanPostProcessor processor = new SqsListenerAnnotationBeanPostProcessor(Optional.of(filters)) {
@Override
protected EndpointRegistrar createEndpointRegistrar() {
return registrar;
}
};

Listener bean = new Listener();
processor.postProcessAfterInitialization(bean, "listener");

verifyNoInteractions(registrar);
}

@Test
void shouldApplyFiltersThatAllow() {
EndpointRegistrar registrar = mock(EndpointRegistrar.class);
List<SqsListenerFilter> filters = List.of(annotation -> true); // Passes all annotations

SqsListenerAnnotationBeanPostProcessor processor = new SqsListenerAnnotationBeanPostProcessor(Optional.of(filters)) {
@Override
protected EndpointRegistrar createEndpointRegistrar() {
return registrar;
}
};

Listener bean = new Listener();
processor.postProcessAfterInitialization(bean, "listener");

verify(registrar).registerEndpoint(isNotNull());
}

static class Listener {

@SqsListener("myQueue")
Expand Down