Class HttpLib
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionstatic java.net.http.HttpRequest.BuilderacceptHeader(java.net.http.HttpRequest.Builder builder, String acceptHeader) Set the "Accept" header if value is not null.static StringCalculate basic auth header value.static java.net.http.HttpRequest.BuildercontentTypeHeader(java.net.http.HttpRequest.Builder builder, String contentType) Set the "Content-Type" header if value is not null.static <X> List<X>static java.net.http.HttpRequest.BuildercreateBuilder(java.net.http.HttpRequest request) Create aHttpRequest.Builderfrom anHttpRequest.static <X> Xdft(X value, X dftValue) static StringReturn a string (assumed to be a URI) without query string or fragment.static URIendpointURI(URI uri) URI, without query string and fragment.static java.net.http.HttpResponse<InputStream>execute(java.net.http.HttpClient httpClient, java.net.http.HttpRequest httpRequest) Execute a request, return aHttpResponse<InputStream>which can be passed tohandleResponseInputStream(HttpResponse)which will convert non-2xx status code toHttpExceptions.static <X> java.net.http.HttpResponse<X>execute(java.net.http.HttpClient httpClient, java.net.http.HttpRequest httpRequest, java.net.http.HttpResponse.BodyHandler<X> bodyHandler) Execute a request, return aHttpResponse<X>which can be passed tohandleHttpStatusCode(HttpResponse)which will convert non-2xx status code toHttpExceptions.static <T> java.net.http.HttpResponse<T>executeJDK(java.net.http.HttpClient httpClient, java.net.http.HttpRequest httpRequest, java.net.http.HttpResponse.BodyHandler<T> bodyHandler) Execute request and return a response without authentication challenge handling.static voidfinish(InputStream input) Read to end ofInputStream.static InputStreamgetInputStream(java.net.http.HttpResponse<InputStream> httpResponse) Get the InputStream from an HttpResponse, handling possible compression settings.static voidhandleHttpStatusCode(java.net.http.HttpResponse<InputStream> response) Deal with status code and any error message sent as a body in the response.static InputStreamhandleResponseInputStream(java.net.http.HttpResponse<InputStream> httpResponse) Handle the HTTP response (seehandleHttpStatusCode(HttpResponse)) and return the InputStream if a 200.static voidhandleResponseNoBody(java.net.http.HttpResponse<InputStream> response) Handle the HTTP response and consume the body if a 200.static StringhandleResponseRtnString(java.net.http.HttpResponse<InputStream> response) Handle the HTTP response and read the body to produce a string if a 200.static TypedInputStreamhandleResponseTypedInputStream(java.net.http.HttpResponse<InputStream> httpResponse) Handle the HTTP response (seehandleHttpStatusCode(HttpResponse)) and return the TypedInputStream that includes theContent-Typeif a 200.static voidhttpPushData(java.net.http.HttpClient httpClient, Push style, String url, Consumer<java.net.http.HttpRequest.Builder> modifier, java.net.http.HttpRequest.BodyPublisher body) Push data.static booleanisEndpoint(URI uri) Test whether a URI is a service endpoint.static booleanTest whether a URL identifies a Fuseki server.static voidAllow setting additional/optional query parameters on a per remote service (including for SERVICE).static java.net.http.HttpRequestnewGetRequest(String url, Consumer<java.net.http.HttpRequest.Builder> modifier) Return a HttpRequeststatic java.net.http.HttpResponse.BodyHandler<Void>noBody()static java.net.http.HttpRequest.BuilderrequestBuilder(String url, Map<String, String> httpHeaders, long readTimeout, TimeUnit readTimeoutUnit) static java.net.http.HttpRequest.BuilderrequestBuilderFor(String serviceEndpoint) static StringrequestTarget(URI uri) RFC7320 "request-target", used in digest authentication.static StringrequestURL(String url, String queryString) Query string is assumed to already be encoded.static StringresponseHeader(java.net.http.HttpResponse<?> response, String headerName) Return the first header of the given name, or null if nonestatic Consumer<java.net.http.HttpRequest.Builder>setHeaders(Map<String, String> headers) Return a modifier that will set the Accept header to the value.static java.net.http.HttpRequest.BodyPublisherstringBody(String str) static URItoRequestURI(String uriStr) String toURI.static StringEncode a string suitable for use in an URL query string
-
Field Details
-
FusekiRequestIdHeader
-
-
Method Details
-
noBody
-
stringBody
-
basicAuth
Calculate basic auth header value. Use with header "Authorization" (constantHttpNames.hAuthorization). Best used over https. -
getInputStream
Get the InputStream from an HttpResponse, handling possible compression settings. The application must consume or close theInputStream(seefinish(InputStream)). Closing the InputStream may close the HTTP connection. Assumes the status code has been handled e.g.handleHttpStatusCode(java.net.http.HttpResponse<java.io.InputStream>)has been called. -
handleHttpStatusCode
Deal with status code and any error message sent as a body in the response.It is this handling 4xx/5xx error messages in the body that forces the use of
InputStream, not genericT. We don't know until we see the status code how we are going to process the response body.Exits normally without processing the body if the response is 200.
Throws
HttpExceptionfor 3xx (redirection should have happened by now), 4xx and 5xx, having consumed the body input stream. -
handleResponseInputStream
public static InputStream handleResponseInputStream(java.net.http.HttpResponse<InputStream> httpResponse) Handle the HTTP response (seehandleHttpStatusCode(HttpResponse)) and return the InputStream if a 200.- Parameters:
httpResponse-- Returns:
- InputStream
-
handleResponseTypedInputStream
public static TypedInputStream handleResponseTypedInputStream(java.net.http.HttpResponse<InputStream> httpResponse) Handle the HTTP response (seehandleHttpStatusCode(HttpResponse)) and return the TypedInputStream that includes theContent-Typeif a 200.- Parameters:
httpResponse-- Returns:
- TypedInputStream
-
handleResponseNoBody
Handle the HTTP response and consume the body if a 200. Otherwise, throw anHttpException.- Parameters:
response-
-
handleResponseRtnString
Handle the HTTP response and read the body to produce a string if a 200. Otherwise, throw anHttpException.- Parameters:
response-- Returns:
- String
-
finish
Read to end ofInputStream.closemay close the underlying HTTP connection. SeeHttpResponse.BodySubscribers.ofInputStream(). -
toRequestURI
String toURI. ThrowsHttpExceptionon bad syntax or if the URI isn't absolute. -
isEndpoint
Test whether a URI is a service endpoint. It must be absolute, with host and path, and without query string or fragment. -
endpoint
Return a string (assumed to be a URI) without query string or fragment. -
requestTarget
RFC7320 "request-target", used in digest authentication. -
endpointURI
URI, without query string and fragment. -
newGetRequest
public static java.net.http.HttpRequest newGetRequest(String url, Consumer<java.net.http.HttpRequest.Builder> modifier) Return a HttpRequest -
dft
public static <X> X dft(X value, X dftValue) -
copyArray
-
urlEncodeQueryString
Encode a string suitable for use in an URL query string -
requestURL
Query string is assumed to already be encoded. -
requestBuilderFor
-
requestBuilder
-
createBuilder
public static java.net.http.HttpRequest.Builder createBuilder(java.net.http.HttpRequest request) Create aHttpRequest.Builderfrom anHttpRequest. -
acceptHeader
public static java.net.http.HttpRequest.Builder acceptHeader(java.net.http.HttpRequest.Builder builder, String acceptHeader) Set the "Accept" header if value is not null. Returns the builder. -
contentTypeHeader
public static java.net.http.HttpRequest.Builder contentTypeHeader(java.net.http.HttpRequest.Builder builder, String contentType) Set the "Content-Type" header if value is not null. Returns the builder. -
execute
public static java.net.http.HttpResponse<InputStream> execute(java.net.http.HttpClient httpClient, java.net.http.HttpRequest httpRequest) Execute a request, return aHttpResponse<InputStream>which can be passed tohandleResponseInputStream(HttpResponse)which will convert non-2xx status code toHttpExceptions.This function applies the HTTP authentication challenge support and will repeat the request if necessary with added authentication.
See
AuthEnvfor authentication registration.
SeeexecuteJDK(java.net.http.HttpClient, java.net.http.HttpRequest, java.net.http.HttpResponse.BodyHandler<T>)to execute exactly once without challenge response handling.- Parameters:
httpClient-httpRequest-- Returns:
- HttpResponse
- See Also:
-
execute
public static <X> java.net.http.HttpResponse<X> execute(java.net.http.HttpClient httpClient, java.net.http.HttpRequest httpRequest, java.net.http.HttpResponse.BodyHandler<X> bodyHandler) Execute a request, return aHttpResponse<X>which can be passed tohandleHttpStatusCode(HttpResponse)which will convert non-2xx status code toHttpExceptions.This function applies the HTTP authentication challenge support and will repeat the request if necessary with added authentication.
See
AuthEnvfor authentication registration.
SeeexecuteJDK(java.net.http.HttpClient, java.net.http.HttpRequest, java.net.http.HttpResponse.BodyHandler<T>)to execute exactly once without challenge response handling.- Parameters:
httpClient-httpRequest-bodyHandler-- Returns:
- HttpResponse
- See Also:
-
executeJDK
public static <T> java.net.http.HttpResponse<T> executeJDK(java.net.http.HttpClient httpClient, java.net.http.HttpRequest httpRequest, java.net.http.HttpResponse.BodyHandler<T> bodyHandler) Execute request and return a response without authentication challenge handling.- Parameters:
httpClient-httpRequest-bodyHandler-- Returns:
- HttpResponse
-
httpPushData
public static void httpPushData(java.net.http.HttpClient httpClient, Push style, String url, Consumer<java.net.http.HttpRequest.Builder> modifier, java.net.http.HttpRequest.BodyPublisher body) Push data. POST, PUT, PATCH request with no response body data. -
modifyByService
public static void modifyByService(String serviceURI, Context context, Params params, Map<String, String> httpHeaders) Allow setting additional/optional query parameters on a per remote service (including for SERVICE).- ARQ.httpRequestModifer - the specific modifier
- ARQ.httpRegistryRequestModifer - the registry, keyed by service URL.
-
setHeaders
Return a modifier that will set the Accept header to the value. An argument of "null" means "no action". -
responseHeader
Return the first header of the given name, or null if none -
isFuseki
Test whether a URL identifies a Fuseki server. This operation can not guarantee to detect a Fuseki server - for example, it may be behind a reverse proxy that masks the signature.
-