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