Class BlueMindRestClient
- All Implemented Interfaces:
BlueMindAuthenticator
BlueMindAccountSource and translates BlueMindException at its
own boundary.
The rules every call is written under:
- Same server, never another. The REST root is the scheme, host
and port of the address the consumer declared, with the
/apiroot BlueMind serves its REST API under (RestServiceApiDescriptionParserprefixes every path with/api). Nothing a caller passes can name a host, a port, a user part or a path outside/api/: aBlueMindSessionrefuses such a path before anything is sent, andencodeSegment(java.lang.String)keeps a value inside its segment. A transport that follows redirects is refused, and a redirect answer is an error; a kept session is only presented at the root it was opened at. - The login.
POST /api/auth/login?login=…&origin=…with the password as a JSON string under exactlyContent-Type: application/json— what BlueMind's own client proxy sends (ClientProxyGenerator,ByMimeTypeCodec.encode); BlueMind picks the body codec by the exactContent-Typevalue, so atext/plaincarrying a charset parameter would be read as JSON, refused with a 500 and logged by BlueMind with the password in it. The answer is aLoginResponsewhoseauthKeytravels inAPI_KEY_HEADER. An HTTP 200 is not a success: a refused login answers 200 withstatus: Bad. - Secrets stay secret. The key and the password are never logged,
never handed to a caller of
call(org.exoplatform.bluemind.client.BlueMindAccountSource, java.util.function.Function<org.exoplatform.bluemind.client.BlueMindSession, T>)(BlueMindSessionexposes who the session is, not what authenticates it), and never part of an exception message; messages name an address without its query. - Bounded. Every body is read up to 1048576 bytes and refused beyond it, and every exchange — body included — completes within the request timeout or fails as unreachable.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringThe header BlueMind's REST API reads a session key from.static final intDefault connect timeout, in seconds.static final intDefault timeout of one whole exchange, body included, in seconds.static final StringThe media type BlueMind's REST API reads and answers.static final intThe longest answer read from this API: a list of a few entries. -
Constructor Summary
ConstructorsConstructorDescriptionBlueMindRestClient(HttpClient httpClient, BlueMindSessionCache sessions, Duration requestTimeout) A client over a handed-in transport: the consumer's configured timeouts, and the seam the protocol tests use.BlueMindRestClient(BlueMindSessionCache sessions) A client on the default transport and timeouts. -
Method Summary
Modifier and TypeMethodDescriptionbooleanWhether the source's configured credentials are a login and password this API can take, without calling the server.static StringThe REST root of a declared server: its scheme, host and port, and nothing a caller supplies — no user part, no path, no query.<T> Tcall(BlueMindAccountSource source, Function<BlueMindSession, T> job) Runs one job inside a REST session opened as the account: the session kept for that account when there is one, a fresh login otherwise.static voidcheckGateway(int status, String method, URI uri) A gateway status becomes the unreachable failure.static StringencodeSegment(String segment) One path segment, percent-encoded outside RFC 3986's unreserved set, so that nothing a caller passes can leave its segment.voidDrops the sessions kept for one eXo user on one server and closes them, because eXo itself changed what that account is: new credentials, a reconnection, a disconnection.voidDrops every kept session, because the declared servers changed under them.voidforgetNamespace(String serverPrefix) Drops the kept sessions of one consumer, because its declared servers changed under them: every session whose key names a server starting with the prefix.voidDrops the session kept for exactly one key and closes it: the narrow counterpart offorget(java.lang.String, java.lang.String), for a consumer that knows which account changed.static booleanisServerOrApiPath(String rawPath) Whether a configured address's path names the server's root or its/api, a trailing slash aside.login(BlueMindAccount account) Opens a REST session:IAuthentication.login.static HttpClientnewHttpClient(Duration connectTimeout) The transport this client expects: it never follows a redirect and gives up connecting after the given delay.open(BlueMindAccountSource source) Opens a REST session as the account, for an operation of several calls that must share it — a read, a change, the read back — instead of one session percall(org.exoplatform.bluemind.client.BlueMindAccountSource, java.util.function.Function<org.exoplatform.bluemind.client.BlueMindSession, T>).tools.jackson.databind.JsonNodeParses a JSON answer.Opens a session in the name of another account, from a technical account's own session — BlueMind's sudo,POST /api/auth/_su?login=…with the technical key inAPI_KEY_HEADERand no body at all.static StringOne text member of a JSON object.
-
Field Details
-
API_KEY_HEADER
The header BlueMind's REST API reads a session key from.- See Also:
-
JSON_MEDIA_TYPE
The media type BlueMind's REST API reads and answers.- See Also:
-
DEFAULT_CONNECT_TIMEOUT_SECONDS
public static final int DEFAULT_CONNECT_TIMEOUT_SECONDSDefault connect timeout, in seconds.- See Also:
-
DEFAULT_REQUEST_TIMEOUT_SECONDS
public static final int DEFAULT_REQUEST_TIMEOUT_SECONDSDefault timeout of one whole exchange, body included, in seconds.- See Also:
-
MAX_BODY_BYTES
public static final int MAX_BODY_BYTESThe longest answer read from this API: a list of a few entries.- See Also:
-
-
Constructor Details
-
BlueMindRestClient
A client on the default transport and timeouts.- Parameters:
sessions- the sessions kept per account;BlueMindSessionCache.unpooled()keeps none
-
BlueMindRestClient
public BlueMindRestClient(HttpClient httpClient, BlueMindSessionCache sessions, Duration requestTimeout) A client over a handed-in transport: the consumer's configured timeouts, and the seam the protocol tests use.- Parameters:
httpClient- the transport; it must not follow redirectssessions- the sessions kept per accountrequestTimeout- how long one whole exchange may take, at least one second- Throws:
IllegalArgumentException- when the transport follows redirects: a followed redirect would carry the session key, or the login's password, to wherever the server pointed
-
-
Method Details
-
newHttpClient
The transport this client expects: it never follows a redirect and gives up connecting after the given delay.- Parameters:
connectTimeout- the connect timeout, at least one second- Returns:
- the transport
-
call
Runs one job inside a REST session opened as the account: the session kept for that account when there is one, a fresh login otherwise. It isopen(org.exoplatform.bluemind.client.BlueMindAccountSource)around the job, the session closed when the job ends.What is reused, and for whom. The session is looked up by the key the source names for its own credentials (
BlueMindAccountSource.sessionKey()), never by a key handed in beside them, so a session is only ever reused for the source that says its credentials act as that account. A source naming no key, or a key naming no eXo login or no target account, opens its own session and closes it.Never to another address. The kept session records the REST root it was opened at; a source that now resolves elsewhere — an administrator changed the declared server's URL — does not reuse it, and the session is closed at the address that knows it.
An expired or refused session re-logs in once. BlueMind expires a session on its own clock, so the kept entry's lifetime is set well below any plausible server-side one and the refusal is handled rather than predicted: a 401 on a reused session drops the entry, opens another and sends the request again — once per opened session, tracked on the session itself, so a server refusing everything costs one extra login and never a loop.
A refused login is retried once on fresh material when the source says its material may be stale (
BlueMindAccountSource.refreshable(), EXO-89649) — on any login refusal, 401, 403 or a 200 answeringBad, because a login endpoint states a refused password in any of the three.Logout. A session nothing keeps is logged out when the job ends — the unkeyed call, the store that had no room for another account, and the session another thread's took the place of. A kept one is closed when eXo drops it on purpose (
forget(java.lang.String, java.lang.String),invalidate(org.exoplatform.bluemind.client.model.BlueMindSessionKey)), and otherwise expires on BlueMind's own clock. A session refused by BlueMind is already closed on its side and is dropped without one.- Type Parameters:
T- what the job produces- Parameters:
source- the account's server and credentials, and the key its session may be kept underjob- the calls to make with the session- Returns:
- what the job produced
- Throws:
UnsupportedOperationException- when the configured credentials are not a login and passwordBlueMindAuthenticationException- when BlueMind refuses the loginBlueMindUnreachableException- when the server cannot be reachedBlueMindException- when the login answers anything else
-
open
Opens a REST session as the account, for an operation of several calls that must share it — a read, a change, the read back — instead of one session percall(org.exoplatform.bluemind.client.BlueMindAccountSource, java.util.function.Function<org.exoplatform.bluemind.client.BlueMindSession, T>). The rules are those ofcall(org.exoplatform.bluemind.client.BlueMindAccountSource, java.util.function.Function<org.exoplatform.bluemind.client.BlueMindSession, T>); closing the session (BlueMindSession.close()) logs out a session nothing keeps, and leaves a kept one for the next caller.- Parameters:
source- the account's server and credentials, and the key its session may be kept under- Returns:
- the session, to close
- Throws:
UnsupportedOperationException- when the configured credentials are not a login and passwordBlueMindAuthenticationException- when BlueMind refuses the loginBlueMindUnreachableException- when the server cannot be reachedBlueMindException- when the login answers anything else
-
acceptsCredentials
Whether the source's configured credentials are a login and password this API can take, without calling the server.- Parameters:
source- the account's server and credentials- Returns:
- true when the source produces an account
-
forget
Drops the sessions kept for one eXo user on one server and closes them, because eXo itself changed what that account is: new credentials, a reconnection, a disconnection. The logout is best-effort and never fails the caller.- Parameters:
server- the declared server registrationexoLogin- the eXo login whose sessions go
-
forgetAll
public void forgetAll()Drops every kept session, because the declared servers changed under them. The sessions are not closed — the entries carrying them are gone — so they expire on BlueMind's own clock. -
forgetNamespace
Drops the kept sessions of one consumer, because its declared servers changed under them: every session whose key names a server starting with the prefix. Another consumer's sessions, on the same node and the same cache, stay kept. As withforgetAll(), the sessions are not closed and expire on BlueMind's own clock.- Parameters:
serverPrefix- the namespace the consumer'sBlueMindSessionKey.server()values start with; null or empty drops nothing
-
invalidate
Drops the session kept for exactly one key and closes it: the narrow counterpart offorget(java.lang.String, java.lang.String), for a consumer that knows which account changed.- Parameters:
key- who the session acts as
-
login
Opens a REST session:IAuthentication.login. The password is the body, encoded as a JSON string under exactlyContent-Type: application/json; see the class comment for why no other shape is safe.Besides the key, the answer names the authenticated user:
authUser.uidandauthUser.domainUid, which a call addressed to an account needs in its path and uses to check that the account it is about to edit is the one the session opened (EXO-90277); and the zone inauthUser.settings.timezone. Their absence is not a refusal: a caller needing them says so itself.- Specified by:
loginin interfaceBlueMindAuthenticator- Parameters:
account- the server, login, password and origin- Returns:
- the session BlueMind opened
- Throws:
BlueMindAuthenticationException- on 401, 403, or a 200 whose status is notOkor that carries no keyBlueMindUnreachableException- when the server cannot be reached, does not answer in time, or answers 502, 503 or 504BlueMindLoginNotFoundException- on 404: no login endpoint at that serverBlueMindException- on a missing piece of configuration, any other status, or an answer that is not JSON
-
sudo
Opens a session in the name of another account, from a technical account's own session — BlueMind's sudo,POST /api/auth/_su?login=…with the technical key inAPI_KEY_HEADERand no body at all.Not every account may sudo, and which ones may is not settled: one live 5.7 instance refused every sudo from a technical account of a regular domain, another accepted them. So nothing is assumed about the right and the answer is read: a refusal comes back as HTTP 200 carrying
status: Bad. Only a 401 refuses the request's own authentication — a dead or logged-out technical session; a 403 came from the load balancer in front of BlueMind on a live instance, so it says nothing about the session.- Specified by:
sudoin interfaceBlueMindAuthenticator- Parameters:
apiRoot- any address on the servertechnicalAuthKey- the technical account's session keytargetLogin- the account to act as- Returns:
- the session BlueMind opened for that account
- Throws:
BlueMindSessionRefusedException- on 401BlueMindTargetRefusedException- on a 200 whose status is notOkand that carries no message: the target account is refusedBlueMindAuthenticationException- on any other 200 whose status is notOk, or that carries no keyBlueMindUnreachableException- when the server cannot be reached, does not answer in time, or answers 502, 503 or 504BlueMindException- on a missing argument or any other status
-
parse
Parses a JSON answer.- Parameters:
body- the bodyuri- the request, for the message- Returns:
- the tree
- Throws:
BlueMindException- when the body is not JSON
-
textOf
One text member of a JSON object.- Parameters:
node- the object, possibly nullname- the member- Returns:
- its text, trimmed, or null when absent, null, blank or not a value
-
encodeSegment
One path segment, percent-encoded outside RFC 3986's unreserved set, so that nothing a caller passes can leave its segment. An empty segment and the dot segments.and..are refused: they are made of unreserved characters only, so they would travel as they are, and a URI resolver reads them as "this level" and "one level up" — the path would then address another resource than the one it spells.- Parameters:
segment- the decoded segment- Returns:
- the encoded segment
- Throws:
BlueMindException- when the segment is null, empty,.or..
-
apiRootOf
The REST root of a declared server: its scheme, host and port, and nothing a caller supplies — no user part, no path, no query.- Parameters:
address- any address on the server- Returns:
scheme://host[:port]- Throws:
BlueMindException- when the address is missing or names no usable http(s) host
-
isServerOrApiPath
Whether a configured address's path names the server's root or its/api, a trailing slash aside. These are the only paths a configured BlueMind address may carry:apiRootOf(URI)keeps an address's scheme, host and port, every call appends/apiitself, and any other path is dropped silently. The add-on applies this one rule wherever an administrator names the server: the mail endpoint's core address and the sudo credentials provider's API address.- Parameters:
rawPath- the address's raw path, null or empty for none- Returns:
- true for none,
/,/apiand/api/
-
checkGateway
A gateway status becomes the unreachable failure.- Parameters:
status- the statusmethod- the methoduri- the request, without its query- Throws:
BlueMindUnreachableException- on 502, 503 and 504
-