Class HttpRequest

  • Direct Known Subclasses:
    DeleteRequest, GetRequest, PatchRequest, PostRequest, PutRequest

    public abstract class HttpRequest
    extends java.lang.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, java.lang.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​(java.lang.String paramName, byte[] fileData, java.lang.String fileName)
      Add a file parameter for a multi-part request.
      HttpRequest addHeader​(java.lang.String name, java.lang.String value)
      Add request header.
      HttpRequest addHeaders​(java.util.Map<java.lang.String,​java.lang.String> headers)
      Add all the request headers.
      HttpRequest addParam​(java.lang.String name, java.lang.String value)
      Add a name and value request parameters.
      HttpRequest addParam​(java.lang.String name, java.lang.String value, java.lang.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.
      java.util.Map<java.lang.String,​HttpRequest.FileParam> getFileParams()
      Get file parameters for a multi-part request.
      java.util.Map<java.lang.String,​java.lang.String> getHeaders()
      Return the request headers.
      java.lang.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.
      java.util.Map<java.lang.String,​HttpRequest.Param> getParams()
      Get the request form parameters.
      java.lang.String getUri()
      Return the request URI.
      HttpRequest setAcceptCompress​(boolean acceptCompress)
      Specify whether to set the header "Accept-Encoding: gzip, deflate".
      HttpRequest setBasicAuth​(java.lang.String username, java.lang.String password)
      Set BASIC Authorization credentials.
      HttpRequest setCharacterEncoding​(java.lang.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​(java.lang.Integer connectTimeout)
      Set the connection timeout in milliseconds.
      HttpRequest setContentType​(java.lang.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​(java.lang.String message)
      Set the POST or PUT message body.
      HttpRequest setMessageData​(byte[] messageData)
      Set the POST or PUT message body.
      HttpRequest setNtlmAuth​(java.lang.String username, java.lang.String password, java.lang.String workstation, java.lang.String domain)
      Set NTLM Authorization credentials.
      HttpRequest setParams​(java.util.Map<java.lang.String,​java.lang.String> params)
      Set the request form parameters.
      HttpRequest setProxy​(java.lang.String proxyHost, int proxyPort)
      Set Proxy host and port.
      HttpRequest setProxyAuth​(java.lang.String username, java.lang.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​(java.lang.Integer socketTimeout)
      Set the socket timeout in milliseconds.
      HttpRequest setTimeouts​(int connectTimeout, int socketTimeout)
      Set the connect and socket timeouts in milliseconds.
      HttpRequest setUserAgent​(java.lang.String userAgent)
      Set the 'User-Agent' header.
      HttpRequest setUseSystemProxy​(boolean useSysProxy)
      Specify whether to use proxy in system properties.
      java.lang.String toString()
      Return the string representation of this class.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • HttpRequest

        protected HttpRequest​(com.avoka.tm.http.HttpRequest.Method method,
                              java.lang.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​(java.lang.String username,
                                        java.lang.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​(java.lang.String username,
                                       java.lang.String password,
                                       java.lang.String workstation,
                                       java.lang.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​(java.lang.String username,
                                        java.lang.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​(java.lang.String proxyHost,
                                    int proxyPort)
        Set Proxy host and port.
        Parameters:
        proxyHost - proxy host
        proxyPort - 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​(java.lang.String contentType)
        Set the 'Content-Type' header.
        Parameters:
        contentType - the 'Content-Type' header (required)
        Returns:
        the HttpRequest object
      • setCharacterEncoding

        public HttpRequest setCharacterEncoding​(java.lang.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 java.util.Map<java.lang.String,​java.lang.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 java.lang.String getUri()
        Return the request URI.
        Returns:
        the request URI
        Since:
        5.1.7
      • setUserAgent

        public HttpRequest setUserAgent​(java.lang.String userAgent)
        Set the 'User-Agent' header.
        Parameters:
        userAgent - the 'User-Agent' header (required)
        Returns:
        the HttpRequest object
      • addHeader

        public HttpRequest addHeader​(java.lang.String name,
                                     java.lang.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​(java.util.Map<java.lang.String,​java.lang.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​(java.lang.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 java.lang.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​(java.util.Map<java.lang.String,​java.lang.String> params)
        Set the request form parameters.
        Parameters:
        params - the request form parameters
        Returns:
        the HttpRequest object
      • getParams

        public java.util.Map<java.lang.String,​HttpRequest.Param> getParams()
        Get the request form parameters.
        Returns:
        the request form parameters
        Since:
        5.1.7
      • addParam

        public HttpRequest addParam​(java.lang.String name,
                                    java.lang.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​(java.lang.String name,
                                    java.lang.String value,
                                    java.lang.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​(java.lang.String paramName,
                                        byte[] fileData,
                                        java.lang.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 java.util.Map<java.lang.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 milliseconds
        socketTimeout - the socket timeout in milliseconds
        Returns:
        the HttpRequest object
      • setSocketTimeout

        public HttpRequest setSocketTimeout​(java.lang.Integer socketTimeout)
        Set the socket timeout in milliseconds.
        Parameters:
        socketTimeout - the socket timeout in milliseconds
        Returns:
        the HttpRequest object
      • setConnectTimeout

        public HttpRequest setConnectTimeout​(java.lang.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
      • 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 java.io.IOException
        Perform a HTTP request and return a response object.
        Returns:
        perform a HTTP request and return a response object
        Throws:
        java.io.IOException - if an IO error occurs, please note HTTP status codes are returned in the response object
      • toString

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