@Test public void testFilterChain() throws Exception { ServiceReference filterReference = EasyMock.createNiceMock(ServiceReference.class); EasyMock.replay(filterReference); ServiceReference[] serviceReferences = {filterReference, filterReference}; BundleContext bundleContext = EasyMock.createNiceMock(BundleContext.class); EasyMock.expect(bundleContext.getServiceReferences((String)EasyMock.anyObject(), (String)EasyMock.anyObject())) .andReturn(serviceReferences); EasyMock.expect(bundleContext.getService((ServiceReference)EasyMock.anyObject())).andReturn(doNothingFilter); EasyMock.expect(bundleContext.getService((ServiceReference)EasyMock.anyObject())).andReturn(commitFilter); EasyMock.replay(bundleContext); httpContext = new SecurityDelegatingHttpContext(bundleContext, defaultHttpContext); HttpServletRequest request = EasyMock.createNiceMock(HttpServletRequest.class); EasyMock.replay(request); HttpServletResponse response = EasyMock.createNiceMock(HttpServletResponse.class); EasyMock.expect(response.isCommitted()).andReturn(false); EasyMock.expect(response.getWriter()).andReturn(new PrintWriter(System.out)); EasyMock.expect(response.isCommitted()).andReturn(false); EasyMock.expect(response.isCommitted()).andReturn(true); EasyMock.replay(response); assertFalse(httpContext.handleSecurity(request, response)); assertTrue(doNothingFilter.called); assertTrue(commitFilter.called); assertFalse(accessDeniedFilter.called); }
@Override @SuppressWarnings({ "unchecked", "rawtypes" }) public boolean handleSecurity(HttpServletRequest request, HttpServletResponse response) throws IOException { ServiceReference[] refs; try { refs = bundleContext.getServiceReferences(Filter.class.getName(), FILTER_FILTER); } catch (InvalidSyntaxException e) { LOG.warn(e.getMessage(), e); return false; } if (refs == null || refs.length == 0) { LOG.info("No filter registered."); return !requireFilter; } Filter[] filters = new Filter[refs.length]; try { for (int i = 0; i < refs.length; i++) { filters[i] = (Filter)bundleContext.getService(refs[i]); } try { new Chain(filters).doFilter(request, response); return !response.isCommitted(); } catch (ServletException e) { LOG.warn(e.getMessage(), e); return false; } } finally { for (int i = 0; i < refs.length; i++) { if (filters[i] != null) { bundleContext.ungetService(refs[i]); } } } }
SecurityDelegatingHttpContext implements HttpContext { @Override @SuppressWarnings({ "unchecked", "rawtypes" }) public boolean handleSecurity(HttpServletRequest request, HttpServletResponse response) throws IOException { ServiceReference[] refs; try { refs = bundleContext.getServiceReferences(Filter.class.getName(), FILTER_FILTER); } catch (InvalidSyntaxException e) { LOG.warn(e.getMessage(), e); return false; } if (refs == null || refs.length == 0) { LOG.info("No filter registered."); return !requireFilter; } Filter[] filters = new Filter[refs.length]; try { for (int i = 0; i < refs.length; i++) { filters[i] = (Filter)bundleContext.getService(refs[i]); } try { new Chain(filters).doFilter(request, response); return !response.isCommitted(); } catch (ServletException e) { LOG.warn(e.getMessage(), e); return false; } } finally { for (int i = 0; i < refs.length; i++) { if (filters[i] != null) { bundleContext.ungetService(refs[i]); } } } } }
SecurityDelegatingHttpContext implements HttpContext { @Override @SuppressWarnings({ "unchecked", "rawtypes" }) public boolean handleSecurity(HttpServletRequest request, HttpServletResponse response) throws IOException { ServiceReference[] refs; try { refs = bundleContext.getServiceReferences(Filter.class.getName(), FILTER_FILTER); } catch (InvalidSyntaxException e) { LOG.warn(e.getMessage(), e); return false; } if (refs == null || refs.length == 0) { LOG.info("No filter registered."); return !requireFilter; } Filter[] filters = new Filter[refs.length]; try { for (int i = 0; i < refs.length; i++) { filters[i] = (Filter)bundleContext.getService(refs[i]); } try { new Chain(filters).doFilter(request, response); return !response.isCommitted(); } catch (ServletException e) { LOG.warn(e.getMessage(), e); return false; } } finally { for (int i = 0; i < refs.length; i++) { if (filters[i] != null) { bundleContext.ungetService(refs[i]); } } } } SecurityDelegatingHttpContext(BundleContext bundleContext, HttpContext delegate); }
SecurityDelegatingHttpContext implements HttpContext { @Override @SuppressWarnings({ "unchecked", "rawtypes" }) public boolean handleSecurity(HttpServletRequest request, HttpServletResponse response) throws IOException { ServiceReference[] refs; try { refs = bundleContext.getServiceReferences(Filter.class.getName(), FILTER_FILTER); } catch (InvalidSyntaxException e) { LOG.warn(e.getMessage(), e); return false; } if (refs == null || refs.length == 0) { LOG.info("No filter registered."); return !requireFilter; } Filter[] filters = new Filter[refs.length]; try { for (int i = 0; i < refs.length; i++) { filters[i] = (Filter)bundleContext.getService(refs[i]); } try { new Chain(filters).doFilter(request, response); return !response.isCommitted(); } catch (ServletException e) { LOG.warn(e.getMessage(), e); return false; } } finally { for (int i = 0; i < refs.length; i++) { if (filters[i] != null) { bundleContext.ungetService(refs[i]); } } } } SecurityDelegatingHttpContext(BundleContext bundleContext, HttpContext delegate); @Override String getMimeType(String name); @Override URL getResource(String name); @Override @SuppressWarnings({ "unchecked", "rawtypes" }) boolean handleSecurity(HttpServletRequest request, HttpServletResponse response); }
SecurityDelegatingHttpContext implements HttpContext { @Override @SuppressWarnings({ "unchecked", "rawtypes" }) public boolean handleSecurity(HttpServletRequest request, HttpServletResponse response) throws IOException { ServiceReference[] refs; try { refs = bundleContext.getServiceReferences(Filter.class.getName(), FILTER_FILTER); } catch (InvalidSyntaxException e) { LOG.warn(e.getMessage(), e); return false; } if (refs == null || refs.length == 0) { LOG.info("No filter registered."); return !requireFilter; } Filter[] filters = new Filter[refs.length]; try { for (int i = 0; i < refs.length; i++) { filters[i] = (Filter)bundleContext.getService(refs[i]); } try { new Chain(filters).doFilter(request, response); return !response.isCommitted(); } catch (ServletException e) { LOG.warn(e.getMessage(), e); return false; } } finally { for (int i = 0; i < refs.length; i++) { if (filters[i] != null) { bundleContext.ungetService(refs[i]); } } } } SecurityDelegatingHttpContext(BundleContext bundleContext, HttpContext delegate); @Override String getMimeType(String name); @Override URL getResource(String name); @Override @SuppressWarnings({ "unchecked", "rawtypes" }) boolean handleSecurity(HttpServletRequest request, HttpServletResponse response); static final String FILTER_PROP; static final String FILTER_REQUIRED_PROP; }