- java.lang.Object
-
- jdk.incubator.http.HttpResponse<T>
-
- Type Parameters:
T- the response body type
public abstract class HttpResponse<T> extends Object
Represents a response to aHttpRequest.
Incubating Feature. Will be removed in a future release.A
HttpResponseis available when the response status code and headers have been received, and typically after the response body has also been received. This depends on the response body handler provided when sending the request. In all cases, the response body handler is invoked before the body is read. This gives applications an opportunity to decide how to handle the body.Methods are provided in this class for accessing the response headers, and response body.
Response handlers and processors
Response bodies are handled at two levels. Application code supplies a response handler (
HttpResponse.BodyHandler) which may examine the response status code and headers, and which then returns aHttpResponse.BodyProcessorto actually read (or discard) the body and convert it into some useful Java object type. The handler can return one of the pre-defined processor types, or a custom processor, or if the body is to be discarded, it can callBodyProcessor.discard()and return a processor which discards the response body. Static implementations of both handlers and processors are provided inBodyHandlerandBodyProcessorrespectively. In all cases, the handler functions provided are convenience implementations which ignore the supplied status code and headers and return the relevant pre-definedBodyProcessor.See
HttpResponse.BodyHandlerfor example usage.- Since:
- 9
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceHttpResponse.BodyHandler<T>A handler for response bodies.static interfaceHttpResponse.BodyProcessor<T>A processor for response bodies.static interfaceHttpResponse.MultiProcessor<U,T>A response processor for a HTTP/2 multi response.
-
Constructor Summary
Constructors Modifier Constructor Description protectedHttpResponse()Creates an HttpResponse.
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description abstract Tbody()Returns the body.abstract HttpRequestfinalRequest()Returns the finalHttpRequestthat was sent on the wire for the exchange ( may, or may not, be the same as the initial request ).abstract HttpHeadersheaders()Returns the received response headers.abstract HttpRequestrequest()Returns the initialHttpRequestthat initiated the exchange.abstract SSLParameterssslParameters()Returns theSSLParametersin effect for this response.abstract intstatusCode()Returns the status code for this response.abstract CompletableFuture<HttpHeaders>trailers()Returns the received response trailers, if there are any, when they become available.abstract URIuri()Returns theURIthat the response was received from.abstract HttpClient.Versionversion()Returns the HTTP protocol version that was used for this response.
-
-
-
Method Detail
-
statusCode
public abstract int statusCode()
Returns the status code for this response.- Returns:
- the response code
-
request
public abstract HttpRequest request()
Returns the initialHttpRequestthat initiated the exchange.- Returns:
- the request
-
finalRequest
public abstract HttpRequest finalRequest()
Returns the finalHttpRequestthat was sent on the wire for the exchange ( may, or may not, be the same as the initial request ).- Returns:
- the request
-
headers
public abstract HttpHeaders headers()
Returns the received response headers.- Returns:
- the response headers
-
trailers
public abstract CompletableFuture<HttpHeaders> trailers()
Returns the received response trailers, if there are any, when they become available. For many response processor types this will be at the same time as theHttpResponseitself is available. In such cases, the returnedCompletableFuturewill be already completed.- Returns:
- a CompletableFuture of the response trailers (may be empty)
-
body
public abstract T body()
Returns the body. Depending on the type ofT, the returned body may represent the body after it was read (such asbyte[], orString, orPath) or it may represent an object with which the body is read, such as anInputStream.- Returns:
- the body
-
sslParameters
public abstract SSLParameters sslParameters()
Returns theSSLParametersin effect for this response. Returnsnullif this is not a HTTPS response.- Returns:
- the SSLParameters associated with the response
-
uri
public abstract URI uri()
Returns theURIthat the response was received from. This may be different from the requestURIif redirection occurred.- Returns:
- the URI of the response
-
version
public abstract HttpClient.Version version()
Returns the HTTP protocol version that was used for this response.- Returns:
- HTTP protocol version
-
-