Class HubDefinitionFile

java.lang.Object
com.iizix.server.hub.HubDefinitionFile

public final class HubDefinitionFile extends Object
The parser of the hub definition file, HUB-DESIGN.md section 4.2: UTF-8, unix-style ini, a byte-order mark ignored, LF or CRLF, # or ; comments, case-insensitive section names, the sections [hub], [members] and [origins], each at most once. The lexical rules are those of PUSH-CREDENTIALS.md section 3 with one addition the design's own example needs: a comment may also follow a value on the same line, starting at a # or ; preceded by whitespace. The shape of this class is PushCredentialsFile's, copied and not extracted (ruling H-9).

[hub]: name (required: the key of the hub's rows in HUB.*, at most 64 characters of letters, digits, - and _), policy (sticky, the default, or priority; ruling H-2), lease (15 s), margin (10 s; both H-3), grace (900 s; H-11), hold (60 s; T-4) and key, the path of the interim cluster key file (H-8), kept as written. The four durations are whole seconds, lease at least 1 and none above 86400; an empty value is an absent key and the default applies; a key set twice uses the later value with a WARNING; an unknown key is a WARNING and ignored.

[members]: memberID = URL [principal], one member per line, in priority order. A member ID is at least one and at most 64 characters of letters, digits, - and _; the URL is the member's public base URL, absolute, http or https, with a host and without a query or fragment; the only flag is principal, marking an eligible member. A member ID that appears twice (in any letter case) is refused on both lines.

[origins]: allow = origins, separated by spaces or commas, each scheme://host[:port] with http or https and an optional leading *. wildcard label; several allow lines are their union; an invalid origin is refused alone, the rest of its line applies.

Pure. No I/O and no logging: parse(byte[],String) answers a HubDefinitionFile.Parsed that carries the values and a HubDefinitionFile.Diagnostic per refusal or warning, which the loader logs. A diagnostic names the file and the line number and never a line's content or a value: a section name, a key name or a flag name is quoted where the operator needs it to act, a value never is.

What is refused, and how: a malformed section header, a section that appears twice, a line outside any section, a missing or invalid name, a policy that is neither value, a duration that is not a whole number or is out of range, a member line without =, an invalid member ID, a missing or invalid URL, an unknown flag, a duplicate member ID and an invalid origin. Every refusal is SEVERE and the line (or the value) is ignored; the valid lines still apply. An unknown section and an unknown key are WARNINGs and are ignored. A file without a usable name is not usable at all (HubDefinitionFile.Parsed.isUsable()).

Author:
Christopher Mindus
  • Field Details

  • Method Details

    • parse

      public static HubDefinitionFile.Parsed parse(byte[] bytes, String fileName)
      Parses the bytes of a file: UTF-8, a byte-order mark ignored.
      Parameters:
      bytes - The file content.
      fileName - The file name, for the diagnostics.
      Returns:
      The result, never null.
    • parse

      public static HubDefinitionFile.Parsed parse(String text, String fileName)
      Parses the text of a file.
      Parameters:
      text - The file content, LF or CRLF line endings.
      fileName - The file name, for the diagnostics.
      Returns:
      The result, never null.
    • isValidMemberID

      public static boolean isValidMemberID(String id)
      Validates a member ID, section 4.2: at least one and at most 64 characters of letters, digits, - and _.
      Parameters:
      id - The member ID, may be null.
      Returns:
      true when valid.
    • isValidHubName

      public static boolean isValidHubName(String name)
      Validates a hub name: the rule of a member ID, at most 64 characters.
      Parameters:
      name - The name, may be null.
      Returns:
      true when valid.
    • normalizeOrigin

      public static String normalizeOrigin(String origin)
      Normalises an origin of the [origins] section: scheme://host[:port], http or https, the host optionally starting with *., no path, query or fragment; answered in lower case.
      Parameters:
      origin - The origin as written.
      Returns:
      The normalised origin, or null when invalid.
    • isValidMemberURL

      public static boolean isValidMemberURL(String url)
      Validates a member URL: absolute, http or https, a host, no query or fragment, at most 254 characters.
      Parameters:
      url - The URL as written.
      Returns:
      true when valid.