/** * @param entity A result content! * @return A new result with {@link Status#OK} and given content. */ @Nonnull public static Result ok(final Object entity) { return ok().set(entity); }
private Result wrap(final Object value) { if (value instanceof Result) { return (Result) value; } return Results.with(value); }
/** * @param content A result content! * @return A new result with {@link Status#ACCEPTED}. */ @Nonnull public static Result accepted(final Object content) { return accepted().set(content); }
return Results.json(Json.mapper().writer().writeValueAsBytes(swagger)); return Results.ok(Yaml.mapper().writer().writeValueAsBytes(swagger)).type("text/yml"); }); } else { boolean json = req.path().endsWith(".json"); if (json) { return Results.json(Json.mapper().writer().writeValueAsBytes(swagger)); return Results.ok(Yaml.mapper().writer().writeValueAsBytes(swagger)).type("text/yml"); }); + ".css\">\n<style>")) .orElse(index); return Results.ok(page).type(MediaType.html); }); router.get(options.redoc, () -> Results.ok(redoc).type(MediaType.html));
@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))); }
return Results.json(Json.mapper().writer().writeValueAsBytes(swagger)); return Results.ok(Yaml.mapper().writer().writeValueAsBytes(swagger)).type("text/yml"); }); } else { boolean json = req.path().endsWith(".json"); if (json) { return Results.json(Json.mapper().writer().writeValueAsBytes(swagger)); return Results.ok(Yaml.mapper().writer().writeValueAsBytes(swagger)).type("text/yml"); }); + ".css\">\n<style>")) .orElse(index); return Results.ok(page).type(MediaType.html); }); router.get(options.redoc, () -> Results.ok(redoc).type(MediaType.html));
@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))); }
@Override public Object handle(final Request req) throws Throwable { Set<Route.Definition> routes = req.require(Route.KEY); WebPageProvider provider = req.require(name, WebPageProvider.class); List<WebPage> pages = routes.stream() .filter(filter) .flatMap(r -> provider.apply(r).stream()) .peek(page -> { String name = page.getName(); if (name.startsWith("/")) { name = name.substring(1); } page.setName(name); }).collect(Collectors.toList()); return Results.ok(gen.apply(pages)).type(MediaType.xml); }
/** * @return A new result with {@link Status#NO_CONTENT}. */ @Nonnull public static Result noContent() { return with(Status.NO_CONTENT); }
/** * Publish application routes as Swagger spec. * * @param app An application. */ public void install(final Jooby app) { requireNonNull(app, "Application is required."); ObjectMapper mapper = Json.mapper(); ObjectWriter yaml = Yaml.pretty(); app.use(new SwaggerModule(mapper)); app.get(path + "/swagger.json", path + "/:tag/swagger.json", req -> { SwaggerBuilder sb = req.require(SwaggerBuilder.class); Swagger swagger = sb.build(req.param("tag").toOptional(), predicate, tag, Swagger.class); byte[] json = mapper.writer().withDefaultPrettyPrinter().writeValueAsBytes(swagger); return Results.json(json); }).name("swagger(json)"); app.get(path + "/swagger.yml", path + "/:tag/swagger.yml", req -> { SwaggerBuilder sb = req.require(SwaggerBuilder.class); Swagger swagger = sb.build(req.param("tag").toOptional(), predicate, tag, SwaggerYml.class); byte[] yml = yaml.writeValueAsBytes(swagger); return Results.ok(yml).type("application/yaml"); }).name("swagger(yml)"); if (ui) { app.assets(path + "/ui/**", "/META-INF/resources/webjars/swagger-ui/" + wjversion(app.getClass()) + "/{0}"); app.get(path, path + "/:tag", new SwaggerHandler(path)) .name("swagger(html)") .produces(MediaType.html); } }
/** * @param content A result content! * @return A new result with {@link Status#ACCEPTED}. */ @Nonnull public static Result accepted(final Object content) { return accepted().set(content); }
@Override public void handle(Request req, Response rsp) throws Throwable { PrometheusMeterRegistry registry = req.require(PrometheusMeterRegistry.class); String response = registry.scrape(); rsp.type(MediaType.plain) .send(Results.ok(response)); } }
/** * @return A new result with {@link Status#OK}. */ @Nonnull public static Result ok() { return with(Status.OK); }
configurer.accept(raml); return Results.ok(raml.toYaml()).type("text/yml"); }); } else { byte[] ramlString = readRaml(options.file); router.get(api, req -> { return Results.ok(ramlString).type("text/yml"); }); .replace("api-console-light-theme.css", "api-console-" + theme + "-theme.css")) .orElse(index); return Results.ok(page).type(MediaType.html); });
/** * @return A new result with {@link Status#ACCEPTED}. */ @Nonnull public static Result accepted() { return with(Status.ACCEPTED); }
String rootpath = Route.normalize(conf.getString("application.path") + path); routes.get(path, req -> Results.ok("<!DOCTYPE HTML>\n" + "<html>\n" + "<head>\n" +
/** * Produces a redirect (302) status code and set the <code>Location</code> header too. * * @param status A HTTP redirect status. * @param location A location. * @return A new result. */ private static Result redirect(final Status status, final String location) { requireNonNull(location, "A location is required."); return with(status).header("location", location); }
/** * @param entity A result content! * @return A new result with {@link Status#OK} and given content. */ @Nonnull public static Result ok(final Object entity) { return ok().set(entity); }
/** * @param entity A result content! * @return A new json result. */ @Nonnull public static Result xml(final Object entity) { return with(entity, 200).type(MediaType.xml); }
@Override protected void send(final Request req, final Response rsp, final Asset asset) throws Exception { LessSource.URLSource src = new LessSource.URLSource(asset.resource(), req.charset().name()); CompilationResult result = compiler.compile(src); result.getWarnings().forEach(warning -> log.warn(warning.toString())); if (req.path().endsWith(".map")) { rsp.type(MediaType.json) .send(Results.ok(result.getSourceMap())); } else { rsp.type(MediaType.css) .send(Results.ok(result.getCss())); } } }