@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 after(final Route.After handler) { rsp.after(handler); }
@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); }
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); } } } } }
/** * 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."); } }
/** * Send an asset to the client. * * @param req Request. * @param rsp Response. * @param asset Resolve asset. * @throws Exception If send fails. */ protected void send(final Request req, final Response rsp, final Asset asset) throws Throwable { rsp.send(asset); }
@Override public Response type(final String type) { rsp.type(type); return this; }
@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); }
@Override public void handle(final Request req, final Response rsp) throws Throwable { String CRLF = "\r\n"; StringBuilder buffer = new StringBuilder("TRACE ").append(req.path()) .append(" ").append(req.protocol()); for (Entry<String, Mutant> entry : req.headers().entrySet()) { buffer.append(CRLF).append(entry.getKey()).append(": ") .append(entry.getValue().toList(String.class).stream().collect(Collectors.joining(", "))); } buffer.append(CRLF); rsp.type(MediaType.valueOf("message/http")); rsp.length(buffer.length()); rsp.send(buffer.toString()); }
@Override public void handle(final Request req, final Response rsp) throws Throwable { rsp.complete((ereq, ersp, x) -> { StringBuilder sb = new StringBuilder(256); long timestamp = req.timestamp(); sb.append(req.protocol()); sb.append(Q).append(SP); int status = ersp.status().orElse(Status.OK).value(); sb.append(status); sb.append(SP); sb.append(ersp.header(CONTENT_LENGTH).value(DASH)); if (extended) { sb.append(SP);
@Override public void handle(final Request req, final Response rsp, final Route.Chain chain) throws Throwable { logger.debug("open handle"); Handle handle = jdbi.open(); Optional.ofNullable(trx.configurer).ifPresent(c -> c.accept(handle)); req.set(trx.handle(), handle); logger.debug("new transaction: {}", handle); handle.begin(); rsp.after(new CommitTransaction(handle)); rsp.complete(new RollbackTransaction(handle)); rsp.complete(new CloseHandle(handle)); chain.next(req, rsp); } }
@Override public Response header(final String name, final Object value) { rsp.header(name, value); return this; }
@Override public Response status(final Status status) { rsp.status(status); return this; }
@Override public Response cookie(final String name, final String value) { rsp.cookie(name, value); return this; }
@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); } }
@Override public void complete(final Route.Complete handler) { rsp.complete(handler); }
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()]); }