Package com.ultikits.ultitools.utils
Class CatalogueHttp
java.lang.Object
com.ultikits.ultitools.utils.CatalogueHttp
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
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescription(package private) static final classA finished download.(package private) static final classA JSON answer.(package private) static enumWhat happened to one request. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final intprivate static final intprivate static final Loggerprivate static final intThe 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.private static final int(package private) static final StringThe response header carrying the SHA-256 of the body the catalogue served. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription(package private) static StringbaseUrl()private static CatalogueHttp.Downloadcopy(HttpURLConnection connection, Path target, long maxBytes, String sha256Header, long declared) private static MessageDigestdigest()private static voiddisconnect(HttpURLConnection connection) (package private) static CatalogueHttp.DownloadStreamsurlintotarget, keeping at mostmaxBytesbytes of it.private static voiddrain(HttpURLConnection connection, int status) (package private) static CatalogueHttp.JsonSendsGET <api-url><pathAndQuery>.private static Stringhex(byte[] digest) private static booleanisRedirect(int status) private static HttpURLConnectionprivate static StringreadBody(HttpURLConnection connection, int status) (package private) static Stringsha256Hex(byte[] bytes)
-
Field Details
-
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_BYTESThe 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 (pageSize1..100, default 20) and a version'schangelogat 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: itsdescriptionis capped at 50,000 characters and itslatestversion'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
-
-
Constructor Details
-
CatalogueHttp
private CatalogueHttp()
-
-
Method Details
-
getJson
SendsGET <api-url><pathAndQuery>.- Parameters:
pathAndQuery- a path beginning with/, with its query when it has one- Returns:
- the answer; never
null
-
download
Streamsurlintotarget, keeping at mostmaxBytesbytes of it.- Parameters:
url- the absolute URL to readtarget- the file to write; created or replacedmaxBytes- the most bytes to keep- Returns:
- the outcome; never
null
-
sha256Hex
- 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
-
open
- Throws:
IOException
-
disconnect
-
isRedirect
private static boolean isRedirect(int status) -
readBody
- Parameters:
connection- the open connectionstatus- its status- Returns:
- the body as UTF-8 text, or
nullwhen it is longer thanMAX_JSON_BYTES(nothing of it is then kept) - Throws:
IOException- when the body cannot be read
-
baseUrl
- Returns:
- the framework's catalogue base URL without trailing slashes, or
null
-
digest
-
hex
-