Class HttpRequest

java.lang.Object
com.avoka.component.http.HttpRequest
Direct Known Subclasses:
DeleteRequest, GetRequest, 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 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:
  • Constructor Details

    • HttpRequest

      protected HttpRequest(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 Details

    • 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
      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

      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
      Since:
      4.3.0
    • setContentType

      public HttpRequest setContentType(String contentType)
      Set the 'Content-Type' header.
      Parameters:
      contentType - the 'Content-Type' header (required)
      Returns:
      the HttpRequest object
    • 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
    • addHeadersAll

      public HttpRequest addHeadersAll(Map<String,Object> headers)
      Add all the request headers.
      Parameters:
      headers - the map of request headers to add (required)
      Returns:
      the HttpRequest object
      Since:
      4.3.0
    • 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
    • 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
    • setParams

      public HttpRequest setParams(Map<String,String> params)
      Set the request form parameters.
      Parameters:
      params - the request form parameters
      Returns:
      the HttpRequest object
    • setTimeouts

      public HttpRequest setTimeouts(int connectTimeout, int socketTimeout)
      Set the connect and socket timeouts in milliseconds.
      Parameters:
      connectTimeout - the connection timeout in milliseconds
      socketTimeout - 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 a SSLConnectionSocketFactory 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

      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
    • toString

      public String toString()
      Return the string representation of object.
      Overrides:
      toString in class Object
      Returns:
      the string representation of object