Class HTTPBuilder.RequestConfigDelegate
- java.lang.Object
-
- groovyx.net.http.HTTPBuilder.RequestConfigDelegate
-
- Enclosing class:
- HTTPBuilder
protected class HTTPBuilder.RequestConfigDelegate extends java.lang.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 callinguri.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.
-
-
Field Summary
Fields Modifier and Type Field Description private java.lang.Object
body
private java.lang.Object
contentType
private HttpContextDecorator
context
private java.util.Map<java.lang.Object,java.lang.Object>
headers
private org.apache.http.client.methods.HttpRequestBase
request
private java.lang.Object
requestContentType
private java.util.Map<java.lang.Object,groovy.lang.Closure>
responseHandlers
private URIBuilder
uri
-
Constructor Summary
Constructors Constructor Description RequestConfigDelegate(java.util.Map<java.lang.String,?> args, org.apache.http.client.methods.HttpRequestBase request, groovy.lang.Closure successHandler)
RequestConfigDelegate(org.apache.http.client.methods.HttpRequestBase request, java.lang.Object contentType, java.util.Map<?,?> defaultRequestHeaders, java.util.Map<?,groovy.lang.Closure> defaultResponseHandlers)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
encodeBody()
protected groovy.lang.Closure
findResponseHandler(int statusCode)
Get the proper response handler for the response code.protected java.lang.Object
getContentType()
Get the content-type of any data sent in the request body and the expected response content-type.HttpContextDecorator
getContext()
Get theHttpContext
that will be used for this request.java.util.Map<?,?>
getHeaders()
Get request headers (including any default headers set on thisHTTPBuilder instance
).protected org.apache.http.client.methods.HttpRequestBase
getRequest()
Directly access the Apache HttpClient instance that will be used to execute this request.protected java.lang.Object
getRequestContentType()
The request content-type, if different from thecontentType
.java.util.Map<java.lang.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(java.lang.Object contentType, java.lang.Object requestBody)
Convenience method to set a request content-type at the same time the request body is set.void
setBody(java.lang.Object body)
Set the request body.protected void
setContentType(java.lang.Object ct)
Set the content-type used for any data in the request body, as well as theAccept
content-type that will be used for parsing the response.void
setContext(org.apache.http.protocol.HttpContext ctx)
Set theHttpContext
that will be used for this request.void
setHeaders(java.util.Map<?,?> newHeaders)
Set request headers.protected void
setPropertiesFromMap(java.util.Map<java.lang.String,?> args)
Valid arguments: uriEither a URI, URL, or object whosetoString()
method produces a valid URI string.protected void
setRequestContentType(java.lang.Object ct)
Assign a different content-type for the request than is expected for the response.void
setUri(java.lang.Object uri)
Set the entire URI to be used for this request.
-
-
-
Field Detail
-
request
private org.apache.http.client.methods.HttpRequestBase request
-
contentType
private java.lang.Object contentType
-
requestContentType
private java.lang.Object requestContentType
-
responseHandlers
private java.util.Map<java.lang.Object,groovy.lang.Closure> responseHandlers
-
uri
private URIBuilder uri
-
headers
private java.util.Map<java.lang.Object,java.lang.Object> headers
-
context
private HttpContextDecorator context
-
body
private java.lang.Object body
-
-
Constructor Detail
-
RequestConfigDelegate
public RequestConfigDelegate(org.apache.http.client.methods.HttpRequestBase request, java.lang.Object contentType, java.util.Map<?,?> defaultRequestHeaders, java.util.Map<?,groovy.lang.Closure> defaultResponseHandlers)
-
RequestConfigDelegate
public RequestConfigDelegate(java.util.Map<java.lang.String,?> args, org.apache.http.client.methods.HttpRequestBase request, groovy.lang.Closure successHandler) throws java.net.URISyntaxException
- Throws:
java.net.URISyntaxException
-
-
Method Detail
-
getUri
public URIBuilder getUri()
Use this object to manipulate parts of the request URI, like query params and request path. Example:builder.request(GET,XML) { uri.path = '../other/request.jsp' uri.query = [p1:1, p2:2] ... }
This method signature returns
Object
so that the complementarysetUri(Object)
method can accept various types.- Returns:
URIBuilder
to manipulate the request URI
-
setUri
public void setUri(java.lang.Object uri) throws java.net.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 aURIBuilder
which can manipulate portions of the request URI.- Parameters:
uri
- the URI to use for this request.- Throws:
java.net.URISyntaxException
- if an argument is given that is not a valid URI- See Also:
URIBuilder.convertToURI(Object)
-
getRequest
protected org.apache.http.client.methods.HttpRequestBase getRequest()
Directly access the Apache HttpClient instance that will be used to execute this request.- See Also:
HttpRequestBase
-
getContentType
protected java.lang.Object getContentType()
Get the content-type of any data sent in the request body and the expected response content-type. If the request content-type is expected to differ from the response content-type (i.e. a URL-encoded POST that should return an HTML page) then this value will be used for the response content-type, while#setRequestContentType(String)
should be used for the request.- Returns:
- whatever value was assigned via
setContentType(Object)
or passed from theHTTPBuilder.defaultContentType
when this RequestConfigDelegate instance was constructed.
-
setContentType
protected void setContentType(java.lang.Object ct)
Set the content-type used for any data in the request body, as well as theAccept
content-type that will be used for parsing the response. The value should be either aContentType
value or a String, i.e."text/plain"
. This will default toHTTPBuilder.getContentType()
for requests that do not explicitly pass acontentType
parameter (such asHTTPBuilder.request(Method, Object, Closure)
).- Parameters:
ct
- the value that will be used for theContent-Type
andAccept
request headers.
-
getRequestContentType
protected java.lang.Object getRequestContentType()
The request content-type, if different from thecontentType
.- Returns:
- either a
ContentType
value or String liketext/plain
-
setRequestContentType
protected void setRequestContentType(java.lang.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 theAccept
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.- Parameters:
ct
- either aContentType
value or a valid content-type String.
-
setPropertiesFromMap
protected void setPropertiesFromMap(java.util.Map<java.lang.String,?> args) throws java.net.URISyntaxException
Valid arguments:- uri
- Either a URI, URL, or object whose
toString()
method produces a valid URI string. If this parameter is not supplied, the HTTPBuilder's default URI is used. - path
- Request path that is merged with the URI
- queryString
- an escaped query string
- query
- Map of URL query parameters
- headers
- Map of HTTP headers
- contentType
- Request content type and Accept header. If not supplied, the HTTPBuilder's default content-type is used.
- requestContentType
- content type for the request, if it is different from the expected response content-type
- body
- Request body that will be encoded based on the given contentType
queryString
andquery
are given,query
will be merged with (and potentially override) the parameters given as part ofqueryString
.- Parameters:
args
- named parameters to set properties on this delegate.- Throws:
java.net.URISyntaxException
- if the uri argument does not represent a valid URI
-
setHeaders
public void setHeaders(java.util.Map<?,?> newHeaders)
Set request headers. These values will be merged with anydefault 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 callheaders.clear()
first.
-
getHeaders
public java.util.Map<?,?> getHeaders()
Get request headers (including any default headers set on this
HTTPBuilder instance
). Note that this will not include anyAccept
,Content-Type
, orContent-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'
- Returns:
- a map of HTTP headers that will be sent in the request.
-
send
public void send(java.lang.Object contentType, java.lang.Object requestBody)
Convenience method to set a request content-type at the same time the request body is set. This is a variation ofsetBody(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 -> } }
Thesend
call is equivalent to the following:requestContentType = 'text/javascript' body = { a : ['one','two','three'] }
- Parameters:
contentType
- either aContentType
or equivalent content-type string like"text/xml"
requestBody
-
-
setBody
public void setBody(java.lang.Object body)
Set the request body. This value may be of any type supported by the associatedrequest encoder
. That is, the value ofbody
will be interpreted by the encoder associated with the currentrequest content-type
.- Parameters:
body
- data or closure interpreted as the request body- See Also:
send(Object, Object)
-
encodeBody
public void encodeBody()
-
findResponseHandler
protected groovy.lang.Closure findResponseHandler(int statusCode)
Get the proper response handler for the response code. This is called by theHTTPBuilder
class in order to find the proper handler based on the response status code.- Parameters:
statusCode
- HTTP response status code- Returns:
- the response handler
-
getResponse
public java.util.Map<java.lang.Object,groovy.lang.Closure> getResponse()
Access the response handler map to set response parsing logic. i.e.builder.request( GET, XML ) { response.success = { xml -> /* for XML content type, the default parser will return an XmlSlurper * / xml.root.children().each { println it } } }
- Returns:
-
getContext
public HttpContextDecorator getContext()
Get theHttpContext
that will be used for this request. By default, a new context is created for each request.- Returns:
- See Also:
ClientContext
-
setContext
public void setContext(org.apache.http.protocol.HttpContext ctx)
Set theHttpContext
that will be used for this request.- Parameters:
ctx
-
-
-