/** * Add a when clause for a custom result for the given media-type. * * @param type A media type to test for. * @param supplier An object supplier. * @return This result. */ @Nonnull public Result when(final String type, final Supplier<Object> supplier) { return when(MediaType.valueOf(type), supplier); }
/** * Performs content-negotiation on the Accept HTTP header on the request object. It select a * handler for the request, based on the acceptable types ordered by their quality values. * If the header is not specified, the first callback is invoked. When no match is found, * the server responds with 406 "Not Acceptable", or invokes the default callback: {@code ** / *}. * * <pre> * get("/jsonOrHtml", () {@literal ->} * Results * .when("text/html", () {@literal ->} Results.html("view").put("model", model))) * .when("application/json", () {@literal ->} model) * .when("*", () {@literal ->} {throw new Err(Status.NOT_ACCEPTABLE);}) * ); * </pre> * * @param type A media type. * @param supplier A result supplier. * @return A new result. */ @Nonnull public static Result when(final MediaType type, final Supplier<Object> supplier) { return new Result().when(type, supplier); }
/** * Performs content-negotiation on the Accept HTTP header on the request object. It select a * handler for the request, based on the acceptable types ordered by their quality values. * If the header is not specified, the first callback is invoked. When no match is found, * the server responds with 406 "Not Acceptable", or invokes the default callback: {@code ** / *}. * * <pre> * get("/jsonOrHtml", () {@literal ->} * Results * .when("text/html", () {@literal ->} Results.html("view").put("model", model))) * .when("application/json", () {@literal ->} model) * .when("*", () {@literal ->} {throw new Err(Status.NOT_ACCEPTABLE);}) * ); * </pre> * * @param type A media type. * @param supplier A result supplier. * @return A new result. */ @Nonnull public static Result when(final String type, final Supplier<Object> supplier) { return new Result().when(type, supplier); }
@Override public void handle(final Request req, final Response rsp, final Err ex) throws Throwable { log.error("execution of: {}{} resulted in exception\nRoute:\n{}\n\nStacktrace:", req.method(), req.path(), req.route().print(6), ex); Config conf = req.require(Config.class); boolean stackstrace = Try.apply(() -> conf.getBoolean("err.stacktrace")) .orElse(req.require(Env.class).name().equals("dev")); rsp.send( Results .when(MediaType.html, () -> Results.html(VIEW).put("err", ex.toMap(stackstrace))) .when(MediaType.all, () -> ex.toMap(stackstrace))); }
/** * Add a when clause for a custom result for the given media-type. * * @param type A media type to test for. * @param supplier An object supplier. * @return This result. */ @Nonnull public Result when(final String type, final Supplier<Object> supplier) { return when(MediaType.valueOf(type), supplier); }
/** * Performs content-negotiation on the Accept HTTP header on the request object. It select a * handler for the request, based on the acceptable types ordered by their quality values. * If the header is not specified, the first callback is invoked. When no match is found, * the server responds with 406 "Not Acceptable", or invokes the default callback: {@code ** / *}. * * <pre> * get("/jsonOrHtml", () {@literal ->} * Results * .when("text/html", () {@literal ->} Results.html("view").put("model", model))) * .when("application/json", () {@literal ->} model) * .when("*", () {@literal ->} {throw new Err(Status.NOT_ACCEPTABLE);}) * ); * </pre> * * @param type A media type. * @param supplier A result supplier. * @return A new result. */ @Nonnull public static Result when(final MediaType type, final Supplier<Object> supplier) { return new Result().when(type, supplier); }
/** * Performs content-negotiation on the Accept HTTP header on the request object. It select a * handler for the request, based on the acceptable types ordered by their quality values. * If the header is not specified, the first callback is invoked. When no match is found, * the server responds with 406 "Not Acceptable", or invokes the default callback: {@code ** / *}. * * <pre> * get("/jsonOrHtml", () {@literal ->} * Results * .when("text/html", () {@literal ->} Results.html("view").put("model", model))) * .when("application/json", () {@literal ->} model) * .when("*", () {@literal ->} {throw new Err(Status.NOT_ACCEPTABLE);}) * ); * </pre> * * @param type A media type. * @param supplier A result supplier. * @return A new result. */ @Nonnull public static Result when(final String type, final Supplier<Object> supplier) { return new Result().when(type, supplier); }
@Override public void handle(final Request req, final Response rsp, final Err ex) throws Throwable { log.error("execution of: {}{} resulted in exception\nRoute:\n{}\n\nStacktrace:", req.method(), req.path(), req.route().print(6), ex); Config conf = req.require(Config.class); boolean stackstrace = Try.apply(() -> conf.getBoolean("err.stacktrace")) .orElse(req.require(Env.class).name().equals("dev")); rsp.send( Results .when(MediaType.html, () -> Results.html(VIEW).put("err", ex.toMap(stackstrace))) .when(MediaType.all, () -> ex.toMap(stackstrace))); }