Package com.avoka.tm.http
Class HttpRequest
- java.lang.Object
-
- com.avoka.tm.http.HttpRequest
-
- Direct Known Subclasses:
DeleteRequest
,GetRequest
,PatchRequest
,PostRequest
,PutRequest
public abstract class HttpRequest extends Object
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:
DeleteRequest
,GetRequest
,PatchRequest
,PostRequest
,PutRequest
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
HttpRequest.FileParam
Provides a multi-part FileParam.static class
HttpRequest.Param
Provides a multi-part Param.
-
Constructor Summary
Constructors Modifier Constructor Description protected
HttpRequest(com.avoka.tm.http.HttpRequest.Method method, String uri)
Create a HTTP request object with the given HTTP method and URL.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description HttpRequest
addFileParam(String paramName, byte[] fileData, String fileName)
Add a file parameter for a multi-part request.HttpRequest
addHeader(String name, String value)
Add request header.HttpRequest
addHeaders(Map<String,String> headers)
Add all the request headers.HttpRequest
addParam(String name, String value)
Add a name and value request parameters.HttpRequest
addParam(String name, String value, String contentType)
Add a name and value request parameters.HttpResponse
execute()
Perform a HTTP request and return a response object.org.apache.http.client.protocol.HttpClientContext
getContext()
Gets the HttpClientContext The context stores details about the call to a server.Map<String,HttpRequest.FileParam>
getFileParams()
Get file parameters for a multi-part request.Map<String,String>
getHeaders()
Return the request headers.String
getMessage()
Get the POST or PUT message body.byte[]
getMessageData()
Get the POST or PUT message body.com.avoka.tm.http.HttpRequest.Method
getMethod()
Return the request method.Map<String,HttpRequest.Param>
getParams()
Get the request form parameters.String
getUri()
Return the request URI.HttpRequest
setAcceptCompress(boolean acceptCompress)
Specify whether to set the header "Accept-Encoding: gzip, deflate".HttpRequest
setBasicAuth(String username, String password)
Set BASIC Authorization credentials.HttpRequest
setCharacterEncoding(String charset)
Sets the character encoding (MIME charset) of the response being sent to the client, for example, to UTF-8.HttpRequest
setCompressMessage(boolean compress)
Specify whether to GZIP compress the POST or PUT message data.HttpRequest
setConnectTimeout(Integer connectTimeout)
Set the connection timeout in milliseconds.HttpRequest
setContentType(String contentType)
Set the 'Content-Type' header.HttpRequest
setContext(org.apache.http.client.protocol.HttpClientContext context)
Sets the HttpClientContext The context stores details about the call to a server.HttpRequest
setMessage(String message)
Set the POST or PUT message body.HttpRequest
setMessageData(byte[] messageData)
Set the POST or PUT message body.HttpRequest
setNtlmAuth(String username, String password, String workstation, String domain)
Set NTLM Authorization credentials.HttpRequest
setParams(Map<String,String> params)
Set the request form parameters.HttpRequest
setProxy(String proxyHost, int proxyPort)
Set Proxy host and port.HttpRequest
setProxyAuth(String username, String password)
Set Proxy Authorization credentials.HttpRequest
setReadLimit(int readLimit)
Set the response content read limit in bytes.HttpRequest
setSocketFactory(org.apache.http.conn.socket.LayeredConnectionSocketFactory socketFactory)
Set an optional ConnectionSocketFactory to enable creating layered sockets such as TLS.HttpRequest
setSocketTimeout(Integer socketTimeout)
Set the socket timeout in milliseconds.HttpRequest
setTimeouts(int connectTimeout, int socketTimeout)
Set the connect and socket timeouts in milliseconds.HttpRequest
setUserAgent(String userAgent)
Set the 'User-Agent' header.HttpRequest
setUseSystemProxy(boolean useSysProxy)
Specify whether to use proxy in system properties.String
toString()
Return the string representation of this class.
-
-
-
Constructor Detail
-
HttpRequest
protected HttpRequest(com.avoka.tm.http.HttpRequest.Method method, String uri)
Create a HTTP request object with the given HTTP method and URL.- Parameters:
method
- the request method (required)uri
- the request URL (required)
-
-
Method Detail
-
setBasicAuth
public HttpRequest setBasicAuth(String username, String password)
Set BASIC Authorization credentials.- Parameters:
username
- the user login name (required)password
- the user login password (required)- Returns:
- the HttpRequest object
-
setNtlmAuth
public HttpRequest setNtlmAuth(String username, String password, String workstation, String domain)
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
public HttpRequest setProxyAuth(String username, String password)
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
public HttpRequest setProxy(String proxyHost, int proxyPort)
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
public HttpRequest setContext(org.apache.http.client.protocol.HttpClientContext context)
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
public HttpRequest setContentType(String contentType)
Set the 'Content-Type' header.- Parameters:
contentType
- the 'Content-Type' header (required)- Returns:
- the HttpRequest object
-
setCharacterEncoding
public HttpRequest setCharacterEncoding(String charset)
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
public Map<String,String> 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
public String getUri()
Return the request URI.- Returns:
- the request URI
- Since:
- 5.1.7
-
setUserAgent
public HttpRequest setUserAgent(String userAgent)
Set the 'User-Agent' header.- Parameters:
userAgent
- the 'User-Agent' header (required)- Returns:
- the HttpRequest object
-
addHeader
public HttpRequest addHeader(String name, String value)
Add request header.- Parameters:
name
- the request header name (required)value
- the request header value (required)- Returns:
- the HttpRequest object
-
addHeaders
public HttpRequest addHeaders(Map<String,String> headers)
Add all the request headers.- Parameters:
headers
- the map of request headers to add (required)- Returns:
- the HttpRequest object
-
setAcceptCompress
public HttpRequest setAcceptCompress(boolean acceptCompress)
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
public HttpRequest setCompressMessage(boolean compress)
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
public HttpRequest setMessage(String message)
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
public String 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
public HttpRequest setMessageData(byte[] messageData)
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
public HttpRequest setParams(Map<String,String> params)
Set the request form parameters.- Parameters:
params
- the request form parameters- Returns:
- the HttpRequest object
-
getParams
public Map<String,HttpRequest.Param> getParams()
Get the request form parameters.- Returns:
- the request form parameters
- Since:
- 5.1.7
-
addParam
public HttpRequest addParam(String name, String value)
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
public HttpRequest addParam(String name, String value, String contentType)
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
public HttpRequest addFileParam(String paramName, byte[] fileData, String fileName)
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
public Map<String,HttpRequest.FileParam> getFileParams()
Get file parameters for a multi-part request.- Returns:
- file parameters
- Since:
- 5.1.7
-
setTimeouts
public HttpRequest setTimeouts(int connectTimeout, int socketTimeout)
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
public HttpRequest setSocketTimeout(Integer socketTimeout)
Set the socket timeout in milliseconds.- Parameters:
socketTimeout
- the socket timeout in milliseconds- Returns:
- the HttpRequest object
-
setConnectTimeout
public HttpRequest setConnectTimeout(Integer connectTimeout)
Set the connection timeout in milliseconds.- Parameters:
connectTimeout
- the connection timeout in milliseconds- Returns:
- the HttpRequest object
-
setReadLimit
public HttpRequest setReadLimit(int readLimit)
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
public HttpRequest setUseSystemProxy(boolean useSysProxy)
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
public HttpResponse execute() throws IOException
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
-
-