public final class RestCall extends BeanSession implements Closeable
Instances of this class are created by the various doX()
methods on the RestClient
class.
This class uses only Java standard APIs. Requests can be built up using a fluent interface with method chaining, like so...
RestClient client =
The actual connection and request/response transaction occurs when calling one of the getResponseXXX()
methods.
Modifier | Constructor and Description |
---|---|
protected |
RestCall(RestClient client,
org.apache.http.client.methods.HttpRequestBase request,
URI uri)
Constructs a REST call with the specified method name.
|
Modifier and Type | Method and Description |
---|---|
RestCall |
accept(Object value)
Sets the value for the
Accept request header. |
RestCall |
acceptCharset(Object value)
Sets the value for the
Accept-Charset request header. |
RestCall |
acceptEncoding(Object value)
Sets the value for the
Accept-Encoding request header. |
RestCall |
acceptLanguage(Object value)
Sets the value for the
Accept-Language request header. |
RestCall |
allowRedirectsOnPosts(boolean b)
For this call, allow automatic redirects when a 302 or 307 occurs when performing a POST.
|
RestCall |
authorization(Object value)
Sets the value for the
Authorization request header. |
RestCall |
byLines()
When output is piped to writers, flush the writers after every line of output.
|
RestCall |
cacheControl(Object value)
Sets the value for the
Cache-Control request header. |
RestCall |
captureResponse()
Stores the response text so that it can later be captured using
getCapturedResponse() . |
RestCall |
clientVersion(String version)
Sets the client version by setting the value for the
|
void |
close()
Cleans up this HTTP call.
|
RestCall |
connect()
Connects to the REST resource.
|
RestCall |
connection(Object value)
Sets the value for the
Connection request header. |
void |
consumeResponse()
Deprecated.
|
RestCall |
contentLength(Object value)
Sets the value for the
Content-Length request header. |
RestCall |
contentType(Object value)
Sets the value for the
Content-Type request header. |
RestCall |
date(Object value)
Sets the value for the
Date request header. |
RestCall |
debug()
Sets
Debug: value header on this request. |
int |
execute()
Deprecated.
Use
run() . |
RestCall |
expect(Object value)
Sets the value for the
Expect request header. |
RestCall |
failurePattern(String errorPattern)
Look for the specified regular expression pattern in the response output.
|
RestCall |
formData(Map<String,Object> params)
Adds form data pairs to this request to perform a URL-encoded form post.
|
RestCall |
formData(NameValuePairs nameValuePairs)
Adds form data pairs to this request to perform a URL-encoded form post.
|
RestCall |
formData(String name,
Object value)
Adds a form data pair to this request to perform a URL-encoded form post.
|
RestCall |
formData(String name,
Object value,
boolean skipIfEmpty,
HttpPartSerializer partSerializer)
Adds a form data pair to this request to perform a URL-encoded form post.
|
RestCall |
formDataIfNE(Map<String,Object> params)
Adds form data parameters to the request for any parameters that aren't null/empty.
|
RestCall |
formDataIfNE(String name,
Object value)
Adds a form data pair to the request if the parameter value is not
|
RestCall |
forwarded(Object value)
Sets the value for the
Forwarded request header. |
RestCall |
from(Object value)
Sets the value for the
From request header. |
String |
getCapturedResponse()
Returns the response text as a string if
captureResponse() was called on this object. |
int |
getContentLength()
Returns the value of the
Content-Length header. |
InputStream |
getInputStream()
Connects to the remote resource (if
connect() hasn't already been called) and returns the HTTP
response message body as an input stream. |
OutputStream |
getOutputStream(String id)
Retrieves an output stream associated with an ID via
pipeTo(String, OutputStream, boolean) |
protected Parser |
getParser()
Returns the parser specified on the client to use for parsing HTTP response bodies.
|
Reader |
getReader()
Connects to the remote resource (if
connect() hasn't already been called) and returns the HTTP
response message body as a reader. |
org.apache.http.client.methods.HttpUriRequest |
getRequest()
Returns access to the
HttpUriRequest passed to HttpClient.execute(HttpUriRequest) . |
org.apache.http.HttpResponse |
getResponse()
Returns access to the
HttpResponse returned by HttpClient.execute(HttpUriRequest) . |
<T> T |
getResponse(Class<T> type)
Same as
getResponse(Type, Type...) except optimized for a non-parameterized class. |
<T> T |
getResponse(Type type,
Type... args)
Parses HTTP body into the specified object type.
|
String |
getResponseAsString()
Connects to the remote resource (if
connect() hasn't already been called) and returns the HTTP response
message body as plain text. |
Future<String> |
getResponseAsStringFuture()
Same as
getResponse(Class) but allows you to run the call asynchronously. |
int |
getResponseCode()
Connects to the remote resource (if
connect() hasn't already been called) and returns the HTTP response code. |
<T> Future<T> |
getResponseFuture(Class<T> type)
Same as
getResponse(Class) but allows you to run the call asynchronously. |
<T> Future<T> |
getResponseFuture(Type type,
Type... args)
Same as
getResponse(Class) but allows you to run the call asynchronously. |
String |
getResponseHeader(String name)
Connects to the remote resource (if
connect() hasn't already been called) and returns the value of
an HTTP header on the response. |
PojoRest |
getResponsePojoRest()
|
PojoRest |
getResponsePojoRest(Class<?> innerType)
Parses the output from the connection into the specified type and then wraps that in a
PojoRest . |
protected Serializer |
getSerializer()
Returns the serializer specified on the client to use for serializing HTTP request bodies.
|
Writer |
getWriter(String id)
Retrieves a writer associated with an ID via
pipeTo(String, Writer, boolean) |
RestCall |
header(org.apache.http.Header header)
Shortcut for calling
getRequest().setHeader(header) |
RestCall |
header(String name,
Object value)
Sets a header on the request.
|
RestCall |
header(String name,
Object value,
boolean skipIfEmpty,
HttpPartSerializer partSerializer)
Sets a header on the request.
|
RestCall |
headerIfNE(String name,
Object value)
Sets a header on the request if the value is not null/empty.
|
RestCall |
headers(Map<String,Object> values)
Sets headers on the request.
|
RestCall |
headersIfNE(Map<String,Object> values)
Sets headers on the request if the values are not null/empty.
|
RestCall |
host(Object value)
Sets the value for the
Host request header. |
RestCall |
host(String host)
Sets the URI host.
|
RestCall |
ifMatch(Object value)
Sets the value for the
If-Match request header. |
RestCall |
ifModifiedSince(Object value)
Sets the value for the
If-Modified-Since request header. |
RestCall |
ifNoneMatch(Object value)
Sets the value for the
If-None-Match request header. |
RestCall |
ifRange(Object value)
Sets the value for the
If-Range request header. |
RestCall |
ifUnmodifiedSince(Object value)
Sets the value for the
If-Unmodified-Since request header. |
RestCall |
ignoreErrors()
Prevent
RestCallExceptions from being thrown when HTTP status 400+ is encountered. |
RestCall |
input(Object input)
Sets the input for this REST call.
|
RestCall |
interceptor(RestCallInterceptor interceptor)
Add an interceptor for this call only.
|
RestCall |
logTo(Level level,
Logger log)
Adds a
RestCallLogger to the list of interceptors on this class. |
RestCall |
maxForwards(Object value)
Sets the value for the
Max-Forwards request header. |
RestCall |
origin(Object value)
Sets the value for the
Origin request header. |
RestCall |
parser(Parser parser)
Specifies the parser to use on this call.
|
RestCall |
path(String name,
Object value)
Replaces a variable of the form
|
RestCall |
path(String name,
Object value,
HttpPartSerializer partSerializer)
Replaces a variable of the form
|
RestCall |
pipeTo(OutputStream os)
Pipes the request output to the specified output stream when
run() is called. |
RestCall |
pipeTo(OutputStream os,
boolean close)
Pipe output from response to the specified output stream when
run() is called. |
RestCall |
pipeTo(String id,
OutputStream os,
boolean close)
Pipe output from response to the specified output stream when
run() is called and associate
that output stream with an ID so it can be retrieved through getOutputStream(String) . |
RestCall |
pipeTo(String id,
Writer w,
boolean close)
Pipe output from response to the specified writer when
run() is called and associate that writer with an
ID so it can be retrieved through getWriter(String) . |
RestCall |
pipeTo(Writer w)
Pipes the request output to the specified writer when
run() is called. |
RestCall |
pipeTo(Writer w,
boolean close)
Pipe output from response to the specified writer when
run() is called. |
RestCall |
port(int port)
Sets the URI port.
|
RestCall |
pragma(Object value)
Sets the value for the
Pragma request header. |
RestCall |
proxyAuthorization(Object value)
Sets the value for the
Proxy-Authorization request header. |
RestCall |
query(Map<String,Object> params)
Adds query parameters to the URI query.
|
RestCall |
query(String query)
Sets a custom URI query.
|
RestCall |
query(String name,
Object value)
Adds a query parameter to the URI query.
|
RestCall |
query(String name,
Object value,
boolean skipIfEmpty,
HttpPartSerializer partSerializer)
Adds a query parameter to the URI query.
|
RestCall |
queryIfNE(Map<String,Object> params)
Adds query parameters to the URI for any parameters that aren't null/empty.
|
RestCall |
queryIfNE(String name,
Object value)
Adds a query parameter to the URI query if the parameter value is not
|
RestCall |
range(Object value)
Sets the value for the
Range request header. |
RestCall |
redirectMaxAttempts(int maxAttempts)
Specify the number of redirects to follow before throwing an exception.
|
RestCall |
referer(Object value)
Sets the value for the
Referer request header. |
RestCall |
responsePattern(ResponsePattern responsePattern)
Adds a response pattern finder to look for regular expression matches in the response output.
|
RestCall |
retryable(int retries,
long interval,
RetryOn retryOn)
Make this call retryable if an error response (>=400) is received.
|
int |
run()
Method used to execute an HTTP response where you're only interested in the HTTP response code.
|
Future<Integer> |
runFuture()
Same as
run() but allows you to run the call asynchronously. |
RestCall |
scheme(String scheme)
Sets the URI scheme.
|
RestCall |
serializer(Serializer serializer)
Specifies the serializer to use on this call.
|
RestCall |
setConfig(org.apache.http.client.config.RequestConfig config)
Set configuration settings on this request.
|
RestCall |
successPattern(String successPattern)
Look for the specified regular expression pattern in the response output.
|
RestCall |
te(Object value)
Sets the value for the
TE request header. |
RestCall |
upgrade(Object value)
Sets the value for the
Upgrade request header. |
RestCall |
uri(Object uri)
Sets the URI for this call.
|
RestCall |
userAgent(Object value)
Sets the value for the
User-Agent request header. |
RestCall |
userInfo(String userInfo)
Sets the URI user info.
|
RestCall |
userInfo(String username,
String password)
Sets the URI user info.
|
RestCall |
via(Object value)
Sets the value for the
Via request header. |
RestCall |
warning(Object value)
Sets the value for the
Warning request header. |
_class, asMap, checkForWarnings, convertToMemberType, convertToMemberType, convertToType, convertToType, convertToType, getArgsClassMeta, getBeanMeta, getBeanRegistry, getBeanTypePropertyName, getClassMeta, getClassMeta, getClassMetaForObject, getLocale, getMediaType, getStringBuilder, getTimeZone, isBean, isBean, isDebug, isIgnoreUnknownBeanProperties, newBean, newBean, newBeanMap, newBeanMap, newInstance, newInstance, newInstanceFromOuter, object, returnStringBuilder, string, toArray, toBeanMap, toBeanMap
addToCache, addToCache, addWarning, getClassProperty, getFromCache, getInstanceArrayProperty, getInstanceProperty, getLogger, getProperties, getProperty, getProperty, getProperty, getPropertyKeys, getWarnings, hasWarnings, toString
protected RestCall(RestClient client, org.apache.http.client.methods.HttpRequestBase request, URI uri) throws RestCallException
client
- The client that created this request.request
- The wrapped Apache HTTP client request object.uri
- The URI for this call.RestCallException
- If an exception or non-200 response code occurred during the connection attempt.public RestCall uri(Object uri) throws RestCallException
Can be any of the following types:
URI
URL
URIBuilder
Object.toString()
.
Relative URL strings will be interpreted as relative to the root URL defined on the client.
uri
- The URI to use for this call.
This overrides the URI passed in from the client.RestCallException
public RestCall scheme(String scheme)
scheme
- The new URI host.public RestCall host(String host)
host
- The new URI host.public RestCall port(int port)
port
- The new URI port.public RestCall query(String name, Object value, boolean skipIfEmpty, HttpPartSerializer partSerializer) throws RestCallException
name
- The parameter name.
Can be null/blank/* if the value is a Map
, String
, NameValuePairs
, or bean.value
- The parameter value converted to a string using UON notation.
Can also be Map
, String
, NameValuePairs
, or bean if the name is null/blank/*.
If a String
and the name is null/blank/*, then calls URIBuilder.setCustomQuery(String)
.skipIfEmpty
- Don't add the pair if the value is empty.partSerializer
- The part serializer to use to convert the value to a string.
If RestCallException
public RestCall query(String name, Object value) throws RestCallException
name
- The parameter name.value
- The parameter value converted to a string using UON notation.RestCallException
public RestCall query(Map<String,Object> params) throws RestCallException
params
- The parameters. Values are converted to a string using UON notation.RestCallException
public RestCall queryIfNE(String name, Object value) throws RestCallException
NE = "not empty"
name
- The parameter name.value
- The parameter value converted to a string using UON notation.RestCallException
public RestCall queryIfNE(Map<String,Object> params) throws RestCallException
NE = "not empty"
params
- The parameters. Values are converted to a string using UON notation.RestCallException
public RestCall query(String query)
query
- The new URI query string.public RestCall formData(String name, Object value, boolean skipIfEmpty, HttpPartSerializer partSerializer) throws RestCallException
name
- The parameter name.
Can be null/blank/* if the value is a Map
, NameValuePairs
, or bean.value
- The parameter value converted to a string using UON notation.
Can also be Map
, NameValuePairs
, or bean if the name is null/blank/*.skipIfEmpty
- Don't add the pair if the value is empty.partSerializer
- The part serializer to use to convert the value to a string.
If RestCallException
public RestCall formData(String name, Object value) throws RestCallException
name
- The parameter name.
Can be null/blank if the value is a Map
or NameValuePairs
.value
- The parameter value converted to a string using UON notation.
Can also be a Map
or NameValuePairs
.RestCallException
- If name was null/blank and value wasn't a Map
or NameValuePairs
.public RestCall formData(NameValuePairs nameValuePairs) throws RestCallException
nameValuePairs
- The name-value pairs of the request.RestCallException
public RestCall formData(Map<String,Object> params) throws RestCallException
params
- The parameters. Values are converted to a string using UON notation.RestCallException
- If name was null/blank and value wasn't a Map
or NameValuePairs
.public RestCall formDataIfNE(String name, Object value) throws RestCallException
NE = "not empty"
name
- The parameter name.value
- The parameter value converted to a string using UON notation.RestCallException
public RestCall formDataIfNE(Map<String,Object> params) throws RestCallException
NE = "not empty"
params
- The parameters. Values are converted to a string using UON notation.RestCallException
public RestCall path(String name, Object value, HttpPartSerializer partSerializer) throws RestCallException
name
- The path variable name.value
- The replacement value.partSerializer
- The part serializer to use to convert the value to a string.
If RestCallException
- If variable could not be found in path.public RestCall path(String name, Object value) throws RestCallException
name
- The path variable name.value
- The replacement value.RestCallException
- If variable could not be found in path.public RestCall userInfo(String userInfo)
userInfo
- The new URI user info.public RestCall userInfo(String username, String password)
username
- The new URI username.password
- The new URI password.public RestCall input(Object input) throws RestCallException
input
- The input to be sent to the REST resource (only valid for PUT and POST) requests. Reader
- Raw contents of Reader
will be serialized to remote resource.
InputStream
- Raw contents of InputStream
will be serialized to remote resource.
Object
- POJO to be converted to text using the Serializer
registered with the
RestClient
.
HttpEntity
- Bypass Juneau serialization and pass HttpEntity directly to HttpClient.
NameValuePairs
- Converted to a URL-encoded FORM post.
RestCallException
- If a retry was attempted, but the entity was not repeatable.public RestCall serializer(Serializer serializer)
Overrides the serializer specified on the RestClient
.
serializer
- The serializer used to serialize POJOs to the body of the HTTP request.public RestCall parser(Parser parser)
Overrides the parser specified on the RestClient
.
parser
- The parser used to parse POJOs from the body of the HTTP response.public RestCall header(String name, Object value, boolean skipIfEmpty, HttpPartSerializer partSerializer) throws RestCallException
name
- The header name.
The name can be null/empty if the value is a Map
.value
- The header value.skipIfEmpty
- Don't add the header if the name is null/empty.partSerializer
- The part serializer to use to convert the value to a string.
If RestCallException
public RestCall header(String name, Object value) throws RestCallException
name
- The header name.
The name can be null/empty if the value is a Map
.value
- The header value.RestCallException
public RestCall headers(Map<String,Object> values) throws RestCallException
values
- The header values.RestCallException
public RestCall headerIfNE(String name, Object value) throws RestCallException
NE = "not empty"
name
- The header name.
The name can be null/empty if the value is a Map
.value
- The header value.RestCallException
public RestCall headersIfNE(Map<String,Object> values) throws RestCallException
NE = "not empty"
values
- The header values.RestCallException
public RestCall accept(Object value) throws RestCallException
Accept
request header.
This overrides the media type specified on the parser, but is overridden by calling
header(
value
- The new header value.RestCallException
public RestCall acceptCharset(Object value) throws RestCallException
Accept-Charset
request header.
This is a shortcut for calling header(
value
- The new header value.RestCallException
public RestCall acceptEncoding(Object value) throws RestCallException
Accept-Encoding
request header.
This is a shortcut for calling header(
value
- The new header value.RestCallException
public RestCall acceptLanguage(Object value) throws RestCallException
Accept-Language
request header.
This is a shortcut for calling header(
value
- The new header value.RestCallException
public RestCall authorization(Object value) throws RestCallException
Authorization
request header.
This is a shortcut for calling header(
value
- The new header value.RestCallException
public RestCall cacheControl(Object value) throws RestCallException
Cache-Control
request header.
This is a shortcut for calling header(
value
- The new header value.RestCallException
public RestCall connection(Object value) throws RestCallException
Connection
request header.
This is a shortcut for calling header(
value
- The new header value.RestCallException
public RestCall contentLength(Object value) throws RestCallException
Content-Length
request header.
This is a shortcut for calling header(
value
- The new header value.RestCallException
public RestCall contentType(Object value) throws RestCallException
Content-Type
request header.
This overrides the media type specified on the serializer, but is overridden by calling
header(
value
- The new header value.RestCallException
public RestCall date(Object value) throws RestCallException
Date
request header.
This is a shortcut for calling header(
value
- The new header value.RestCallException
public RestCall expect(Object value) throws RestCallException
Expect
request header.
This is a shortcut for calling header(
value
- The new header value.RestCallException
public RestCall forwarded(Object value) throws RestCallException
Forwarded
request header.
This is a shortcut for calling header(
value
- The new header value.RestCallException
public RestCall from(Object value) throws RestCallException
From
request header.
This is a shortcut for calling header(
value
- The new header value.RestCallException
public RestCall host(Object value) throws RestCallException
Host
request header.
This is a shortcut for calling header(
value
- The new header value.RestCallException
public RestCall ifMatch(Object value) throws RestCallException
If-Match
request header.
This is a shortcut for calling header(
value
- The new header value.RestCallException
public RestCall ifModifiedSince(Object value) throws RestCallException
If-Modified-Since
request header.
This is a shortcut for calling header(
value
- The new header value.RestCallException
public RestCall ifNoneMatch(Object value) throws RestCallException
If-None-Match
request header.
This is a shortcut for calling header(
value
- The new header value.RestCallException
public RestCall ifRange(Object value) throws RestCallException
If-Range
request header.
This is a shortcut for calling header(
value
- The new header value.RestCallException
public RestCall ifUnmodifiedSince(Object value) throws RestCallException
If-Unmodified-Since
request header.
This is a shortcut for calling header(
value
- The new header value.RestCallException
public RestCall maxForwards(Object value) throws RestCallException
Max-Forwards
request header.
This is a shortcut for calling header(
value
- The new header value.RestCallException
public RestCall origin(Object value) throws RestCallException
Origin
request header.
This is a shortcut for calling header(
value
- The new header value.RestCallException
public RestCall pragma(Object value) throws RestCallException
Pragma
request header.
This is a shortcut for calling header(
value
- The new header value.RestCallException
public RestCall proxyAuthorization(Object value) throws RestCallException
Proxy-Authorization
request header.
This is a shortcut for calling header(
value
- The new header value.RestCallException
public RestCall range(Object value) throws RestCallException
Range
request header.
This is a shortcut for calling header(
value
- The new header value.RestCallException
public RestCall referer(Object value) throws RestCallException
Referer
request header.
This is a shortcut for calling header(
value
- The new header value.RestCallException
public RestCall te(Object value) throws RestCallException
TE
request header.
This is a shortcut for calling header(
value
- The new header value.RestCallException
public RestCall userAgent(Object value) throws RestCallException
User-Agent
request header.
This is a shortcut for calling header(
value
- The new header value.RestCallException
public RestCall upgrade(Object value) throws RestCallException
Upgrade
request header.
This is a shortcut for calling header(
value
- The new header value.RestCallException
public RestCall via(Object value) throws RestCallException
Via
request header.
This is a shortcut for calling header(
value
- The new header value.RestCallException
public RestCall warning(Object value) throws RestCallException
Warning
request header.
This is a shortcut for calling header(
value
- The new header value.RestCallException
public RestCall clientVersion(String version) throws RestCallException
version
- The version string (e.g. RestCallException
public RestCall retryable(int retries, long interval, RetryOn retryOn) throws RestCallException
retries
- The number of retries to attempt.interval
- The time in milliseconds between attempts.retryOn
- Optional object used for determining whether a retry should be attempted.
If RetryOn.DEFAULT
.RestCallException
- If current entity is not repeatable.public RestCall allowRedirectsOnPosts(boolean b)
Note that this can be inefficient since the POST body needs to be serialized twice.
The preferred approach if possible is to use the LaxRedirectStrategy
strategy on the underlying HTTP
client.
However, this method is provided if you don't have access to the underlying client.
b
- Redirect flag.public RestCall redirectMaxAttempts(int maxAttempts)
maxAttempts
- Allow a redirect to occur this number of times.public RestCall interceptor(RestCallInterceptor interceptor)
interceptor
- The interceptor to add to this call.public RestCall pipeTo(Writer w)
run()
is called.
The writer is not closed.
This method can be called multiple times to pipe to multiple writers.
w
- The writer to pipe the output to.public RestCall pipeTo(Writer w, boolean close)
run()
is called.
This method can be called multiple times to pipe to multiple writers.
w
- The writer to write the output to.close
- Close the writer when close()
is called.public RestCall pipeTo(String id, Writer w, boolean close)
run()
is called and associate that writer with an
ID so it can be retrieved through getWriter(String)
.
This method can be called multiple times to pipe to multiple writers.
id
- A string identifier that can be used to retrieve the writer using getWriter(String)
w
- The writer to write the output to.close
- Close the writer when close()
is called.public Writer getWriter(String id)
pipeTo(String, Writer, boolean)
id
- A string identifier that can be used to retrieve the writer using getWriter(String)
public RestCall byLines()
public RestCall pipeTo(OutputStream os)
run()
is called.
The output stream is not closed.
This method can be called multiple times to pipe to multiple output streams.
os
- The output stream to pipe the output to.public RestCall pipeTo(OutputStream os, boolean close)
run()
is called.
This method can be called multiple times to pipe to multiple output stream.
os
- The output stream to write the output to.close
- Close the output stream when close()
is called.public RestCall pipeTo(String id, OutputStream os, boolean close)
run()
is called and associate
that output stream with an ID so it can be retrieved through getOutputStream(String)
.
This method can be called multiple times to pipe to multiple output stream.
id
- A string identifier that can be used to retrieve the output stream using getOutputStream(String)
os
- The output stream to write the output to.close
- Close the output stream when close()
is called.public OutputStream getOutputStream(String id)
pipeTo(String, OutputStream, boolean)
id
- A string identifier that can be used to retrieve the writer using getWriter(String)
public RestCall ignoreErrors()
RestCallExceptions
from being thrown when HTTP status 400+ is encountered.public RestCall captureResponse()
getCapturedResponse()
.
This method should only be called once. Multiple calls to this method are ignored.
public RestCall failurePattern(String errorPattern)
Causes a RestCallException
to be thrown if the specified pattern is found in the output.
This method uses getCapturedResponse()
to read the response text and so does not affect the other output
methods such as getResponseAsString()
.
errorPattern
- A regular expression to look for in the response output.public RestCall successPattern(String successPattern)
Causes a RestCallException
to be thrown if the specified pattern is not found in the output.
This method uses getCapturedResponse()
to read the response text and so does not affect the other output
methods such as getResponseAsString()
.
successPattern
- A regular expression to look for in the response output.public RestCall responsePattern(ResponsePattern responsePattern)
This method can be called multiple times to add multiple response pattern finders.
ResponsePatterns
use the getCapturedResponse()
to read the response text and so
does not affect the other output methods such as getResponseAsString()
.
responsePattern
- The response pattern finder.public RestCall setConfig(org.apache.http.client.config.RequestConfig config)
Use RequestConfig.custom()
to create configuration parameters for the request.
config
- The new configuration settings for this request.@Deprecated public int execute() throws RestCallException
run()
.RestCallException
public int run() throws RestCallException
The response entity is discarded unless one of the pipe methods have been specified to pipe the output to an output stream or writer.
RestCallException
- If an exception or non-200 response code occurred during the connection attempt.public Future<Integer> runFuture() throws RestCallException
run()
but allows you to run the call asynchronously.RestCallException
- If the executor service was not defined.for defining the executor service for creating
{@link Future Futures}.
public RestCall connect() throws RestCallException
If this is a PUT
or POST
, also sends the input to the remote resource.
Typically, you would only call this method if you're not interested in retrieving the body of the HTTP response.
Otherwise, you're better off just calling one of the getReader()
/getResponse(Class)
/pipeTo(Writer)
methods directly which automatically call this method already.
RestCallException
- If an exception or 400+
HTTP status code occurred during the connection attempt.public Reader getReader() throws IOException
connect()
hasn't already been called) and returns the HTTP
response message body as a reader.
If an Encoder
has been registered with the RestClient
, then the underlying input stream will be
wrapped in the encoded stream (e.g. a GZIPInputStream
).
If present, automatically handles the charset
value in the Content-Type
response header.
IMPORTANT: It is your responsibility to close this reader once you have finished with it.
IOException
- If an exception occurred while streaming was already occurring.public String getCapturedResponse()
captureResponse()
was called on this object.
Note that while similar to getResponseAsString()
, this method can be called multiple times to retrieve
the response text multiple times.
Note that this method returns run()
, getResponse()
, getResponseAsString()
.
captureResponse()
has not been called.IllegalStateException
- If trying to call this method before the response is consumed.protected Parser getParser() throws RestCallException
RestCallException
- If no parser was defined on the client.protected Serializer getSerializer() throws RestCallException
RestCallException
- If no serializer was defined on the client.public int getContentLength() throws IOException
Content-Length
header.Content-Length
header, or -1
if header is not present.IOException
public InputStream getInputStream() throws IOException
connect()
hasn't already been called) and returns the HTTP
response message body as an input stream.
If an Encoder
has been registered with the RestClient
, then the underlying input stream will be
wrapped in the encoded stream (e.g. a GZIPInputStream
).
IMPORTANT: It is your responsibility to close this reader once you have finished with it.
IOException
- If an exception occurred while streaming was already occurring.IllegalStateException
- If an attempt is made to read the response more than once.public String getResponseAsString() throws IOException
connect()
hasn't already been called) and returns the HTTP response
message body as plain text.
The response entity is discarded unless one of the pipe methods have been specified to pipe the output to an output stream or writer.
RestCallException
- If an exception or non-200 response code occurred during the connection attempt.IOException
- If an exception occurred while streaming was already occurring.public String getResponseHeader(String name) throws IOException
connect()
hasn't already been called) and returns the value of
an HTTP header on the response.
Useful if you're only interested in a particular header value from the response and not the body of the response.
The response entity is discarded unless one of the pipe methods have been specified to pipe the output to an output stream or writer.
name
- The header name.RestCallException
- If an exception or non-200 response code occurred during the connection attempt.IOException
- If an exception occurred while streaming was already occurring.public int getResponseCode() throws IOException
connect()
hasn't already been called) and returns the HTTP response code.
Useful if you're only interested in the status code and not the body of the response.
The response entity is discarded unless one of the pipe methods have been specified to pipe the output to an output stream or writer.
RestCallException
- If an exception or non-200 response code occurred during the connection attempt.IOException
- If an exception occurred while streaming was already occurring.public Future<String> getResponseAsStringFuture() throws RestCallException
getResponse(Class)
but allows you to run the call asynchronously.RestCallException
- If the executor service was not defined.for defining the executor service for creating
{@link Future Futures}.
public <T> T getResponse(Class<T> type) throws IOException, ParseException
getResponse(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.
HttpResponse
- Returns the raw HttpResponse
returned by the inner HttpClient
.
Reader
- Returns access to the raw reader of the response.
InputStream
- Returns access to the raw input stream of the response.
T
- The class type of the object being created.
See getResponse(Type, Type...)
for details.type
- The object type to create.ParseException
- If the input contains a syntax error or is malformed, or is not valid for the specified type.IOException
- If a connection error occurred.public <T> Future<T> getResponseFuture(Class<T> type) throws RestCallException
getResponse(Class)
but allows you to run the call asynchronously.T
- The class type of the object being created.
See getResponse(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> T getResponse(Type type, Type... args) throws IOException, ParseException
The type can be a simple type (e.g. beans, strings, numbers) or parameterized type (collections/maps).
Collection
classes are assumed to be followed by zero or one objects indicating the element type.
Map
classes are assumed to be followed by zero or two meta objects indicating the key and value types.
The array can be arbitrarily long to indicate arbitrarily complex data structures.
getResponse(Class)
method instead if you don't need a parameterized map/collection.
HttpResponse
- Returns the raw HttpResponse
returned by the inner HttpClient
.
Reader
- Returns access to the raw reader of the response.
InputStream
- Returns access to the raw input stream of the response.
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
ParseException
- If the input contains a syntax error or is malformed, or is not valid for the specified type.IOException
- If a connection error occurred.for argument syntax for maps and collections.
public <T> Future<T> getResponseFuture(Type type, Type... args) throws RestCallException
getResponse(Class)
but allows you to run the call asynchronously.T
- The class type of the object being created.
See getResponse(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 PojoRest getResponsePojoRest(Class<?> innerType) throws IOException, ParseException
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
.IOException
- If a connection error occurred.ParseException
- If the input contains a syntax error or is malformed for the Content-Type
header.public PojoRest getResponsePojoRest() throws IOException, ParseException
ObjectMap
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
.IOException
- If a connection error occurred.ParseException
- If the input contains a syntax error or is malformed for the Content-Type
header.public org.apache.http.client.methods.HttpUriRequest getRequest()
HttpUriRequest
passed to HttpClient.execute(HttpUriRequest)
.HttpUriRequest
object.public org.apache.http.HttpResponse getResponse() throws IOException
HttpResponse
returned by HttpClient.execute(HttpUriRequest)
.
Returns connect()
has not yet been called.
IOException
public RestCall header(org.apache.http.Header header)
getRequest().setHeader(header)
header
- The header to set on the request.@Deprecated public void consumeResponse()
public void close() throws RestCallException
close
in interface Closeable
close
in interface AutoCloseable
RestCallException
- Can be thrown by one of the RestCallInterceptor.onClose(RestCall)
calls.public RestCall logTo(Level level, Logger log)
RestCallLogger
to the list of interceptors on this class.level
- The log level to log events at.log
- The logger.public RestCall debug() throws RestCallException
Debug: value
header on this request.RestCallException
Copyright © 2018 Apache. All rights reserved.