protected class HTTPBuilder.RequestConfigDelegate extends Object
Encloses all properties and method calls used within the
HTTPBuilder.request(Object, Method, Object, Closure)
'config'
closure argument. That is, an instance of this class is set as the
closure's delegate. This allows the user to configure various parameters
within the scope of a single request.
All properties of this class are available from within the closure.
For example, you can manipulate various aspects of the
default request URI
for this request
by calling uri.path = '/api/location'
. This allows for the
ability to modify parameters per-request while leaving any values set
directly on the HTTPBuilder instance unchanged for subsequent requests.
Constructor and Description |
---|
HTTPBuilder.RequestConfigDelegate(org.apache.http.client.methods.HttpRequestBase request,
Object contentType,
Map<?,?> defaultRequestHeaders,
Map<?,groovy.lang.Closure> defaultResponseHandlers) |
HTTPBuilder.RequestConfigDelegate(Map<String,?> args,
org.apache.http.client.methods.HttpRequestBase request,
groovy.lang.Closure successHandler) |
Modifier and Type | Method and Description |
---|---|
void |
encodeBody() |
protected groovy.lang.Closure |
findResponseHandler(int statusCode)
Get the proper response handler for the response code.
|
protected Object |
getContentType()
Get the content-type of any data sent in the request body and the
expected response content-type.
|
HttpContextDecorator |
getContext()
Get the
HttpContext that will be used for this request. |
Map<?,?> |
getHeaders()
Get request headers (including any default headers set on this
HTTPBuilder instance ). |
protected org.apache.http.client.methods.HttpRequestBase |
getRequest()
Directly access the Apache HttpClient instance that will
be used to execute this request.
|
protected Object |
getRequestContentType()
The request content-type, if different from the
contentType . |
Map<Object,groovy.lang.Closure> |
getResponse()
Access the response handler map to set response parsing logic.
|
URIBuilder |
getUri()
Use this object to manipulate parts of the request URI, like
query params and request path.
|
void |
send(Object contentType,
Object requestBody)
Convenience method to set a request content-type at the same time
the request body is set.
|
void |
setBody(Object body)
Set the request body.
|
protected void |
setContentType(Object ct)
Set the content-type used for any data in the request body, as well
as the
Accept content-type that will be used for parsing
the response. |
void |
setContext(org.apache.http.protocol.HttpContext ctx)
Set the
HttpContext that will be used for this request. |
void |
setHeaders(Map<?,?> newHeaders)
Set request headers.
|
protected void |
setPropertiesFromMap(Map<String,?> args)
Valid arguments:
uriEither a URI, URL, or object whose
toString() method produces a valid URI string. |
protected void |
setRequestContentType(Object ct)
Assign a different content-type for the request than is expected for
the response.
|
void |
setUri(Object uri)
Set the entire URI to be used for this request.
|
public HTTPBuilder.RequestConfigDelegate(org.apache.http.client.methods.HttpRequestBase request, Object contentType, Map<?,?> defaultRequestHeaders, Map<?,groovy.lang.Closure> defaultResponseHandlers)
public HTTPBuilder.RequestConfigDelegate(Map<String,?> args, org.apache.http.client.methods.HttpRequestBase request, groovy.lang.Closure successHandler) throws URISyntaxException
URISyntaxException
public URIBuilder getUri()
builder.request(GET,XML) { uri.path = '../other/request.jsp' uri.query = [p1:1, p2:2] ... }
This method signature returns Object
so that the
complementary setUri(Object)
method can accept various
types.
URIBuilder
to manipulate the request URIpublic void setUri(Object uri) throws URISyntaxException
Set the entire URI to be used for this request. Acceptable parameter types are:
URL
URI
URIBuilder
toString()
method produces a valid URI.
Note that if you want to change just a portion of the request URI,
(e.g. the host, port, path, etc.) you can call getUri()
which will return a URIBuilder
which can manipulate portions
of the request URI.
uri
- the URI to use for this request.URISyntaxException
- if an argument is given that is not a valid URIURIBuilder.convertToURI(Object)
protected org.apache.http.client.methods.HttpRequestBase getRequest()
HttpRequestBase
protected Object getContentType()
#setRequestContentType(String)
should be used for the request.setContentType(Object)
or passed from the HTTPBuilder.defaultContentType
when this
RequestConfigDelegate instance was constructed.protected void setContentType(Object ct)
Accept
content-type that will be used for parsing
the response. The value should be either a ContentType
value
or a String, i.e. "text/plain"
. This will default to
HTTPBuilder.getContentType()
for requests that do not
explicitly pass a contentType
parameter (such as
HTTPBuilder.request(Method, Object, Closure)
).ct
- the value that will be used for the Content-Type
and Accept
request headers.protected Object getRequestContentType()
contentType
.ContentType
value or String like text/plain
protected void setRequestContentType(Object ct)
Assign a different content-type for the request than is expected for
the response. This is useful if i.e. you want to post URL-encoded
form data but expect the response to be XML or HTML. The
getContentType()
will always control the Accept
header, and will be used for the request content unless this
value is also explicitly set.
Note that this method is used internally; calls within a request
configuration closure should call send(Object, Object)
to set the request body and content-type at the same time.
ct
- either a ContentType
value or a valid content-type
String.protected void setPropertiesFromMap(Map<String,?> args) throws URISyntaxException
toString()
method produces a valid URI string.
If this parameter is not supplied, the HTTPBuilder's default
URI is used.queryString
and query
are given,
query
will be merged with (and potentially override)
the parameters given as part of queryString
.args
- named parameters to set properties on this delegate.URISyntaxException
- if the uri argument does not represent a valid URIpublic void setHeaders(Map<?,?> newHeaders)
default request headers.
(The assumption is you'll probably want to add a bunch of headers to
whatever defaults you've already set). If you only want to
use values set here, simply call headers.clear()
first.public Map<?,?> getHeaders()
Get request headers (including any default headers set on this
HTTPBuilder instance
). Note that
this will not include any Accept
, Content-Type
,
or Content-Encoding
headers that are automatically
handled by any encoder or parsers in effect. Note that any values
set here will override any of those automatically assigned
values.
Example: headers.'Accept-Language' = 'en, en-gb;q=0.8'
public void send(Object contentType, Object requestBody)
setBody(Object)
that allows for a different content-type
than what is expected for the response.
Example:
http.request(POST,HTML) { /* request data is interpreted as a JsonBuilder closure by HTTPBuilder's default EncoderRegistry implementation * / send( 'text/javascript' ) { a : ['one','two','three'] } // response content-type is what was specified in the outer request() argument: response.success = { resp, html -> } }The
send
call is equivalent to the following:
requestContentType = 'text/javascript' body = { a : ['one','two','three'] }
contentType
- either a ContentType
or equivalent
content-type string like "text/xml"
requestBody
- public void setBody(Object body)
request encoder
. That is,
the value of body
will be interpreted by the encoder
associated with the current request
content-type
.body
- data or closure interpreted as the request bodysend(Object, Object)
public void encodeBody()
protected groovy.lang.Closure findResponseHandler(int statusCode)
HTTPBuilder
class in order to find the proper handler
based on the response status code.statusCode
- HTTP response status codepublic Map<Object,groovy.lang.Closure> getResponse()
builder.request( GET, XML ) { response.success = { xml -> /* for XML content type, the default parser will return an XmlSlurper * / xml.root.children().each { println it } } }
public HttpContextDecorator getContext()
HttpContext
that will be used for this request. By
default, a new context is created for each request.ClientContext
public void setContext(org.apache.http.protocol.HttpContext ctx)
HttpContext
that will be used for this request.ctx
- Copyright © 2008–2014. All rights reserved.