Package com.ultikits.ultitools.utils
Class CloudHttp
java.lang.Object
com.ultikits.ultitools.utils.CloudHttp
The single JSON REST exchange the protocol v2 client uses against UltiCloud (
/v1/*).
It keeps these exchange rules (the ones the 6.3.0 UltiCloudRequests exchange kept), because the
server credential travels through it:
- No redirect is followed. Following one would re-send the
Authorizationheader to whatever host theLocationheader names; a 3xx is returned as a status. - No exception, log line or
CloudHttp.Response.toString()carries a request header. The JDK puts the whole header value into some exception messages (an uncheckedIllegalArgumentExceptionfor a value with CR or LF), so every failure of the exchange becomesCloudHttp.Outcome.IO_ERRORand the exception itself is dropped. - Network failure and HTTP status stay distinct.
CloudHttp.Outcome.OKmeans an exchange completed, whatever its status; the caller readsCloudHttp.Response.status(). - The host is not caller-controlled. The base URL is the jar's build-time
env.ymlapi-url(HttpRequestUtils.getBaseUrl()); callers pass only a constant path.
Retry-After is exposed only in
its delta-seconds form (CloudHttp.Response.retryAfterSeconds()).
UltiCloudRequests makes no HTTP request in 6.3.1 (plan 22-17 removed its exchange), so this
class is the one REST exchange implementation the framework uses.
- Since:
- 6.3.1
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescription(package private) static enumWhat happened to one request.(package private) static final classThe result of one exchange. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final int(package private) static final intUpper bound on the bytes read from one response body.private static final intLongest digit string parsed as aRetry-Aftervalue; anything longer is clamped.private static final longprivate static final int -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate static StringbaseUrl()private static CloudHttp.Response(package private) static longparseRetryAfter(String header) (package private) static CloudHttp.ResponseSendsPOST <api-url><path>.private static StringreadBody(HttpURLConnection connection, int status) private static void
-
Field Details
-
MAX_BODY_BYTES
static final int MAX_BODY_BYTESUpper bound on the bytes read from one response body.- 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_RETRY_AFTER_SECONDS
private static final long MAX_RETRY_AFTER_SECONDS- See Also:
-
MAX_RETRY_AFTER_DIGITS
private static final int MAX_RETRY_AFTER_DIGITSLongest digit string parsed as aRetry-Aftervalue; anything longer is clamped.- See Also:
-
-
Constructor Details
-
CloudHttp
private CloudHttp()
-
-
Method Details
-
postJson
static CloudHttp.Response postJson(String path, com.google.gson.JsonObject bodyOrNull, String bearerOrNull) SendsPOST <api-url><path>.- Parameters:
path- a constant path beginning with/bodyOrNull- the JSON body, sent as UTF-8 withContent-Type: application/json; charset=utf-8;nullsends an empty POST without a JSONContent-Type(the JDK may add its default form media type)bearerOrNull- the value afterBearerin theAuthorizationheader, ornullfor a public route- Returns:
- the outcome; never
null - Throws:
IllegalStateException- if called on the server's primary thread
-
requireOffPrimaryThread
private static void requireOffPrimaryThread() -
baseUrl
- Returns:
- the configured base URL without trailing slashes, or
nullwhen blank
-
exchange
-
parseRetryAfter
- Parameters:
header- the rawRetry-Aftervalue, ornull- Returns:
- the delay in seconds clamped to 0..3600, or
-1when the header is absent or not in delta-seconds form (an HTTP date, a negative number, any other text)
-
readBody
- Throws:
IOException
-