@Override public Response status(final Status status) { rsp.status(status); return this; }
@Override public Response status(final int status) { rsp.status(status); return this; }
@Override public Optional<Status> status() { return rsp.status(); }
@Override public void setResponseStatus(int code) { rsp.status(code); }
@Override public void setResponseStatus(final int code) { rsp.status(code); }
/** * Set the HTTP response status. * * @param status A HTTP status. * @return This response. */ @Nonnull default Response status(final int status) { return status(Status.valueOf(status)); }
@Override public void handle(final Request req, final Response rsp) throws Throwable { MetricRegistry registry = req.require(MetricRegistry.class); Counter counter = registry.counter("request.actives"); Timer.Context timer = registry.timer("request").time(); counter.inc(); rsp.complete((ereq, ersp, x) -> { timer.stop(); counter.dec(); Meter meter = registry.meter("responses." + rsp.status().orElse(Status.OK).value()); meter.mark(); }); }
@Override public void handle(Request req, Response rsp, Route.Chain chain) throws Throwable { Object result = invoke(req, rsp, chain); if (!rsp.committed()) { Class<?> returnType = handler.getReturnType(); if (returnType == void.class) { rsp.status(Status.NO_CONTENT); } else { rsp.status(Status.OK); rsp.send(result); } } chain.next(req, rsp); }
@Override public void handle(final Request req, final Response rsp) throws Exception { if (!rsp.header(ALLOW).isSet()) { Set<String> allow = new LinkedHashSet<>(); Set<String> methods = new LinkedHashSet<>(Route.METHODS); String path = req.path(); methods.remove(req.method()); for (String method : methods) { routes.stream() .filter(route -> route.matches(method, path, MediaType.all, MediaType.ALL).isPresent()) .forEach(route -> allow.add(route.method())); } rsp.header(ALLOW, Joiner.on(SEP).join(allow)) .length(0) .status(Status.OK); } }
/** * Responsible of writing the given body into the HTTP response. * * @param result The HTTP body. * @throws Exception If the response write fails. */ default void send(final @Nullable Object result) throws Throwable { if (result instanceof Result) { send((Result) result); } else if (result != null) { // wrap body Result b = Results.with(result); status().ifPresent(b::status); type().ifPresent(b::type); send(b); } else { throw new NullPointerException("Response required."); } }
@Override public void handle(final Request req, final Response rsp) throws Throwable { rsp.status(Status.OK) .type(MediaType.plain) .header("Cache-Control", "must-revalidate,no-cache,no-store") .send("pong"); ; }
public static RouteWithFilter notFound(final String method, final String path) { return new FallbackRoute("404", method, path, MediaType.ALL, (req, rsp, chain) -> { if (!rsp.status().isPresent()) { throw new Err(Status.NOT_FOUND, req.path(true)); } }); }
sb.append(req.protocol()); sb.append(Q).append(SP); int status = ersp.status().orElse(Status.OK).value(); sb.append(status); sb.append(SP);
@Override public void handle(final Request req, final Response rsp) throws Throwable { Object data; Status status; if (threadDump == null) { data = "Sorry your runtime environment does not allow to dump threads."; status = Status.NOT_IMPLEMENTED; } else { final ByteArrayOutputStream output = new ByteArrayOutputStream(); threadDump.dump(output); data = output.toByteArray(); status = Status.OK; } rsp.type(MediaType.plain) .status(status) .header("Cache-Control", "must-revalidate,no-cache,no-store") .send(data); }
private static Route[] routes(final Set<Route.Definition> routeDefs, final String method, final String path, final MediaType type, final List<MediaType> accept) { List<Route> routes = findRoutes(routeDefs, method, path, type, accept); routes.add(RouteImpl.fallback((req, rsp, chain) -> { if (!rsp.status().isPresent()) { // 406 or 415 Err ex = handle406or415(routeDefs, method, path, type, accept); if (ex != null) { throw ex; } // 405 ex = handle405(routeDefs, method, path, type, accept); if (ex != null) { throw ex; } // favicon.ico if (path.equals("/favicon.ico")) { // default /favicon.ico handler: rsp.status(Status.NOT_FOUND).end(); } else { throw new Err(Status.NOT_FOUND, req.path(true)); } } }, method, path, "err", accept)); return routes.toArray(new Route[routes.size()]); }
@Override public void handle(final Request req, final Response rsp) throws Throwable { HealthCheckRegistry registry = req.require(HealthCheckRegistry.class); SortedMap<String, Result> checks = req.param("name").toOptional().map(name -> { SortedMap<String, Result> set = ImmutableSortedMap.of(name, registry.runHealthCheck(name)); return set; }).orElseGet(() -> registry.runHealthChecks()); final Status status; if (checks.isEmpty()) { status = Status.NOT_IMPLEMENTED; } else { status = checks.values().stream() .filter(it -> !it.isHealthy()) .findFirst() .map(it -> Status.SERVER_ERROR) .orElse(Status.OK); } rsp.status(status) .header("Cache-Control", "must-revalidate,no-cache,no-store") .send(checks); }
public void handle(final Client<?, ?> client, final HttpAction action) { if (!rsp.committed()) { int statusCode = action.getCode(); // on error, let jooby handle it if (statusCode >= 400) { if (client instanceof IndirectBasicAuthClient) { rsp.status(statusCode).end(); } else { throw new Err(statusCode, action); } } } } }
rsp.status(Status.OK).end();