/** * Creates a new {@link Err}. * * @param status A HTTP status. Required. * @param message A error message. Required. */ public Err(final int status, final String message) { this(Status.valueOf(status), message); }
/** * Creates a new {@link Err}. * * @param status A HTTP status. Required. */ public Err(final int status) { this(Status.valueOf(status)); }
/** * Creates a new {@link Err}. * * @param status A HTTP status. Required. * @param cause The cause of the problem. */ public Err(final int status, final Throwable cause) { this(Status.valueOf(status), cause); }
/** * 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)); }
/** * Set the result * * @param entity A result value. * @param status A HTTP status. * @return A new result. */ @Nonnull public static Result with(final Object entity, final int status) { return with(entity, Status.valueOf(status)); }
/** * Set response status. * * @param status A new response status to use. * @return This content. */ @Nonnull public Result status(final int status) { return status(Status.valueOf(status)); }
private void parse(final String text) { Pattern pattern = Pattern.compile("(\\d+)\\s*([:=]\\s*((.*)))?"); Matcher matcher = pattern.matcher(text); if (matcher.find()) { try { int statusCode = Integer.parseInt(matcher.group(1).trim()); String desc = Optional.ofNullable(matcher.group(3)) .orElse(Status.valueOf(statusCode).reason()); statusCodes.put(statusCode, desc); } catch (IllegalArgumentException x) { LoggerFactory.getLogger(DocParser.class).debug("Ignoring status code", x); } } } };
public Status apply(final Throwable cause) { if (cause instanceof Err) { return Status.valueOf(((Err) cause).statusCode()); } /** * usually a class name, except for inner classes where '$' is replaced it by '.' */ Function<Class<?>, String> name = type -> Optional.ofNullable(type.getDeclaringClass()) .map(dc -> new StringBuilder(dc.getName()) .append('.') .append(type.getSimpleName()) .toString()) .orElse(type.getName()); Config err = conf.getConfig("err"); int status = -1; Class<?> type = cause.getClass(); while (type != Throwable.class && status == -1) { String classname = name.apply(type); if (err.hasPath(classname)) { status = err.getInt(classname); } else { type = type.getSuperclass(); } } return status == -1 ? Status.SERVER_ERROR : Status.valueOf(status); } }
@Override public Object adapt(int code, WebContext context) { Status statusCode = Status.valueOf(code); if (statusCode.isError()) { throw new Err(statusCode); } // Any other result is already handled by pac4j via webcontext return null; } }
/** * 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; }
/** * 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 predicate Apply the error handler if the predicate evaluates to <code>true</code>. * @param handler A route error handler. * @return This router. */ @Nonnull default Router err(final Predicate<Status> predicate, final Err.Handler handler) { return err((req, rsp, err) -> { if (predicate.test(Status.valueOf(err.statusCode()))) { handler.handle(req, rsp, err); } }); }
if (status != null) { routeResponse.status(ImmutableMap.of(status, Try.apply(() -> Status.valueOf(status.intValue()).reason()) .orElse(status.toString())));
/** * Creates a new {@link Err}. * * @param status A HTTP status. Required. * @param cause The cause of the problem. */ public Err(final int status, final Throwable cause) { this(Status.valueOf(status), cause); }
/** * Creates a new {@link Err}. * * @param status A HTTP status. Required. * @param message A error message. Required. */ public Err(final int status, final String message) { this(Status.valueOf(status), message); }
/** * Creates a new {@link Err}. * * @param status A HTTP status. Required. */ public Err(final int status) { this(Status.valueOf(status)); }
/** * Set the result * * @param entity A result value. * @param status A HTTP status. * @return A new result. */ @Nonnull public static Result with(final Object entity, final int status) { return with(entity, Status.valueOf(status)); }
/** * Set response status. * * @param status A new response status to use. * @return This content. */ @Nonnull public Result status(final int status) { return status(Status.valueOf(status)); }
/** * 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)); }
private void parse(final String text) { Pattern pattern = Pattern.compile("(\\d+)\\s*([:=]\\s*((.*)))?"); Matcher matcher = pattern.matcher(text); if (matcher.find()) { try { int statusCode = Integer.parseInt(matcher.group(1).trim()); String desc = Optional.ofNullable(matcher.group(3)) .orElse(Status.valueOf(statusCode).reason()); statusCodes.put(statusCode, desc); } catch (IllegalArgumentException x) { LoggerFactory.getLogger(DocParser.class).debug("Ignoring status code", x); } } } };
/** * 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 predicate Apply the error handler if the predicate evaluates to <code>true</code>. * @param handler A route error handler. * @return This router. */ @Nonnull default Router err(final Predicate<Status> predicate, final Err.Handler handler) { return err((req, rsp, err) -> { if (predicate.test(Status.valueOf(err.statusCode()))) { handler.handle(req, rsp, err); } }); }