Package com.avoka.component.http
Class HttpRequest
java.lang.Object
com.avoka.component.http.HttpRequest
- Direct Known Subclasses:
DeleteRequest
,GetRequest
,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 16 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:
- 4.2.0
- See Also:
-
Nested Class Summary
-
Constructor Summary
ModifierConstructorDescriptionprotected
HttpRequest
(HttpRequest.Method method, String uri) Create a HTTP request object with the given HTTP method and URL. -
Method Summary
Modifier and TypeMethodDescriptionAdd request header.addHeadersAll
(Map<String, Object> headers) Add all the request headers.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.setAcceptCompress
(boolean acceptCompress) Specify whether to set the header "Accept-Encoding: gzip, deflate".setBasicAuth
(String username, String password) Set BASIC Authorization credentials.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.setMessage
(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.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.toString()
Return the string representation of object.
-
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
- Since:
- 4.3.0
-
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
- Since:
- 4.3.0
-
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
- Since:
- 4.3.0
-
setContentType
Set the 'Content-Type' header.- Parameters:
contentType
- the 'Content-Type' header (required)- Returns:
- the HttpRequest object
-
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
-
addHeadersAll
Add all the request headers.- Parameters:
headers
- the map of request headers to add (required)- Returns:
- the HttpRequest object
- Since:
- 4.3.0
-
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
-
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
-
setParams
Set the request form parameters.- Parameters:
params
- the request form parameters- Returns:
- the HttpRequest object
-
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
- Since:
- 4.3.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 object.
-