public class RestResponseBody extends Object implements HttpEntity
An extension of an HttpClient HttpEntity
that provides various support for converting the body to POJOs and
other convenience methods.
Constructor and Description |
---|
RestResponseBody(RestClient client,
RestRequest request,
RestResponse response,
Parser parser)
Constructor.
|
Modifier and Type | Method and Description |
---|---|
<T> T |
as(Class<T> type)
Same as
as(Type,Type...) except optimized for a non-parameterized class. |
<T> T |
as(ClassMeta<T> type)
|
<T> RestResponse |
as(Mutable<T> m,
Class<T> type)
Same as
as(Class) but sets the value in a mutable for fluent calls. |
<T> RestResponse |
as(Mutable<T> m,
ClassMeta<T> type)
Identical to
as(ClassMeta) but sets the value in a mutable for fluent calls. |
<T> RestResponse |
as(Mutable<T> m,
Type type,
Type... args)
Same as
as(Type,Type...) but sets the value in a mutable for fluent calls. |
<T> T |
as(Type type,
Type... args)
Parses HTTP body into the specified object type.
|
String |
asAbbreviatedString(int length)
Same as
asString() but truncates the string to the specified length. |
RestResponse |
asAbbreviatedString(Mutable<String> m,
int length)
Same as
asAbbreviatedString(int) but sets the value in a mutable for fluent calls. |
byte[] |
asBytes()
Returns the HTTP response message body as a byte array.
|
<T> Future<T> |
asFuture(Class<T> type)
Same as
as(Class) but allows you to run the call asynchronously. |
<T> Future<T> |
asFuture(ClassMeta<T> type)
Same as
as(ClassMeta) but allows you to run the call asynchronously. |
<T> RestResponse |
asFuture(Mutable<Future<T>> m,
Class<T> type)
Same as
as(Mutable,Class) but allows you to run the call asynchronously. |
<T> RestResponse |
asFuture(Mutable<Future<T>> m,
ClassMeta<T> type)
Same as
as(Mutable,ClassMeta) but allows you to run the call asynchronously. |
<T> RestResponse |
asFuture(Mutable<Future<T>> m,
Type type,
Type... args)
Same as
as(Mutable,Type,Type...) but allows you to run the call asynchronously. |
<T> Future<T> |
asFuture(Type type,
Type... args)
Same as
as(Type,Type...) but allows you to run the call asynchronously. |
InputStream |
asInputStream()
Returns the HTTP response message body as an input stream.
|
RestResponse |
asMatcher(Mutable<Matcher> m,
Pattern pattern)
Same as
asMatcher(Pattern) but sets the value in a mutable for fluent calls. |
RestResponse |
asMatcher(Mutable<Matcher> m,
String regex)
Same as
asMatcher(String) but sets the value in a mutable for fluent calls. |
RestResponse |
asMatcher(Mutable<Matcher> m,
String regex,
int flags)
Same as
asMatcher(String,int) but sets the value in a mutable for fluent calls. |
Matcher |
asMatcher(Pattern pattern)
Converts the contents of the response body to a string and then matches the specified pattern against it.
|
Matcher |
asMatcher(String regex)
Converts the contents of the response body to a string and then matches the specified pattern against it.
|
Matcher |
asMatcher(String regex,
int flags)
Converts the contents of the response body to a string and then matches the specified pattern against it.
|
PojoRest |
asPojoRest()
|
PojoRest |
asPojoRest(Class<?> innerType)
Parses the output from the body into the specified type and then wraps that in a
PojoRest . |
RestResponse |
asPojoRest(Mutable<PojoRest> m)
Same as
asPojoRest() but sets the value in a mutable for fluent calls. |
RestResponse |
asPojoRest(Mutable<PojoRest> m,
Class<?> innerType)
Same as
asPojoRest(Class) but sets the value in a mutable for fluent calls. |
Reader |
asReader()
Returns the HTTP response message body as a reader based on the charset on the
Content-Type response header. |
Reader |
asReader(Charset charset)
Returns the HTTP response message body as a reader using the specified charset.
|
FluentByteArrayAssertion<RestResponse> |
assertBytes()
Provides the ability to perform fluent-style assertions on the bytes of the response body.
|
FluentObjectAssertion<RestResponse> |
assertObject(Class<?> type)
Provides the ability to perform fluent-style assertions on this response body.
|
FluentStringAssertion<RestResponse> |
assertString()
Provides the ability to perform fluent-style assertions on this response body.
|
String |
asString()
Returns the contents of this body as a string.
|
RestResponse |
asString(Mutable<String> m)
Same as
asString() but sets the value in a mutable for fluent calls. |
Future<String> |
asStringFuture()
Same as
asString() but allows you to run the call asynchronously. |
RestResponse |
asStringFuture(Mutable<Future<String>> m)
Same as
asStringFuture() but sets the value in a mutable for fluent calls. |
RestResponseBody |
cache()
Causes the contents of the response body to be stored so that it can be repeatedly read.
|
void |
consumeContent()
Deprecated.
Use standard java convention to ensure resource deallocation by calling
InputStream.close() on
the input stream returned by getContent() |
InputStream |
getContent()
Returns a content stream of the entity.
|
RestResponseHeader |
getContentEncoding()
Obtains the Content-Encoding header, if known.
|
long |
getContentLength()
Tells the length of the content, if known.
|
RestResponseHeader |
getContentType()
Obtains the
|
boolean |
isChunked()
Tells about chunked encoding for this entity.
|
boolean |
isRepeatable()
Tells if the entity is capable of producing its data more than once.
|
boolean |
isStreaming()
Tells whether this entity depends on an underlying stream.
|
RestResponseBody |
parser(Parser value)
Specifies the parser to use for this body.
|
RestResponse |
pipeTo(OutputStream os)
Pipes the contents of the response to the specified output stream.
|
RestResponse |
pipeTo(Writer w)
Pipes the contents of the response to the specified writer.
|
RestResponse |
pipeTo(Writer w,
boolean byLines)
Pipes the contents of the response to the specified writer.
|
RestResponse |
pipeTo(Writer w,
Charset charset)
Pipes the contents of the response to the specified writer.
|
RestResponse |
pipeTo(Writer w,
Charset charset,
boolean byLines)
Pipes the contents of the response to the specified writer.
|
RestResponseBody |
schema(HttpPartSchema value)
Specifies the schema for this body.
|
RestResponse |
toResponse()
Returns the response that created this object.
|
void |
writeTo(OutputStream outstream)
Writes the entity content out to the output stream.
|
public RestResponseBody(RestClient client, RestRequest request, RestResponse response, Parser parser)
client
- The client used to build this request.request
- The request object.response
- The response object.parser
- The parser to use to consume the body. Can be public RestResponseBody parser(Parser value)
If not specified, uses the parser defined on the client set via RestClientBuilder.parser(Class)
.
value
- The new part parser to use for this body.public RestResponseBody schema(HttpPartSchema value)
Used by schema-based parsers such as OpenApiParser
.
value
- The schema.public RestResponseBody cache()
Calling this method allows methods that read the response body to be called multiple times.
public InputStream asInputStream() throws IOException
cache()
has been called.
RestCallException
with an inner IllegalStateException
to be thrown.
IOException
- If a stream or illegal state exception was thrown.public Reader asReader() throws IOException
Content-Type
response header.
Content-Type
response header, cache()
has been called.
RestCallException
with an inner IllegalStateException
to be thrown.
IOException
- If an exception occurred.public Reader asReader(Charset charset) throws IOException
cache()
has been called.
RestCallException
with an inner IllegalStateException
to be thrown.
charset
- The charset to use for the reader.
IOException
- If an exception occurred.public byte[] asBytes() throws RestCallException
RestCallException
- If an exception occurred.public RestResponse pipeTo(OutputStream os) throws IOException
cache()
has been called.
RestCallException
with an inner IllegalStateException
to be thrown.
os
- The output stream to pipe the output to.IOException
- If an IO exception occurred.public RestResponse pipeTo(Writer w) throws IOException
Content-Type
response header, cache()
has been called.
RestCallException
with an inner IllegalStateException
to be thrown.
w
- The writer to pipe the output to.IOException
- If an IO exception occurred.public RestResponse pipeTo(Writer w, Charset charset) throws IOException
cache()
has been called.
RestCallException
with an inner IllegalStateException
to be thrown.
w
- The writer to pipe the output to.charset
- The charset to use for the reader.
IOException
- If an IO exception occurred.public RestResponse pipeTo(Writer w, boolean byLines) throws IOException
Content-Type
response header, cache()
has been called.
RestCallException
with an inner IllegalStateException
to be thrown.
w
- The writer to write the output to.byLines
- Flush the writers after every line of output.IOException
- If an IO exception occurred.public RestResponse pipeTo(Writer w, Charset charset, boolean byLines) throws IOException
cache()
has been called.
RestCallException
with an inner IllegalStateException
to be thrown.
w
- The writer to pipe the output to.byLines
- Flush the writers after every line of output.charset
- The charset to use for the reader.
IOException
- If an IO exception occurred.public <T> T as(Type type, Type... args) throws RestCallException
The type can be a simple type (e.g. beans, strings, numbers) or parameterized type (collections/maps).
The array can be arbitrarily long to indicate arbitrarily complex data structures.
as(Class)
method instead if you don't need a parameterized map/collection.
RestResponseBody
/HttpEntity
- Returns access to this object.
Reader
- Returns access to the raw reader of the response.
InputStream
- Returns access to the raw input stream of the response.
BasicHttpResource
- Raw contents will be serialized to remote resource. Additional headers and media type will be set on request.
cache()
or RestResponse.cacheBody()
has been called, this method can be can be called multiple times and/or combined with
other methods that retrieve the content of the response. Otherwise a RestCallException
with an inner IllegalStateException
will be thrown.
T
- The class type of the object to create.type
- The object type to create.
ClassMeta
, Class
, ParameterizedType
, GenericArrayType
args
- The type arguments of the class if it's a collection or map.
ClassMeta
, Class
, ParameterizedType
, GenericArrayType
RestCallException
- for argument syntax for maps and collections.
public <T> RestResponse as(Mutable<T> m, Type type, Type... args) throws RestCallException
as(Type,Type...)
but sets the value in a mutable for fluent calls.
cache()
or RestResponse.cacheBody()
has been called, this method can be can be called multiple times and/or combined with
other methods that retrieve the content of the response. Otherwise a RestCallException
with an inner IllegalStateException
will be thrown.
T
- The class type of the object to create.m
- The mutable to set the parsed value in.type
- The object type to create.
ClassMeta
, Class
, ParameterizedType
, GenericArrayType
args
- The type arguments of the class if it's a collection or map.
ClassMeta
, Class
, ParameterizedType
, GenericArrayType
RestCallException
- for argument syntax for maps and collections.
public <T> T as(Class<T> type) throws RestCallException
as(Type,Type...)
except optimized for a non-parameterized class.
This is the preferred parse method for simple types since you don't need to cast the results.
RestResponseBody
/HttpEntity
- Returns access to this object.
Reader
- Returns access to the raw reader of the response.
InputStream
- Returns access to the raw input stream of the response.
BasicHttpResource
- Raw contents will be serialized to remote resource. Additional headers and media type will be set on request.
cache()
or RestResponse.cacheBody()
has been called, this method can be can be called multiple times and/or combined with
other methods that retrieve the content of the response. Otherwise a RestCallException
with an inner IllegalStateException
will be thrown.
T
- The class type of the object being created.
See as(Type,Type...)
for details.type
- The object type to create.RestCallException
- If the input contains a syntax error or is malformed, or is not valid for the specified type, or if a connection
error occurred.public <T> RestResponse as(Mutable<T> m, Class<T> type) throws RestCallException
as(Class)
but sets the value in a mutable for fluent calls.
cache()
or RestResponse.cacheBody()
has been called, this method can be can be called multiple times and/or combined with
other methods that retrieve the content of the response. Otherwise a RestCallException
with an inner IllegalStateException
will be thrown.
T
- The class type of the object to create.m
- The mutable to set the parsed value in.type
- The object type to create.RestCallException
- for argument syntax for maps and collections.
public <T> T as(ClassMeta<T> type) throws RestCallException
as(Class)
except allows you to predefine complex data types using the ClassMeta
API.
BeanContext
cache()
or RestResponse.cacheBody()
has been called, this method can be can be called multiple times and/or combined with
other methods that retrieve the content of the response. Otherwise a RestCallException
with an inner IllegalStateException
will be thrown.
T
- The class type of the object to create.type
- The object type to create.RestCallException
- for argument syntax for maps and collections.
public <T> RestResponse as(Mutable<T> m, ClassMeta<T> type) throws RestCallException
as(ClassMeta)
but sets the value in a mutable for fluent calls.
cache()
or RestResponse.cacheBody()
has been called, this method can be can be called multiple times and/or combined with
other methods that retrieve the content of the response. Otherwise a RestCallException
with an inner IllegalStateException
will be thrown.
T
- The class type of the object to create.m
- The mutable to set the parsed value in.type
- The object type to create.RestCallException
- for argument syntax for maps and collections.
public <T> Future<T> asFuture(Class<T> type) throws RestCallException
as(Class)
but allows you to run the call asynchronously.
cache()
or RestResponse.cacheBody()
has been called, this method can be can be called multiple times and/or combined with
other methods that retrieve the content of the response. Otherwise a RestCallException
with an inner IllegalStateException
will be thrown.
T
- The class type of the object being created.type
- The object type to create.RestCallException
- If the executor service was not defined.for defining the executor service for creating
{@link Future Futures}.
public <T> RestResponse asFuture(Mutable<Future<T>> m, Class<T> type) throws RestCallException
as(Mutable,Class)
but allows you to run the call asynchronously.
cache()
or RestResponse.cacheBody()
has been called, this method can be can be called multiple times and/or combined with
other methods that retrieve the content of the response. Otherwise a RestCallException
with an inner IllegalStateException
will be thrown.
T
- The class type of the object being created.m
- The mutable to set the parsed value in.type
- The object type to create.RestCallException
- If the executor service was not defined.for defining the executor service for creating
{@link Future Futures}.
public <T> Future<T> asFuture(ClassMeta<T> type) throws RestCallException
as(ClassMeta)
but allows you to run the call asynchronously.
cache()
or RestResponse.cacheBody()
has been called, this method can be can be called multiple times and/or combined with
other methods that retrieve the content of the response. Otherwise a RestCallException
with an inner IllegalStateException
will be thrown.
T
- The class type of the object being created.
See as(Type, Type...)
for details.type
- The object type to create.RestCallException
- If the executor service was not defined.for defining the executor service for creating
{@link Future Futures}.
public <T> RestResponse asFuture(Mutable<Future<T>> m, ClassMeta<T> type) throws RestCallException
as(Mutable,ClassMeta)
but allows you to run the call asynchronously.
cache()
or RestResponse.cacheBody()
has been called, this method can be can be called multiple times and/or combined with
other methods that retrieve the content of the response. Otherwise a RestCallException
with an inner IllegalStateException
will be thrown.
T
- The class type of the object being created.
See as(Type, Type...)
for details.m
- The mutable to set the parsed value in.type
- The object type to create.RestCallException
- If the executor service was not defined.for defining the executor service for creating
{@link Future Futures}.
public <T> Future<T> asFuture(Type type, Type... args) throws RestCallException
as(Type,Type...)
but allows you to run the call asynchronously.
cache()
or RestResponse.cacheBody()
has been called, this method can be can be called multiple times and/or combined with
other methods that retrieve the content of the response. Otherwise a RestCallException
with an inner IllegalStateException
will be thrown.
T
- The class type of the object being created.
See as(Type, Type...)
for details.type
- The object type to create.
ClassMeta
, Class
, ParameterizedType
, GenericArrayType
args
- The type arguments of the class if it's a collection or map.
ClassMeta
, Class
, ParameterizedType
, GenericArrayType
RestCallException
- If the executor service was not defined.for defining the executor service for creating
{@link Future Futures}.
public <T> RestResponse asFuture(Mutable<Future<T>> m, Type type, Type... args) throws RestCallException
as(Mutable,Type,Type...)
but allows you to run the call asynchronously.
cache()
or RestResponse.cacheBody()
has been called, this method can be can be called multiple times and/or combined with
other methods that retrieve the content of the response. Otherwise a RestCallException
with an inner IllegalStateException
will be thrown.
T
- The class type of the object being created.
See as(Type, Type...)
for details.m
- The mutable to set the parsed value in.type
- The object type to create.
ClassMeta
, Class
, ParameterizedType
, GenericArrayType
args
- The type arguments of the class if it's a collection or map.
ClassMeta
, Class
, ParameterizedType
, GenericArrayType
RestCallException
- If the executor service was not defined.for defining the executor service for creating
{@link Future Futures}.
public String asString() throws RestCallException
Content-Type
response header, cache()
so that the body can be retrieved multiple times.
RestCallException
- public RestResponse asString(Mutable<String> m) throws RestCallException
asString()
but sets the value in a mutable for fluent calls.
Content-Type
response header, cache()
so that the body can be retrieved multiple times.
m
- The mutable to set the value in.RestCallException
- public Future<String> asStringFuture() throws RestCallException
asString()
but allows you to run the call asynchronously.
Content-Type
response header, cache()
so that the body can be retrieved multiple times.
RestCallException
- If the executor service was not defined.for defining the executor service for creating
{@link Future Futures}.
public RestResponse asStringFuture(Mutable<Future<String>> m) throws RestCallException
asStringFuture()
but sets the value in a mutable for fluent calls.
Content-Type
response header, cache()
or RestResponse.cacheBody()
has been called, this method can be can be called multiple times and/or combined with
other methods that retrieve the content of the response. Otherwise a RestCallException
with an inner IllegalStateException
will be thrown.
m
- The mutable to set the value in.RestCallException
- If the executor service was not defined.for defining the executor service for creating
{@link Future Futures}.
public String asAbbreviatedString(int length) throws RestCallException
asString()
but truncates the string to the specified length.
If truncation occurs, the string will be suffixed with
length
- The max length of the returned string.RestCallException
- public RestResponse asAbbreviatedString(Mutable<String> m, int length) throws RestCallException
asAbbreviatedString(int)
but sets the value in a mutable for fluent calls.
If truncation occurs, the string will be suffixed with
m
- The mutable to set the value in.length
- The max length of the returned string.RestCallException
- public PojoRest asPojoRest(Class<?> innerType) throws RestCallException
PojoRest
.
Useful if you want to quickly retrieve a single value from inside of a larger JSON document.
innerType
- The class type of the POJO being wrapped.PojoRest
.RestCallException
- public RestResponse asPojoRest(Mutable<PojoRest> m, Class<?> innerType) throws RestCallException
asPojoRest(Class)
but sets the value in a mutable for fluent calls.m
- The mutable to set the value in.innerType
- The class type of the POJO being wrapped.RestCallException
- public PojoRest asPojoRest() throws RestCallException
OMap
and then wraps that in a PojoRest
.
Useful if you want to quickly retrieve a single value from inside of a larger JSON document.
PojoRest
.RestCallException
- public RestResponse asPojoRest(Mutable<PojoRest> m) throws RestCallException
asPojoRest()
but sets the value in a mutable for fluent calls.m
- The mutable to set the value in.RestCallException
- public Matcher asMatcher(Pattern pattern) throws RestCallException
Content-Type
response header, cache()
so that the body can be retrieved multiple times.
pattern
- The regular expression pattern to match.RestCallException
- If a connection error occurred.public RestResponse asMatcher(Mutable<Matcher> m, Pattern pattern) throws RestCallException
asMatcher(Pattern)
but sets the value in a mutable for fluent calls.
Content-Type
response header, cache()
so that the body can be retrieved multiple times.
m
- The mutable to set the value in.pattern
- The regular expression pattern to match.RestCallException
- If a connection error occurred.public Matcher asMatcher(String regex) throws RestCallException
Content-Type
response header, cache()
or RestResponse.cacheBody()
has been called, this method can be can be called multiple times and/or combined with
other methods that retrieve the content of the response. Otherwise a RestCallException
with an inner IllegalStateException
will be thrown.
regex
- The regular expression pattern to match.RestCallException
- If a connection error occurred.public RestResponse asMatcher(Mutable<Matcher> m, String regex) throws RestCallException
asMatcher(String)
but sets the value in a mutable for fluent calls.
Content-Type
response header, cache()
or RestResponse.cacheBody()
has been called, this method can be can be called multiple times and/or combined with
other methods that retrieve the content of the response. Otherwise a RestCallException
with an inner IllegalStateException
will be thrown.
m
- The mutable to set the value in.regex
- The regular expression pattern to match.RestCallException
- If a connection error occurred.public Matcher asMatcher(String regex, int flags) throws RestCallException
Content-Type
response header, cache()
or RestResponse.cacheBody()
has been called, this method can be can be called multiple times and/or combined with
other methods that retrieve the content of the response. Otherwise a RestCallException
with an inner IllegalStateException
will be thrown.
regex
- The regular expression pattern to match.flags
- Pattern match flags. See Pattern.compile(String, int)
.RestCallException
- If a connection error occurred.public RestResponse asMatcher(Mutable<Matcher> m, String regex, int flags) throws RestCallException
asMatcher(String,int)
but sets the value in a mutable for fluent calls.
Content-Type
response header, cache()
or RestResponse.cacheBody()
has been called, this method can be can be called multiple times and/or combined with
other methods that retrieve the content of the response. Otherwise a RestCallException
with an inner IllegalStateException
will be thrown.
m
- The mutable to set the value in.regex
- The regular expression pattern to match.flags
- Pattern match flags. See Pattern.compile(String, int)
.RestCallException
- If a connection error occurred.public RestResponse toResponse()
public FluentStringAssertion<RestResponse> assertString() throws RestCallException
This method is called directly from the RestResponse.assertBody()
method to instantiate a fluent assertions object.
The assertion test returns the original response object allowing you to chain multiple requests like so:
Content-Type
response header, cache()
so that the body can be retrieved multiple times.
RestCallException
- If REST call failed.public FluentByteArrayAssertion<RestResponse> assertBytes() throws RestCallException
This method is called directly from the RestResponse.assertBodyBytes()
method to instantiate a fluent assertions object.
Content-Type
response header, cache()
.
RestCallException
- If REST call failed.public FluentObjectAssertion<RestResponse> assertObject(Class<?> type) throws RestCallException
This method is called directly from the RestResponse.assertBody(Class)
method to instantiate a fluent assertions object.
Converts the body of the response to the specified object using as(Class)
and returns it as a fluent assertions object.
Content-Type
response header, cache()
or RestResponse.cacheBody()
has been called, this method can be can be called multiple times and/or combined with
other methods that retrieve the content of the response. Otherwise a RestCallException
with an inner IllegalStateException
will be thrown.
type
- The object type to create.RestCallException
- If REST call failed.public boolean isRepeatable()
A repeatable entity's getContent()
and writeTo(OutputStream)
methods can be called more than
once whereas a non-repeatable entity's can not.
cache()
).
isRepeatable
in interface HttpEntity
public boolean isChunked()
The primary purpose of this method is to indicate whether chunked encoding should be used when the entity is sent.
For entities that are received, it can also indicate whether the entity was received with chunked encoding.
The behavior of wrapping entities is implementation dependent, but should respect the primary purpose.
isChunked
in interface HttpEntity
public long getContentLength()
getContentLength
in interface HttpEntity
Long.MAX_VALUE
, a negative number is returned.public RestResponseHeader getContentType()
This is the header that should be used when sending the entity, or the one that was received with the entity. It can include a charset attribute.
getContentType
in interface HttpEntity
public RestResponseHeader getContentEncoding()
This is the header that should be used when sending the entity, or the one that was received with the entity.
Wrapping entities that modify the content encoding should adjust this header accordingly.
getContentEncoding
in interface HttpEntity
public InputStream getContent() throws IOException, UnsupportedOperationException
asInputStream()
which is the preferred method for fluent-style coding.
IllegalStateException
.
Note that this is different from the HttpClient specs for this method.
getContent
in interface HttpEntity
IOException
UnsupportedOperationException
public void writeTo(OutputStream outstream) throws IOException
pipeTo(OutputStream)
which is the preferred method for fluent-style coding.
writeTo
in interface HttpEntity
outstream
- The output stream to write entity content to.IOException
public boolean isStreaming()
cache()
.
isStreaming
in interface HttpEntity
@Deprecated public void consumeContent() throws IOException
InputStream.close()
on
the input stream returned by getContent()
This method is of particular importance for entities being received from a connection.
The entity needs to be consumed completely in order to re-use the connection with keep-alive.
consumeContent
in interface HttpEntity
IOException
- If an I/O error occurs.Copyright © 2016–2020 The Apache Software Foundation. All rights reserved.