Class Client

  • Direct Known Subclasses:
    SSLClient

    public class Client
    extends com.logicmonitor.common.sse.utils.SSEResource
    Http client, represents only the most basic contract for HTTP request execution. It imposes no restrictions or particular details on the request execution process and leaves the specifics of state management and authentication
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.String DEFAULT_TLS  
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      protected Client​(org.apache.http.impl.client.DefaultHttpClient client)  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      protected static org.apache.http.impl.client.DefaultHttpClient _createHTTPClient​(org.apache.http.HttpHost defHost, java.lang.String tlsVersion)  
      void close()
      Close the http client
      java.lang.String delete​(java.lang.String uri, java.lang.String data, java.util.Map headers)
      Get the http response by DELETE http method
      java.lang.String get​(java.lang.String uri)
      Get the http response on uri
      java.lang.String get​(java.lang.String uri, java.util.Map headers)
      Get the http response on uri with http headers
      static Client getDefaultClient()
      Create a default http client
      java.lang.String getHeader​(java.lang.String header)
      Get the http header from last http response
      java.lang.String getHeaders()
      Get the all http headers from last http response
      org.apache.http.HttpResponse getLastHTTPResponse()
      Get the last http response
      java.lang.String getResponseBody()
      Get the http response body from last http response
      int getStatusCode()
      Get the last http response status code
      long[] getTimeStatistics()
      Get the connect time and read time
      boolean isReleased()
      Resource interface
      static Client open​(java.lang.String host)
      Open a default http client to host, and the default port is 80
      static Client open​(java.lang.String host, int port)
      Open a default http client to host and port
      static Client open​(java.lang.String host, int port, java.lang.String tlsVersion)
      Open a http client to host and port with specific TLS version
      static Client open​(java.lang.String host, int port, java.lang.String user, java.lang.String pass)
      Open a default http client to host with credentials
      static Client open​(java.lang.String host, int port, java.lang.String user, java.lang.String pass, java.lang.String tlsVersion)
      Open a http client to host with credentials and specific TLS version
      static Client open​(java.lang.String host, java.lang.String user, java.lang.String pass)
      Open a default http client to host with credentials, and the default port is 80
      java.lang.String post​(java.lang.String uri, java.lang.String data, java.util.Map headers)
      Get the http response by POST http method
      java.lang.String put​(java.lang.String uri, java.lang.String data, java.util.Map headers)
      Get the http response by PUT http method
      void release()
      Release the http client
      java.lang.String request​(org.apache.http.client.methods.HttpRequestBase method, java.lang.String uri, java.lang.String data, java.util.Map headers)
      Send http request and return the http response
      void setAuthentication​(java.lang.String user, java.lang.String pass)
      Set the authentication for get and post method
      void setConnectTimeout​(int connTOMillis)
      Set the connect timeout
      void setFollowRedirect​(boolean follow)
      Enable or disable to follow redirect
      void setHTTPProxy​(java.lang.String host, int port)
      Set the http proxy without credentials
      void setHTTPProxy​(java.lang.String host, int port, java.lang.String user, java.lang.String pass)
      Set the http proxy with credentials, only support BASIC authentication method
      void setReadTimeout​(int readTOMillis)
      Set the read timeout
      java.lang.String toString()  
      • Methods inherited from class com.logicmonitor.common.sse.utils.SSEResource

        register, unregister
      • Methods inherited from class java.lang.Object

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

      • DEFAULT_TLS

        public static java.lang.String DEFAULT_TLS
    • Constructor Detail

      • Client

        protected Client​(org.apache.http.impl.client.DefaultHttpClient client)
    • Method Detail

      • isReleased

        public boolean isReleased()
        Resource interface
        Specified by:
        isReleased in class com.logicmonitor.common.sse.utils.SSEResource
      • release

        public void release()
        Release the http client
        Specified by:
        release in class com.logicmonitor.common.sse.utils.SSEResource
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • getDefaultClient

        public static Client getDefaultClient()
                                       throws java.io.IOException
        Create a default http client
        Returns:
        Http client
        Throws:
        java.io.IOException - If some other I/O error occurs
      • open

        public static Client open​(java.lang.String host)
                           throws java.io.IOException
        Open a default http client to host, and the default port is 80
        Parameters:
        host - the target http host
        Returns:
        Http client
        Throws:
        java.io.IOException - If some other I/O error occurs
      • open

        public static Client open​(java.lang.String host,
                                  int port)
                           throws java.io.IOException
        Open a default http client to host and port
        Parameters:
        host - the http host
        port - the http port
        Returns:
        Http client
        Throws:
        java.io.IOException - If some other I/O error occurs
      • open

        public static Client open​(java.lang.String host,
                                  int port,
                                  java.lang.String tlsVersion)
                           throws java.io.IOException
        Open a http client to host and port with specific TLS version
        Parameters:
        host - the http host
        port - the http port
        tlsVersion - tls version like TLS, TLSv1, TLSv1.1, TLSv1.2
        Returns:
        Http client
        Throws:
        java.io.IOException - If some other I/O error occurs
      • open

        public static Client open​(java.lang.String host,
                                  java.lang.String user,
                                  java.lang.String pass)
                           throws java.io.IOException
        Open a default http client to host with credentials, and the default port is 80
        Parameters:
        host - the http host
        user - the username for get or post
        pass - the password for get or post
        Returns:
        Http client
        Throws:
        java.io.IOException - If some other I/O error occurs
      • open

        public static Client open​(java.lang.String host,
                                  int port,
                                  java.lang.String user,
                                  java.lang.String pass)
                           throws java.io.IOException
        Open a default http client to host with credentials
        Parameters:
        host - the http host
        port - the http port
        user - the username for get or post
        pass - the password for get or post
        Returns:
        Http client
        Throws:
        java.io.IOException - If some other I/O error occurs
      • open

        public static Client open​(java.lang.String host,
                                  int port,
                                  java.lang.String user,
                                  java.lang.String pass,
                                  java.lang.String tlsVersion)
                           throws java.io.IOException
        Open a http client to host with credentials and specific TLS version
        Parameters:
        host - the http host
        port - the http port
        user - the username for get or post
        pass - the password for get or post
        tlsVersion - tls version like TLS, TLSv1, TLSv1.1, TLSv1.2
        Returns:
        Http client
        Throws:
        java.io.IOException - If some other I/O error occurs
      • close

        public void close()
        Close the http client
        See Also:
        release()
      • setAuthentication

        public void setAuthentication​(java.lang.String user,
                                      java.lang.String pass)
                               throws java.io.IOException
        Set the authentication for get and post method
        Parameters:
        user - the username
        pass - the password
        Throws:
        java.io.IOException
        See Also:
        get(String), get(String, Map), post(String, String, Map)
      • setConnectTimeout

        public void setConnectTimeout​(int connTOMillis)
                               throws java.io.IOException
        Set the connect timeout
        Parameters:
        connTOMillis - the connect timeout in milliseconds
        Throws:
        java.io.IOException - If some other I/O error occurs
      • setReadTimeout

        public void setReadTimeout​(int readTOMillis)
                            throws java.io.IOException
        Set the read timeout
        Parameters:
        readTOMillis - the read timeout in milliseconds
        Throws:
        java.io.IOException - If some other I/O error occurs
      • setHTTPProxy

        public void setHTTPProxy​(java.lang.String host,
                                 int port)
                          throws java.io.IOException
        Set the http proxy without credentials
        Parameters:
        host - the http proxy host
        port - the http proxy port
        Throws:
        java.io.IOException - If some other I/O error occurs
      • setHTTPProxy

        public void setHTTPProxy​(java.lang.String host,
                                 int port,
                                 java.lang.String user,
                                 java.lang.String pass)
                          throws java.io.IOException
        Set the http proxy with credentials, only support BASIC authentication method
        Parameters:
        host - the http proxy host
        port - the http proxy port
        user - the http proxy username
        pass - the http proxy password
        Throws:
        java.io.IOException - If some other I/O error occurs
      • setFollowRedirect

        public void setFollowRedirect​(boolean follow)
                               throws java.io.IOException
        Enable or disable to follow redirect
        Parameters:
        follow - Whether the server jumps to the new address when the server returns to 301/302
        Throws:
        java.io.IOException - If some other I/O error occurs
      • get

        public java.lang.String get​(java.lang.String uri)
                             throws java.io.IOException
        Get the http response on uri
        Parameters:
        uri - the resource uri
        Returns:
        This url content
        Throws:
        java.io.IOException - If some other I/O error occurs
      • get

        public java.lang.String get​(java.lang.String uri,
                                    java.util.Map headers)
                             throws java.io.IOException
        Get the http response on uri with http headers
        Parameters:
        uri - the resource uri
        headers - the http headers map
        Returns:
        This url content
        Throws:
        java.io.IOException - If some other I/O error occurs
      • post

        public java.lang.String post​(java.lang.String uri,
                                     java.lang.String data,
                                     java.util.Map headers)
                              throws java.io.IOException
        Get the http response by POST http method
        Parameters:
        uri - the resource uri
        data - the request body
        headers - the http headers map
        Returns:
        This post request response text
        Throws:
        java.io.IOException - If some other I/O error occurs
      • put

        public java.lang.String put​(java.lang.String uri,
                                    java.lang.String data,
                                    java.util.Map headers)
                             throws java.io.IOException
        Get the http response by PUT http method
        Parameters:
        uri - the resource uri
        data - the request body
        headers - the http headers map
        Returns:
        This post request response text
        Throws:
        java.io.IOException - If some other I/O error occurs
      • delete

        public java.lang.String delete​(java.lang.String uri,
                                       java.lang.String data,
                                       java.util.Map headers)
                                throws java.io.IOException
        Get the http response by DELETE http method
        Parameters:
        uri - the resource uri
        data - the request body
        headers - the http headers map
        Returns:
        This post request response text
        Throws:
        java.io.IOException - If some other I/O error occurs
      • request

        public java.lang.String request​(org.apache.http.client.methods.HttpRequestBase method,
                                        java.lang.String uri,
                                        java.lang.String data,
                                        java.util.Map headers)
                                 throws java.io.IOException
        Send http request and return the http response
        Parameters:
        method - the http request method
        uri - the resource uri
        data - the request body
        headers - the http headers map
        Returns:
        This post request response text
        Throws:
        java.io.IOException - If some other I/O error occurs
      • getLastHTTPResponse

        public org.apache.http.HttpResponse getLastHTTPResponse()
                                                         throws java.io.IOException
        Get the last http response
        Returns:
        Last response
        Throws:
        java.io.IOException - If some other I/O error occurs
        See Also:
        get(String, Map), post(String, String, Map)
      • getStatusCode

        public int getStatusCode()
                          throws java.io.IOException
        Get the last http response status code
        Returns:
        The http status code of current request
        Throws:
        java.io.IOException - If some other I/O error occurs
        See Also:
        get(String, Map), post(String, String, Map)
      • getHeader

        public java.lang.String getHeader​(java.lang.String header)
                                   throws java.io.IOException
        Get the http header from last http response
        Parameters:
        header - the http header name
        Returns:
        The value of header
        Throws:
        java.io.IOException - If some other I/O error occurs
        See Also:
        get(String, Map), post(String, String, Map)
      • getHeaders

        public java.lang.String getHeaders()
                                    throws java.io.IOException
        Get the all http headers from last http response
        Returns:
        All header
        Throws:
        java.io.IOException - If some other I/O error occurs
        See Also:
        get(String, Map), post(String, String, Map)
      • getResponseBody

        public java.lang.String getResponseBody()
                                         throws java.io.IOException
        Get the http response body from last http response
        Returns:
        The http response body
        Throws:
        java.io.IOException - If some other I/O error occurs
        See Also:
        get(String, Map), post(String, String, Map)
      • getTimeStatistics

        public long[] getTimeStatistics()
                                 throws java.io.IOException
        Get the connect time and read time
        Returns:
        the connect time and read time
        Throws:
        java.io.IOException - If some other I/O error occurs
      • _createHTTPClient

        protected static org.apache.http.impl.client.DefaultHttpClient _createHTTPClient​(org.apache.http.HttpHost defHost,
                                                                                         java.lang.String tlsVersion)
                                                                                  throws java.io.IOException
        Throws:
        java.io.IOException