Search in sources :

Example 1 with GlobalChannelInterceptorWrapper

use of org.springframework.integration.channel.interceptor.GlobalChannelInterceptorWrapper 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 2 with GlobalChannelInterceptorWrapper

use of org.springframework.integration.channel.interceptor.GlobalChannelInterceptorWrapper 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)

Example 3 with GlobalChannelInterceptorWrapper

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

the class GlobalChannelInterceptorProcessorTests method testProcessorWithInterceptorMatchingPattern.

@Test
public void testProcessorWithInterceptorMatchingPattern() {
    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).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 4 with GlobalChannelInterceptorWrapper

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

the class GlobalChannelInterceptorProcessorTests method testProcessorWithInterceptorMatchingNegativePattern.

@Test
public void testProcessorWithInterceptorMatchingNegativePattern() {
    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*", "!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 GlobalChannelInterceptorWrapper

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

the class GlobalChannelInterceptorProcessor method addMatchingInterceptors.

/**
 * Add any interceptor whose pattern matches against the channel's name.
 * @param channel the message channel to add interceptors.
 * @param beanName the message channel bean name to match the pattern.
 */
public void addMatchingInterceptors(ChannelInterceptorAware channel, String beanName) {
    if (logger.isDebugEnabled()) {
        logger.debug("Applying global interceptors on channel '" + beanName + "'");
    }
    List<GlobalChannelInterceptorWrapper> tempInterceptors = new ArrayList<>();
    for (GlobalChannelInterceptorWrapper globalChannelInterceptorWrapper : this.positiveOrderInterceptors) {
        String[] patterns = globalChannelInterceptorWrapper.getPatterns();
        patterns = StringUtils.trimArrayElements(patterns);
        if (beanName != null && Boolean.TRUE.equals(PatternMatchUtils.smartMatch(beanName, patterns))) {
            tempInterceptors.add(globalChannelInterceptorWrapper);
        }
    }
    Collections.sort(tempInterceptors, this.comparator);
    for (GlobalChannelInterceptorWrapper next : tempInterceptors) {
        ChannelInterceptor channelInterceptor = next.getChannelInterceptor();
        if (!(channelInterceptor instanceof VetoCapableInterceptor) || ((VetoCapableInterceptor) channelInterceptor).shouldIntercept(beanName, channel)) {
            channel.addInterceptor(channelInterceptor);
        }
    }
    tempInterceptors.clear();
    for (GlobalChannelInterceptorWrapper globalChannelInterceptorWrapper : this.negativeOrderInterceptors) {
        String[] patterns = globalChannelInterceptorWrapper.getPatterns();
        patterns = StringUtils.trimArrayElements(patterns);
        if (beanName != null && Boolean.TRUE.equals(PatternMatchUtils.smartMatch(beanName, patterns))) {
            tempInterceptors.add(globalChannelInterceptorWrapper);
        }
    }
    Collections.sort(tempInterceptors, this.comparator);
    if (!tempInterceptors.isEmpty()) {
        for (int i = tempInterceptors.size() - 1; i >= 0; i--) {
            ChannelInterceptor channelInterceptor = tempInterceptors.get(i).getChannelInterceptor();
            if (!(channelInterceptor instanceof VetoCapableInterceptor) || ((VetoCapableInterceptor) channelInterceptor).shouldIntercept(beanName, channel)) {
                channel.addInterceptor(0, channelInterceptor);
            }
        }
    }
}
Also used : VetoCapableInterceptor(org.springframework.integration.channel.interceptor.VetoCapableInterceptor) ChannelInterceptor(org.springframework.messaging.support.ChannelInterceptor) ArrayList(java.util.ArrayList) GlobalChannelInterceptorWrapper(org.springframework.integration.channel.interceptor.GlobalChannelInterceptorWrapper)

Aggregations

GlobalChannelInterceptorWrapper (org.springframework.integration.channel.interceptor.GlobalChannelInterceptorWrapper)6 ChannelInterceptorAware (org.springframework.integration.channel.ChannelInterceptorAware)5 ChannelInterceptor (org.springframework.messaging.support.ChannelInterceptor)5 HashMap (java.util.HashMap)4 Test (org.junit.Test)4 ArrayList (java.util.ArrayList)1 Properties (java.util.Properties)1 VetoCapableInterceptor (org.springframework.integration.channel.interceptor.VetoCapableInterceptor)1 IntegrationProperties (org.springframework.integration.context.IntegrationProperties)1