Package groovyx.net.http
Class HttpURLClient
- java.lang.Object
-
- groovyx.net.http.HttpURLClient
-
public class HttpURLClient extends java.lang.ObjectThis class provides a simplified API similar to
HTTPBuilder, but usesHttpURLConnectionfor I/O so that it is compatible with Google App Engine. Features:- Parser and Encoder support
- Easy request and response header manipulation
- Basic authentication
- Since:
- 0.5.0
- See Also:
- GAE URLFetch
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private classHttpURLClient.HttpURLResponseAdapterThis class makes a HttpURLConnection look like an HttpResponse for use byParserRegistryandHttpResponseDecorator.private static classHttpURLClient.OAuthWrapperThis class basically wraps Signpost classes so they are not loaded untilsetOAuth(Object, Object, Object, Object)is called.
-
Field Summary
Fields Modifier and Type Field Description private java.lang.ObjectcontentTypeprivate java.util.Map<java.lang.String,java.lang.String>defaultHeadersprivate URIBuilderdefaultURLprivate EncoderRegistryencoderRegistryprivate booleanfollowRedirectsprotected org.apache.commons.logging.LoglogLogger instance defined for use by sub-classesprotected HttpURLClient.OAuthWrapperoauthprivate ParserRegistryparserRegistryprivate java.lang.ObjectrequestContentType
-
Constructor Summary
Constructors Constructor Description HttpURLClient()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private java.lang.StringgetBasicAuthHeader(java.lang.String user, java.lang.String pass)java.lang.ObjectgetContentType()Get the default content-type used for parsing response data.EncoderRegistrygetEncoders()Get the encoder registry used by this instance, which can be used to directly modify the request serialization behavior.java.util.Map<java.lang.String,java.lang.String>getHeaders()Retrieve the default headers that will be sent in each request.private java.lang.ObjectgetparsedResult(java.lang.String method, java.lang.Object contentType, org.apache.http.HttpResponse response)ParserRegistrygetParsers()Retrieve the parser registry used by this instance, which can be used to directly modify the parsing behavior.java.lang.ObjectgetRequestContentType()Get the default content-type used to serialize the request data.java.lang.ObjectgetUrl()The default URL for this request.booleanisFollowRedirects()HttpResponseDecoratorrequest(java.util.Map<java.lang.String,?> args)Perform a request.voidsetBasicAuth(java.lang.Object user, java.lang.Object pass)Set basic user and password authorization to be used for every request.voidsetContentType(java.lang.Object ct)Set the default content-type used to control response parsing and request serialization behavior.voidsetEncoders(EncoderRegistry encoderRegistry)voidsetFollowRedirects(boolean follow)Control whether this instance should automatically follow redirect responses.voidsetHeaders(java.util.Map<?,?> headers)Set default headers to be sent with every request.voidsetOAuth(java.lang.Object consumerKey, java.lang.Object consumerSecret, java.lang.Object accessToken, java.lang.Object accessSecret)Sign all outbound requests with the given OAuth keys and tokens.voidsetParsers(ParserRegistry parserRegistry)voidsetRequestContentType(java.lang.Object requestContentType)Set the default content-type used to control request body serialization.voidsetUrl(java.lang.Object url)Set the default request URL.
-
-
-
Field Detail
-
defaultHeaders
private java.util.Map<java.lang.String,java.lang.String> defaultHeaders
-
encoderRegistry
private EncoderRegistry encoderRegistry
-
parserRegistry
private ParserRegistry parserRegistry
-
contentType
private java.lang.Object contentType
-
requestContentType
private java.lang.Object requestContentType
-
defaultURL
private URIBuilder defaultURL
-
followRedirects
private boolean followRedirects
-
oauth
protected HttpURLClient.OAuthWrapper oauth
-
log
protected org.apache.commons.logging.Log log
Logger instance defined for use by sub-classes
-
-
Method Detail
-
request
public HttpResponseDecorator request(java.util.Map<java.lang.String,?> args) throws java.net.URISyntaxException, java.net.MalformedURLException, java.io.IOException
Perform a request. Parameters are:- url
- the entire request URL
- path
- the path portion of the request URL, if a default URL is set on this instance.
- query
- URL query parameters for this request.
- timeout
- see
URLConnection.setReadTimeout(int) - method
- This defaults to GET, or POST if a
bodyparameter is also specified. - contentType
- Explicitly specify how to parse the response.
If this value is ContentType.ANY, the response
Content-Typeheader is used to determine how to parse the response. - requestContentType
- used in a PUT or POST request to
transform the request body and set the proper
Content-Typeheader. This defaults to thecontentTypeif unset. - auth
- Basic authorization; pass the value as a list in the form [user, pass]
- headers
- additional request headers, as a map
- body
- request content body, for a PUT or POST request. This will be encoded using the requestContentType
- Parameters:
args- named parameters- Returns:
- the parsed response
- Throws:
java.net.URISyntaxExceptionjava.net.MalformedURLExceptionjava.io.IOException
-
getparsedResult
private java.lang.Object getparsedResult(java.lang.String method, java.lang.Object contentType, org.apache.http.HttpResponse response) throws ResponseParseException- Throws:
ResponseParseException
-
getBasicAuthHeader
private java.lang.String getBasicAuthHeader(java.lang.String user, java.lang.String pass) throws java.io.UnsupportedEncodingException- Throws:
java.io.UnsupportedEncodingException
-
setBasicAuth
public void setBasicAuth(java.lang.Object user, java.lang.Object pass) throws java.io.UnsupportedEncodingExceptionSet basic user and password authorization to be used for every request. Passnullto un-set authorization for this instance.- Parameters:
user-pass-- Throws:
java.io.UnsupportedEncodingException
-
setOAuth
public void setOAuth(java.lang.Object consumerKey, java.lang.Object consumerSecret, java.lang.Object accessToken, java.lang.Object accessSecret)Sign all outbound requests with the given OAuth keys and tokens. It is assumed you have already generated a consumer keypair and retrieved a proper access token pair from your target service (see Signpost documentation for more details.) Once this has been done all requests will be signed.- Parameters:
consumerKey- null if you want to _stop_ signing requests.consumerSecret-accessToken-accessSecret-
-
setFollowRedirects
public void setFollowRedirects(boolean follow)
Control whether this instance should automatically follow redirect responses. SeeHttpURLConnection.setInstanceFollowRedirects(boolean)- Parameters:
follow- true if the connection should automatically follow redirect responses from the server.
-
isFollowRedirects
public boolean isFollowRedirects()
- Returns:
-
getUrl
public java.lang.Object getUrl()
The default URL for this request. This is aURIBuilderwhich can be used to easily manipulate portions of the request URL.- Returns:
-
setUrl
public void setUrl(java.lang.Object url) throws java.net.URISyntaxExceptionSet the default request URL.- Parameters:
url- any object whosetoString()produces a valid URI.- Throws:
java.net.URISyntaxException- See Also:
URIBuilder.convertToURI(Object)
-
getHeaders
public java.util.Map<java.lang.String,java.lang.String> getHeaders()
Retrieve the default headers that will be sent in each request. Note that this is a 'live' map that can be directly manipulated to add or remove the default request headers.- Returns:
-
setHeaders
public void setHeaders(java.util.Map<?,?> headers)
Set default headers to be sent with every request.- Parameters:
headers-
-
getEncoders
public EncoderRegistry getEncoders()
Get the encoder registry used by this instance, which can be used to directly modify the request serialization behavior. i.e.client.encoders.'application/xml' = {....}.- Returns:
-
setEncoders
public void setEncoders(EncoderRegistry encoderRegistry)
-
getParsers
public ParserRegistry getParsers()
Retrieve the parser registry used by this instance, which can be used to directly modify the parsing behavior.- Returns:
-
setParsers
public void setParsers(ParserRegistry parserRegistry)
-
getContentType
public java.lang.Object getContentType()
Get the default content-type used for parsing response data.- Returns:
- a String or
ContentTypeobject. Defaults toContentType.ANY
-
setContentType
public void setContentType(java.lang.Object ct)
Set the default content-type used to control response parsing and request serialization behavior. Ifnullis passed,ContentType.ANYwill be used. If this value isContentType.ANY, the responseContent-Typeheader is used to parse the response.- Parameters:
ct- a String orContentTypevalue.
-
getRequestContentType
public java.lang.Object getRequestContentType()
Get the default content-type used to serialize the request data.- Returns:
-
setRequestContentType
public void setRequestContentType(java.lang.Object requestContentType)
Set the default content-type used to control request body serialization. If null, thecontentType propertyis used. Additionally, if thecontentTypeisContentType.ANY, arequestContentTypemust be specified when performing a POST or PUT request that sends request data.- Parameters:
requestContentType- String orContentTypevalue.
-
-