Search in sources :

Example 1 with ImmutableMessageChannelInterceptor

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

the class AbstractMessageBrokerConfiguration method brokerChannel.

@Bean
public AbstractSubscribableChannel brokerChannel() {
    ChannelRegistration reg = getBrokerRegistry().getBrokerChannelRegistration();
    ExecutorSubscribableChannel channel = reg.hasTaskExecutor() ? new ExecutorSubscribableChannel(brokerChannelExecutor()) : new ExecutorSubscribableChannel();
    reg.setInterceptors(new ImmutableMessageChannelInterceptor());
    channel.setInterceptors(reg.getInterceptors());
    return channel;
}
Also used : ExecutorSubscribableChannel(org.springframework.messaging.support.ExecutorSubscribableChannel) ImmutableMessageChannelInterceptor(org.springframework.messaging.support.ImmutableMessageChannelInterceptor) Bean(org.springframework.context.annotation.Bean)

Example 2 with ImmutableMessageChannelInterceptor

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

the class BinderAwareChannelResolverTests method resolveChannel.

@Test
public void resolveChannel() {
    Map<String, Bindable> bindables = context.getBeansOfType(Bindable.class);
    assertThat(bindables).hasSize(1);
    for (Bindable bindable : bindables.values()) {
        // producer
        assertEquals(0, bindable.getInputs().size());
        // consumer
        assertEquals(0, bindable.getOutputs().size());
    }
    MessageChannel registered = resolver.resolveDestination("foo");
    assertEquals(2, ((AbstractMessageChannel) registered).getChannelInterceptors().size());
    assertTrue(((AbstractMessageChannel) registered).getChannelInterceptors().get(1) instanceof ImmutableMessageChannelInterceptor);
    bindables = context.getBeansOfType(Bindable.class);
    assertThat(bindables).hasSize(1);
    for (Bindable bindable : bindables.values()) {
        // producer
        assertEquals(0, bindable.getInputs().size());
        // consumer
        assertEquals(1, bindable.getOutputs().size());
    }
    DirectChannel testChannel = new DirectChannel();
    testChannel.setComponentName("INPUT");
    final CountDownLatch latch = new CountDownLatch(1);
    final List<Message<?>> received = new ArrayList<>();
    testChannel.subscribe(new MessageHandler() {

        @Override
        public void handleMessage(Message<?> message) throws MessagingException {
            received.add(message);
            latch.countDown();
        }
    });
    this.binder.bindConsumer("foo", null, testChannel, new ConsumerProperties());
    assertThat(received).hasSize(0);
    registered.send(MessageBuilder.withPayload("hello").build());
    try {
        assertThat(latch.await(1, TimeUnit.SECONDS)).describedAs("Latch timed out");
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        fail("interrupted while awaiting latch");
    }
    assertThat(received).hasSize(1);
    assertThat(new String((byte[]) received.get(0).getPayload())).isEqualTo("hello");
    this.context.close();
    for (Bindable bindable : bindables.values()) {
        assertEquals(0, bindable.getInputs().size());
        // Must not be bound"
        assertEquals(0, bindable.getOutputs().size());
    }
}
Also used : AbstractMessageChannel(org.springframework.integration.channel.AbstractMessageChannel) Message(org.springframework.messaging.Message) MessageHandler(org.springframework.messaging.MessageHandler) DirectChannel(org.springframework.integration.channel.DirectChannel) MessagingException(org.springframework.messaging.MessagingException) ImmutableMessageChannelInterceptor(org.springframework.messaging.support.ImmutableMessageChannelInterceptor) Bindable(org.springframework.cloud.stream.binding.Bindable) DynamicDestinationsBindable(org.springframework.cloud.stream.binding.DynamicDestinationsBindable) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) AbstractMessageChannel(org.springframework.integration.channel.AbstractMessageChannel) MessageChannel(org.springframework.messaging.MessageChannel) Test(org.junit.Test)

Example 3 with ImmutableMessageChannelInterceptor

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

the class StompSubProtocolHandlerTests method handleMessageFromClientWithTokenAuthentication.

// SPR-14690
@Test
public void handleMessageFromClientWithTokenAuthentication() {
    ExecutorSubscribableChannel channel = new ExecutorSubscribableChannel();
    channel.addInterceptor(new AuthenticationInterceptor("__pete__@gmail.com"));
    channel.addInterceptor(new ImmutableMessageChannelInterceptor());
    TestMessageHandler messageHandler = new TestMessageHandler();
    channel.subscribe(messageHandler);
    StompSubProtocolHandler handler = new StompSubProtocolHandler();
    handler.afterSessionStarted(this.session, channel);
    TextMessage wsMessage = StompTextMessageBuilder.create(StompCommand.CONNECT).build();
    handler.handleMessageFromClient(this.session, wsMessage, channel);
    assertThat(messageHandler.getMessages().size()).isEqualTo(1);
    Message<?> message = messageHandler.getMessages().get(0);
    Principal user = SimpMessageHeaderAccessor.getUser(message.getHeaders());
    assertThat(user).isNotNull();
    assertThat(user.getName()).isEqualTo("__pete__@gmail.com");
    StompHeaderAccessor accessor = StompHeaderAccessor.create(StompCommand.CONNECTED);
    message = MessageBuilder.createMessage(EMPTY_PAYLOAD, accessor.getMessageHeaders());
    handler.handleMessageToClient(this.session, message);
    assertThat(this.session.getSentMessages()).hasSize(1);
    WebSocketMessage<?> textMessage = this.session.getSentMessages().get(0);
    assertThat(textMessage.getPayload()).isEqualTo("CONNECTED\n" + "user-name:__pete__@gmail.com\n" + "\n" + "\u0000");
}
Also used : ExecutorSubscribableChannel(org.springframework.messaging.support.ExecutorSubscribableChannel) ImmutableMessageChannelInterceptor(org.springframework.messaging.support.ImmutableMessageChannelInterceptor) StompHeaderAccessor(org.springframework.messaging.simp.stomp.StompHeaderAccessor) TextMessage(org.springframework.web.socket.TextMessage) TestPrincipal(org.springframework.core.testfixture.security.TestPrincipal) Principal(java.security.Principal) Test(org.junit.jupiter.api.Test)

Example 4 with ImmutableMessageChannelInterceptor

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

the class AbstractMessageBrokerConfiguration method brokerChannel.

@Bean
public AbstractSubscribableChannel brokerChannel(AbstractSubscribableChannel clientInboundChannel, AbstractSubscribableChannel clientOutboundChannel, TaskExecutor brokerChannelExecutor) {
    MessageBrokerRegistry registry = getBrokerRegistry(clientInboundChannel, clientOutboundChannel);
    ChannelRegistration registration = registry.getBrokerChannelRegistration();
    ExecutorSubscribableChannel channel = (registration.hasTaskExecutor() ? new ExecutorSubscribableChannel(brokerChannelExecutor) : new ExecutorSubscribableChannel());
    registration.interceptors(new ImmutableMessageChannelInterceptor());
    channel.setLogger(SimpLogging.forLog(channel.getLogger()));
    channel.setInterceptors(registration.getInterceptors());
    return channel;
}
Also used : ExecutorSubscribableChannel(org.springframework.messaging.support.ExecutorSubscribableChannel) ImmutableMessageChannelInterceptor(org.springframework.messaging.support.ImmutableMessageChannelInterceptor) Bean(org.springframework.context.annotation.Bean)

Example 5 with ImmutableMessageChannelInterceptor

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

the class AbstractMessageBrokerConfiguration method getClientOutboundChannelRegistration.

protected final ChannelRegistration getClientOutboundChannelRegistration() {
    if (this.clientOutboundChannelRegistration == null) {
        ChannelRegistration registration = new ChannelRegistration();
        configureClientOutboundChannel(registration);
        registration.interceptors(new ImmutableMessageChannelInterceptor());
        this.clientOutboundChannelRegistration = registration;
    }
    return this.clientOutboundChannelRegistration;
}
Also used : ImmutableMessageChannelInterceptor(org.springframework.messaging.support.ImmutableMessageChannelInterceptor)

Aggregations

ImmutableMessageChannelInterceptor (org.springframework.messaging.support.ImmutableMessageChannelInterceptor)8 ExecutorSubscribableChannel (org.springframework.messaging.support.ExecutorSubscribableChannel)4 Test (org.junit.jupiter.api.Test)2 Bean (org.springframework.context.annotation.Bean)2 Message (org.springframework.messaging.Message)2 MessageChannel (org.springframework.messaging.MessageChannel)2 TextMessage (org.springframework.web.socket.TextMessage)2 Principal (java.security.Principal)1 ArrayList (java.util.ArrayList)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Test (org.junit.Test)1 ConstructorArgumentValues (org.springframework.beans.factory.config.ConstructorArgumentValues)1 RuntimeBeanReference (org.springframework.beans.factory.config.RuntimeBeanReference)1 ManagedList (org.springframework.beans.factory.support.ManagedList)1 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)1 Bindable (org.springframework.cloud.stream.binding.Bindable)1 DynamicDestinationsBindable (org.springframework.cloud.stream.binding.DynamicDestinationsBindable)1 TestPrincipal (org.springframework.core.testfixture.security.TestPrincipal)1 AbstractMessageChannel (org.springframework.integration.channel.AbstractMessageChannel)1