java.lang.Object
io.helidon.integrations.common.rest.RestApiBase
All Implemented Interfaces:
RestApi
Direct Known Subclasses:
VaultRestApi

public abstract class RestApiBase extends Object implements RestApi
Base REST API implementation. Each integration module is expected to have its own implementation of this class to handle system specific operations, such as security, processing of headers etc.
  • Constructor Details

    • RestApiBase

      protected RestApiBase(RestApi.Builder<?,?> builder)
      A new instance, requires a subclass of the RestApi.Builder.
      Parameters:
      builder - builder to set this instance from
  • Method Details

    • invoke

      public <T extends ApiResponse> T invoke(Method method, String path, ApiRequest<?> request, ApiResponse.Builder<?,T> responseBuilder)
      Description copied from interface: RestApi
      Invoke a request that is not expected to yield an entity.
      Specified by:
      invoke in interface RestApi
      Type Parameters:
      T - type of the response
      Parameters:
      method - HTTP method to invoke
      path - path to invoke
      request - request to use, should be an instance of ApiJsonRequest if and entity is desired
      responseBuilder - builder to construct response from API call
      Returns:
      response
    • invokeWithResponse

      public <T extends ApiEntityResponse> T invokeWithResponse(Method method, String path, ApiRequest<?> request, ApiEntityResponse.Builder<?,T,JsonObject> responseBuilder)
      Description copied from interface: RestApi
      Invoke a request that is expected to yield an entity.
      Specified by:
      invokeWithResponse in interface RestApi
      Type Parameters:
      T - type of the response
      Parameters:
      method - HTTP method to invoke
      path - path to invoke
      request - request to use, should be an instance of ApiJsonRequest if and entity is desired
      responseBuilder - builder to construct response from API call
      Returns:
      future with entity and metadata if successful, future with error otherwise
    • invokeBytesRequest

      public <T extends ApiResponse> T invokeBytesRequest(Method method, String path, ApiRequest<?> apiRequest, InputStream is, ApiResponse.Builder<?,T> responseBuilder)
      Description copied from interface: RestApi
      The request media type should be provided in request, falls back to MediaTypes.APPLICATION_OCTET_STREAM.
      Specified by:
      invokeBytesRequest in interface RestApi
      Type Parameters:
      T - type of the response
      Parameters:
      method - method to invoke
      path - path to invoke
      apiRequest - request used to configure query parameters and headers
      is - entity bytes
      responseBuilder - builder to construct response from API call
      Returns:
      future with the response or error
    • invokeEntityResponse

      public <R, T extends ApiOptionalResponse<R>> T invokeEntityResponse(Method method, String path, ApiRequest<?> request, ApiOptionalResponse.BuilderBase<?,T,InputStream,R> responseBuilder)
      Description copied from interface: RestApi
      Invoke API call that is expected to return bytes as a publisher.

      The accepted media type must be provided in request, falls back to MediaTypes.APPLICATION_OCTET_STREAM.

      Specified by:
      invokeEntityResponse in interface RestApi
      Type Parameters:
      R - type of the optional part of the response
      T - type of the response
      Parameters:
      method - method to invoke
      path - path to invoke
      request - request used to configure query parameters and headers
      responseBuilder - builder to construct response from API call with appropriate processor to handle the returned publisher
      Returns:
      future with the response
    • invokeBytesResponse

      public <R, T extends ApiOptionalResponse<R>> T invokeBytesResponse(Method method, String path, ApiRequest<?> request, ApiOptionalResponse.BuilderBase<?,T,byte[],R> responseBuilder)
      Description copied from interface: RestApi
      Invoke API call that is expected to return bytes. This method collects all bytes in memory, so it cannot be used for large data. See RestApi.invokeEntityResponse(io.helidon.http.Method, String, ApiRequest, BuilderBase).

      The accepted media type must be provided in request, falls back to MediaTypes.APPLICATION_OCTET_STREAM.

      Specified by:
      invokeBytesResponse in interface RestApi
      Type Parameters:
      R - type of the optional part of the response
      T - type of the response
      Parameters:
      method - method to invoke
      path - path to invoke
      request - request used to configure query parameters and headers
      responseBuilder - builder to construct response from API call with appropriate processor to handle the returned bytes
      Returns:
      response
    • invokeOptional

      public <R, T extends ApiOptionalResponse<R>> T invokeOptional(Method method, String path, ApiRequest<?> request, ApiOptionalResponse.BuilderBase<?,T,JsonObject,R> responseBuilder)
      Description copied from interface: RestApi
      Invoke a request that may yield an entity. The entity is expected to be missing if Status.NOT_FOUND_404 is returned by the API call (and for some other cases, such as not modified).
      Specified by:
      invokeOptional in interface RestApi
      Type Parameters:
      R - type of the optional part of the response
      T - type of the response
      Parameters:
      method - HTTP method to invoke
      path - path to invoke
      request - request to use, should be an instance of ApiJsonRequest if and entity is desired
      responseBuilder - response builder with appropriate processor to create the optional part
      Returns:
      response
    • responseSupplier

      protected Supplier<HttpClientResponse> responseSupplier(Method method, String path, ApiRequest<?> request, String requestId)
      Create a response supplier from the request. This method checks if there is a payload, and prepares the supplier based on this information.
      Parameters:
      method - HTTP method to invoke
      path - path to invoke
      request - request that may contain a JSON entity
      requestId - request ID to use for this request
      Returns:
      supplier of response that is used with fault tolerance
    • addQueryParams

      protected void addQueryParams(HttpClientRequest request, Map<String,List<String>> queryParams)
      Add HTTP query parameters.
      Parameters:
      request - client request
      queryParams - query parameters
    • addHeaders

      protected void addHeaders(HttpClientRequest request, Map<String,List<String>> headers)
      Add HTTP headers.
      Parameters:
      request - client request
      headers - headers to add
    • handleBytesResponse

      protected <R, T extends ApiOptionalResponse<R>> T handleBytesResponse(String path, ApiRequest<?> request, Method method, String requestId, HttpClientResponse response, ApiOptionalResponse.BuilderBase<?,T,byte[],R> responseBuilder)
      Handle bytes response for optional bytes entity. This method checks if this was a success and if the response should contain an entity. For success, it returns a response using the provided response builder.
      Type Parameters:
      R - type of the optional part of the response
      T - type of the response
      Parameters:
      path - requested path
      request - API request
      method - HTTP method
      requestId - request ID
      response - the web client response
      responseBuilder - builder to configure success response
      Returns:
      typed response
      Throws:
      ApiRestException - if an error occurs
    • handleEntityResponse

      protected <R, T extends ApiOptionalResponse<R>> T handleEntityResponse(String path, ApiRequest<?> request, Method method, String requestId, HttpClientResponse response, ApiOptionalResponse.BuilderBase<?,T,InputStream,R> responseBuilder)
      Handle response for optional publisher entity. This method checks if this was a success and if the response should contain an entity. For success, it returns a response using the provided response builder.
      Type Parameters:
      R - type of the optional part of the response
      T - type of the response
      Parameters:
      path - requested path
      request - API request
      method - HTTP method
      requestId - request ID
      response - the web client response
      responseBuilder - builder to configure success response
      Returns:
      typed response
      Throws:
      ApiRestException - if an error occurs
    • isSuccess

      protected boolean isSuccess(String path, ApiRequest<?> request, Method method, String requestId, Status status)
      Provide information whether the response is a success response for requests with optional entity.
      Parameters:
      path - requested path
      request - API request
      method - HTTP method
      requestId - request ID
      status - returned HTTP status
      Returns:
      true for success states, false for errors
    • isEntityExpected

      protected boolean isEntityExpected(String path, ApiRequest<?> request, Method method, String requestId, Status status)
      This method is only called for methods that return an optional entity. If a method (such as RestApi.invokeWithResponse(Method, String, ApiRequest, io.helidon.integrations.common.rest.ApiEntityResponse.Builder)) receives a status that would not yield an entity (such as 404), it is automatically an error. Also this method is never called for codes in the success family.
      Parameters:
      path - requested path
      request - API request
      method - HTTP method
      requestId - request ID
      status - returned HTTP status
      Returns:
      true if entity is expected, false otherwise (such as for 404 status code)
    • handleOptionalJsonResponse

      protected <R, T extends ApiOptionalResponse<R>> T handleOptionalJsonResponse(String path, ApiRequest<?> request, Method method, String requestId, HttpClientResponse response, ApiOptionalResponse.BuilderBase<?,T,JsonObject,R> responseBuilder)
      Handle response for optional JSON entity. This method checks if this was a success and if the response should contain an entity. For success, it returns a response using the provided response builder.
      Type Parameters:
      R - type of the optional part of the response
      T - type of the response
      Parameters:
      path - requested path
      request - API request
      method - HTTP method
      requestId - request ID
      response - the web client response
      responseBuilder - builder to configure success response
      Returns:
      typed response
      Throws:
      ApiRestException - if an error occurs
    • emptyResponse

      protected <T> T emptyResponse(String path, ApiRequest<?> request, Method method, String requestId, HttpClientResponse response, ResponseBuilder<?,T,?> responseBuilder)
      Empty response, may be because of a Status.NOT_FOUND_404, or some other status, such as Status.NOT_MODIFIED_304.
      Type Parameters:
      T - type of the response
      Parameters:
      path - requested path
      request - original request
      method - HTTP method
      requestId - ID of the request
      response - actual response where we do not expect an entity
      responseBuilder - builder to create a response instance
      Returns:
      typed response with no entity
    • jsonOkResponse

      protected <T> T jsonOkResponse(String path, ApiRequest<?> request, Method method, String requestId, HttpClientResponse response, JsonObject json, ResponseBuilder<?,T,JsonObject> responseBuilder)
      Builds the response using the response builder provided. This is the last chance to update the response builder with system specific information.
      Type Parameters:
      T - type of the response
      Parameters:
      path - requested path
      request - original request
      method - HTTP method
      requestId - ID of the request
      response - actual response where we do not expect an entity
      json - the JsonObject parsed from entity
      responseBuilder - builder to create a response instance
      Returns:
      typed response
    • handleJsonResponse

      protected <T extends ApiEntityResponse> T handleJsonResponse(String path, ApiRequest<?> request, Method method, String requestId, HttpClientResponse response, ApiEntityResponse.Builder<?,T,JsonObject> responseBuilder)
      Reads JsonObject from response entity and either calls the jsonOkResponse.
      Type Parameters:
      T - type of the response
      Parameters:
      path - requested path
      request - original request
      method - HTTP method
      requestId - ID of the request
      response - actual response where we do not expect an entity
      responseBuilder - builder to create a response instance
      Returns:
      typed response
      Throws:
      ApiRestException - if an error occurs
    • handleResponse

      protected <T extends ApiResponse> T handleResponse(String path, ApiRequest<?> request, Method method, String requestId, HttpClientResponse response, ApiResponse.Builder<?,T> responseBuilder)
      Handle response for a request not expecting an entity.
      Type Parameters:
      T - type of the response
      Parameters:
      path - requested path
      request - original request
      method - HTTP method
      requestId - ID of the request
      response - actual response where we do not expect an entity
      responseBuilder - builder to create a response instance
      Returns:
      typed response
      Throws:
      ApiRestException - if an error occurs
    • responseError

      protected ApiRestException responseError(String path, ApiRequest<?> request, Method method, String requestId, HttpClientResponse response)
      Create an ApiRestException. This method attempts to read the response entity as a string, parse it into a JsonObject and depending on result, calls methods to create a proper exception.
      Parameters:
      path - requested path
      request - original request
      method - HTTP method
      requestId - ID of the request
      response - actual response where we do not expect an entity
      Returns:
      ApiException
    • readErrorFailedEntity

      protected ApiRestException readErrorFailedEntity(String path, ApiRequest<?> request, Method method, String requestId, HttpClientResponse response, Throwable throwable)
      Read error information when we failed to read response entity.
      Parameters:
      path - requested path
      request - original request
      method - HTTP method
      requestId - ID of the request
      response - actual response where we do not expect an entity
      throwable - throwable that caused this problem (such as parsing exception)
      Returns:
      an ApiRestException
    • readError

      protected ApiRestException readError(String path, ApiRequest<?> request, Method method, String requestId, HttpClientResponse response, String entity)
      Read error with an entity that failed to be parsed into a JSON object.
      Parameters:
      path - requested path
      request - original API request
      method - HTTP method
      requestId - request ID
      response - web client response with entity consumed
      entity - entity as a string
      Returns:
      an ApiRestException
    • readError

      protected ApiRestException readError(String path, ApiRequest<?> request, Method method, String requestId, HttpClientResponse response)
      Read error with no entity (content length set to 0).
      Parameters:
      path - requested path
      request - original API request
      method - HTTP method
      requestId - request ID
      response - web client response with entity consumed
      Returns:
      an ApiRestException
    • readError

      protected ApiRestException readError(String path, ApiRequest<?> request, Method method, String requestId, HttpClientResponse response, JsonObject errorObject)
      Read error with a JSON entity.
      Parameters:
      path - requested path
      request - original API request
      method - HTTP method
      requestId - request ID
      response - web client response with entity consumed
      errorObject - entity as a JSON object
      Returns:
      an ApiRestException
    • noEntityOkResponse

      protected <T extends ApiResponse> T noEntityOkResponse(String path, ApiRequest<?> request, Method method, String requestId, HttpClientResponse response, ApiResponse.Builder<?,T> responseBuilder)
      Create a response for no entity. This method builds the response from builder.
      Type Parameters:
      T - type of the response
      Parameters:
      path - requested path
      request - original API request
      method - HTTP method
      requestId - request ID
      response - web client response with entity consumed
      responseBuilder - response builder
      Returns:
      typed response
    • requestJsonPayload

      protected Supplier<HttpClientResponse> requestJsonPayload(String path, ApiRequest<?> request, Method method, String requestId, HttpClientRequest requestBuilder, JsonObject jsonObject)
      Create a supplier for a response with JSON request. Defaults to "() -> clientRequest.submit(jsonObject)". Also configures content type and accept headers.
      Parameters:
      path - path requested
      request - API request
      method - HTTP method
      requestId - ID of this request
      requestBuilder - HttpClientRequest request builder
      jsonObject - JSON object that should be sent as a request entity
      Returns:
      supplier of a client response
    • requestBytesPayload

      protected Supplier<HttpClientResponse> requestBytesPayload(String path, ApiRequest<?> request, Method method, String requestId, HttpClientRequest requestBuilder, InputStream is)
      Create a supplier for a response with publisher request. Defaults to "() -> clientRequest.submit(publisher)". Also configures content type and accept headers.
      Parameters:
      path - path requested
      request - API request
      method - HTTP method
      requestId - ID of this request
      requestBuilder - HttpClientRequest request builder
      is - entity input stream
      Returns:
      supplier of a client response
    • requestPayload

      protected Supplier<HttpClientResponse> requestPayload(String path, ApiRequest<?> request, Method method, String requestId, HttpClientRequest requestBuilder)
      Create a supplier for a response. Defaults to requestBuilder::request.
      Parameters:
      path - path requested
      request - API request
      method - HTTP method
      requestId - ID of this request
      requestBuilder - HttpClientRequest request builder
      Returns:
      supplier of a client response
    • updateRequestBuilder

      protected HttpClientRequest updateRequestBuilder(HttpClientRequest requestBuilder, String path, ApiRequest<?> request, Method method, String requestId)
      Update request builder with no request payload. Default implementation does nothing.
      Parameters:
      requestBuilder - current request builder
      path - path to be executed
      request - API request
      method - method
      requestId - request ID
      Returns:
      updated builder
    • updateRequestBuilderBytesPayload

      protected HttpClientRequest updateRequestBuilderBytesPayload(HttpClientRequest requestBuilder, String path, ApiRequest<?> request, Method method, String requestId)
      Update request builder with publisher request payload. Default implementation does nothing.
      Parameters:
      requestBuilder - current request builder
      path - path to be executed
      request - API request
      method - method
      requestId - request ID
      Returns:
      updated builder
    • updateRequestBuilder

      protected HttpClientRequest updateRequestBuilder(HttpClientRequest requestBuilder, String path, ApiRequest<?> request, Method method, String requestId, JsonObject jsonObject)
      Update request builder with no request payload. Default implementation does nothing.
      Parameters:
      requestBuilder - current request builder
      path - path to be executed
      request - API request
      method - method
      requestId - request ID
      jsonObject - json object with the request
      Returns:
      updated builder
    • updateRequestBuilderCommon

      protected HttpClientRequest updateRequestBuilderCommon(HttpClientRequest requestBuilder, String path, ApiRequest<?> request, Method method, String requestId)
      Update request builder used by all default implementation in RestApiBase.
      Parameters:
      requestBuilder - current request builder
      path - path to be executed
      request - API request
      method - method
      requestId - request ID
      Returns:
      updated builder
    • requestId

      protected String requestId(ApiRequest<?> request)
      Attempts to find a good request id. This looks in the following (sequentially):
      Parameters:
      request - request
      Returns:
      request ID
    • webClient

      protected WebClient webClient()
      WebClient to be used to invoke requests.
      Returns:
      web client
    • ftHandler

      protected FtHandler ftHandler()
      Fault tolerance handler to use to invoke requests.
      Returns:
      fault tolerance handler
    • jsonBuilderFactory

      protected JsonBuilderFactory jsonBuilderFactory()
      JSON builder factory.
      Returns:
      builder factory
    • jsonReaderFactory

      protected JsonReaderFactory jsonReaderFactory()
      JSON reader factory.
      Returns:
      reader factory
    • jsonWriterFactory

      protected JsonWriterFactory jsonWriterFactory()
      JSON writer factory.
      Returns:
      writer factory