groovyx.net.http
Class HTTPBuilder.SendDelegate

java.lang.Object
  extended by groovyx.net.http.HTTPBuilder.SendDelegate
Enclosing class:
HTTPBuilder

protected class HTTPBuilder.SendDelegate
extends Object

Encloses all properties and method calls used within the HTTPBuilder.request(Object, Method, Object, Closure) 'config' closure argument.


Field Summary
protected  Object contentType
           
protected  Map<String,String> headers
           
protected  org.apache.http.client.methods.HttpRequestBase request
           
protected  String requestContentType
           
protected  Map<String,Closure> responseHandlers
           
protected  URIBuilder url
           
 
Constructor Summary
HTTPBuilder.SendDelegate(org.apache.http.client.methods.HttpRequestBase request, Object contentType, Map<String,String> defaultRequestHeaders, Map<String,Closure> defaultResponseHandlers)
           
 
Method Summary
protected  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.
 Map<String,String> getHeaders()
          Get request headers (including any default headers).
protected  org.apache.http.client.methods.HttpRequestBase getRequest()
           
protected  String getRequestContentType()
          The request content-type, if different from the contentType.
 Map<String,Closure> getResponse()
          Access the response handler map to set response parsing logic.
 URIBuilder getURL()
          Use this object to manipulate parts of the request URL, 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 setHeaders(Map<?,?> newHeaders)
          Set request headers.
protected  void setPropertiesFromMap(Map<String,?> args)
          Valid arguments: urlEither a URI, URL, or String.
protected  void setRequestContentType(String ct)
          Assign a different content-type for the request than is expected for the response.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

request

protected org.apache.http.client.methods.HttpRequestBase request

contentType

protected Object contentType

requestContentType

protected String requestContentType

responseHandlers

protected Map<String,Closure> responseHandlers

url

protected URIBuilder url

headers

protected Map<String,String> headers
Constructor Detail

HTTPBuilder.SendDelegate

public HTTPBuilder.SendDelegate(org.apache.http.client.methods.HttpRequestBase request,
                                Object contentType,
                                Map<String,String> defaultRequestHeaders,
                                Map<String,Closure> defaultResponseHandlers)
Method Detail

getURL

public URIBuilder getURL()
Use this object to manipulate parts of the request URL, like query params and request path. Example:
 builder.request(GET,XML) {
   url.path = '../other/request.jsp'
   url.params = [p1:1, p2:2]
   ...
 }

Returns:
URIBuilder to manipulate the request URL

getRequest

protected org.apache.http.client.methods.HttpRequestBase getRequest()

getContentType

protected Object getContentType()
Get the content-type of any data sent in the request body and the expected response content-type.

Returns:
whatever value was assigned via setContentType(Object) or passed from the HTTPBuilder.defaultContentType when this SendDelegateinstance was constructed.

setContentType

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. The value should be either a ContentType value or a String, i.e. "text/plain"

Parameters:
ct - content-type to send and recieve content

getRequestContentType

protected String getRequestContentType()
The request content-type, if different from the contentType.

Returns:

setRequestContentType

protected void setRequestContentType(String 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.

Parameters:
ct - either a ContentType value or a valid content-type String.

setPropertiesFromMap

protected void setPropertiesFromMap(Map<String,?> args)
                             throws MalformedURLException,
                                    URISyntaxException
Valid arguments:
url
Either a URI, URL, or String. If not supplied, the HTTPBuilder's default URL is used.
path
Request path that is merged with the URL
params
Map of request 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

Parameters:
args - named parameters to set properties on this delegate.
Throws:
MalformedURLException
URISyntaxException

setHeaders

public void setHeaders(Map<?,?> newHeaders)
Set request headers. These values will be merged with any 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.


getHeaders

public Map<String,String> getHeaders()
Get request headers (including any default headers). 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. header that is a

Returns:

send

public void send(Object contentType,
                 Object requestBody)
Convenience method to set a request content-type at the same time the request body is set. This is a variation of 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 in the 
      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 -> 
   
   }
 }
 

Parameters:
contentType - either a ContentType or content-type string like "text/xml"
requestBody -

setBody

public void setBody(Object body)
Set the request body. This value may be of any type supported by the associated request encoder.

Parameters:
body - data or closure interpretes as the request body
See Also:
send(Object, Object)

findResponseHandler

protected Closure findResponseHandler(int statusCode)
Get the proper response handler for the response code. This is called by the HTTPBuilder 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 Map<String,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:


Copyright © 2008-2009. All Rights Reserved.