public class HttpEasy
extends java.lang.Object
HttpURLConnection with full support for HTTP messages such as GET, POST, HEAD, etc
and supports the REST and SOAP protocols and parsing JSON and XML responses.
This is been designed with as a fluent REST API similar to RestEasy and RestAssurred with the only real difference being that it has great proxy support.
There are two starting points for creating a rest request:
1. HttpEasy.withDefaults() - allows you to set some settings that apply to
all requests such as configuring a proxy
1. HttpEasy.request() - performs the actual call, these HTTP methods are implemented: GET, HEAD, POST, PUT, DELETE
Note: if your url can contain weird characters you will want to encode it, something like this: myUrl = URLEncoder.encode(myUrl, "UTF-8");
Example
HttpEasyReader r = HttpEasy.request()
.baseURI(someUrl)
.path(viewPath + "?startkey=\"{startkey}\"&endkey=\"{endkey}\")
.urlParameters(startKey[0], endKey[0])
.get();
String id = r.jsonPath("rows[0].doc._id").getAsString();
String rev = r.jsonPath("rows[0].doc._rev").getAsString();
Error Handling
An IOException is thrown whenever a call returns a response code that is not part of the SUCCESS family (ie 200-299).
In order to prevent an exception being thrown for an expected response use one of the following methods: * request().doNotFailOn(Integer... reponseCodes) * request().doNotFailOn(Family... responseFamily)
Authentication
Supports two formats * http://username:password@where.ever * request().authorization(username, password)
Host and Certificate Verification
There is no fine grained control, its more of an all or nothing approach:
HttpEasy.withDefaults()
.allowAllHosts()
.trustAllCertificates();
Proxy
Only basic authentication is supported, although I believe the domain can be added by included "domain/" in front of the username (not tested)
HttpEasy.withDefaults()
.proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(user, password))))
.proxyAuth(userName, password)
.bypassProxyForLocalAddresses(true);
Redirects
Redirects are NOT automatically followed - at least for REST base calls - even though the documentation for HttpURLConnection says that it should...
HttpEasyReader response = HttpEasy.request()
.doNotFailOn(Family.REDIRECTION)
.path(url)
.head();
if (response.getResponseCodeFamily() == Family.REDIRECTION) {
url = response.getHeaderField("Location");
...
}
Logging
Logging of requests and responses can be enabled by logRequestDetails() or, if using Eclipse, the TCP/IP Monitor utility.
| Constructor and Description |
|---|
HttpEasy() |
| Modifier and Type | Method and Description |
|---|---|
HttpEasy |
authorization(java.lang.String username,
java.lang.String password)
Add an authorization header to request.
|
HttpEasy |
baseURI(java.lang.String uri)
Set the host and port of the URL for the end-point.
|
HttpEasy |
data(java.lang.Object data,
com.google.common.net.MediaType mediaType)
Sets the content type request property to the value of the supplied media type.
|
HttpEasy |
data(java.lang.Object data,
com.google.common.net.MediaType mediaType,
java.lang.String fileName)
Sets the content type request property to the value of the supplied media type.
|
HttpEasy |
dataForm()
Sets the content type request property to "multipart/form-data"
Any data that is required must be added via field(name, value, mediaType) method.
|
HttpEasyReader |
delete()
Performs an HTTP DELETE.
|
HttpEasy |
doNotFailOn(Family... responseFamily)
Add a list of response family codes to ignore that would otherwise case a exception to be thrown.
|
HttpEasy |
doNotFailOn(java.lang.Integer... reponseCodes)
Add a list of response codes to ignore that would otherwise case a exception to be thrown.
|
HttpEasy |
field(java.lang.String name,
java.io.File value,
com.google.common.net.MediaType type)
Add a simple text field, if urlEncodedForm() has been used will try to guess the content type.
|
HttpEasy |
field(java.lang.String name,
java.io.InputStream value,
com.google.common.net.MediaType type,
java.lang.String fileName)
Add a text field or attach file.
|
HttpEasy |
field(java.lang.String name,
java.lang.Object value)
Add a simple text field, if urlEncodedForm() has been used will try to guess the content type.
|
HttpEasy |
field(java.lang.String name,
java.lang.Object value,
com.google.common.net.MediaType type)
Add a simple text field, if urlEncodedForm() has been used will try to guess the content type.
|
HttpEasy |
field(java.lang.String name,
java.lang.Object value,
com.google.common.net.MediaType type,
java.lang.String fileName)
Add a text field or attach file.
|
HttpEasyReader |
get()
Performs an HTTP GET.
|
com.inedo.http.EventManager |
getEventManager() |
HttpEasyReader |
head()
Performs an HTTP HEAD.
|
HttpEasy |
header(java.lang.String name,
java.lang.String value)
Add a header to request.
|
HttpEasy |
logRequestDetails()
If called will cause the request and response details to be logged.
|
HttpEasy |
parameterTokens(java.lang.String startToken,
java.lang.String endToken)
Override the default parameter start and end tokens.
|
HttpEasy |
path(java.lang.String path)
Set the path part of the URL for the end-point.
|
HttpEasyReader |
post()
Performs an HTTP POST.
|
HttpEasyReader |
put()
Performs an HTTP PUT.
|
HttpEasy |
query(java.lang.String query)
Set the query part of the URL for the end-point.
|
HttpEasy |
queryParam(java.lang.String name,
java.lang.Object value)
Appends parameter to query portion of url.
|
static HttpEasy |
request() |
HttpEasy |
setTimeout(int milliseconds)
Sets a specified timeout value, in milliseconds, to be used when opening a communications link to the resource referenced by this URLConnection,
and when reading from Input stream when a connection is established .
|
HttpEasy |
urlEncodedForm()
Sets the content type request property to "application/x-www-form-urlencoded"
Any data that is required must be added via field(name, value) method
and the value must be easily converted to a string value.
|
HttpEasy |
urlParameters(java.lang.Object... pathParams)
Set the parameter values for the parameters in the URL.
|
static HttpEasyDefaults |
withDefaults() |
HttpEasy |
withLogListener(HttpEasyListener listener) |
public static HttpEasyDefaults withDefaults()
public static HttpEasy request()
public HttpEasy header(java.lang.String name, java.lang.String value)
name - Header namevalue - Header valuepublic HttpEasy authorization(java.lang.String username, java.lang.String password)
username - usernamepassword - passwordpublic HttpEasy setTimeout(int milliseconds)
milliseconds - Timeout value in millisecondspublic HttpEasy baseURI(java.lang.String uri)
uri - The host and port of the URLpublic HttpEasy path(java.lang.String path)
path - The host and port of the URLpublic HttpEasy query(java.lang.String query)
query - The host and port of the URLpublic HttpEasy queryParam(java.lang.String name, java.lang.Object value)
name - Parameter namevalue - Parameter valuepublic HttpEasy logRequestDetails()
An alternative to this if using Eclipse is to use the TCP/IP monitor
public HttpEasy parameterTokens(java.lang.String startToken, java.lang.String endToken)
urlParameters(Object...).startToken - Start tokenendToken - End Tokenpublic HttpEasy urlParameters(java.lang.Object... pathParams)
pathParams - A list of parameters to fill in any parameters required by the URL. These are replaced in the order they are found in the URL.public HttpEasy dataForm()
public HttpEasy urlEncodedForm()
public HttpEasy field(java.lang.String name, java.lang.Object value)
name - Field namevalue - Field value - this should not be URL Encoded!public HttpEasy field(java.lang.String name, java.lang.Object value, com.google.common.net.MediaType type)
name - Field namevalue - Field value - this should not be URL Encoded!type - Field's media typepublic HttpEasy field(java.lang.String name, java.io.File value, com.google.common.net.MediaType type)
name - Field namevalue - Filetype - File's media typepublic HttpEasy field(java.lang.String name, java.io.InputStream value, com.google.common.net.MediaType type, java.lang.String fileName)
name - Field's namevalue - Field's value - File, Inputstream, or object easily converted to a stringtype - Field's media typefileName - Field's file name - only required if field is an InputStream as this is assumed to be an attachmentpublic HttpEasy field(java.lang.String name, java.lang.Object value, com.google.common.net.MediaType type, java.lang.String fileName)
If urlEncodedForm() or dataForm() have not been called then the form type will be auto selected: if field containing a file or input stream has been added then content type will be set to "multipart/form-data" else it will be set to "application/x-www-form-urlencoded"
name - Field's namevalue - Field's value, if File or Inputstream will upload content as "multipart/form-data"type - Field's media typefileName - name ofpublic HttpEasy data(java.lang.Object data, com.google.common.net.MediaType mediaType)
This can only be called once as passing raw data can only support one text block or binary file. With this in mind files are supported but any other objects must be easily converted to a string value.
data - File, or object easily converted to a stringmediaType - Media type of the datapublic HttpEasy data(java.lang.Object data, com.google.common.net.MediaType mediaType, java.lang.String fileName)
This can only be called once as passing raw data can only support one text block or binary file. With this in mind files are supported but any other objects must be easily converted to a string value.
data - File, InputStream, or object easily converted to a stringmediaType - Media type of the datafileName - name of filepublic HttpEasy doNotFailOn(java.lang.Integer... reponseCodes)
reponseCodes - A list of failing response codespublic HttpEasy doNotFailOn(Family... responseFamily)
responseFamily - A list of failing response family codespublic HttpEasy withLogListener(HttpEasyListener listener)
public HttpEasyReader get() throws java.io.IOException
HttpEasyReaderjava.io.IOException - If any connection or request errorspublic HttpEasyReader head() throws java.io.IOException
HttpEasyReaderjava.io.IOException - If any connection or request errorspublic HttpEasyReader post() throws java.io.IOException
HttpEasyReaderjava.io.IOException - If any connection or request errorspublic HttpEasyReader put() throws java.io.IOException
HttpEasyReaderjava.io.IOException - If any connection or request errorspublic HttpEasyReader delete() throws java.io.IOException
HttpEasyReaderjava.io.IOException - If any connection or request errorspublic com.inedo.http.EventManager getEventManager()