Search in sources :

Example 11 with ChannelInterceptor

use of org.springframework.messaging.support.ChannelInterceptor in project spring-cloud-stream by spring-cloud.

the class DefaultPollableMessageSource method setSource.

public void setSource(MessageSource<?> source) {
    ProxyFactory pf = new ProxyFactory(source);
    class ReceiveAdvice implements MethodInterceptor {

        private final List<ChannelInterceptor> interceptors = new ArrayList<>();

        @Override
        public Object invoke(MethodInvocation invocation) throws Throwable {
            Object result = invocation.proceed();
            if (result instanceof Message) {
                Message<?> received = (Message<?>) result;
                for (ChannelInterceptor interceptor : this.interceptors) {
                    received = interceptor.preSend(received, null);
                    if (received == null) {
                        return null;
                    }
                }
                return received;
            }
            return result;
        }
    }
    final ReceiveAdvice advice = new ReceiveAdvice();
    advice.interceptors.addAll(this.interceptors);
    NameMatchMethodPointcutAdvisor sourceAdvisor = new NameMatchMethodPointcutAdvisor(advice);
    sourceAdvisor.addMethodName("receive");
    pf.addAdvisor(sourceAdvisor);
    this.source = (MessageSource<?>) pf.getProxy();
}
Also used : MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) Message(org.springframework.messaging.Message) ProxyFactory(org.springframework.aop.framework.ProxyFactory) ChannelInterceptor(org.springframework.messaging.support.ChannelInterceptor) NameMatchMethodPointcutAdvisor(org.springframework.aop.support.NameMatchMethodPointcutAdvisor) ArrayList(java.util.ArrayList) List(java.util.List) MethodInvocation(org.aopalliance.intercept.MethodInvocation)

Example 12 with ChannelInterceptor

use of org.springframework.messaging.support.ChannelInterceptor in project spring-framework by spring-projects.

the class TestValidator method testChannel.

private void testChannel(String channelName, List<Class<? extends MessageHandler>> subscriberTypes, int interceptorCount) {
    AbstractSubscribableChannel channel = this.appContext.getBean(channelName, AbstractSubscribableChannel.class);
    for (Class<? extends MessageHandler> subscriberType : subscriberTypes) {
        MessageHandler subscriber = this.appContext.getBean(subscriberType);
        assertThat(subscriber).as("No subscription for " + subscriberType).isNotNull();
        assertThat(channel.hasSubscription(subscriber)).isTrue();
    }
    List<ChannelInterceptor> interceptors = channel.getInterceptors();
    assertThat(interceptors.size()).isEqualTo(interceptorCount);
    assertThat(interceptors.get(interceptors.size() - 1).getClass()).isEqualTo(ImmutableMessageChannelInterceptor.class);
}
Also used : AbstractSubscribableChannel(org.springframework.messaging.support.AbstractSubscribableChannel) StompBrokerRelayMessageHandler(org.springframework.messaging.simp.stomp.StompBrokerRelayMessageHandler) MessageHandler(org.springframework.messaging.MessageHandler) SimpleBrokerMessageHandler(org.springframework.messaging.simp.broker.SimpleBrokerMessageHandler) UserRegistryMessageHandler(org.springframework.messaging.simp.user.UserRegistryMessageHandler) UserDestinationMessageHandler(org.springframework.messaging.simp.user.UserDestinationMessageHandler) SimpAnnotationMethodMessageHandler(org.springframework.messaging.simp.annotation.support.SimpAnnotationMethodMessageHandler) ChannelInterceptor(org.springframework.messaging.support.ChannelInterceptor) ImmutableMessageChannelInterceptor(org.springframework.messaging.support.ImmutableMessageChannelInterceptor)

Example 13 with ChannelInterceptor

use of org.springframework.messaging.support.ChannelInterceptor in project spring-framework by spring-projects.

the class WebSocketMessageBrokerConfigurationSupportTests method clientInboundChannelSendMessage.

@Test
void clientInboundChannelSendMessage() throws Exception {
    ApplicationContext context = createContext(TestChannelConfig.class, TestConfigurer.class);
    TestChannel channel = context.getBean("clientInboundChannel", TestChannel.class);
    SubProtocolWebSocketHandler webSocketHandler = context.getBean(SubProtocolWebSocketHandler.class);
    List<ChannelInterceptor> interceptors = channel.getInterceptors();
    assertThat(interceptors.get(interceptors.size() - 1).getClass()).isEqualTo(ImmutableMessageChannelInterceptor.class);
    TestWebSocketSession session = new TestWebSocketSession("s1");
    session.setOpen(true);
    webSocketHandler.afterConnectionEstablished(session);
    webSocketHandler.handleMessage(session, StompTextMessageBuilder.create(StompCommand.SEND).headers("destination:/foo").build());
    Message<?> message = channel.messages.get(0);
    StompHeaderAccessor accessor = StompHeaderAccessor.getAccessor(message, StompHeaderAccessor.class);
    assertThat(accessor).isNotNull();
    assertThat(accessor.isMutable()).isFalse();
    assertThat(accessor.getMessageType()).isEqualTo(SimpMessageType.MESSAGE);
    assertThat(accessor.getDestination()).isEqualTo("/foo");
}
Also used : ApplicationContext(org.springframework.context.ApplicationContext) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) TestWebSocketSession(org.springframework.web.socket.handler.TestWebSocketSession) SubProtocolWebSocketHandler(org.springframework.web.socket.messaging.SubProtocolWebSocketHandler) ChannelInterceptor(org.springframework.messaging.support.ChannelInterceptor) ImmutableMessageChannelInterceptor(org.springframework.messaging.support.ImmutableMessageChannelInterceptor) StompHeaderAccessor(org.springframework.messaging.simp.stomp.StompHeaderAccessor) Test(org.junit.jupiter.api.Test)

Example 14 with ChannelInterceptor

use of org.springframework.messaging.support.ChannelInterceptor in project spring-framework by spring-projects.

the class WebSocketMessageBrokerConfigurationSupportTests method clientOutboundChannel.

@Test
void clientOutboundChannel() {
    ApplicationContext context = createContext(TestChannelConfig.class, TestConfigurer.class);
    TestChannel channel = context.getBean("clientOutboundChannel", TestChannel.class);
    Set<MessageHandler> handlers = channel.getSubscribers();
    List<ChannelInterceptor> interceptors = channel.getInterceptors();
    assertThat(interceptors.get(interceptors.size() - 1).getClass()).isEqualTo(ImmutableMessageChannelInterceptor.class);
    assertThat(handlers).hasSize(1);
    assertThat(handlers).contains(context.getBean(SubProtocolWebSocketHandler.class));
}
Also used : ApplicationContext(org.springframework.context.ApplicationContext) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) SimpleBrokerMessageHandler(org.springframework.messaging.simp.broker.SimpleBrokerMessageHandler) UserDestinationMessageHandler(org.springframework.messaging.simp.user.UserDestinationMessageHandler) MessageHandler(org.springframework.messaging.MessageHandler) SubProtocolWebSocketHandler(org.springframework.web.socket.messaging.SubProtocolWebSocketHandler) ChannelInterceptor(org.springframework.messaging.support.ChannelInterceptor) ImmutableMessageChannelInterceptor(org.springframework.messaging.support.ImmutableMessageChannelInterceptor) Test(org.junit.jupiter.api.Test)

Example 15 with ChannelInterceptor

use of org.springframework.messaging.support.ChannelInterceptor in project spring-cloud-stream by spring-cloud.

the class CustomPartitionedProducerTest method testCustomPartitionedProducerByName.

@Test
public void testCustomPartitionedProducerByName() {
    ApplicationContext context = SpringApplication.run(CustomPartitionedProducerTest.TestSource.class, "--spring.jmx.enabled=false", "--spring.main.web-application-type=none", "--spring.cloud.stream.bindings.output.producer.partitionKeyExtractorName=customPartitionKeyExtractor", "--spring.cloud.stream.bindings.output.producer.partitionSelectorName=customPartitionSelector");
    Source testSource = context.getBean(Source.class);
    DirectChannel messageChannel = (DirectChannel) testSource.output();
    for (ChannelInterceptor channelInterceptor : messageChannel.getChannelInterceptors()) {
        if (channelInterceptor instanceof MessageConverterConfigurer.PartitioningInterceptor) {
            Field partitionHandlerField = ReflectionUtils.findField(MessageConverterConfigurer.PartitioningInterceptor.class, "partitionHandler");
            ReflectionUtils.makeAccessible(partitionHandlerField);
            PartitionHandler partitionHandler = (PartitionHandler) ReflectionUtils.getField(partitionHandlerField, channelInterceptor);
            Field partitonKeyExtractorField = ReflectionUtils.findField(PartitionHandler.class, "partitionKeyExtractorStrategy");
            ReflectionUtils.makeAccessible(partitonKeyExtractorField);
            Field partitonSelectorField = ReflectionUtils.findField(PartitionHandler.class, "partitionSelectorStrategy");
            ReflectionUtils.makeAccessible(partitonSelectorField);
            Assert.assertTrue(((PartitionKeyExtractorStrategy) ReflectionUtils.getField(partitonKeyExtractorField, partitionHandler)).getClass().equals(CustomPartitionKeyExtractorClass.class));
            Assert.assertTrue(((PartitionSelectorStrategy) ReflectionUtils.getField(partitonSelectorField, partitionHandler)).getClass().equals(CustomPartitionSelectorClass.class));
        }
    }
}
Also used : PartitionSelectorStrategy(org.springframework.cloud.stream.binder.PartitionSelectorStrategy) DirectChannel(org.springframework.integration.channel.DirectChannel) ChannelInterceptor(org.springframework.messaging.support.ChannelInterceptor) PartitionHandler(org.springframework.cloud.stream.binder.PartitionHandler) CustomPartitionSelectorClass(org.springframework.cloud.stream.partitioning.CustomPartitionSelectorClass) PropertySource(org.springframework.context.annotation.PropertySource) MessageSource(org.springframework.integration.core.MessageSource) Source(org.springframework.cloud.stream.messaging.Source) Field(java.lang.reflect.Field) PartitionKeyExtractorStrategy(org.springframework.cloud.stream.binder.PartitionKeyExtractorStrategy) CustomPartitionKeyExtractorClass(org.springframework.cloud.stream.partitioning.CustomPartitionKeyExtractorClass) ApplicationContext(org.springframework.context.ApplicationContext) Test(org.junit.Test)

Aggregations

ChannelInterceptor (org.springframework.messaging.support.ChannelInterceptor)30 Test (org.junit.Test)18 ChannelInterceptorAware (org.springframework.integration.channel.ChannelInterceptorAware)8 ApplicationContext (org.springframework.context.ApplicationContext)7 ArrayList (java.util.ArrayList)6 ImmutableMessageChannelInterceptor (org.springframework.messaging.support.ImmutableMessageChannelInterceptor)6 Test (org.junit.jupiter.api.Test)5 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)5 DirectChannel (org.springframework.integration.channel.DirectChannel)5 GlobalChannelInterceptorWrapper (org.springframework.integration.channel.interceptor.GlobalChannelInterceptorWrapper)5 Message (org.springframework.messaging.Message)5 Field (java.lang.reflect.Field)4 HashMap (java.util.HashMap)4 PartitionHandler (org.springframework.cloud.stream.binder.PartitionHandler)4 PartitionKeyExtractorStrategy (org.springframework.cloud.stream.binder.PartitionKeyExtractorStrategy)4 PartitionSelectorStrategy (org.springframework.cloud.stream.binder.PartitionSelectorStrategy)4 Source (org.springframework.cloud.stream.messaging.Source)4 CustomPartitionKeyExtractorClass (org.springframework.cloud.stream.partitioning.CustomPartitionKeyExtractorClass)4 CustomPartitionSelectorClass (org.springframework.cloud.stream.partitioning.CustomPartitionSelectorClass)4 PropertySource (org.springframework.context.annotation.PropertySource)4