org.codegist.crest
Class HttpRequest.Builder

java.lang.Object
  extended by org.codegist.crest.HttpRequest.Builder
Enclosing class:
HttpRequest

public static class HttpRequest.Builder
extends Object

Handy builder for HttpRequest objects.

The default call :

 HttpRequest request = new HttpRequest.Builder("http://127.0.0.1").build();
 

Will create an GET utf-8 HttpRequest object.


Constructor Summary
HttpRequest.Builder(String uriString)
          Creates a GET request pointing to the given url
HttpRequest.Builder(String uriString, String encoding)
          Creates a GET request pointing to the given url
 
Method Summary
 HttpRequest.Builder addBodyParam(String key, Object value)
          Adds a body parameter to the resulting request's body parameters
 HttpRequest.Builder addBodyParams(Map<String,Object> params)
          Adds the given map to the resulting request's body parameter
 HttpRequest.Builder addHeader(String key, Object value)
          Adds a request header to the resulting request's headers
 HttpRequest.Builder addHeaders(Map<String,String> headers)
          Adds the the given map to the resulting request's headers
 HttpRequest.Builder addQueryParam(String key, String value)
          Adds a parameter to the resulting request's query string
 HttpRequest.Builder addQueryParams(Map<String,String> params)
          Adds the given map to the resulting request's query string parameter
 HttpRequest build()
           
 String getBaseUri()
           
 Map<String,Object> getBodyParams()
           
 Long getConnectionTimeout()
           
 String getEncoding()
           
 Map<String,String> getHeaders()
           
 HttpMethod getMeth()
           
 List<String> getQueryParamNameByPlaceholderIndex(int index)
          Returns the query string parameter name list by placeholder index.
 Map<String,String> getQueryString()
           
 Long getSocketTimeout()
           
 URL getUrl(boolean includeQueryString)
          Return the current url include or not the query string
 String getUrlString(boolean includeQueryString)
          Return the current url include or not the query string
 HttpRequest.Builder pointsTo(String uriString)
          Sets the url the request will point to using the default encoding (utf-8)
 HttpRequest.Builder pointsTo(String uriString, String encoding)
          Sets the url the request will point to.
 HttpRequest.Builder replacePlaceholderInUri(int index, String value)
          Replace all url placeholder at the given index with the given value.
 HttpRequest.Builder setBodyParams(Map<String,Object> params)
          Sets the resulting request's body to the given map
 HttpRequest.Builder setHeaders(Map<String,String> headers)
          Sets the resulting request's headers to the given map
 HttpRequest.Builder setQueryString(Map<String,String> params)
          Sets the resulting request's query string parameters to the given map
 HttpRequest.Builder timeoutAfter(Long timeout)
           
 HttpRequest.Builder timeoutConnectionAfter(Long timeout)
           
 HttpRequest.Builder timeoutSocketAfter(Long timeout)
           
 HttpRequest.Builder using(HttpMethod meth)
           
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

HttpRequest.Builder

public HttpRequest.Builder(String uriString)
                    throws URISyntaxException
Creates a GET request pointing to the given url

Parameters:
uriString - Base url to use
Throws:
URISyntaxException - Invalid url
See Also:
pointsTo(String)

HttpRequest.Builder

public HttpRequest.Builder(String uriString,
                           String encoding)
                    throws URISyntaxException
Creates a GET request pointing to the given url

Parameters:
uriString - Base url to use
encoding - Url encoding
Throws:
URISyntaxException - Invalid url
See Also:
pointsTo(String,String)
Method Detail

build

public HttpRequest build()
                  throws URISyntaxException
Throws:
URISyntaxException

pointsTo

public HttpRequest.Builder pointsTo(String uriString)
                             throws URISyntaxException
Sets the url the request will point to using the default encoding (utf-8)

Can contains a predefined query string

Parameters:
uriString - Url the request will point to
Returns:
current builder
Throws:
URISyntaxException - If the uriString is not a valid URL
See Also:
pointsTo(String,String)

pointsTo

public HttpRequest.Builder pointsTo(String uriString,
                                    String encoding)
                             throws URISyntaxException
Sets the url the request will point to.

Can contains a predefined query string

This value can contain placeholders that points to method arguments. eg http://localhost:8080/my-path/{2}/{0}/{2}.json?my-param={1}.

Any placeholder can then be replaced by a value using replacePlaceholderInUri(int, String)

Parameters:
uriString - Url the request will point to
encoding - Request encoding
Returns:
current builder
Throws:
URISyntaxException - If the uriString is not a valid URL
See Also:
replacePlaceholderInUri(int, String)

getUrlString

public String getUrlString(boolean includeQueryString)
                    throws UnsupportedEncodingException
Return the current url include or not the query string

Parameters:
includeQueryString - Flag to indicate to include or not the query string
Returns:
the url as a string
Throws:
UnsupportedEncodingException - not supported parameter encoding

getUrl

public URL getUrl(boolean includeQueryString)
           throws MalformedURLException,
                  UnsupportedEncodingException
Return the current url include or not the query string

Parameters:
includeQueryString - Flag to indicate to include or not the query string
Returns:
the url
Throws:
MalformedURLException - invalid url
UnsupportedEncodingException - not supported parameter encoding

getQueryParamNameByPlaceholderIndex

public List<String> getQueryParamNameByPlaceholderIndex(int index)
Returns the query string parameter name list by placeholder index.

Parameters:
index - the index of the place holder to retrieve the parameter names from
Returns:
parameter name list of the given placeholder index
See Also:
pointsTo(String, String), replacePlaceholderInUri(int, String)

replacePlaceholderInUri

public HttpRequest.Builder replacePlaceholderInUri(int index,
                                                   String value)
Replace all url placeholder at the given index with the given value.

NB: any placeholder that belong to the query string will be URL-encoded. Other placeholder will be set as given.

Given an url as http://localhost:8080/my-path/{2}/{0}/{2}.json?my-param={1}.

Calling this method with index=1,value="hello world" will gives :

Given an url as http://localhost:8080/my-path/{2}/{0}/{2}.json?my-param=hello%20world.

Calling this method with index=0,value="hello world" will gives :

Given an url as http://localhost:8080/my-path/{2}/hello world/{2}.json?my-param={1}.

Calling this method with index=2,value="hi/there" will gives :

Given an url as http://localhost:8080/my-path/hi/there/{0}/hi/there.json?my-param={1}.

NB : You can put placeholder inside a predefined query string parameter as well, eg :

http://localhost:8080/my-path.json?my-preformatted-param={1}%20{0}%20with%20formatting%20{3}.

When doing so, the preformatted parameter should already by pre-encoded, and the merged values won't be encoded.

Parameters:
index - The placeholder index
value - The value to merge
Returns:
current builder

timeoutAfter

public HttpRequest.Builder timeoutAfter(Long timeout)
Parameters:
timeout - connection and socket timeout used for the resulting request.
Returns:
current builder

timeoutSocketAfter

public HttpRequest.Builder timeoutSocketAfter(Long timeout)
Parameters:
timeout - socket timeout used for the resulting request.
Returns:
current builder

timeoutConnectionAfter

public HttpRequest.Builder timeoutConnectionAfter(Long timeout)
Parameters:
timeout - connection timeout used for the resulting request.
Returns:
current builder

using

public HttpRequest.Builder using(HttpMethod meth)
Parameters:
meth - Http method to use to the resulting request.
Returns:
current builder

addHeader

public HttpRequest.Builder addHeader(String key,
                                     Object value)
Adds a request header to the resulting request's headers

Parameters:
key - Header name
value - Header value
Returns:
current builder

setHeaders

public HttpRequest.Builder setHeaders(Map<String,String> headers)
Sets the resulting request's headers to the given map

Parameters:
headers - request headers map
Returns:
current builder

addHeaders

public HttpRequest.Builder addHeaders(Map<String,String> headers)
Adds the the given map to the resulting request's headers

Parameters:
headers - request headers map
Returns:
current builder

addQueryParam

public HttpRequest.Builder addQueryParam(String key,
                                         String value)
Adds a parameter to the resulting request's query string

Parameters:
key - query string parameter name
value - query string parameter value
Returns:
current builder

setQueryString

public HttpRequest.Builder setQueryString(Map<String,String> params)
Sets the resulting request's query string parameters to the given map

Parameters:
params - query string parameters map
Returns:
current builder

addQueryParams

public HttpRequest.Builder addQueryParams(Map<String,String> params)
Adds the given map to the resulting request's query string parameter

Parameters:
params - query string parameters map
Returns:
current builder

addBodyParam

public HttpRequest.Builder addBodyParam(String key,
                                        Object value)
Adds a body parameter to the resulting request's body parameters

Parameters:
key - query string parameter name
value - query string parameter value
Returns:
current builder

setBodyParams

public HttpRequest.Builder setBodyParams(Map<String,Object> params)
Sets the resulting request's body to the given map

Parameters:
params - query string parameters map
Returns:
current builder

addBodyParams

public HttpRequest.Builder addBodyParams(Map<String,Object> params)
Adds the given map to the resulting request's body parameter

Parameters:
params - query string parameters map
Returns:
current builder

getBodyParams

public Map<String,Object> getBodyParams()

getQueryString

public Map<String,String> getQueryString()

getMeth

public HttpMethod getMeth()

getBaseUri

public String getBaseUri()

getHeaders

public Map<String,String> getHeaders()

getSocketTimeout

public Long getSocketTimeout()

getConnectionTimeout

public Long getConnectionTimeout()

getEncoding

public String getEncoding()


Copyright © 2011. All Rights Reserved.