Package edu.iu.client

Class IuHttp

java.lang.Object
edu.iu.client.IuHttp

public class IuHttp extends Object
Provides common base-level whitelisting, logging, and exception handling utilities for HttpRequest and HttpResponse.

All requests are handled via a cached HttpClient instance configured with default settings.

  • Field Details

  • Method Details

    • validate

      public static <T> HttpResponseHandler<T> validate(Function<InputStream,T> bodyDeserializer, HttpResponseValidator... responseValidators)
      Creates an HTTP response handler.
      Type Parameters:
      T - value type
      Parameters:
      bodyDeserializer - function that deserializes the response body
      responseValidators - one or more verification checks to apply to the response before passing to the handler
      Returns:
      decorated response handler
    • expectStatus

      public static HttpResponseValidator expectStatus(int expectedStatusCode)
      Gets a HttpResponseValidator that verifies an expected status code.
      Parameters:
      expectedStatusCode - status code
      Returns:
      HttpResponseValidator
    • checkHeaders

      public static HttpResponseValidator checkHeaders(BiPredicate<String,String> headerValidator)
      Gets a HttpResponseValidator that tests response headers.
      Parameters:
      headerValidator - test response headers
      Returns:
      HttpResponseValidator
    • get

      public static HttpResponse<InputStream> get(URI uri) throws HttpException
      Sends an HTTP GET request to a public URI.
      Parameters:
      uri - public URI
      Returns:
      HttpResponse
      Throws:
      HttpException - If the response has error status code.
    • get

      public static <T> T get(URI uri, HttpResponseHandler<T> responseHandler) throws HttpException
      Sends an HTTP GET request to a public URI.
      Type Parameters:
      T - response type
      Parameters:
      uri - public URI
      responseHandler - function that converts HTTP response data to the response type.
      Returns:
      response value
      Throws:
      HttpException - If the response has error status code.
    • send

      public static HttpResponse<InputStream> send(URI uri, UnsafeConsumer<HttpRequest.Builder> requestConsumer) throws HttpException
      Sends a synchronous HTTP request.
      Parameters:
      uri - request URI
      requestConsumer - receives the HttpRequest.Builder before sending to the server.
      Returns:
      HttpResponse
      Throws:
      HttpException - If the response has error status code.
    • isAllowed

      static boolean isAllowed(Collection<URI> allowList, URI uri)
      Determines whether or not an allow list contains a root of a URI.
      Parameters:
      allowList - allowed root URIs
      uri - URI to check
      Returns:
      true if the allow list is non-null and contains at least one entry that is a root of the supplied URI
    • send

      public static <E extends Exception> HttpResponse<InputStream> send(Class<E> exceptionClass, URI uri, UnsafeConsumer<HttpRequest.Builder> requestConsumer) throws HttpException, E
      Sends a synchronous HTTP request.
      Type Parameters:
      E - additional exception type
      Parameters:
      exceptionClass - additional checked exception type to allow thrown from requestConsumer
      uri - request URI
      requestConsumer - receives the HttpRequest.Builder before sending to the server.
      Returns:
      HttpResponse
      Throws:
      HttpException - If the response has error status code.
      E - from requestConsumer
    • send

      public static <T> T send(URI uri, UnsafeConsumer<HttpRequest.Builder> requestConsumer, HttpResponseHandler<T> responseHandler) throws HttpException
      Sends a synchronous HTTP request expecting 200 OK and accepting all response headers.
      Type Parameters:
      T - response type
      Parameters:
      uri - request URI
      requestConsumer - receives the HttpRequest.Builder before sending to the server.
      responseHandler - function that converts HTTP response data to the response type.
      Returns:
      response value
      Throws:
      HttpException - If the response has error status code.
    • send

      public static <T, E extends Exception> T send(Class<E> exceptionClass, URI uri, UnsafeConsumer<HttpRequest.Builder> requestConsumer, HttpResponseHandler<T> responseHandler) throws HttpException, E
      Sends a synchronous HTTP request expecting 200 OK and accepting all response headers.
      Type Parameters:
      T - response type
      E - additional exception type
      Parameters:
      exceptionClass - additional checked exception class to allow from requestConsumer
      uri - request URI
      requestConsumer - receives the HttpRequest.Builder before sending to the server.
      responseHandler - function that converts HTTP response data to the response type.
      Returns:
      response value
      Throws:
      HttpException - If the response has error status code.
      E - from requestConsumer