Search in sources :

Example 1 with ChannelInterceptorAware

use of org.springframework.integration.channel.ChannelInterceptorAware in project spring-integration by spring-projects.

the class IntegrationFlowDefinition method wireTap.

/**
 * Populate the {@code Wire Tap} EI Pattern specific
 * {@link org.springframework.messaging.support.ChannelInterceptor} implementation
 * to the current {@link #currentMessageChannel}.
 * <p> It is useful when an implicit {@link MessageChannel} is used between endpoints:
 * <pre class="code">
 * {@code
 *  .transform("payload")
 *  .wireTap(new WireTap(tapChannel().selector(m -> m.getPayload().equals("foo")))
 *  .channel("foo")
 * }
 * </pre>
 * This method can be used after any {@link #channel} for explicit {@link MessageChannel},
 * but with the caution do not impact existing {@link org.springframework.messaging.support.ChannelInterceptor}s.
 * @param wireTapSpec the {@link WireTapSpec} to use.
 * <p> When this EIP-method is used in the end of flow, it appends {@code nullChannel} to terminate flow properly,
 * Otherwise {@code Dispatcher has no subscribers} exception is thrown for implicit {@link DirectChannel}.
 * @return the current {@link IntegrationFlowDefinition}.
 */
public B wireTap(WireTapSpec wireTapSpec) {
    WireTap interceptor = wireTapSpec.get();
    if (this.currentMessageChannel == null || !(this.currentMessageChannel instanceof ChannelInterceptorAware)) {
        this.implicitChannel = true;
        channel(new DirectChannel());
    }
    addComponent(wireTapSpec);
    ((ChannelInterceptorAware) this.currentMessageChannel).addInterceptor(interceptor);
    return _this();
}
Also used : DirectChannel(org.springframework.integration.channel.DirectChannel) WireTap(org.springframework.integration.channel.interceptor.WireTap) ChannelInterceptorAware(org.springframework.integration.channel.ChannelInterceptorAware)

Example 2 with ChannelInterceptorAware

use of org.springframework.integration.channel.ChannelInterceptorAware in project spring-integration by spring-projects.

the class GlobalChannelInterceptorTests method validateGlobalInterceptor.

@Test
public void validateGlobalInterceptor() throws Exception {
    Map<String, ChannelInterceptorAware> channels = applicationContext.getBeansOfType(ChannelInterceptorAware.class);
    for (String channelName : channels.keySet()) {
        ChannelInterceptorAware channel = channels.get(channelName);
        if (channelName.equals("nullChannel")) {
            continue;
        }
        ChannelInterceptor[] interceptors = channel.getChannelInterceptors().toArray(new ChannelInterceptor[channel.getChannelInterceptors().size()]);
        if (channelName.equals("inputA")) {
            // 328741
            assertTrue(interceptors.length == 10);
            assertEquals("interceptor-three", interceptors[0].toString());
            assertEquals("interceptor-two", interceptors[1].toString());
            assertEquals("interceptor-eight", interceptors[2].toString());
            assertEquals("interceptor-seven", interceptors[3].toString());
            assertEquals("interceptor-five", interceptors[4].toString());
            assertEquals("interceptor-six", interceptors[5].toString());
            assertEquals("interceptor-ten", interceptors[6].toString());
            assertEquals("interceptor-eleven", interceptors[7].toString());
            assertEquals("interceptor-four", interceptors[8].toString());
            assertEquals("interceptor-one", interceptors[9].toString());
        } else if (channelName.equals("inputB")) {
            assertTrue(interceptors.length == 6);
            assertEquals("interceptor-three", interceptors[0].toString());
            assertEquals("interceptor-two", interceptors[1].toString());
            assertEquals("interceptor-ten", interceptors[2].toString());
            assertEquals("interceptor-eleven", interceptors[3].toString());
            assertEquals("interceptor-four", interceptors[4].toString());
            assertEquals("interceptor-one", interceptors[5].toString());
        } else if (channelName.equals("foo")) {
            assertTrue(interceptors.length == 6);
            assertEquals("interceptor-two", interceptors[0].toString());
            assertEquals("interceptor-five", interceptors[1].toString());
            assertEquals("interceptor-ten", interceptors[2].toString());
            assertEquals("interceptor-eleven", interceptors[3].toString());
            assertEquals("interceptor-four", interceptors[4].toString());
            assertEquals("interceptor-one", interceptors[5].toString());
        } else if (channelName.equals("bar")) {
            assertTrue(interceptors.length == 4);
            assertEquals("interceptor-eight", interceptors[0].toString());
            assertEquals("interceptor-seven", interceptors[1].toString());
            assertEquals("interceptor-ten", interceptors[2].toString());
            assertEquals("interceptor-eleven", interceptors[3].toString());
        } else if (channelName.equals("baz")) {
            assertTrue(interceptors.length == 2);
            assertEquals("interceptor-ten", interceptors[0].toString());
            assertEquals("interceptor-eleven", interceptors[1].toString());
        } else if (channelName.equals("inputWithProxy")) {
            assertTrue(interceptors.length == 6);
        } else if (channelName.equals("test")) {
            assertNotNull(interceptors);
            assertTrue(interceptors.length == 2);
            List<String> interceptorNames = new ArrayList<String>();
            for (ChannelInterceptor interceptor : interceptors) {
                interceptorNames.add(interceptor.toString());
            }
            assertTrue(interceptorNames.contains("interceptor-ten"));
            assertTrue(interceptorNames.contains("interceptor-eleven"));
        }
    }
}
Also used : ChannelInterceptor(org.springframework.messaging.support.ChannelInterceptor) ArrayList(java.util.ArrayList) ChannelInterceptorAware(org.springframework.integration.channel.ChannelInterceptorAware) Test(org.junit.Test)

Example 3 with ChannelInterceptorAware

use of org.springframework.integration.channel.ChannelInterceptorAware in project spring-integration by spring-projects.

the class ChannelInterceptorTests method testInterceptorBeanWithPNamespace.

@Test
public void testInterceptorBeanWithPNamespace() {
    ConfigurableApplicationContext ac = new ClassPathXmlApplicationContext("ChannelInterceptorTests-context.xml", ChannelInterceptorTests.class);
    ChannelInterceptorAware channel = ac.getBean("input", AbstractMessageChannel.class);
    List<ChannelInterceptor> interceptors = channel.getChannelInterceptors();
    ChannelInterceptor channelInterceptor = interceptors.get(0);
    assertThat(channelInterceptor, Matchers.instanceOf(PreSendReturnsMessageInterceptor.class));
    String foo = ((PreSendReturnsMessageInterceptor) channelInterceptor).getFoo();
    assertTrue(StringUtils.hasText(foo));
    assertEquals("foo", foo);
    ac.close();
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) ChannelInterceptor(org.springframework.messaging.support.ChannelInterceptor) ExecutorChannelInterceptor(org.springframework.messaging.support.ExecutorChannelInterceptor) ChannelInterceptorAware(org.springframework.integration.channel.ChannelInterceptorAware) Test(org.junit.Test)

Example 4 with ChannelInterceptorAware

use of org.springframework.integration.channel.ChannelInterceptorAware in project spring-integration by spring-projects.

the class GlobalChannelInterceptorProcessorTests method testProcessorWithInterceptorNotMatchingPattern.

@Test
public void testProcessorWithInterceptorNotMatchingPattern() {
    Map<String, GlobalChannelInterceptorWrapper> interceptors = new HashMap<>();
    Map<String, ChannelInterceptorAware> channels = new HashMap<>();
    ChannelInterceptor channelInterceptor = Mockito.mock(ChannelInterceptor.class);
    GlobalChannelInterceptorWrapper globalChannelInterceptorWrapper = new GlobalChannelInterceptorWrapper(channelInterceptor);
    ChannelInterceptorAware channel = Mockito.mock(ChannelInterceptorAware.class);
    globalChannelInterceptorWrapper.setPatterns(new String[] { "te*" });
    interceptors.put("Test-1", globalChannelInterceptorWrapper);
    channels.put("Test-1", channel);
    when(this.beanFactory.getBeansOfType(GlobalChannelInterceptorWrapper.class)).thenReturn(interceptors);
    when(this.beanFactory.getBeansOfType(ChannelInterceptorAware.class)).thenReturn(channels);
    this.globalChannelInterceptorProcessor.afterSingletonsInstantiated();
    verify(channel, Mockito.never()).addInterceptor(channelInterceptor);
}
Also used : HashMap(java.util.HashMap) ChannelInterceptor(org.springframework.messaging.support.ChannelInterceptor) GlobalChannelInterceptorWrapper(org.springframework.integration.channel.interceptor.GlobalChannelInterceptorWrapper) ChannelInterceptorAware(org.springframework.integration.channel.ChannelInterceptorAware) Test(org.junit.Test)

Example 5 with ChannelInterceptorAware

use of org.springframework.integration.channel.ChannelInterceptorAware in project spring-integration by spring-projects.

the class GlobalChannelInterceptorProcessorTests method testProcessorWithInterceptorDefaultPattern.

@Test
public void testProcessorWithInterceptorDefaultPattern() {
    Map<String, GlobalChannelInterceptorWrapper> interceptors = new HashMap<>();
    Map<String, ChannelInterceptorAware> channels = new HashMap<>();
    ChannelInterceptor channelInterceptor = Mockito.mock(ChannelInterceptor.class);
    GlobalChannelInterceptorWrapper globalChannelInterceptorWrapper = new GlobalChannelInterceptorWrapper(channelInterceptor);
    ChannelInterceptorAware channel = Mockito.mock(ChannelInterceptorAware.class);
    interceptors.put("Test-1", globalChannelInterceptorWrapper);
    channels.put("Test-1", channel);
    when(this.beanFactory.getBeansOfType(GlobalChannelInterceptorWrapper.class)).thenReturn(interceptors);
    when(this.beanFactory.getBeansOfType(ChannelInterceptorAware.class)).thenReturn(channels);
    this.globalChannelInterceptorProcessor.afterSingletonsInstantiated();
    verify(channel).addInterceptor(channelInterceptor);
}
Also used : HashMap(java.util.HashMap) ChannelInterceptor(org.springframework.messaging.support.ChannelInterceptor) GlobalChannelInterceptorWrapper(org.springframework.integration.channel.interceptor.GlobalChannelInterceptorWrapper) ChannelInterceptorAware(org.springframework.integration.channel.ChannelInterceptorAware) Test(org.junit.Test)

Aggregations

ChannelInterceptorAware (org.springframework.integration.channel.ChannelInterceptorAware)10 ChannelInterceptor (org.springframework.messaging.support.ChannelInterceptor)8 Test (org.junit.Test)7 GlobalChannelInterceptorWrapper (org.springframework.integration.channel.interceptor.GlobalChannelInterceptorWrapper)5 HashMap (java.util.HashMap)4 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)3 ConfigurableApplicationContext (org.springframework.context.ConfigurableApplicationContext)2 WireTap (org.springframework.integration.channel.interceptor.WireTap)2 ArrayList (java.util.ArrayList)1 Properties (java.util.Properties)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 DirectChannel (org.springframework.integration.channel.DirectChannel)1 IntegrationProperties (org.springframework.integration.context.IntegrationProperties)1 ExecutorChannelInterceptor (org.springframework.messaging.support.ExecutorChannelInterceptor)1