Class CatalogueHttp

java.lang.Object
com.ultikits.ultitools.utils.CatalogueHttp

final class CatalogueHttp extends Object
The HTTP the module catalogue is asked with (plan 22-18, S-27, S-61).

The catalogue is public, so no request carries an Authorization header, and no redirect is followed: a 3xx is reported as such, so a compromised catalogue cannot send the client to another host. A network failure and an HTTP status stay distinct: CatalogueHttp.Kind.OK means an exchange completed, whatever its status.

download(java.lang.String, java.nio.file.Path, long) streams into a file while hashing what it wrote and keeps the stream open only while the body is at most the caller's limit; a body beyond it is cut at the limit, which is how a download larger than its declared sizeBytes is aborted while streaming rather than after. Timeouts: connect 10 s, read 15 s.

Since:
6.3.1
  • Field Details

    • SHA256_HEADER

      static final String SHA256_HEADER
      The response header carrying the SHA-256 of the body the catalogue served.
      See Also:
    • CONNECT_TIMEOUT_MS

      private static final int CONNECT_TIMEOUT_MS
      See Also:
    • READ_TIMEOUT_MS

      private static final int READ_TIMEOUT_MS
      See Also:
    • MAX_JSON_BYTES

      private static final int MAX_JSON_BYTES
      The most bytes of a JSON answer that is read, derived from the catalogue contract rather than picked: the largest answer this client asks for is one page of versions. The contract caps a page at 100 entries (pageSize 1..100, default 20) and a version's changelog at 5,000 characters, so one page carries at most 500,000 characters of changelog. UTF-8 spends up to four bytes on one character, and a JSON serializer up to six (a four-hex-digit escape), so that page is at most 3,000,000 bytes; the envelope and the hundred versions' other fields stay well inside the remaining megabyte. A module's own answer is smaller: its description is capped at 50,000 characters and its latest version's changelog at 5,000. The bound is rounded up to 4 MiB. An answer beyond it is refused as too large, never cut and read as malformed.
      See Also:
    • CHUNK

      private static final int CHUNK
      See Also:
    • LOGGER

      private static final Logger LOGGER
  • Constructor Details

    • CatalogueHttp

      private CatalogueHttp()
  • Method Details

    • getJson

      static CatalogueHttp.Json getJson(String pathAndQuery)
      Sends GET <api-url><pathAndQuery>.
      Parameters:
      pathAndQuery - a path beginning with /, with its query when it has one
      Returns:
      the answer; never null
    • download

      static CatalogueHttp.Download download(String url, Path target, long maxBytes)
      Streams url into target, keeping at most maxBytes bytes of it.
      Parameters:
      url - the absolute URL to read
      target - the file to write; created or replaced
      maxBytes - the most bytes to keep
      Returns:
      the outcome; never null
    • sha256Hex

      static String sha256Hex(byte[] bytes)
      Parameters:
      bytes - any bytes
      Returns:
      the lower-case hex SHA-256 of them
    • copy

      private static CatalogueHttp.Download copy(HttpURLConnection connection, Path target, long maxBytes, String sha256Header, long declared) throws IOException
      Throws:
      IOException
    • drain

      private static void drain(HttpURLConnection connection, int status)
    • open

      private static HttpURLConnection open(String url) throws IOException
      Throws:
      IOException
    • disconnect

      private static void disconnect(HttpURLConnection connection)
    • isRedirect

      private static boolean isRedirect(int status)
    • readBody

      private static String readBody(HttpURLConnection connection, int status) throws IOException
      Parameters:
      connection - the open connection
      status - its status
      Returns:
      the body as UTF-8 text, or null when it is longer than MAX_JSON_BYTES (nothing of it is then kept)
      Throws:
      IOException - when the body cannot be read
    • baseUrl

      static String baseUrl()
      Returns:
      the framework's catalogue base URL without trailing slashes, or null
    • digest

      private static MessageDigest digest()
    • hex

      private static String hex(byte[] digest)