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();
}
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"));
}
}
}
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();
}
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);
}
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);
}
Aggregations