Class KiwiResponses


  • public class KiwiResponses
    extends Object
    Static utilities related to evaluating and acting upon JAX-RS 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:
    KiwiResources, KiwiStandardResponses
    • Constructor Summary

      Constructors 
      Constructor Description
      KiwiResponses()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void accept​(javax.ws.rs.core.Response response, Consumer<javax.ws.rs.core.Response> responseConsumer)
      Given a Response, perform some action using the supplied consumer.
      static <T> T apply​(javax.ws.rs.core.Response response, Function<javax.ws.rs.core.Response,​T> fun)
      Given a Response, perform an action tha returns a result using the given function.
      static boolean clientError​(javax.ws.rs.core.Response response)
      Check if the given response is in the CLIENT_ERROR (4xx codes) family.
      static void closeQuietly​(javax.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​(javax.ws.rs.core.Response response)
      Check if the given response has status 201 Created.
      static boolean hasFamily​(javax.ws.rs.core.Response response, javax.ws.rs.core.Response.Status.Family family)
      Check if the given response has the expected family.
      static boolean hasStatus​(javax.ws.rs.core.Response response, javax.ws.rs.core.Response.Status status)
      Check if the given response has the expected status.
      static boolean informational​(javax.ws.rs.core.Response response)
      Check if the given response is in the INFORMATIONAL (1xx codes) family.
      static boolean internalServerError​(javax.ws.rs.core.Response response)
      Check if the given response has status 500 Internal Server Error.
      static Optional<String> mediaType​(javax.ws.rs.core.Response response)
      Return a media type suitable for use as the value of a corresponding HTTP header.
      static boolean notFound​(javax.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​(javax.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​(javax.ws.rs.core.Response.Status status)
      Check if the given status code is not in Response.Status.Family.SUCCESSFUL.
      static boolean notSuccessful​(javax.ws.rs.core.Response.Status.Family family)
      Check if the given response family is not Response.Status.Family.SUCCESSFUL.
      static boolean notSuccessful​(javax.ws.rs.core.Response.StatusType status)
      Check if the given status type is not in Response.Status.Family.SUCCESSFUL.
      static boolean notSuccessfulAlwaysClosing​(javax.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​(javax.ws.rs.core.Response response)
      Check if the given response has status 200 OK.
      static void onFailure​(javax.ws.rs.core.Response response, Consumer<javax.ws.rs.core.Response> failedConsumer)
      Given a Response, perform an action only if it was not successful (failedConsumer).
      static void onFailureThrow​(javax.ws.rs.core.Response response, Function<javax.ws.rs.core.Response,​? extends RuntimeException> throwingFun)
      Given a Response, throw a (subclass of) RuntimeException for failed responses using throwingFun.
      static void onSuccess​(javax.ws.rs.core.Response response, Consumer<javax.ws.rs.core.Response> successConsumer)
      Given a Response, perform an action only if it was successful (successConsumer.
      static void onSuccessOrFailure​(javax.ws.rs.core.Response response, Consumer<javax.ws.rs.core.Response> successConsumer, Consumer<javax.ws.rs.core.Response> failedConsumer)
      Given a Response, perform an action depending on whether it was successful (successConsumer) or failed (failedConsumer).
      static void onSuccessOrFailureThrow​(javax.ws.rs.core.Response response, Consumer<javax.ws.rs.core.Response> successConsumer, Function<javax.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 <T> T onSuccessOrFailureWithResult​(javax.ws.rs.core.Response response, Function<javax.ws.rs.core.Response,​T> successFun, Function<javax.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> Optional<T> onSuccessWithResult​(javax.ws.rs.core.Response response, Function<javax.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> onSuccessWithResultOrFailure​(javax.ws.rs.core.Response response, Function<javax.ws.rs.core.Response,​T> successFun, Consumer<javax.ws.rs.core.Response> failedConsumer)
      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 (failedConsumer.
      static <T> T onSuccessWithResultOrFailureThrow​(javax.ws.rs.core.Response response, Function<javax.ws.rs.core.Response,​T> successFun, Function<javax.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 boolean otherFamily​(javax.ws.rs.core.Response response)
      Check if the given response is in the OTHER (unrecognized status codes) family.
      static boolean redirection​(javax.ws.rs.core.Response response)
      Check if the given response is in the REDIRECTION (3xx codes) family.
      static boolean serverError​(javax.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​(javax.ws.rs.core.Response response)
      Check if the given response has a status code in Response.Status.Family.SUCCESSFUL.
      static boolean successful​(javax.ws.rs.core.Response.Status status)
      Check if the given status code is in Response.Status.Family.SUCCESSFUL.
      static boolean successful​(javax.ws.rs.core.Response.Status.Family family)
      Check if the given response family is Response.Status.Family.SUCCESSFUL.
      static boolean successful​(javax.ws.rs.core.Response.StatusType status)
      Check if the given status type is in Response.Status.Family.SUCCESSFUL.
      static boolean successfulAlwaysClosing​(javax.ws.rs.core.Response response)
      Check if the given response has a status code in Response.Status.Family.SUCCESSFUL, then close the response.
    • Constructor Detail

      • KiwiResponses

        public KiwiResponses()
    • Method Detail

      • mediaType

        public static Optional<String> mediaType​(javax.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​(javax.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​(javax.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​(javax.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​(javax.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​(javax.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​(javax.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​(javax.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​(javax.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​(javax.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​(javax.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​(javax.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​(javax.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​(javax.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​(javax.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​(javax.ws.rs.core.Response response,
                                        javax.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​(javax.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​(javax.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​(javax.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​(javax.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​(javax.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​(javax.ws.rs.core.Response response,
                                        javax.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​(javax.ws.rs.core.Response response,
                                              Consumer<javax.ws.rs.core.Response> successConsumer,
                                              Consumer<javax.ws.rs.core.Response> failedConsumer)
        Given a Response, perform an action depending on whether it was successful (successConsumer) or failed (failedConsumer).

        Ensures the response is closed after performing the action.

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

        public static void onSuccessOrFailureThrow​(javax.ws.rs.core.Response response,
                                                   Consumer<javax.ws.rs.core.Response> successConsumer,
                                                   Function<javax.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​(javax.ws.rs.core.Response response,
                                     Consumer<javax.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​(javax.ws.rs.core.Response response,
                                                          Function<javax.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 performer 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​(javax.ws.rs.core.Response response,
                                     Consumer<javax.ws.rs.core.Response> failedConsumer)
        Given a Response, perform an action only if it was not successful (failedConsumer). No action is performed for a successful response.

        Ensures the response is closed after performing the action.

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

        public static void onFailureThrow​(javax.ws.rs.core.Response response,
                                          Function<javax.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​(javax.ws.rs.core.Response response,
                                                                   Function<javax.ws.rs.core.Response,​T> successFun,
                                                                   Consumer<javax.ws.rs.core.Response> failedConsumer)
        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 (failedConsumer.

        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
        failedConsumer - 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​(javax.ws.rs.core.Response response,
                                                         Function<javax.ws.rs.core.Response,​T> successFun,
                                                         Function<javax.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​(javax.ws.rs.core.Response response,
                                                              Function<javax.ws.rs.core.Response,​T> successFun,
                                                              Function<javax.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​(javax.ws.rs.core.Response response,
                                  Consumer<javax.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​(javax.ws.rs.core.Response response,
                                  Function<javax.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​(javax.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