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; }
@Test public void testFilterRequired() throws Exception { ServiceReference[] serviceReferences = {}; BundleContext bundleContext = EasyMock.createNiceMock(BundleContext.class); EasyMock.expect(bundleContext.getServiceReferences(Filter.class.getName(), "(org.apache.cxf.httpservice.filter=true)")) .andReturn(serviceReferences); EasyMock.replay(bundleContext); httpContext = new SecurityDelegatingHttpContext(bundleContext, defaultHttpContext); httpContext.requireFilter = true; HttpServletRequest request = EasyMock.createNiceMock(HttpServletRequest.class); EasyMock.replay(request); HttpServletResponse response = EasyMock.createNiceMock(HttpServletResponse.class); EasyMock.replay(response); boolean requestAllowed = httpContext.handleSecurity(request, response); assertFalse(requestAllowed); httpContext.requireFilter = false; requestAllowed = httpContext.handleSecurity(request, response); assertTrue(requestAllowed); }
@Test public void testSingleCommitFilter() throws Exception { ServiceReference filterReference = EasyMock.createNiceMock(ServiceReference.class); EasyMock.replay(filterReference); ServiceReference[] serviceReferences = {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(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.isCommitted()).andReturn(true); EasyMock.expect(response.getWriter()).andReturn(new PrintWriter(System.out)); EasyMock.replay(response); assertFalse(httpContext.handleSecurity(request, response)); assertTrue(commitFilter.called); assertFalse(doNothingFilter.called); assertFalse(accessDeniedFilter.called); }
@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); }
@Test public void testAllowRequest() throws Exception { ServiceReference filterReference = EasyMock.createNiceMock(ServiceReference.class); EasyMock.replay(filterReference); ServiceReference[] serviceReferences = {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.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.replay(response); assertTrue(httpContext.handleSecurity(request, response)); assertTrue(doNothingFilter.called); assertFalse(commitFilter.called); assertFalse(accessDeniedFilter.called); }