AmbariDelegatingAuthenticationFilter implements Filter { @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) throws IOException, ServletException { boolean handled = false; HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest; for (AmbariAuthenticationFilter filter : filters) { if (LOG.isTraceEnabled()) { LOG.trace("Attempting to apply authentication filter {}", filter.getClass().getName()); } if (filter.shouldApply(httpServletRequest)) { if (LOG.isDebugEnabled()) { LOG.debug("Using authentication filter {} since it applies", filter.getClass().getName()); } filter.doFilter(servletRequest, servletResponse, chain); handled = true; break; } else { if (LOG.isDebugEnabled()) { LOG.debug("Filter {} does not apply skipping", filter.getClass().getName()); } } } if (!handled) { LOG.debug("No delegated filters applied while attempting to authenticate a user, continuing with the filter chain."); chain.doFilter(servletRequest, servletResponse); } } AmbariDelegatingAuthenticationFilter(Collection<AmbariAuthenticationFilter> filters); @Override void init(FilterConfig filterConfig); @Override void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain); @Override void destroy();  }
@Test public void testDoFilterNoneApply() throws Exception { HttpServletRequest httpServletRequest = createMock(HttpServletRequest.class); HttpServletResponse httpServletResponse = createMock(HttpServletResponse.class); AmbariAuthenticationFilter filter1 = createMock(AmbariAuthenticationFilter.class); expect(filter1.shouldApply(httpServletRequest)).andReturn(false).once(); AmbariAuthenticationFilter filter2 = createMock(AmbariAuthenticationFilter.class); expect(filter2.shouldApply(httpServletRequest)).andReturn(false).once(); AmbariAuthenticationFilter filter3 = createMock(AmbariAuthenticationFilter.class); expect(filter3.shouldApply(httpServletRequest)).andReturn(false).once(); FilterChain filterChain = createMock(FilterChain.class); filterChain.doFilter(httpServletRequest, httpServletResponse); expectLastCall().once(); replayAll(); Filter filter = new AmbariDelegatingAuthenticationFilter(Arrays.asList(filter1, filter2, filter3)); filter.doFilter(httpServletRequest, httpServletResponse, filterChain); verifyAll(); }
@Test public void testDoFilterFirstApplies() throws Exception { HttpServletRequest httpServletRequest = createMock(HttpServletRequest.class); HttpServletResponse httpServletResponse = createMock(HttpServletResponse.class); FilterChain filterChain = createMock(FilterChain.class); AmbariAuthenticationFilter filter1 = createMock(AmbariAuthenticationFilter.class); expect(filter1.shouldApply(httpServletRequest)).andReturn(true).once(); filter1.doFilter(httpServletRequest, httpServletResponse, filterChain); expectLastCall().once(); AmbariAuthenticationFilter filter2 = createMock(AmbariAuthenticationFilter.class); AmbariAuthenticationFilter filter3 = createMock(AmbariAuthenticationFilter.class); replayAll(); Filter filter = new AmbariDelegatingAuthenticationFilter(Arrays.asList(filter1, filter2, filter3)); filter.doFilter(httpServletRequest, httpServletResponse, filterChain); verifyAll(); }
@Test public void testDoFilterLastApplies() throws Exception { HttpServletRequest httpServletRequest = createMock(HttpServletRequest.class); HttpServletResponse httpServletResponse = createMock(HttpServletResponse.class); FilterChain filterChain = createMock(FilterChain.class); AmbariAuthenticationFilter filter1 = createMock(AmbariAuthenticationFilter.class); expect(filter1.shouldApply(httpServletRequest)).andReturn(false).once(); AmbariAuthenticationFilter filter2 = createMock(AmbariAuthenticationFilter.class); expect(filter2.shouldApply(httpServletRequest)).andReturn(false).once(); AmbariAuthenticationFilter filter3 = createMock(AmbariAuthenticationFilter.class); expect(filter3.shouldApply(httpServletRequest)).andReturn(true).once(); filter3.doFilter(httpServletRequest, httpServletResponse, filterChain); expectLastCall().once(); replayAll(); Filter filter = new AmbariDelegatingAuthenticationFilter(Arrays.asList(filter1, filter2, filter3)); filter.doFilter(httpServletRequest, httpServletResponse, filterChain); verifyAll(); }
@Test public void testDoFilterNthApplies() throws Exception { HttpServletRequest httpServletRequest = createMock(HttpServletRequest.class); HttpServletResponse httpServletResponse = createMock(HttpServletResponse.class); FilterChain filterChain = createMock(FilterChain.class); AmbariAuthenticationFilter filter1 = createMock(AmbariAuthenticationFilter.class); expect(filter1.shouldApply(httpServletRequest)).andReturn(false).once(); AmbariAuthenticationFilter filter2 = createMock(AmbariAuthenticationFilter.class); expect(filter2.shouldApply(httpServletRequest)).andReturn(false).once(); AmbariAuthenticationFilter filterN = createMock(AmbariAuthenticationFilter.class); expect(filterN.shouldApply(httpServletRequest)).andReturn(true).once(); filterN.doFilter(httpServletRequest, httpServletResponse, filterChain); expectLastCall().once(); AmbariAuthenticationFilter filter3 = createMock(AmbariAuthenticationFilter.class); replayAll(); Filter filter = new AmbariDelegatingAuthenticationFilter(Arrays.asList(filter1, filter2, filterN, filter3)); filter.doFilter(httpServletRequest, httpServletResponse, filterChain); verifyAll(); }