Class KiwiResponses

java.lang.Object
org.kiwiproject.jaxrs.KiwiResponses

public final class KiwiResponses extends Object
Static utilities related to evaluating and acting upon Jakarta REST responses. For example, this class contains utilities to determine whether responses are successful or not, whether they are a specific type of response, and to perform actions (or throw exceptions) based on success or failure.

These utilities are intended mainly to be used in classes that make HTTP requests and need to evaluate and/or take action with the HTTP responses.

See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    static void
    accept(jakarta.ws.rs.core.Response response, Consumer<jakarta.ws.rs.core.Response> responseConsumer)
    Given a Response, perform some action using the supplied consumer.
    static void
    accept(Supplier<jakarta.ws.rs.core.Response> responseSupplier, Consumer<jakarta.ws.rs.core.Response> responseConsumer, Consumer<RuntimeException> exceptionConsumer)
    Given a Response Supplier, perform some action using one of the supplied consumers.
    static <T> T
    apply(jakarta.ws.rs.core.Response response, Function<jakarta.ws.rs.core.Response,T> fun)
    Given a Response, perform an action tha returns a result using the given function.
    static <T> T
    apply(Supplier<jakarta.ws.rs.core.Response> responseSupplier, Function<jakarta.ws.rs.core.Response,T> fun, Function<RuntimeException,T> exceptionFun)
    Given a Response Supplier, perform an action tha returns a result using one of the given functions.
    static boolean
    clientError(jakarta.ws.rs.core.Response response)
    Check if the given response is in the CLIENT_ERROR (4xx codes) family.
    static void
    closeQuietly(jakarta.ws.rs.core.Response response)
    Closes the given Response, which can be null, swallowing any exceptions and logging them at INFO level.
    static boolean
    created(jakarta.ws.rs.core.Response response)
    Check if the given response has status 201 Created.
    static boolean
    hasFamily(jakarta.ws.rs.core.Response response, jakarta.ws.rs.core.Response.Status.Family family)
    Check if the given response has the expected family.
    static boolean
    hasStatus(jakarta.ws.rs.core.Response response, jakarta.ws.rs.core.Response.Status status)
    Check if the given response has the expected status.
    static boolean
    informational(jakarta.ws.rs.core.Response response)
    Check if the given response is in the INFORMATIONAL (1xx codes) family.
    static boolean
    internalServerError(jakarta.ws.rs.core.Response response)
    Check if the given response has status 500 Internal Server Error.
    mediaType(jakarta.ws.rs.core.Response response)
    Return a media type suitable for use as the value of a corresponding HTTP header.
    static boolean
    notFound(jakarta.ws.rs.core.Response response)
    Check if the given response has status 404 Not Found.
    static boolean
    notSuccessful(int statusCode)
    Check if the given status code is not in Response.Status.Family.SUCCESSFUL.
    static boolean
    notSuccessful(jakarta.ws.rs.core.Response response)
    Check if the given response has a status code that is not in Response.Status.Family.SUCCESSFUL.
    static boolean
    notSuccessful(jakarta.ws.rs.core.Response.Status status)
    Check if the given status code is not in Response.Status.Family.SUCCESSFUL.
    static boolean
    notSuccessful(jakarta.ws.rs.core.Response.Status.Family family)
    Check if the given response family is not Response.Status.Family.SUCCESSFUL.
    static boolean
    notSuccessful(jakarta.ws.rs.core.Response.StatusType status)
    Check if the given status type is not in Response.Status.Family.SUCCESSFUL.
    static boolean
    notSuccessfulAlwaysClosing(jakarta.ws.rs.core.Response response)
    Check if the given response has a status code that is not in Response.Status.Family.SUCCESSFUL, then close the response.
    static boolean
    ok(jakarta.ws.rs.core.Response response)
    Check if the given response has status 200 OK.
    static void
    onFailure(jakarta.ws.rs.core.Response response, Consumer<jakarta.ws.rs.core.Response> failureConsumer)
    Given a Response, perform an action only if it was not successful (failureConsumer).
    static void
    onFailure(Supplier<jakarta.ws.rs.core.Response> responseSupplier, Consumer<jakarta.ws.rs.core.Response> failureConsumer, Consumer<RuntimeException> exceptionConsumer)
    Given a Response Supplier, perform an action only if it was not successful (failureConsumer), or if the Supplier threw an exception (exceptionConsumer).
    static void
    onFailureThrow(jakarta.ws.rs.core.Response response, Function<jakarta.ws.rs.core.Response,? extends RuntimeException> throwingFun)
    Given a Response, throw a (subclass of) RuntimeException for failed responses using throwingFun.
    static void
    onFailureThrow(Supplier<jakarta.ws.rs.core.Response> responseSupplier, Function<jakarta.ws.rs.core.Response,? extends RuntimeException> throwingFun)
    Given a Response Supplier, throw a (subclass of) RuntimeException for failed responses using throwingFun.
    static void
    onSuccess(jakarta.ws.rs.core.Response response, Consumer<jakarta.ws.rs.core.Response> successConsumer)
    Given a Response, perform an action only if it was successful (successConsumer.
    static void
    onSuccess(Supplier<jakarta.ws.rs.core.Response> responseSupplier, Consumer<jakarta.ws.rs.core.Response> successConsumer)
    Given a Response Supplier, perform an action only if it was successful (successConsumer.
    static void
    onSuccessOrFailure(jakarta.ws.rs.core.Response response, Consumer<jakarta.ws.rs.core.Response> successConsumer, Consumer<jakarta.ws.rs.core.Response> failureConsumer)
    Given a Response, perform an action depending on whether it was successful (successConsumer) or failed (failureConsumer).
    static void
    onSuccessOrFailure(Supplier<jakarta.ws.rs.core.Response> responseSupplier, Consumer<jakarta.ws.rs.core.Response> successConsumer, Consumer<jakarta.ws.rs.core.Response> failureConsumer, Consumer<RuntimeException> exceptionConsumer)
    Given a Response Supplier, perform an action depending on whether it was successful (successConsumer), failed (failureConsumer), or if the Supplier threw an exception (exceptionConsumer).
    static void
    onSuccessOrFailureThrow(jakarta.ws.rs.core.Response response, Consumer<jakarta.ws.rs.core.Response> successConsumer, Function<jakarta.ws.rs.core.Response,? extends RuntimeException> throwingFun)
    Given a Response, perform an action if it was successful (successConsumer or throw an exception supplied by throwingFun.
    static void
    onSuccessOrFailureThrow(Supplier<jakarta.ws.rs.core.Response> responseSupplier, Consumer<jakarta.ws.rs.core.Response> successConsumer, Function<jakarta.ws.rs.core.Response,? extends RuntimeException> throwingFun)
    Given a Response Supplier, perform an action if it was successful (successConsumer.
    static <T> T
    onSuccessOrFailureWithResult(jakarta.ws.rs.core.Response response, Function<jakarta.ws.rs.core.Response,T> successFun, Function<jakarta.ws.rs.core.Response,T> failedFun)
    Given a Response, perform an action that returns a result if the response was successful (successFun) or if not successful (failedFun).
    static <T> T
    onSuccessOrFailureWithResult(Supplier<jakarta.ws.rs.core.Response> responseSupplier, Function<jakarta.ws.rs.core.Response,T> successFun, Function<jakarta.ws.rs.core.Response,T> failedFun, Function<RuntimeException,T> exceptionFun)
    Given a Response Supplier, perform an action that returns a result if the response was successful (successFun.
    static <T> Optional<T>
    onSuccessWithResult(jakarta.ws.rs.core.Response response, Function<jakarta.ws.rs.core.Response,T> successFun)
    Given a Response, perform an action that returns a result only if it was successful (successFun).
    static <T> Optional<T>
    onSuccessWithResult(Supplier<jakarta.ws.rs.core.Response> responseSupplier, Function<jakarta.ws.rs.core.Response,T> successFun)
    Given a Response Supplier, perform an action that returns a result only if it was successful (successFun).
    static <T> Optional<T>
    onSuccessWithResultOrFailure(jakarta.ws.rs.core.Response response, Function<jakarta.ws.rs.core.Response,T> successFun, Consumer<jakarta.ws.rs.core.Response> failureConsumer)
    Given a Response, perform an action that returns a result if the response was successful (successFun) or perform an action if the response was unsuccessful (failureConsumer.
    static <T> Optional<T>
    onSuccessWithResultOrFailure(Supplier<jakarta.ws.rs.core.Response> responseSupplier, Function<jakarta.ws.rs.core.Response,T> successFun, Consumer<jakarta.ws.rs.core.Response> failureConsumer, Consumer<RuntimeException> exceptionConsumer)
    Given a Response Supplier, perform an action that returns a result if the response was successful (successFun).
    static <T> T
    onSuccessWithResultOrFailureThrow(jakarta.ws.rs.core.Response response, Function<jakarta.ws.rs.core.Response,T> successFun, Function<jakarta.ws.rs.core.Response,? extends RuntimeException> throwingFun)
    Given a Response, perform an action that returns a result if it was successful (successFun or throw a (subclass of) RuntimeException if it failed (throwingFun).
    static <T> T
    onSuccessWithResultOrFailureThrow(Supplier<jakarta.ws.rs.core.Response> responseSupplier, Function<jakarta.ws.rs.core.Response,T> successFun, Function<jakarta.ws.rs.core.Response,? extends RuntimeException> throwingFun)
    Given a Response Supplier, perform an action that returns a result if it was successful (successFun or throw a (subclass of) RuntimeException if it failed (throwingFun).
    static boolean
    otherFamily(jakarta.ws.rs.core.Response response)
    Check if the given response is in the OTHER (unrecognized status codes) family.
    static boolean
    redirection(jakarta.ws.rs.core.Response response)
    Check if the given response is in the REDIRECTION (3xx codes) family.
    static boolean
    serverError(jakarta.ws.rs.core.Response response)
    Check if the given response is in the SERVER_ERROR (5xx codes) family.
    static boolean
    successful(int statusCode)
    Check if the given status code is in Response.Status.Family.SUCCESSFUL.
    static boolean
    successful(jakarta.ws.rs.core.Response response)
    Check if the given response has a status code in Response.Status.Family.SUCCESSFUL.
    static boolean
    successful(jakarta.ws.rs.core.Response.Status status)
    Check if the given status code is in Response.Status.Family.SUCCESSFUL.
    static boolean
    successful(jakarta.ws.rs.core.Response.Status.Family family)
    Check if the given response family is Response.Status.Family.SUCCESSFUL.
    static boolean
    successful(jakarta.ws.rs.core.Response.StatusType status)
    Check if the given status type is in Response.Status.Family.SUCCESSFUL.
    static boolean
    successfulAlwaysClosing(jakarta.ws.rs.core.Response response)
    Check if the given response has a status code in Response.Status.Family.SUCCESSFUL, then close the response.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • mediaType

      public static Optional<String> mediaType(jakarta.ws.rs.core.Response response)
      Return a media type suitable for use as the value of a corresponding HTTP header. This will consist of the primary type and subtype, e.g. application/json.
      Parameters:
      response - the response object
      Returns:
      Optional that may or may not contain a media type
      See Also:
      • MediaType.toString()
      • MediaType.getType()
      • MediaType.getSubtype()
    • successful

      public static boolean successful(jakarta.ws.rs.core.Response response)
      Check if the given response has a status code in Response.Status.Family.SUCCESSFUL.

      NOTE: This does not close the Response or read the response entity.

      Parameters:
      response - the response object
      Returns:
      true if the response is successful, false otherwise
      See Also:
      • Response.Status.Family.SUCCESSFUL
    • notSuccessful

      public static boolean notSuccessful(jakarta.ws.rs.core.Response response)
      Check if the given response has a status code that is not in Response.Status.Family.SUCCESSFUL.

      NOTE: This does not close the Response or read the response entity.

      Parameters:
      response - the response object
      Returns:
      true if the response is not successful, false otherwise
    • successful

      public static boolean successful(jakarta.ws.rs.core.Response.Status status)
      Check if the given status code is in Response.Status.Family.SUCCESSFUL.
      Parameters:
      status - the response Status object
      Returns:
      true if the status indicates success, false otherwise
    • notSuccessful

      public static boolean notSuccessful(jakarta.ws.rs.core.Response.Status status)
      Check if the given status code is not in Response.Status.Family.SUCCESSFUL.
      Parameters:
      status - the response Status object
      Returns:
      true if the status does not indicate success, false otherwise
    • successful

      public static boolean successful(jakarta.ws.rs.core.Response.StatusType status)
      Check if the given status type is in Response.Status.Family.SUCCESSFUL.
      Parameters:
      status - the response StatusType object
      Returns:
      true if the status indicates success, false otherwise
    • notSuccessful

      public static boolean notSuccessful(jakarta.ws.rs.core.Response.StatusType status)
      Check if the given status type is not in Response.Status.Family.SUCCESSFUL.
      Parameters:
      status - the response StatusType object
      Returns:
      true if the status does not indicate success, false otherwise
    • successful

      public static boolean successful(int statusCode)
      Check if the given status code is in Response.Status.Family.SUCCESSFUL.
      Parameters:
      statusCode - the response status code
      Returns:
      true if the status code indicates success, false otherwise
    • notSuccessful

      public static boolean notSuccessful(int statusCode)
      Check if the given status code is not in Response.Status.Family.SUCCESSFUL.
      Parameters:
      statusCode - the response status code
      Returns:
      true if the status code doe not indicate success, false otherwise
    • successful

      public static boolean successful(jakarta.ws.rs.core.Response.Status.Family family)
      Check if the given response family is Response.Status.Family.SUCCESSFUL.
      Parameters:
      family - the response family
      Returns:
      true if the family is successful, false otherwise
    • notSuccessful

      public static boolean notSuccessful(jakarta.ws.rs.core.Response.Status.Family family)
      Check if the given response family is not Response.Status.Family.SUCCESSFUL.
      Parameters:
      family - the response family
      Returns:
      true if the family is not successful, false otherwise
    • successfulAlwaysClosing

      public static boolean successfulAlwaysClosing(jakarta.ws.rs.core.Response response)
      Check if the given response has a status code in Response.Status.Family.SUCCESSFUL, then close the response.

      NOTE: Closes the response after performing the check.

      Parameters:
      response - the response object
      Returns:
      true if the response is successful, false otherwise
    • notSuccessfulAlwaysClosing

      public static boolean notSuccessfulAlwaysClosing(jakarta.ws.rs.core.Response response)
      Check if the given response has a status code that is not in Response.Status.Family.SUCCESSFUL, then close the response.

      NOTE: Closes the response after performing the check.

      Parameters:
      response - the response object
      Returns:
      true if the response is not successful, false otherwise
    • ok

      public static boolean ok(jakarta.ws.rs.core.Response response)
      Check if the given response has status 200 OK.
      Parameters:
      response - the response object
      Returns:
      true if the response status is 200 OK, false otherwise
    • created

      public static boolean created(jakarta.ws.rs.core.Response response)
      Check if the given response has status 201 Created.
      Parameters:
      response - the response object
      Returns:
      true if the response status is 201 Created, false otherwise
    • notFound

      public static boolean notFound(jakarta.ws.rs.core.Response response)
      Check if the given response has status 404 Not Found.
      Parameters:
      response - the response object
      Returns:
      true if the response status is 404 Not Found, false otherwise
    • internalServerError

      public static boolean internalServerError(jakarta.ws.rs.core.Response response)
      Check if the given response has status 500 Internal Server Error.
      Parameters:
      response - the response object
      Returns:
      true if the response status is 500 Internal Server Error, false otherwise
    • hasStatus

      public static boolean hasStatus(jakarta.ws.rs.core.Response response, jakarta.ws.rs.core.Response.Status status)
      Check if the given response has the expected status.
      Parameters:
      response - the response object
      status - the expected status
      Returns:
      true if the response has the given status, false otherwise
    • informational

      public static boolean informational(jakarta.ws.rs.core.Response response)
      Check if the given response is in the INFORMATIONAL (1xx codes) family.
      Parameters:
      response - the response object
      Returns:
      true if the response is in the INFORMATIONAL family, false otherwise
      See Also:
      • Response.Status.Family.INFORMATIONAL
    • redirection

      public static boolean redirection(jakarta.ws.rs.core.Response response)
      Check if the given response is in the REDIRECTION (3xx codes) family.
      Parameters:
      response - the response object
      Returns:
      true if the response is in the REDIRECTION family, false otherwise
      See Also:
      • Response.Status.Family.REDIRECTION
    • clientError

      public static boolean clientError(jakarta.ws.rs.core.Response response)
      Check if the given response is in the CLIENT_ERROR (4xx codes) family.
      Parameters:
      response - the response object
      Returns:
      true if the response is in the CLIENT_ERROR family, false otherwise
      See Also:
      • Response.Status.Family.CLIENT_ERROR
    • serverError

      public static boolean serverError(jakarta.ws.rs.core.Response response)
      Check if the given response is in the SERVER_ERROR (5xx codes) family.
      Parameters:
      response - the response object
      Returns:
      true if the response is in the SERVER_ERROR family, false otherwise
      See Also:
      • Response.Status.Family.SERVER_ERROR
    • otherFamily

      public static boolean otherFamily(jakarta.ws.rs.core.Response response)
      Check if the given response is in the OTHER (unrecognized status codes) family.
      Parameters:
      response - the response object
      Returns:
      true if the response is in the OTHER family, false otherwise
      See Also:
      • Response.Status.Family.OTHER
    • hasFamily

      public static boolean hasFamily(jakarta.ws.rs.core.Response response, jakarta.ws.rs.core.Response.Status.Family family)
      Check if the given response has the expected family.
      Parameters:
      response - the response object
      family - the expected family
      Returns:
      true if the response has the given status, false otherwise
    • onSuccessOrFailure

      public static void onSuccessOrFailure(Supplier<jakarta.ws.rs.core.Response> responseSupplier, Consumer<jakarta.ws.rs.core.Response> successConsumer, Consumer<jakarta.ws.rs.core.Response> failureConsumer, Consumer<RuntimeException> exceptionConsumer)
      Given a Response Supplier, perform an action depending on whether it was successful (successConsumer), failed (failureConsumer), or if the Supplier threw an exception (exceptionConsumer).

      Ensures the response is closed after performing the action.

      Parameters:
      responseSupplier - a Supplier that provides the response
      successConsumer - the action to run if the response is successful
      failureConsumer - the action to run if the response is not successful
      exceptionConsumer - the action to run if the Supplier throws an exception
    • onSuccessOrFailure

      public static void onSuccessOrFailure(jakarta.ws.rs.core.Response response, Consumer<jakarta.ws.rs.core.Response> successConsumer, Consumer<jakarta.ws.rs.core.Response> failureConsumer)
      Given a Response, perform an action depending on whether it was successful (successConsumer) or failed (failureConsumer).

      Ensures the response is closed after performing the action.

      Parameters:
      response - the response object
      successConsumer - the action to run if the response is successful
      failureConsumer - the action to run if the response is not successful
    • onSuccessOrFailureThrow

      public static void onSuccessOrFailureThrow(Supplier<jakarta.ws.rs.core.Response> responseSupplier, Consumer<jakarta.ws.rs.core.Response> successConsumer, Function<jakarta.ws.rs.core.Response,? extends RuntimeException> throwingFun)
      Given a Response Supplier, perform an action if it was successful (successConsumer. If the response was unsuccessful, throw the exception supplied by throwingFun. If the Supplier throws an exception, then that exception is rethrown.

      Ensures the response is closed after performing the action.

      Parameters:
      responseSupplier - a Supplier that provides the response
      successConsumer - the action to run if the response is successful
      throwingFun - a function that creates an appropriate (subclass of) RuntimeException
      Throws:
      RuntimeException - the result of throwingFun, or the exception thrown by the Supplier
    • onSuccessOrFailureThrow

      public static void onSuccessOrFailureThrow(jakarta.ws.rs.core.Response response, Consumer<jakarta.ws.rs.core.Response> successConsumer, Function<jakarta.ws.rs.core.Response,? extends RuntimeException> throwingFun)
      Given a Response, perform an action if it was successful (successConsumer or throw an exception supplied by throwingFun.

      Ensures the response is closed after performing the action.

      Parameters:
      response - the response object
      successConsumer - the action to run if the response is successful
      throwingFun - a function that creates an appropriate (subclass of) RuntimeException
      Throws:
      RuntimeException - the result of throwingFun
    • onSuccess

      public static void onSuccess(Supplier<jakarta.ws.rs.core.Response> responseSupplier, Consumer<jakarta.ws.rs.core.Response> successConsumer)
      Given a Response Supplier, perform an action only if it was successful (successConsumer.

      No action is performed for an unsuccessful response, and exceptions thrown by the Supplier are ignored.

      Ensures the response is closed after performing the action.

      Parameters:
      responseSupplier - a Supplier that provides the response
      successConsumer - the action to run if the response is successful
    • onSuccess

      public static void onSuccess(jakarta.ws.rs.core.Response response, Consumer<jakarta.ws.rs.core.Response> successConsumer)
      Given a Response, perform an action only if it was successful (successConsumer.

      No action is performed for an unsuccessful response.

      Ensures the response is closed after performing the action.

      Parameters:
      response - the response object
      successConsumer - the action to run if the response is successful
    • onSuccessWithResult

      public static <T> Optional<T> onSuccessWithResult(Supplier<jakarta.ws.rs.core.Response> responseSupplier, Function<jakarta.ws.rs.core.Response,T> successFun)
      Given a Response Supplier, perform an action that returns a result only if it was successful (successFun).

      No action is performed for an unsuccessful response, and exceptions thrown by the Supplier are ignored.

      Ensures the response is closed after performing the action.

      Type Parameters:
      T - the result type
      Parameters:
      responseSupplier - a Supplier that provides the response
      successFun - the function to apply if the response is successful
      Returns:
      an Optional containing a result for successful responses, or an empty Optional
    • onSuccessWithResult

      public static <T> Optional<T> onSuccessWithResult(jakarta.ws.rs.core.Response response, Function<jakarta.ws.rs.core.Response,T> successFun)
      Given a Response, perform an action that returns a result only if it was successful (successFun).

      No action is performed for an unsuccessful response.

      Ensures the response is closed after performing the action.

      Type Parameters:
      T - the result type
      Parameters:
      response - the response object
      successFun - the function to apply if the response is successful
      Returns:
      an Optional containing a result for successful responses, or an empty Optional
    • onFailure

      public static void onFailure(Supplier<jakarta.ws.rs.core.Response> responseSupplier, Consumer<jakarta.ws.rs.core.Response> failureConsumer, Consumer<RuntimeException> exceptionConsumer)
      Given a Response Supplier, perform an action only if it was not successful (failureConsumer), or if the Supplier threw an exception (exceptionConsumer).

      No action is performed for a successful response.

      Ensures the response is closed after performing the action.

      Parameters:
      responseSupplier - a Supplier that provides the response
      failureConsumer - the action to run if the response is not successful
      exceptionConsumer - the action to run if the Supplier throws an exception
    • onFailure

      public static void onFailure(jakarta.ws.rs.core.Response response, Consumer<jakarta.ws.rs.core.Response> failureConsumer)
      Given a Response, perform an action only if it was not successful (failureConsumer).

      No action is performed for a successful response.

      Ensures the response is closed after performing the action.

      Parameters:
      response - the response object
      failureConsumer - the action to run if the response is not successful
    • onFailureThrow

      public static void onFailureThrow(Supplier<jakarta.ws.rs.core.Response> responseSupplier, Function<jakarta.ws.rs.core.Response,? extends RuntimeException> throwingFun)
      Given a Response Supplier, throw a (subclass of) RuntimeException for failed responses using throwingFun. If the Supplier throws an exception, that exception is rethrown.

      No action is performed for a successful response.

      Ensures the response is closed after performing the action.

      Parameters:
      responseSupplier - a Supplier that provides the response
      throwingFun - function that creates an appropriate (subclass of) RuntimeException
      Throws:
      RuntimeException - the result of throwingFun, or the exception thrown by the Supplier
    • onFailureThrow

      public static void onFailureThrow(jakarta.ws.rs.core.Response response, Function<jakarta.ws.rs.core.Response,? extends RuntimeException> throwingFun)
      Given a Response, throw a (subclass of) RuntimeException for failed responses using throwingFun.

      No action is performed for a successful response.

      Ensures the response is closed after performing the action.

      Parameters:
      response - the response object
      throwingFun - a function that creates an appropriate (subclass of) RuntimeException
      Throws:
      RuntimeException - the result of throwingFun
    • onSuccessWithResultOrFailure

      public static <T> Optional<T> onSuccessWithResultOrFailure(Supplier<jakarta.ws.rs.core.Response> responseSupplier, Function<jakarta.ws.rs.core.Response,T> successFun, Consumer<jakarta.ws.rs.core.Response> failureConsumer, Consumer<RuntimeException> exceptionConsumer)
      Given a Response Supplier, perform an action that returns a result if the response was successful (successFun). Perform an action if the response was unsuccessful (failureConsumer, or if the Supplier threw an exception (exceptionConsumer).

      Ensures the response is closed after performing the action.

      Type Parameters:
      T - the result type
      Parameters:
      responseSupplier - a Supplier that provides the response
      successFun - the function to apply if the response is successful
      failureConsumer - the action to run if the response is not successful
      exceptionConsumer - the action to run if the Supplier throws an exception
      Returns:
      the result from successFun for successful responses, or an empty Optional for unsuccessful responses or if the Supplier throws an exception
    • onSuccessWithResultOrFailure

      public static <T> Optional<T> onSuccessWithResultOrFailure(jakarta.ws.rs.core.Response response, Function<jakarta.ws.rs.core.Response,T> successFun, Consumer<jakarta.ws.rs.core.Response> failureConsumer)
      Given a Response, perform an action that returns a result if the response was successful (successFun) or perform an action if the response was unsuccessful (failureConsumer.

      Ensures the response is closed after performing the action.

      Type Parameters:
      T - the result type
      Parameters:
      response - the response object
      successFun - the function to apply if the response is successful
      failureConsumer - the action to run if the response is not successful
      Returns:
      the result from successFun for successful responses, or an empty Optional for unsuccessful ones
    • onSuccessOrFailureWithResult

      public static <T> T onSuccessOrFailureWithResult(Supplier<jakarta.ws.rs.core.Response> responseSupplier, Function<jakarta.ws.rs.core.Response,T> successFun, Function<jakarta.ws.rs.core.Response,T> failedFun, Function<RuntimeException,T> exceptionFun)
      Given a Response Supplier, perform an action that returns a result if the response was successful (successFun. If the response was not successful return the result of a function (failedFun). If the Supplier threw an exception, return the result of a different function (exceptionFun).

      Ensures the response is closed after performing the action.

      Type Parameters:
      T - the result type
      Parameters:
      responseSupplier - a Supplier that provides the response
      successFun - the function to apply if the response is successful
      failedFun - the function to apply if the response is not successful
      exceptionFun - the function to apply if the Supplier throws an exception
      Returns:
      the result from applying successFun, failedFun, or exceptionFun
    • onSuccessOrFailureWithResult

      public static <T> T onSuccessOrFailureWithResult(jakarta.ws.rs.core.Response response, Function<jakarta.ws.rs.core.Response,T> successFun, Function<jakarta.ws.rs.core.Response,T> failedFun)
      Given a Response, perform an action that returns a result if the response was successful (successFun) or if not successful (failedFun).

      Ensures the response is closed after performing the action.

      Type Parameters:
      T - the result type
      Parameters:
      response - the response object
      successFun - the function to apply if the response is successful
      failedFun - the function to apply if the response is not successful
      Returns:
      the result from applying either successFun or failedFun
    • onSuccessWithResultOrFailureThrow

      public static <T> T onSuccessWithResultOrFailureThrow(Supplier<jakarta.ws.rs.core.Response> responseSupplier, Function<jakarta.ws.rs.core.Response,T> successFun, Function<jakarta.ws.rs.core.Response,? extends RuntimeException> throwingFun)
      Given a Response Supplier, perform an action that returns a result if it was successful (successFun or throw a (subclass of) RuntimeException if it failed (throwingFun). If the Supplier threw an exception, then that exception is rethrown.

      Ensures the response is closed after performing the action.

      Type Parameters:
      T - the result type
      Parameters:
      responseSupplier - a Supplier that provides the response
      successFun - the function to apply if the response is successful
      throwingFun - a function that creates an appropriate (subclass of) RuntimeException
      Returns:
      the result from applying successFun
      Throws:
      RuntimeException - the result of throwingFun or the exception thrown by the Supplier
    • onSuccessWithResultOrFailureThrow

      public static <T> T onSuccessWithResultOrFailureThrow(jakarta.ws.rs.core.Response response, Function<jakarta.ws.rs.core.Response,T> successFun, Function<jakarta.ws.rs.core.Response,? extends RuntimeException> throwingFun)
      Given a Response, perform an action that returns a result if it was successful (successFun or throw a (subclass of) RuntimeException if it failed (throwingFun).

      Ensures the response is closed after performing the action.

      Type Parameters:
      T - the result type
      Parameters:
      response - the response object
      successFun - the function to apply if the response is successful
      throwingFun - a function that creates an appropriate (subclass of) RuntimeException
      Returns:
      the result from applying successFun
      Throws:
      RuntimeException - the result of throwingFun
    • accept

      public static void accept(Supplier<jakarta.ws.rs.core.Response> responseSupplier, Consumer<jakarta.ws.rs.core.Response> responseConsumer, Consumer<RuntimeException> exceptionConsumer)
      Given a Response Supplier, perform some action using one of the supplied consumers.

      Ensures the response is closed after performing the action.

      Parameters:
      responseSupplier - a Supplier that provides the response
      responseConsumer - the action to run on any response
      exceptionConsumer - the action to run if the Supplier throws an exception
    • accept

      public static void accept(jakarta.ws.rs.core.Response response, Consumer<jakarta.ws.rs.core.Response> responseConsumer)
      Given a Response, perform some action using the supplied consumer.

      Ensures the response is closed after performing the action.

      Parameters:
      response - the response object
      responseConsumer - the action to run
    • apply

      public static <T> T apply(Supplier<jakarta.ws.rs.core.Response> responseSupplier, Function<jakarta.ws.rs.core.Response,T> fun, Function<RuntimeException,T> exceptionFun)
      Given a Response Supplier, perform an action tha returns a result using one of the given functions.

      Ensures the response is closed after performing the action.

      Type Parameters:
      T - the result type
      Parameters:
      responseSupplier - a Supplier that provides the response
      fun - the function to apply to the response
      exceptionFun - the function to apply if the Supplier throws an exception
      Returns:
      the result of applying the given function
    • apply

      public static <T> T apply(jakarta.ws.rs.core.Response response, Function<jakarta.ws.rs.core.Response,T> fun)
      Given a Response, perform an action tha returns a result using the given function.

      Ensures the response is closed after performing the action.

      Type Parameters:
      T - the result type
      Parameters:
      response - the response object
      fun - the function to apply to the response
      Returns:
      the result of applying the given function
    • closeQuietly

      public static void closeQuietly(jakarta.ws.rs.core.Response response)
      Closes the given Response, which can be null, swallowing any exceptions and logging them at INFO level.
      Parameters:
      response - the response object