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 HttpResponseValidatorValidates 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 HttpResponseValidatorcheckHeaders(BiPredicate<String, String> headerValidator) Gets aHttpResponseValidatorthat tests response headers.static HttpResponseValidatorexpectStatus(int expectedStatusCode) Gets aHttpResponseValidatorthat verifies an expected status code.static HttpResponse<InputStream> Sends an HTTP GET request to a public URI.static <T> Tget(URI uri, HttpResponseHandler<T> responseHandler) Sends an HTTP GET request to a public URI.(package private) static booleanisAllowed(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> Tsend(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 aHttpResponseValidatorthat verifies an expected status code.- Parameters:
expectedStatusCode- status code- Returns:
HttpResponseValidator
-
checkHeaders
Gets aHttpResponseValidatorthat 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.Builderbefore 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-URIto 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.Builderbefore 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.Builderbefore 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.Builderbefore 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
-