java.lang.Object
edu.iu.client.IuHttp
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 Summary
FieldsModifier and TypeFieldDescriptionstatic final HttpResponseHandler
<?> Validates a 204 NO CONTENT response and returns null.static final HttpResponseValidator
Validates a 200 OK response.static final HttpResponseHandler
<JsonValue> Validates 200 OK then parses the response as a JSON object.static final HttpResponseHandler
<JsonObject> Validates 200 OK then parses the response as a JSON object. -
Method Summary
Modifier and TypeMethodDescriptionstatic HttpResponseValidator
checkHeaders
(BiPredicate<String, String> headerValidator) Gets aHttpResponseValidator
that tests response headers.static HttpResponseValidator
expectStatus
(int expectedStatusCode) Gets aHttpResponseValidator
that verifies an expected status code.static HttpResponse
<InputStream> Sends an HTTP GET request to a public URI.static <T> T
get
(URI uri, HttpResponseHandler<T> responseHandler) Sends an HTTP GET request to a public URI.(package private) static boolean
isAllowed
(Collection<URI> allowList, URI uri) Determines whether or not an allow list contains a root of a URI.static <E extends Exception>
HttpResponse<InputStream> send
(Class<E> exceptionClass, URI uri, UnsafeConsumer<HttpRequest.Builder> requestConsumer) Sends a synchronous HTTP request.static <T,
E extends Exception>
Tsend
(Class<E> exceptionClass, URI uri, UnsafeConsumer<HttpRequest.Builder> requestConsumer, HttpResponseHandler<T> responseHandler) Sends a synchronous HTTP request expecting 200 OK and accepting all response headers.static HttpResponse
<InputStream> send
(URI uri, UnsafeConsumer<HttpRequest.Builder> requestConsumer) Sends a synchronous HTTP request.static <T> T
send
(URI uri, UnsafeConsumer<HttpRequest.Builder> requestConsumer, HttpResponseHandler<T> responseHandler) Sends a synchronous HTTP request expecting 200 OK and accepting all response headers.static <T> HttpResponseHandler
<T> validate
(Function<InputStream, T> bodyDeserializer, HttpResponseValidator... responseValidators) Creates an HTTP response handler.
-
Field Details
-
OK
Validates a 200 OK response. -
NO_CONTENT
Validates a 204 NO CONTENT response and returns null. -
READ_JSON
Validates 200 OK then parses the response as a JSON object. -
READ_JSON_OBJECT
Validates 200 OK then parses the response as a JSON object.
-
-
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 bodyresponseValidators
- one or more verification checks to apply to the response before passing to the handler- Returns:
- decorated response handler
-
expectStatus
Gets aHttpResponseValidator
that verifies an expected status code.- Parameters:
expectedStatusCode
- status code- Returns:
HttpResponseValidator
-
checkHeaders
Gets aHttpResponseValidator
that tests response headers.- Parameters:
headerValidator
- test response headers- Returns:
HttpResponseValidator
-
get
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
Sends an HTTP GET request to a public URI.- Type Parameters:
T
- response type- Parameters:
uri
- public URIresponseHandler
- 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 URIrequestConsumer
- receives theHttpRequest.Builder
before sending to the server.- Returns:
HttpResponse
- Throws:
HttpException
- If the response has error status code.
-
isAllowed
Determines whether or not an allow list contains a root of a URI.- Parameters:
allowList
- allowed root URIsuri
-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 requestConsumeruri
- request URIrequestConsumer
- receives theHttpRequest.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 URIrequestConsumer
- receives theHttpRequest.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 typeE
- additional exception type- Parameters:
exceptionClass
- additional checked exception class to allow from requestConsumeruri
- request URIrequestConsumer
- receives theHttpRequest.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
-