Class BlueMindRestClient

java.lang.Object
org.exoplatform.bluemind.client.BlueMindRestClient
All Implemented Interfaces:
BlueMindAuthenticator

public class BlueMindRestClient extends Object implements BlueMindAuthenticator
The one way eXo talks to BlueMind's own REST API: one transport, one login door, one sudo door, one session reuse policy: the kept-session rules (EXO-90397), the refreshed-material retry (EXO-89649), the sudo door and the whole-exchange deadline (EXO-89647). Plain Java: no Spring, no eXo type; the consumer hands values in through 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 /api root BlueMind serves its REST API under (RestServiceApiDescriptionParser prefixes every path with /api). Nothing a caller passes can name a host, a port, a user part or a path outside /api/: a BlueMindSession refuses such a path before anything is sent, and encodeSegment(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 exactly Content-Type: application/json — what BlueMind's own client proxy sends (ClientProxyGenerator, ByMimeTypeCodec.encode); BlueMind picks the body codec by the exact Content-Type value, so a text/plain carrying a charset parameter would be read as JSON, refused with a 500 and logged by BlueMind with the password in it. The answer is a LoginResponse whose authKey travels in API_KEY_HEADER. An HTTP 200 is not a success: a refused login answers 200 with status: 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>) (BlueMindSession exposes 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 Details

    • API_KEY_HEADER

      public static final String API_KEY_HEADER
      The header BlueMind's REST API reads a session key from.
      See Also:
    • JSON_MEDIA_TYPE

      public static final String 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_SECONDS
      Default connect timeout, in seconds.
      See Also:
    • DEFAULT_REQUEST_TIMEOUT_SECONDS

      public static final int DEFAULT_REQUEST_TIMEOUT_SECONDS
      Default timeout of one whole exchange, body included, in seconds.
      See Also:
    • MAX_BODY_BYTES

      public static final int MAX_BODY_BYTES
      The longest answer read from this API: a list of a few entries.
      See Also:
  • Constructor Details

    • BlueMindRestClient

      public BlueMindRestClient(BlueMindSessionCache sessions)
      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 redirects
      sessions - the sessions kept per account
      requestTimeout - 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

      public static HttpClient newHttpClient(Duration connectTimeout)
      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

      public <T> T call(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. It is open(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 answering Bad, 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 under
      job - the calls to make with the session
      Returns:
      what the job produced
      Throws:
      UnsupportedOperationException - when the configured credentials are not a login and password
      BlueMindAuthenticationException - when BlueMind refuses the login
      BlueMindUnreachableException - when the server cannot be reached
      BlueMindException - when the login answers anything else
    • open

      public BlueMindSession 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 per call(org.exoplatform.bluemind.client.BlueMindAccountSource, java.util.function.Function<org.exoplatform.bluemind.client.BlueMindSession, T>). The rules are those of call(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 password
      BlueMindAuthenticationException - when BlueMind refuses the login
      BlueMindUnreachableException - when the server cannot be reached
      BlueMindException - when the login answers anything else
    • acceptsCredentials

      public boolean acceptsCredentials(BlueMindAccountSource source)
      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

      public void forget(String server, String exoLogin)
      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 registration
      exoLogin - 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

      public void forgetNamespace(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. Another consumer's sessions, on the same node and the same cache, stay kept. As with forgetAll(), the sessions are not closed and expire on BlueMind's own clock.
      Parameters:
      serverPrefix - the namespace the consumer's BlueMindSessionKey.server() values start with; null or empty drops nothing
    • invalidate

      public void invalidate(BlueMindSessionKey key)
      Drops the session kept for exactly one key and closes it: the narrow counterpart of forget(java.lang.String, java.lang.String), for a consumer that knows which account changed.
      Parameters:
      key - who the session acts as
    • login

      public BlueMindLogin login(BlueMindAccount account)
      Opens a REST session: IAuthentication.login. The password is the body, encoded as a JSON string under exactly Content-Type: application/json; see the class comment for why no other shape is safe.

      Besides the key, the answer names the authenticated user: authUser.uid and authUser.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 in authUser.settings.timezone. Their absence is not a refusal: a caller needing them says so itself.

      Specified by:
      login in interface BlueMindAuthenticator
      Parameters:
      account - the server, login, password and origin
      Returns:
      the session BlueMind opened
      Throws:
      BlueMindAuthenticationException - on 401, 403, or a 200 whose status is not Ok or that carries no key
      BlueMindUnreachableException - when the server cannot be reached, does not answer in time, or answers 502, 503 or 504
      BlueMindLoginNotFoundException - on 404: no login endpoint at that server
      BlueMindException - on a missing piece of configuration, any other status, or an answer that is not JSON
    • sudo

      public BlueMindLogin sudo(URI apiRoot, String technicalAuthKey, String targetLogin)
      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 in API_KEY_HEADER and 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:
      sudo in interface BlueMindAuthenticator
      Parameters:
      apiRoot - any address on the server
      technicalAuthKey - the technical account's session key
      targetLogin - the account to act as
      Returns:
      the session BlueMind opened for that account
      Throws:
      BlueMindSessionRefusedException - on 401
      BlueMindTargetRefusedException - on a 200 whose status is not Ok and that carries no message: the target account is refused
      BlueMindAuthenticationException - on any other 200 whose status is not Ok, or that carries no key
      BlueMindUnreachableException - when the server cannot be reached, does not answer in time, or answers 502, 503 or 504
      BlueMindException - on a missing argument or any other status
    • parse

      public tools.jackson.databind.JsonNode parse(String body, URI uri)
      Parses a JSON answer.
      Parameters:
      body - the body
      uri - the request, for the message
      Returns:
      the tree
      Throws:
      BlueMindException - when the body is not JSON
    • textOf

      public static String textOf(tools.jackson.databind.JsonNode node, String name)
      One text member of a JSON object.
      Parameters:
      node - the object, possibly null
      name - the member
      Returns:
      its text, trimmed, or null when absent, null, blank or not a value
    • encodeSegment

      public static String encodeSegment(String segment)
      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

      public static String apiRootOf(URI address)
      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

      public static boolean isServerOrApiPath(String rawPath)
      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 /api itself, 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, /, /api and /api/
    • checkGateway

      public static void checkGateway(int status, String method, URI uri)
      A gateway status becomes the unreachable failure.
      Parameters:
      status - the status
      method - the method
      uri - the request, without its query
      Throws:
      BlueMindUnreachableException - on 502, 503 and 504