Package com.avoka.tm.http
Class HttpRequest
java.lang.Object
com.avoka.tm.http.HttpRequest
- Direct Known Subclasses:
DeleteRequest
,GetRequest
,PatchRequest
,PostRequest
,PutRequest
Provides an abstract HttpRequest class enabling simpler use of the Apache HTTP Components library.
The default connection timeout is 10 seconds and socket read timeout is 60 seconds. Socket connections will also apply any JVM proxy settings automatically.
By default the maximum response read size is 8 MB. If the response is larger than this an IOException will be thrown.
To increase the maximum read limit use the setReadLimit(int)
method.
- Since:
- 5.0.0
- See Also:
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
Provides a multi-part FileParam.static class
Provides a multi-part Param. -
Constructor Summary
ModifierConstructorDescriptionprotected
HttpRequest
(com.avoka.tm.http.HttpRequest.Method method, String uri) Create a HTTP request object with the given HTTP method and URL. -
Method Summary
Modifier and TypeMethodDescriptionaddFileParam
(String paramName, byte[] fileData, String fileName) Add a file parameter for a multi-part request.Add request header.addHeaders
(Map<String, String> headers) Add all the request headers.Add a name and value request parameters.Add a name and value request parameters.execute()
Perform a HTTP request and return a response object.org.apache.http.client.protocol.HttpClientContext
Gets the HttpClientContext The context stores details about the call to a server.Get file parameters for a multi-part request.Return the request headers.org.apache.http.HttpEntity
Get the httpEntity parameter.Get the POST or PUT message body.byte[]
Get the POST or PUT message body.com.avoka.tm.http.HttpRequest.Method
Return the request method.Get the request form parameters.getUri()
Return the request URI.setAcceptCompress
(boolean acceptCompress) Specify whether to set the header "Accept-Encoding: gzip, deflate".setBasicAuth
(String username, String password) Set BASIC Authorization credentials.setCharacterEncoding
(String charset) Sets the character encoding (MIME charset) of the response being sent to the client, for example, to UTF-8.setCompressMessage
(boolean compress) Specify whether to GZIP compress the POST or PUT message data.setConnectTimeout
(Integer connectTimeout) Set the connection timeout in milliseconds.setContentType
(String contentType) Set the 'Content-Type' header.setContext
(org.apache.http.client.protocol.HttpClientContext context) Sets the HttpClientContext The context stores details about the call to a server.setHttpEntity
(org.apache.http.HttpEntity httpEntity) Set the httpEntity parametersetMessage
(String message) Set the POST or PUT message body.setMessageData
(byte[] messageData) Set the POST or PUT message body.setNtlmAuth
(String username, String password, String workstation, String domain) Set NTLM Authorization credentials.Set the request form parameters.Set Proxy host and port.setProxyAuth
(String username, String password) Set Proxy Authorization credentials.setReadLimit
(int readLimit) Set the response content read limit in bytes.setSocketFactory
(org.apache.http.conn.socket.LayeredConnectionSocketFactory socketFactory) Set an optional ConnectionSocketFactory to enable creating layered sockets such as TLS.setSocketTimeout
(Integer socketTimeout) Set the socket timeout in milliseconds.setTimeouts
(int connectTimeout, int socketTimeout) Set the connect and socket timeouts in milliseconds.setUserAgent
(String userAgent) Set the 'User-Agent' header.setUseSystemProxy
(boolean useSysProxy) Specify whether to use proxy in system properties.toString()
Return the string representation of this class.
-
Constructor Details
-
HttpRequest
Create a HTTP request object with the given HTTP method and URL.- Parameters:
method
- the request method (required)uri
- the request URL (required)
-
-
Method Details
-
setBasicAuth
Set BASIC Authorization credentials.- Parameters:
username
- the user login name (required)password
- the user login password (required)- Returns:
- the HttpRequest object
-
setNtlmAuth
Set NTLM Authorization credentials.- Parameters:
username
- the user login name (required)password
- the user login password (required)workstation
- the workstation computer name (optional)domain
- the name domain the user login belongs to (optional)- Returns:
- the HttpRequest object
-
setProxyAuth
Set Proxy Authorization credentials.- Parameters:
username
- the proxy user login name (required)password
- the proxy user login password (required)- Returns:
- the HttpRequest object
- Since:
- 5.1.3
-
setProxy
Set Proxy host and port.- Parameters:
proxyHost
- proxy hostproxyPort
- proxy port- Returns:
- the HttpRequest object
- Since:
- 5.1.3
-
getContext
public org.apache.http.client.protocol.HttpClientContext getContext()Gets the HttpClientContext The context stores details about the call to a server. It is useful in NTLM which has a fairly expensive authentication process. Once connected the authentication details are stored in the HttpClientContext and following HttpRequest calls do not need to be made. the process is as follows. Initially do a connection to a cheap service GetRequest. The execute() method creates the context. Create a new httpRequest with the expensive operation such as a PUT and call setContext(context)- Returns:
- the HttpClientContext context
-
setContext
Sets the HttpClientContext The context stores details about the call to a server. It is useful in NTLM which has a fairly expensive authentication process. Once connected the authentication details are stored in the HttpClientContext and following HttpRequest calls do not need to be made. the process is as follows. Initially do a connection to a cheap service GetRequest. After the execute() method runs get the context. Create a new httpRequest with the expensive operation such as a PUT and call setContext(context)- Parameters:
context
- the HttpClientContext from a previous HttpRequest call.- Returns:
- the HttpRequest object
-
setContentType
Set the 'Content-Type' header.- Parameters:
contentType
- the 'Content-Type' header (required)- Returns:
- the HttpRequest object
-
setCharacterEncoding
Sets the character encoding (MIME charset) of the response being sent to the client, for example, to UTF-8. If no content type is set, the content type will default to 'application/json; charset=...'.- Parameters:
charset
- a valid charset (required)- Returns:
- the HttpRequest object
- Since:
- 18.5.0
-
getHeaders
Return the request headers.- Returns:
- the request headers
- Since:
- 5.1.7
-
getMethod
public com.avoka.tm.http.HttpRequest.Method getMethod()Return the request method.- Returns:
- the request method
- Since:
- 5.1.7
-
getUri
Return the request URI.- Returns:
- the request URI
- Since:
- 5.1.7
-
setUserAgent
Set the 'User-Agent' header.- Parameters:
userAgent
- the 'User-Agent' header (required)- Returns:
- the HttpRequest object
-
addHeader
Add request header.- Parameters:
name
- the request header name (required)value
- the request header value (required)- Returns:
- the HttpRequest object
-
addHeaders
Add all the request headers.- Parameters:
headers
- the map of request headers to add (required)- Returns:
- the HttpRequest object
-
setAcceptCompress
Specify whether to set the header "Accept-Encoding: gzip, deflate".- Parameters:
acceptCompress
- specify whether to set the header "Accept-Encoding: gzip, deflate"- Returns:
- the HttpRequest object
-
setCompressMessage
Specify whether to GZIP compress the POST or PUT message data.- Parameters:
compress
- specify whether to GZIP compress the POST or PUT message data.- Returns:
- the HttpRequest object
-
setMessage
Set the POST or PUT message body. If specified the Content-Type will be set to 'plain/text'.- Parameters:
message
- the POST or PUT message body- Returns:
- the HttpRequest object
-
getMessage
Get the POST or PUT message body. If specified the Content-Type will be set to 'plain/text'.- Returns:
- the POST or PUT message body
- Since:
- 5.1.7
-
setMessageData
Set the POST or PUT message body. If specified the Content-Type will be set to 'application/octet-stream'.- Parameters:
messageData
- the POST or PUT message body- Returns:
- the HttpRequest object
-
getMessageData
public byte[] getMessageData()Get the POST or PUT message body. If specified the Content-Type will be set to 'application/octet-stream'.- Returns:
- the POST or PUT message body
- Since:
- 5.1.7
-
setParams
Set the request form parameters.- Parameters:
params
- the request form parameters- Returns:
- the HttpRequest object
-
getParams
Get the request form parameters.- Returns:
- the request form parameters
- Since:
- 5.1.7
-
getHttpEntity
public org.apache.http.HttpEntity getHttpEntity()Get the httpEntity parameter.- Returns:
- the httpEntity parameter
- Since:
- 22.10.0
-
setHttpEntity
Set the httpEntity parameter- Parameters:
httpEntity
- the httpEntity parameter- Returns:
- the HttpRequest object
- Since:
- 22.10.0
-
addParam
Add a name and value request parameters.- Parameters:
name
- the request parameter name (required)value
- the request parameter value (required)- Returns:
- the HttpRequest object
-
addParam
Add a name and value request parameters.- Parameters:
name
- the request parameter name (required)value
- the request parameter value (required)contentType
- the request parameter content type (required)- Returns:
- the HttpRequest object
- Since:
- 5.0.1
-
addFileParam
Add a file parameter for a multi-part request.- Parameters:
paramName
- the request parameter name (required)fileData
- the file data to add (required)fileName
- the file data file name (required)- Returns:
- the HttpRequest object
-
getFileParams
Get file parameters for a multi-part request.- Returns:
- file parameters
- Since:
- 5.1.7
-
setTimeouts
Set the connect and socket timeouts in milliseconds.- Parameters:
connectTimeout
- the connection timeout in millisecondssocketTimeout
- the socket timeout in milliseconds- Returns:
- the HttpRequest object
-
setSocketTimeout
Set the socket timeout in milliseconds.- Parameters:
socketTimeout
- the socket timeout in milliseconds- Returns:
- the HttpRequest object
-
setConnectTimeout
Set the connection timeout in milliseconds.- Parameters:
connectTimeout
- the connection timeout in milliseconds- Returns:
- the HttpRequest object
-
setReadLimit
Set the response content read limit in bytes.- Parameters:
readLimit
- the response content read limit in bytes- Returns:
- the HttpRequest object
-
setSocketFactory
public HttpRequest setSocketFactory(org.apache.http.conn.socket.LayeredConnectionSocketFactory socketFactory) Set an optional ConnectionSocketFactory to enable creating layered sockets such as TLS. This method can be used to specify aSSLConnectionSocketFactory
to support mutual SSL authentication.Example
import java.io.File import javax.net.ssl.SSLContext import org.apache.http.ssl.SSLContexts import org.apache.http.conn.ssl.TrustSelfSignedStrategy import org.apache.http.conn.ssl.SSLConnectionSocketFactory // Trust own CA and all self-signed certs SSLContext sslContext = SSLContexts.custom() .loadTrustMaterial(new File("my.keystore"), "nopassword".toCharArray(), new TrustSelfSignedStrategy()) .build() // Allow TLSv1 protocol only SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory( sslContext, new String[]{ "TLSv1" }, null, SSLConnectionSocketFactory.getDefaultHostnameVerifier()) // execute GET request and return a HttpResponse object def response = new GetRequest('https://service.mycorp.com/secure/rest/accounts/') .setBasicAuth(username, password) .setSocketFactory(socketFactory) .execute()
- Parameters:
socketFactory
- the optional ConnectionSocketFactory used to build the client- Returns:
- the HttpRequest object
-
setUseSystemProxy
Specify whether to use proxy in system properties.- Parameters:
useSysProxy
- specify whether to use proxy in system properties- Returns:
- the HttpRequest object
- Since:
- 18.11.0
-
execute
Perform a HTTP request and return a response object.- Returns:
- perform a HTTP request and return a response object
- Throws:
IOException
- if an IO error occurs, please note HTTP status codes are returned in the response object
-
toString
Return the string representation of this class.
-