@Override public void doFilter(final HttpExchange httpExchange, final Chain chain) throws IOException { if (authenticator == null) { chain.doFilter(httpExchange); return; } final Result authResult = this.authenticator.authenticate(httpExchange); if (authResult instanceof Success) { @SuppressWarnings("unused") final Success success = (Success) authResult; // auth succeeded - move to next filter chain.doFilter(httpExchange); } else if (authResult instanceof Retry) { final Retry retry = (Retry) authResult; this.drainInput(httpExchange); // send auth retry (401) httpExchange.sendResponseHeaders(retry.getResponseCode(), -1L); } else if (authResult instanceof Failure) { final Failure failure = (Failure) authResult; this.drainInput(httpExchange); // send auth failure (401) httpExchange.sendResponseHeaders(failure.getResponseCode(), -1L); } }