public class RestResponse extends Object implements HttpResponse
Instances of this class are created by calling the RestRequest.run()
method.
Modifier | Constructor and Description |
---|---|
protected |
RestResponse(RestClient client,
RestRequest request,
HttpResponse response,
Parser parser)
Constructor.
|
Modifier and Type | Method and Description |
---|---|
void |
addHeader(Header header)
Adds a header to this message.
|
void |
addHeader(String name,
String value)
Adds a header to this message.
|
FluentStringAssertion<RestResponse> |
assertBody()
Provides the ability to perform fluent-style assertions on this response body.
|
FluentObjectAssertion<RestResponse> |
assertBody(Class<?> type)
Provides the ability to perform fluent-style assertions on this response body.
|
FluentByteArrayAssertion<RestResponse> |
assertBodyBytes()
Provides the ability to perform fluent-style assertions on the bytes of the response body.
|
FluentStringAssertion<RestResponse> |
assertCharset()
Provides the ability to perform fluent-style assertions on the response character encoding.
|
FluentIntegerAssertion<RestResponse> |
assertCode()
Provides the ability to perform fluent-style assertions on the response status code.
|
FluentStringAssertion<RestResponse> |
assertContentType()
Provides the ability to perform fluent-style assertions on the response content type.
|
FluentListAssertion<RestResponse> |
assertCsvArrayHeader(String name)
Provides the ability to perform fluent-style assertions on a date response header.
|
FluentZonedDateTimeAssertion<RestResponse> |
assertDateHeader(String name)
Provides the ability to perform fluent-style assertions on a date response header.
|
FluentIntegerAssertion<RestResponse> |
assertIntegerHeader(String name)
Provides the ability to perform fluent-style assertions on an integer response header.
|
FluentLongAssertion<RestResponse> |
assertLongHeader(String name)
Provides the ability to perform fluent-style assertions on a long response header.
|
RestResponseStatusLineAssertion |
assertStatus()
Provides the ability to perform fluent-style assertions on the response
StatusLine object. |
FluentStringAssertion<RestResponse> |
assertStringHeader(String name)
Provides the ability to perform fluent-style assertions on a response header.
|
RestResponse |
cacheBody()
Caches the response body so that it can be read as a stream multiple times.
|
RestResponse |
consume()
Consumes the response body.
|
boolean |
containsHeader(String name)
Checks if a certain header is present in this message.
|
RestResponseHeader[] |
getAllHeaders()
Returns all the headers of this message.
|
RestResponseBody |
getBody()
Returns the body of the response.
|
String |
getCharacterEncoding()
Shortcut for retrieving the response charset from the
|
ContentType |
getContentType()
Shortcut for retrieving the response content type from the
|
RestResponseBody |
getEntity()
Obtains the message entity of this response.
|
RestResponseHeader |
getFirstHeader(String name)
Returns the first header with a specified name of this message.
|
RestResponseHeader |
getHeader(String name)
Returns the last header with the specified name.
|
RestResponseHeader[] |
getHeaders(String name)
Returns all the headers with a specified name of this message.
|
RestResponseHeader |
getLastHeader(String name)
Returns the last header with a specified name of this message.
|
Locale |
getLocale()
Obtains the locale of this response.
|
HttpParams |
getParams()
Deprecated.
Use configuration classes provided
|
ProtocolVersion |
getProtocolVersion()
Returns the protocol version this message is compatible with.
|
String |
getReasonPhrase()
Returns the status line reason phrase of the response.
|
RestResponse |
getReasonPhrase(Mutable<String> m)
Same as
getReasonPhrase() but sets the value in a mutable for fluent calls. |
RestRequest |
getRequest()
Returns the request object that created this response object.
|
int |
getStatusCode()
Returns the status code of the response.
|
RestResponse |
getStatusCode(Mutable<Integer> m)
Same as
getStatusCode() but sets the value in a mutable for fluent calls. |
StatusLine |
getStatusLine()
Obtains the status line of this response.
|
RestResponse |
getStatusLine(Mutable<StatusLine> m)
Same as
getStatusLine() but sets the value in a mutable for fluent calls. |
String |
getStringHeader(String name)
Shortcut for calling
getHeader(name).asString() . |
String |
getStringHeader(String name,
String def)
Shortcut for calling
getHeader(name).asStringOrElse(def) . |
HeaderIterator |
headerIterator()
Returns an iterator of all the headers.
|
HeaderIterator |
headerIterator(String name)
Returns an iterator of the headers with a given name.
|
RestResponse |
log(Level level,
String msg,
Object... args)
Logs a message.
|
RestResponse |
log(Level level,
Throwable t,
String msg,
Object... args)
Logs a message.
|
void |
removeHeader(Header header)
Removes a header from this message.
|
void |
removeHeaders(String name)
Removes all headers with a certain name from this message.
|
void |
setEntity(HttpEntity entity)
Associates a response entity with this response.
|
void |
setHeader(Header header)
Overwrites the first header with the same name.
|
void |
setHeader(String name,
String value)
Overwrites the first header with the same name.
|
void |
setHeaders(Header[] headers)
Overwrites all the headers in the message.
|
void |
setLocale(Locale loc)
Changes the locale of this response.
|
void |
setParams(HttpParams params)
Deprecated.
Use configuration classes provided
|
void |
setReasonPhrase(String reason)
Updates the status line of this response with a new reason phrase.
|
void |
setStatusCode(int code)
Updates the status line of this response with a new status code.
|
void |
setStatusLine(ProtocolVersion ver,
int code)
Sets the status line of this response.
|
void |
setStatusLine(ProtocolVersion ver,
int code,
String reason)
Sets the status line of this response with a reason phrase.
|
void |
setStatusLine(StatusLine statusline)
Sets the status line of this response.
|
protected RestResponse(RestClient client, RestRequest request, HttpResponse response, Parser parser)
client
- The RestClient that created this response.request
- The REST request.response
- The HTTP response. Can be parser
- The overridden parser passed into RestRequest.parser(Parser)
.public RestRequest getRequest()
public RestResponse consume() throws RestCallException
This is equivalent to closing the input stream.
RestCallException
- If one of the RestCallInterceptors
threw an exception.public RestResponse getStatusLine(Mutable<StatusLine> m)
getStatusLine()
but sets the value in a mutable for fluent calls.m
- The mutable to set the status line in.public int getStatusCode()
getStatusLine().getStatusCode()
.public RestResponse getStatusCode(Mutable<Integer> m)
getStatusCode()
but sets the value in a mutable for fluent calls.m
- The mutable to set the status code in.public String getReasonPhrase()
getStatusLine().getReasonPhrase()
.public RestResponse getReasonPhrase(Mutable<String> m)
getReasonPhrase()
but sets the value in a mutable for fluent calls.m
- The mutable to set the status line reason phrase in.public RestResponseStatusLineAssertion assertStatus() throws RestCallException
StatusLine
object.
MyBean
RestCallException
- If REST call failed.public FluentIntegerAssertion<RestResponse> assertCode() throws RestCallException
MyBean
RestCallException
- If REST call failed.public String getStringHeader(String name)
getHeader(name).asString()
.name
- The header name.public String getStringHeader(String name, String def)
getHeader(name).asStringOrElse(def)
.name
- The header name.def
- The default value if the header was not found.public RestResponseHeader getHeader(String name)
getFirstHeader(String)
and getLastHeader(String)
, this method returns an empty
RestResponseHeader
object instead of returning name
- The header name.public String getCharacterEncoding() throws RestCallException
RestCallException
- If REST call failed.public ContentType getContentType() throws RestCallException
This is equivalent to calling
RestCallException
- If REST call failed.public FluentStringAssertion<RestResponse> assertStringHeader(String name)
The assertion test returns the original response object allowing you to chain multiple requests like so:
name
- The header name.public FluentIntegerAssertion<RestResponse> assertIntegerHeader(String name)
name
- The header name.public FluentLongAssertion<RestResponse> assertLongHeader(String name)
name
- The header name.public FluentZonedDateTimeAssertion<RestResponse> assertDateHeader(String name)
name
- The header name.public FluentListAssertion<RestResponse> assertCsvArrayHeader(String name)
name
- The header name.public FluentStringAssertion<RestResponse> assertCharset() throws RestCallException
RestCallException
- If REST call failed.public FluentStringAssertion<RestResponse> assertContentType()
Note that this is equivalent to the following code:
public RestResponseBody getBody()
public FluentStringAssertion<RestResponse> assertBody() throws RestCallException
The assertion test returns the original response object allowing you to chain multiple requests like so:
Content-Type
response header, RestResponseBody.cache()
.
RestCallException
- If REST call failed.public FluentByteArrayAssertion<RestResponse> assertBodyBytes() throws RestCallException
Content-Type
response header, RestResponseBody.cache()
.
RestCallException
- If REST call failed.public FluentObjectAssertion<RestResponse> assertBody(Class<?> type) throws RestCallException
Combines the functionality of RestResponseBody.as(Class)
with assertBody()
by converting the body to the specified
bean and then serializing it to simplified JSON for easy string comparison.
Content-Type
response header, RestResponseBody.cache()
.
type
- The object type to create.RestCallException
- If REST call failed.public RestResponse cacheBody()
getBody().cache();
public RestResponse log(Level level, Throwable t, String msg, Object... args)
level
- The log level.t
- The throwable cause.msg
- The message with MessageFormat
-style arguments.args
- The arguments.public RestResponse log(Level level, String msg, Object... args)
level
- The log level.msg
- The message with MessageFormat
-style arguments.args
- The arguments.public StatusLine getStatusLine()
getStatusLine
in interface HttpResponse
public void setStatusLine(StatusLine statusline)
setStatusLine
in interface HttpResponse
statusline
- The status line of this responsepublic void setStatusLine(ProtocolVersion ver, int code)
The reason phrase will be determined based on the current locale.
setStatusLine
in interface HttpResponse
ver
- The HTTP version.code
- The status code.public void setStatusLine(ProtocolVersion ver, int code, String reason)
setStatusLine
in interface HttpResponse
ver
- The HTTP version.code
- The status code.reason
- The reason phrase, or public void setStatusCode(int code)
setStatusCode
in interface HttpResponse
code
- The HTTP status code.IllegalStateException
- If the status line has not be set.public void setReasonPhrase(String reason)
setReasonPhrase
in interface HttpResponse
reason
- The new reason phrase as a single-line string, or IllegalStateException
- If the status line has not be set.public RestResponseBody getEntity()
The entity is provided by calling setEntity.
HttpResponse.getEntity()
method, this method never returns a getEntity
in interface HttpResponse
public void setEntity(HttpEntity entity)
HttpEntity.isStreaming()
returns setEntity
in interface HttpResponse
entity
- The entity to associate with this response, or public Locale getLocale()
setLocale(Locale)
.getLocale
in interface HttpResponse
public void setLocale(Locale loc)
setLocale
in interface HttpResponse
loc
- The new locale.public ProtocolVersion getProtocolVersion()
getProtocolVersion
in interface HttpMessage
public boolean containsHeader(String name)
Header values are ignored.
containsHeader
in interface HttpMessage
name
- The header name to check for.public RestResponseHeader[] getHeaders(String name)
getHeaders
in interface HttpMessage
name
- The name of the headers to return.public RestResponseHeader getFirstHeader(String name)
getHeaders(String)
is returned.getFirstHeader
in interface HttpMessage
name
- The name of the header to return.public RestResponseHeader getLastHeader(String name)
getHeaders(String)
is returned.getLastHeader
in interface HttpMessage
name
- The name of the header to return.public RestResponseHeader[] getAllHeaders()
getAllHeaders
in interface HttpMessage
public void addHeader(Header header)
addHeader
in interface HttpMessage
header
- The header to append.public void addHeader(String name, String value)
addHeader
in interface HttpMessage
name
- The name of the header.value
- The value of the header.public void setHeader(Header header)
setHeader
in interface HttpMessage
header
- The header to set.public void setHeader(String name, String value)
setHeader
in interface HttpMessage
name
- The name of the header.value
- The value of the header.public void setHeaders(Header[] headers)
setHeaders
in interface HttpMessage
headers
- The array of headers to set.public void removeHeader(Header header)
removeHeader
in interface HttpMessage
header
- The header to remove.public void removeHeaders(String name)
removeHeaders
in interface HttpMessage
name
- The name of the headers to remove.public HeaderIterator headerIterator()
headerIterator
in interface HttpMessage
Iterator
that returns Header
objects in the sequence they are sent over a connection.public HeaderIterator headerIterator(String name)
headerIterator
in interface HttpMessage
name
- The name of the headers over which to iterate, or Iterator
that returns Header
objects with the argument name in the sequence they are sent over a connection.@Deprecated public HttpParams getParams()
setParams(HttpParams)
.getParams
in interface HttpMessage
setParams(HttpParams)
.@Deprecated public void setParams(HttpParams params)
setParams
in interface HttpMessage
params
- The parameters.Copyright © 2016–2020 The Apache Software Foundation. All rights reserved.