Class TerminalSafe

java.lang.Object
co.ankatech.ankasecure.keymanagement.security.TerminalSafe

public final class TerminalSafe extends Object
Renders a server-supplied string so it can enter an exception message, a log line or terminal output without changing what the operator's terminal shows or does.

Every value read from an HTTP response (a body member, a header, a problem type, title or detail, an OAuth error, a metadata value, an enum wire value) passes through text(String), or uri(String) when it is a URI, before it is concatenated into any of those channels. A Location header is never rendered at all, by either method.

  • A code point that is an ISO control (C0 including TAB, CR and LF; DEL; C1), a format character (bidirectional controls and isolates, zero-width characters, the soft hyphen, the byte order mark, tag characters) or a line or paragraph separator becomes a visible escape: a backslash, u and four uppercase hexadecimal digits, one per UTF-16 unit.
  • A lone surrogate becomes U+FFFD; a valid surrogate pair is kept.
  • The rendering is capped at 512 code points of output; an escape counts as six and is never split. A capped rendering ends with the ASCII marker ...[truncated] (three full stops), outside the cap.
  • Nothing is decoded: %0D%0A stays as it is.

Fields an exception keeps for programmatic use (such as its problem detail) hold the server's value; whoever prints them renders them here.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    The most code points a rendering holds before the truncation marker.
    static final String
    Appended to a rendering cut at the cap, in ASCII: three full stops, then [truncated].
    static final String
    The rendering of a value that is not an absolute URI with a scheme and a host.
  • Method Summary

    Modifier and Type
    Method
    Description
    static boolean
    isEscaped(int codePoint)
    Whether a code point is rendered as a visible escape: an ISO control, a format character, a line separator or a paragraph separator.
    static String
    text(String value)
    Renders a server-supplied string.
    static String
    uri(String value)
    Renders a server-supplied URI from its parsed parts only: scheme://host[:port][path], the host converted to ASCII (punycode), the path as received (still percent-encoded).

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • MAX_CODE_POINTS

      public static final int MAX_CODE_POINTS
      The most code points a rendering holds before the truncation marker.
      See Also:
    • TRUNCATED

      public static final String TRUNCATED
      Appended to a rendering cut at the cap, in ASCII: three full stops, then [truncated].
      See Also:
    • UNPARSEABLE

      public static final String UNPARSEABLE
      The rendering of a value that is not an absolute URI with a scheme and a host.
      See Also:
  • Method Details

    • text

      public static String text(String value)
      Renders a server-supplied string.
      Parameters:
      value - the value, possibly null
      Returns:
      the terminal-safe rendering; empty for null
    • uri

      public static String uri(String value)
      Renders a server-supplied URI from its parsed parts only: scheme://host[:port][path], the host converted to ASCII (punycode), the path as received (still percent-encoded). User information, query and fragment never appear. The result is all ASCII: on top of what text(String) escapes, every code point of the path outside U+0020 to U+007E is a visible escape, one per UTF-16 unit, but a lone surrogate, which is U+FFFD as in text(String); the same cap applies. A port, after the authority's last colon outside an IPv6 literal, is one to five ASCII digits whose value is at most 65535, rendered as its value (leading zeros dropped).
      Parameters:
      value - the value, possibly null
      Returns:
      the rendering, or UNPARSEABLE when the value does not parse as an absolute URI with a scheme and a host, or names an empty, non-ASCII, longer or out-of-range port
    • isEscaped

      public static boolean isEscaped(int codePoint)
      Whether a code point is rendered as a visible escape: an ISO control, a format character, a line separator or a paragraph separator.
      Parameters:
      codePoint - the code point
      Returns:
      true when text(String) escapes it