/** * Creates a new {@link Err}. * * @param status A HTTP status. Required. * @param cause The cause of the problem. */ public Err(final Status status, final Throwable cause) { super(message(status, null), cause); this.status = status.value(); }
/** * Produces a friendly view of the err, resulting map has these attributes: * * <pre> * message: exception message (if present) * stacktrace: array with the stacktrace * status: status code * reason: a status code reason * </pre> * * @param stacktrace True for adding stacktrace. * @return A lightweight view of the err. */ public Map<String, Object> toMap(boolean stacktrace) { Status status = Status.valueOf(this.status); Throwable cause = Optional.ofNullable(getCause()).orElse(this); String message = Optional.ofNullable(cause.getMessage()).orElse(status.reason()); Map<String, Object> err = new LinkedHashMap<>(); err.put("message", message); if (stacktrace) { err.put("stacktrace", Throwables.getStackTraceAsString(cause).replace("\r", "").split("\\n")); } err.put("status", status.value()); err.put("reason", status.reason()); return err; }
/** * Creates a new {@link Err}. * * @param status A HTTP status. Required. * @param message A error message. Required. * @param cause The cause of the problem. */ public Err(final Status status, final String message, final Throwable cause) { super(message(status, message), cause); this.status = status.value(); }
/** * Creates a new {@link Err}. * * @param status A HTTP status. Required. */ public Err(final Status status) { super(message(status, null)); this.status = status.value(); }
/** * Creates a new {@link Err}. * * @param status A HTTP status. Required. * @param message A error message. Required. */ public Err(final Status status, final String message) { super(message(status, message)); this.status = status.value(); }
@Override public Response status(final Status status) { this.status = requireNonNull(status, "Status required."); rsp.statusCode(status.value()); failure = status.isError(); return this; }
/** * Build an error message using the HTTP status. * * @param status The HTTP Status. * @param tail A message to append. * @return An error message. */ private static String message(final Status status, @Nullable final String tail) { return message(status.reason(), status.value(), tail); }
@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(); }); }
private Integer statusCodeFor(MethodInsnNode node) { if (node.name.equals("noContent")) { return 204; } if (node.name.equals("with")) { AbstractInsnNode previous = node.getPrevious(); if (previous instanceof IntInsnNode) { return ((IntInsnNode) previous).operand; } if (previous instanceof FieldInsnNode) { String owner = ((FieldInsnNode) previous).owner.replace("/", "."); if (owner.equals(Status.class.getName())) { String statusName = ((FieldInsnNode) previous).name; return Try.apply(() -> Status.class.getDeclaredField(statusName).get(null)) .map(it -> ((Status) it).value()) .orElse(null); } } } return null; }
/** * Setup a route error handler. The error handler will be executed if current status code matches * the one provided. * * All headers are reset while generating the error response. * * @param code The status code to match. * @param handler A route error handler. * @return This router. */ @Nonnull default Router err(final Status code, final Err.Handler handler) { return err((req, rsp, x) -> { if (code.value() == x.statusCode()) { handler.handle(req, rsp, x); } }); }
sb.append(req.protocol()); sb.append(Q).append(SP); int status = ersp.status().orElse(Status.OK).value(); sb.append(status); sb.append(SP);
private long[] byteRange() { long len = rsp.header("Content-Length").map(Long::parseLong).orElse(-1L); if (len > 0) { if (byteRange.isPresent()) { String raw = byteRange.get(); long[] range = ByteRange.parse(raw); long start = range[0]; long end = range[1]; if (start == -1) { start = len - end; end = len - 1; } if (end == -1 || end > len - 1) { end = len - 1; } if (start > end) { throw new Err(Status.REQUESTED_RANGE_NOT_SATISFIABLE, raw); } // offset long limit = (end - start + 1); rsp.header("Accept-Ranges", "bytes"); rsp.header("Content-Range", "bytes " + start + "-" + end + "/" + len); rsp.header("Content-Length", Long.toString(limit)); rsp.statusCode(Status.PARTIAL_CONTENT.value()); return new long[]{start, limit}; } } return null; }
@Override public void end() { if (!rsp.committed()) { if (status == null) { status(rsp.statusCode()); } writeCookies(); /** * Do we need to figure it out Content-Length? */ boolean lenSet = rsp.header("Content-Length").isPresent() || rsp.header("Transfer-Encoding").isPresent(); if (!lenSet) { int statusCode = status.value(); boolean hasBody = true; if (statusCode >= 100 && statusCode < 200) { hasBody = false; } else if (statusCode == 204 || statusCode == 304) { hasBody = false; } if (hasBody) { rsp.header("Content-Length", "0"); } } } rsp.end(); }
/** * Creates a new {@link Err}. * * @param status A HTTP status. Required. * @param message A error message. Required. * @param cause The cause of the problem. */ public Err(final Status status, final String message, final Throwable cause) { super(message(status, message), cause); this.status = status.value(); }
/** * Creates a new {@link Err}. * * @param status A HTTP status. Required. * @param cause The cause of the problem. */ public Err(final Status status, final Throwable cause) { super(message(status, null), cause); this.status = status.value(); }
/** * Creates a new {@link Err}. * * @param status A HTTP status. Required. */ public Err(final Status status) { super(message(status, null)); this.status = status.value(); }
/** * Creates a new {@link Err}. * * @param status A HTTP status. Required. * @param message A error message. Required. */ public Err(final Status status, final String message) { super(message(status, message)); this.status = status.value(); }
/** * Build an error message using the HTTP status. * * @param status The HTTP Status. * @param tail A message to append. * @return An error message. */ private static String message(final Status status, @Nullable final String tail) { return message(status.reason(), status.value(), tail); }
@Override public Response status(final Status status) { this.status = requireNonNull(status, "Status required."); rsp.statusCode(status.value()); failure = status.isError(); return this; }
@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(); }); }