ServiceAuthenticationHttpContext extends DefaultHttpContext { @Override public boolean handleSecurity(final HttpServletRequest request, final HttpServletResponse response) throws IOException { synchronized(authenticators) { if (authenticators.isEmpty()) { response.sendError(HttpServletResponse.SC_UNAUTHORIZED); return false; } for (final Authenticator authenticator : authenticators) { boolean authenticated = authenticator.authenticate(request); if (checkModeAny && authenticated) { return true; } else if (!checkModeAny && !authenticated) { response.sendError(HttpServletResponse.SC_UNAUTHORIZED); return false; } } return !checkModeAny; } } ServiceAuthenticationHttpContext(final BundleContext bundleContext, final String authMode); @Override boolean handleSecurity(final HttpServletRequest request, final HttpServletResponse response); void close(); static boolean shouldBeUsed(String authMode);  }
@Test public void anyAuthenticator() throws Exception { Object[] testData = new Object[]{ "service-any", new boolean[]{true, false}, true, "service-all", new boolean[]{true, false}, false, "service-any", new boolean[]{true, true}, true, "service-all", new boolean[]{true, true}, true, "service-any", new boolean[]{false, false}, false, "service-all", new boolean[]{false, false}, false, "service-any", new boolean[]{false, true}, true, "service-all", new boolean[]{false, true}, false, "service-all", new boolean[]{true, true, true, true, false, true}, false, "service-any", new boolean[]{false, false, false, false, false, true}, true }; for (int i = 0; i < testData.length; i += 3) { BundleContext bundleContext = createBundleContext(); ServiceReference[] serviceRefs = createServiceReferences(bundleContext, (boolean[]) testData[i + 1]); expect(bundleContext.getServiceReferences(eq(Authenticator.class.getName()), (String) isNull())).andReturn(serviceRefs); replay(bundleContext); ServiceAuthenticationHttpContext ctx = createContext(bundleContext, (String) testData[i]); HttpServletResponse resp = prepareResponse(); assertEquals(ctx.handleSecurity(null, resp), testData[i + 2], String.format("%s: %s --> %s", testData[i], printBooleanList((boolean[]) testData[i + 1]), testData[i + 2])); } }