/** * 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; }
/** * Return the enum constant of this type with the specified numeric value. * * @param statusCode the numeric value of the enum to be returned * @return the enum constant with the specified numeric value * @throws IllegalArgumentException if this enum has no constant for the specified numeric value */ public static Status valueOf(final int statusCode) { Integer key = Integer.valueOf(statusCode); Status status = statusMap.get(key); return status == null? new Status(key, key.toString()) : status; } }
/** * 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 int status) { this(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); } } } };
/** * Return a string representation of this status code. */ @Override public String toString() { return reason() + " (" + 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(); }
/** * 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); }
/** * Return a string representation of this status code. */ @Override public String toString() { return reason() + " (" + 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. * @param cause The cause of the problem. */ public Err(final int status, final Throwable cause) { this(Status.valueOf(status), cause); }
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 Map<String, Object> accept(final Node node, final String method, final Context ctx) { try { node.accept(this, ctx); if (!doc.containsKey("@statusCodes")) { Map<Object, Object> codes = new LinkedHashMap<>(); Status status = Status.OK; if ("DELETE".equals(method)) { status = Status.NO_CONTENT; } codes.put(status.value(), status.reason()); doc.put("@statusCodes", codes); } } catch (Exception x) { log.debug("Doc collector resulted in exception", x); } return doc; }