AuthenticationMiddleware implements enkan.Middleware<REQ, RES, REQ, RES> { @Override public RES handle(REQ req, MiddlewareChain<REQ, RES, ?, ?> next) { final REQ request = MixinUtils.mixin(req, PrincipalAvailable.class); for (AuthBackend<REQ, T> backend : backends) { Optional<Principal> principal = ThreadingUtils.some( backend.parse(request), data -> backend.authenticate(request, data)); principal.ifPresent(((PrincipalAvailable) request)::setPrincipal); } return next.next(request); } AuthenticationMiddleware(List<AuthBackend<REQ, T>> backends); @Override RES handle(REQ req, MiddlewareChain<REQ, RES, ?, ?> next);  }
@Test public void test() { AuthenticationMiddleware<Request, String, String> middleware = new AuthenticationMiddleware<>(Collections.singletonList(createAuthBackend())); Request request = new Request("kawasima"); middleware.handle(request, new DefaultMiddlewareChain<>(Predicates.any(), "", (req, chain) -> "ok")); assertThat(request.getPrincipal()).isNotNull(); }