@Test public void anonymousDisabled() throws Exception { TestResponse response = TestClient.create(Main.createRouting(false)) .path("/comment/one") .get(); assertEquals(Http.Status.FORBIDDEN_403, response.status()); }
static Routing createRouting(boolean acceptAnonymousUsers) { return Routing.builder() .any((req, res) -> { String user = req.headers().first("user-identity") .or(() -> acceptAnonymousUsers ? Optional.of("anonymous") : Optional.empty()) .orElseThrow(() -> new HttpException("Anonymous access is forbidden!", Http.Status.FORBIDDEN_403)); req.context().register("user", user); req.next(); }) .register("/comments", new CommentsService()) .error(CompletionException.class, (req, res, ex) -> req.next(ex.getCause())) .error(ProfanityException.class, (req, res, ex) -> { res.status(Http.Status.NOT_ACCEPTABLE_406); res.send("Expressions like '" + ex.getObfuscatedProfanity() + "' are unacceptable!"); }) .error(HttpException.class, (req, res, ex) -> { if (ex.status() == Http.Status.FORBIDDEN_403) { res.status(ex.status()); res.send(ex.getMessage()); } else { req.next(); } }) .build(); }
Main { static Routing createRouting(boolean acceptAnonymousUsers) { return Routing.builder() .any((req, res) -> { String user = req.headers().first("user-identity") .or(() -> acceptAnonymousUsers ? Optional.of("anonymous") : Optional.empty()) .orElseThrow(() -> new HttpException("Anonymous access is forbidden!", Http.Status.FORBIDDEN_403)); req.context().register("user", user); req.next(); }) .register("/comments", new CommentsService()) .error(CompletionException.class, (req, res, ex) -> req.next(ex.getCause())) .error(ProfanityException.class, (req, res, ex) -> { res.status(Http.Status.NOT_ACCEPTABLE_406); res.send("Expressions like '" + ex.getObfuscatedProfanity() + "' are unacceptable!"); }) .error(HttpException.class, (req, res, ex) -> { if (ex.status() == Http.Status.FORBIDDEN_403) { res.status(ex.status()); res.send(ex.getMessage()); } else { req.next(); } }) .build(); } }
Main { static Routing createRouting(boolean acceptAnonymousUsers) { return Routing.builder() .any((req, res) -> { String user = req.headers().first("user-identity") .or(() -> acceptAnonymousUsers ? Optional.of("anonymous") : Optional.empty()) .orElseThrow(() -> new HttpException("Anonymous access is forbidden!", Http.Status.FORBIDDEN_403)); req.context().register("user", user); req.next(); }) .register("/comments", new CommentsService()) .error(CompletionException.class, (req, res, ex) -> req.next(ex.getCause())) .error(ProfanityException.class, (req, res, ex) -> { res.status(Http.Status.NOT_ACCEPTABLE_406); res.send("Expressions like '" + ex.getObfuscatedProfanity() + "' are unacceptable!"); }) .error(HttpException.class, (req, res, ex) -> { if (ex.status() == Http.Status.FORBIDDEN_403) { res.status(ex.status()); res.send(ex.getMessage()); } else { req.next(); } }) .build(); } private  Main(); }
Main { static Routing createRouting(boolean acceptAnonymousUsers) { return Routing.builder() .any((req, res) -> { String user = req.headers().first("user-identity") .or(() -> acceptAnonymousUsers ? Optional.of("anonymous") : Optional.empty()) .orElseThrow(() -> new HttpException("Anonymous access is forbidden!", Http.Status.FORBIDDEN_403)); req.context().register("user", user); req.next(); }) .register("/comments", new CommentsService()) .error(CompletionException.class, (req, res, ex) -> req.next(ex.getCause())) .error(ProfanityException.class, (req, res, ex) -> { res.status(Http.Status.NOT_ACCEPTABLE_406); res.send("Expressions like '" + ex.getObfuscatedProfanity() + "' are unacceptable!"); }) .error(HttpException.class, (req, res, ex) -> { if (ex.status() == Http.Status.FORBIDDEN_403) { res.status(ex.status()); res.send(ex.getMessage()); } else { req.next(); } }) .build(); } private  Main(); static void main(String[] args); }
Main { static Routing createRouting(boolean acceptAnonymousUsers) { return Routing.builder() .any((req, res) -> { String user = req.headers().first("user-identity") .or(() -> acceptAnonymousUsers ? Optional.of("anonymous") : Optional.empty()) .orElseThrow(() -> new HttpException("Anonymous access is forbidden!", Http.Status.FORBIDDEN_403)); req.context().register("user", user); req.next(); }) .register("/comments", new CommentsService()) .error(CompletionException.class, (req, res, ex) -> req.next(ex.getCause())) .error(ProfanityException.class, (req, res, ex) -> { res.status(Http.Status.NOT_ACCEPTABLE_406); res.send("Expressions like '" + ex.getObfuscatedProfanity() + "' are unacceptable!"); }) .error(HttpException.class, (req, res, ex) -> { if (ex.status() == Http.Status.FORBIDDEN_403) { res.status(ex.status()); res.send(ex.getMessage()); } else { req.next(); } }) .build(); } private  Main(); static void main(String[] args);  }