|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.codegist.crest.HttpRequest.Builder
public static class HttpRequest.Builder
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 |
---|
public HttpRequest.Builder(String uriString) throws URISyntaxException
uriString
- Base url to use
URISyntaxException
- Invalid urlpointsTo(String)
public HttpRequest.Builder(String uriString, String encoding) throws URISyntaxException
uriString
- Base url to useencoding
- Url encoding
URISyntaxException
- Invalid urlpointsTo(String,String)
Method Detail |
---|
public HttpRequest build() throws URISyntaxException
URISyntaxException
public HttpRequest.Builder pointsTo(String uriString) throws URISyntaxException
Can contains a predefined query string
uriString
- Url the request will point to
URISyntaxException
- If the uriString is not a valid URLpointsTo(String,String)
public HttpRequest.Builder pointsTo(String uriString, String encoding) throws URISyntaxException
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)
uriString
- Url the request will point toencoding
- Request encoding
URISyntaxException
- If the uriString is not a valid URLreplacePlaceholderInUri(int, String)
public String getUrlString(boolean includeQueryString) throws UnsupportedEncodingException
includeQueryString
- Flag to indicate to include or not the query string
UnsupportedEncodingException
- not supported parameter encodingpublic URL getUrl(boolean includeQueryString) throws MalformedURLException, UnsupportedEncodingException
includeQueryString
- Flag to indicate to include or not the query string
MalformedURLException
- invalid url
UnsupportedEncodingException
- not supported parameter encodingpublic List<String> getQueryParamNameByPlaceholderIndex(int index)
index
- the index of the place holder to retrieve the parameter names from
pointsTo(String, String)
,
replacePlaceholderInUri(int, String)
public HttpRequest.Builder replacePlaceholderInUri(int index, String 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.
index
- The placeholder indexvalue
- The value to merge
public HttpRequest.Builder timeoutAfter(Long timeout)
timeout
- connection and socket timeout used for the resulting request.
public HttpRequest.Builder timeoutSocketAfter(Long timeout)
timeout
- socket timeout used for the resulting request.
public HttpRequest.Builder timeoutConnectionAfter(Long timeout)
timeout
- connection timeout used for the resulting request.
public HttpRequest.Builder using(HttpMethod meth)
meth
- Http method to use to the resulting request.
public HttpRequest.Builder addHeader(String key, Object value)
key
- Header namevalue
- Header value
public HttpRequest.Builder setHeaders(Map<String,String> headers)
headers
- request headers map
public HttpRequest.Builder addHeaders(Map<String,String> headers)
headers
- request headers map
public HttpRequest.Builder addQueryParam(String key, String value)
key
- query string parameter namevalue
- query string parameter value
public HttpRequest.Builder setQueryString(Map<String,String> params)
params
- query string parameters map
public HttpRequest.Builder addQueryParams(Map<String,String> params)
params
- query string parameters map
public HttpRequest.Builder addBodyParam(String key, Object value)
key
- query string parameter namevalue
- query string parameter value
public HttpRequest.Builder setBodyParams(Map<String,Object> params)
params
- query string parameters map
public HttpRequest.Builder addBodyParams(Map<String,Object> params)
params
- query string parameters map
public Map<String,Object> getBodyParams()
public Map<String,String> getQueryString()
public HttpMethod getMeth()
public String getBaseUri()
public Map<String,String> getHeaders()
public Long getSocketTimeout()
public Long getConnectionTimeout()
public String getEncoding()
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |