SimpleStanzaHandlerInterceptorChain implements StanzaHandlerInterceptorChain { @Override public void intercept(Stanza stanza, ServerRuntimeContext serverRuntimeContext, boolean isOutboundStanza, SessionContext sessionContext, SessionStateHolder sessionStateHolder, StanzaBroker stanzaBroker) throws ProtocolException { StanzaHandlerInterceptor interceptor = interceptors.poll(); if (interceptor == null) { LOG.debug("No more interceptor to execute. Executing stanza handler {}.", stanzaHandler); stanzaHandler.execute(stanza, serverRuntimeContext, isOutboundStanza, sessionContext, sessionStateHolder, stanzaBroker); return; } LOG.debug("Executing interceptor {}", interceptor); interceptor.intercept(stanza, serverRuntimeContext, isOutboundStanza, sessionContext, sessionStateHolder, stanzaBroker, this); } SimpleStanzaHandlerInterceptorChain(StanzaHandler stanzaHandler,
            List<StanzaHandlerInterceptor> interceptors); @Override void intercept(Stanza stanza, ServerRuntimeContext serverRuntimeContext, boolean isOutboundStanza,
            SessionContext sessionContext, SessionStateHolder sessionStateHolder, StanzaBroker stanzaBroker);  }
@Test public void test_no_interceptors() throws ProtocolException { new SimpleStanzaHandlerInterceptorChain(stanzaHandler, Collections.emptyList()).intercept(stanza, serverRuntimeContext, false, sessionContext, sessionStateHolder, firstStanzaBroker); verify(stanzaHandler).execute(stanza, serverRuntimeContext, false, sessionContext, sessionStateHolder, firstStanzaBroker); }
@Test public void test_two_interceptors() throws ProtocolException { List<StanzaHandlerInterceptor> interceptors = new ArrayList<>(); interceptors.add(new InterceptorMock()); interceptors.add(new InterceptorMock()); new SimpleStanzaHandlerInterceptorChain(stanzaHandler, interceptors).intercept(stanza, serverRuntimeContext, false, sessionContext, sessionStateHolder, firstStanzaBroker); verify(stanzaHandler).execute(stanza, serverRuntimeContext, false, sessionContext, sessionStateHolder, firstStanzaBroker); assertTrue(((InterceptorMock) interceptors.get(0)).intercepted); assertTrue(((InterceptorMock) interceptors.get(1)).intercepted); }
@Test public void test_stanzabroker_substitution_chaining() throws ProtocolException { StanzaBroker secondStanzaBroker = mock(StanzaBroker.class); StanzaBroker thirdStanzaBroker = mock(StanzaBroker.class); InterceptorMock firstInterceptor = new InterceptorMock().replaceStanzaBroker(secondStanzaBroker); InterceptorMock secondInterceptor = new InterceptorMock().replaceStanzaBroker(thirdStanzaBroker); List<StanzaHandlerInterceptor> interceptors = new ArrayList<>(); interceptors.add(firstInterceptor); interceptors.add(secondInterceptor); new SimpleStanzaHandlerInterceptorChain(stanzaHandler, interceptors).intercept(stanza, serverRuntimeContext, false, sessionContext, sessionStateHolder, firstStanzaBroker); assertEquals(firstStanzaBroker, firstInterceptor.receivedStanzaBroker); assertEquals(secondStanzaBroker, secondInterceptor.receivedStanzaBroker); verify(stanzaHandler).execute(stanza, serverRuntimeContext, false, sessionContext, sessionStateHolder, thirdStanzaBroker); }