Class Client

java.lang.Object
org.apache.hadoop.hbase.rest.client.Client

@Public public class Client extends Object
A wrapper around HttpClient which provides some useful function and semantics for interacting with the REST gateway.
  • Field Details

  • Constructor Details

    • Client

      public Client()
      Default Constructor
    • Client

      public Client(Cluster cluster)
      Constructor This constructor will create an object using the old faulty load balancing logic. When specifying multiple servers in the cluster object, it is highly recommended to call setSticky() on the created client, or use the preferred constructor instead.
      Parameters:
      cluster - the cluster definition
    • Client

      public Client(Cluster cluster, boolean sslEnabled)
      Constructor This constructor will create an object using the old faulty load balancing logic. When specifying multiple servers in the cluster object, it is highly recommended to call setSticky() on the created client, or use the preferred constructor instead.
      Parameters:
      cluster - the cluster definition
      sslEnabled - enable SSL or not
    • Client

      public Client(Cluster cluster, org.apache.hadoop.conf.Configuration conf, boolean sslEnabled)
      Constructor This constructor will create an object using the old faulty load balancing logic. When specifying multiple servers in the cluster object, it is highly recommended to call setSticky() on the created client, or use the preferred constructor instead.
      Parameters:
      cluster - the cluster definition
      conf - Configuration
      sslEnabled - enable SSL or not
    • Client

      public Client(Cluster cluster, String trustStorePath, Optional<String> trustStorePassword, Optional<String> trustStoreType)
      Constructor, allowing to define custom trust store (only for SSL connections) This constructor will create an object using the old faulty load balancing logic. When specifying multiple servers in the cluster object, it is highly recommended to call setSticky() on the created client, or use the preferred constructor instead.
      Parameters:
      cluster - the cluster definition
      trustStorePath - custom trust store to use for SSL connections
      trustStorePassword - password to use for custom trust store
      trustStoreType - type of custom trust store
      Throws:
      Client.ClientTrustStoreInitializationException - if the trust store file can not be loaded
    • Client

      @Private public Client(Cluster cluster, org.apache.hadoop.conf.Configuration conf, boolean sslEnabled, Optional<KeyStore> trustStore, Optional<String> userName, Optional<String> password, Optional<String> bearerToken, Optional<org.apache.http.conn.HttpClientConnectionManager> connManager)
      Constructor that accepts an optional trustStore and authentication information for either BASIC or BEARER authentication in sticky mode, which does not use the old faulty load balancing logic, and enables correct session handling. If neither userName/password, nor the bearer token is specified, the client falls back to SPNEGO auth. The loadTrustsore static method can be used to load a local TrustStore file. If connManager is specified, it must be fully configured. Even then, the TrustStore related parameters must be specified because they are also used for SPNEGO authentication which uses a separate HTTP client implementation. Specifying the HttpClientConnectionManager is an experimental feature. It exposes the internal HTTP library details, and may be changed/removed when the library is updated or replaced.
      Parameters:
      cluster - the cluster definition
      conf - HBase/Hadoop configuration
      sslEnabled - use HTTPS
      trustStore - the optional trustStore object
      userName - for BASIC auth
      password - for BASIC auth
      bearerToken - for BEAERER auth
    • Client

      public Client(Cluster cluster, org.apache.hadoop.conf.Configuration conf, boolean sslEnabled, Optional<KeyStore> trustStore, Optional<String> userName, Optional<String> password, Optional<String> bearerToken)
    • Client

      public Client(Cluster cluster, org.apache.hadoop.conf.Configuration conf, String trustStorePath, Optional<String> trustStorePassword, Optional<String> trustStoreType)
      Constructor, allowing to define custom trust store (only for SSL connections). This constructor will create an object using the old faulty load balancing logic. When specifying multiple servers in the cluster object, it is highly recommended to call setSticky() on the created client, or use the preferred constructor instead.
      Parameters:
      cluster - the cluster definition
      conf - HBase/Hadoop Configuration
      trustStorePath - custom trust store to use for SSL connections
      trustStorePassword - password to use for custom trust store
      trustStoreType - type of custom trust store
      Throws:
      Client.ClientTrustStoreInitializationException - if the trust store file can not be loaded
  • Method Details

    • initialize

      private void initialize(Cluster cluster, org.apache.hadoop.conf.Configuration conf, boolean sslEnabled, boolean sticky, Optional<KeyStore> trustStore, Optional<String> userName, Optional<String> password, Optional<String> bearerToken, Optional<org.apache.http.conn.HttpClientConnectionManager> connManager)
    • loadTruststore

      public static KeyStore loadTruststore(String trustStorePath, Optional<String> trustStorePassword, Optional<String> trustStoreType)
      Loads a trustStore from the local fileSystem. Can be used to load the trustStore for the preferred constructor.
    • shutdown

      public void shutdown()
      Shut down the client. Close any open persistent connections.
    • getHttpClient

      public org.apache.http.client.HttpClient getHttpClient()
      Returns the wrapped HttpClient
    • addExtraHeader

      public void addExtraHeader(String name, String value)
      Add extra headers. These extra headers will be applied to all http methods before they are removed. If any header is not used any more, client needs to remove it explicitly.
    • getExtraHeader

      public String getExtraHeader(String name)
      Get an extra header value.
    • getExtraHeaders

      Get all extra headers (read-only).
    • removeExtraHeader

      public void removeExtraHeader(String name)
      Remove an extra header.
    • executePathOnly

      public org.apache.http.HttpResponse executePathOnly(Cluster cluster, org.apache.http.client.methods.HttpUriRequest method, org.apache.http.Header[] headers, String path) throws IOException
      Execute a transaction method given only the path. If sticky is false: Will select at random one of the members of the supplied cluster definition and iterate through the list until a transaction can be successfully completed. The definition of success here is a complete HTTP transaction, irrespective of result code. If sticky is true: For the first request it will select a random one of the members of the supplied cluster definition. For subsequent requests it will use the same member, and it will not automatically re-try if the call fails.
      Parameters:
      cluster - the cluster definition
      method - the transaction method
      headers - HTTP header values to send
      path - the properly urlencoded path
      Returns:
      the HTTP response code
      Throws:
      IOException
    • executeURI

      public org.apache.http.HttpResponse executeURI(org.apache.http.client.methods.HttpUriRequest method, org.apache.http.Header[] headers, String uri) throws IOException
      Execute a transaction method given a complete URI.
      Parameters:
      method - the transaction method
      headers - HTTP header values to send
      uri - a properly urlencoded URI
      Returns:
      the HTTP response code
      Throws:
      IOException
    • execute

      public org.apache.http.HttpResponse execute(Cluster cluster, org.apache.http.client.methods.HttpUriRequest method, org.apache.http.Header[] headers, String path) throws IOException
      Execute a transaction method. Will call either executePathOnly or executeURI depending on whether a path only is supplied in 'path', or if a complete URI is passed instead, respectively.
      Parameters:
      cluster - the cluster definition
      method - the HTTP method
      headers - HTTP header values to send
      path - the properly urlencoded path or URI
      Returns:
      the HTTP response code
      Throws:
      IOException
    • negotiate

      private void negotiate(org.apache.http.client.methods.HttpUriRequest method, String uri) throws IOException, GeneralSecurityException
      Initiate client side Kerberos negotiation with the server.
      Parameters:
      method - method to inject the authentication token into.
      uri - the String to parse as a URL.
      Throws:
      IOException - if unknown protocol is found.
      GeneralSecurityException
    • setupTrustStoreForHadoop

      private org.apache.hadoop.conf.Configuration setupTrustStoreForHadoop(KeyStore trustStore) throws IOException, KeyStoreException, NoSuchAlgorithmException, CertificateException
      Throws:
      IOException
      KeyStoreException
      NoSuchAlgorithmException
      CertificateException
    • injectToken

      private void injectToken(org.apache.http.client.methods.HttpUriRequest method, org.apache.hadoop.security.authentication.client.AuthenticatedURL.Token token)
      Helper method that injects an authentication token to send with the method.
      Parameters:
      method - method to inject the authentication token into.
      token - authentication token to inject.
    • getCluster

      public Cluster getCluster()
      Returns the cluster definition
    • setCluster

      public void setCluster(Cluster cluster)
      Parameters:
      cluster - the cluster definition
    • isSticky

      public boolean isSticky()
      The default behaviour is load balancing by sending each request to a random host. This DOES NOT work with scans, which have state on the REST servers. Make sure sticky is set to true before attempting Scan related operations if more than one host is defined in the cluster.
      Returns:
      whether subsequent requests will use the same host
    • setSticky

      public void setSticky(boolean sticky)
      The default behaviour is load balancing by sending each request to a random host. This DOES NOT work with scans, which have state on the REST servers. Set sticky to true before attempting Scan related operations if more than one host is defined in the cluster. Nodes must not be added or removed from the Cluster object while sticky is true. Setting the sticky flag also enables session handling, which eliminates the need to re-authenticate each request, and lets the client handle any other cookies (like the sticky cookie set by load balancers) correctly.
      Parameters:
      sticky - whether subsequent requests will use the same host
    • head

      public Response head(String path) throws IOException
      Send a HEAD request
      Parameters:
      path - the path or URI
      Returns:
      a Response object with response detail
      Throws:
      IOException
    • head

      public Response head(Cluster cluster, String path, org.apache.http.Header[] headers) throws IOException
      Send a HEAD request
      Parameters:
      cluster - the cluster definition
      path - the path or URI
      headers - the HTTP headers to include in the request
      Returns:
      a Response object with response detail
      Throws:
      IOException
    • get

      public Response get(String path) throws IOException
      Send a GET request
      Parameters:
      path - the path or URI
      Returns:
      a Response object with response detail
      Throws:
      IOException
    • get

      public Response get(Cluster cluster, String path) throws IOException
      Send a GET request
      Parameters:
      cluster - the cluster definition
      path - the path or URI
      Returns:
      a Response object with response detail
      Throws:
      IOException
    • get

      public Response get(String path, String accept) throws IOException
      Send a GET request
      Parameters:
      path - the path or URI
      accept - Accept header value
      Returns:
      a Response object with response detail
      Throws:
      IOException
    • get

      public Response get(Cluster cluster, String path, String accept) throws IOException
      Send a GET request
      Parameters:
      cluster - the cluster definition
      path - the path or URI
      accept - Accept header value
      Returns:
      a Response object with response detail
      Throws:
      IOException
    • get

      public Response get(String path, org.apache.http.Header[] headers) throws IOException
      Send a GET request
      Parameters:
      path - the path or URI
      headers - the HTTP headers to include in the request, Accept must be supplied
      Returns:
      a Response object with response detail
      Throws:
      IOException
    • getResponseBody

      public static byte[] getResponseBody(org.apache.http.HttpResponse resp) throws IOException
      Returns the response body of the HTTPResponse, if any, as an array of bytes. If response body is not available or cannot be read, returns null Note: This will cause the entire response body to be buffered in memory. A malicious server may easily exhaust all the VM memory. It is strongly recommended, to use getResponseAsStream if the content length of the response is unknown or reasonably large.
      Parameters:
      resp - HttpResponse
      Returns:
      The response body, null if body is empty
      Throws:
      IOException - If an I/O (transport) problem occurs while obtaining the response body.
    • get

      public Response get(Cluster c, String path, org.apache.http.Header[] headers) throws IOException
      Send a GET request
      Parameters:
      c - the cluster definition
      path - the path or URI
      headers - the HTTP headers to include in the request
      Returns:
      a Response object with response detail
      Throws:
      IOException
    • put

      public Response put(String path, String contentType, byte[] content) throws IOException
      Send a PUT request
      Parameters:
      path - the path or URI
      contentType - the content MIME type
      content - the content bytes
      Returns:
      a Response object with response detail
      Throws:
      IOException
    • put

      public Response put(String path, String contentType, byte[] content, org.apache.http.Header extraHdr) throws IOException
      Send a PUT request
      Parameters:
      path - the path or URI
      contentType - the content MIME type
      content - the content bytes
      extraHdr - extra Header to send
      Returns:
      a Response object with response detail
      Throws:
      IOException
    • put

      public Response put(Cluster cluster, String path, String contentType, byte[] content) throws IOException
      Send a PUT request
      Parameters:
      cluster - the cluster definition
      path - the path or URI
      contentType - the content MIME type
      content - the content bytes
      Returns:
      a Response object with response detail
      Throws:
      IOException - for error
    • put

      public Response put(Cluster cluster, String path, String contentType, byte[] content, org.apache.http.Header extraHdr) throws IOException
      Send a PUT request
      Parameters:
      cluster - the cluster definition
      path - the path or URI
      contentType - the content MIME type
      content - the content bytes
      extraHdr - additional Header to send
      Returns:
      a Response object with response detail
      Throws:
      IOException - for error
    • put

      public Response put(String path, org.apache.http.Header[] headers, byte[] content) throws IOException
      Send a PUT request
      Parameters:
      path - the path or URI
      headers - the HTTP headers to include, Content-Type must be supplied
      content - the content bytes
      Returns:
      a Response object with response detail
      Throws:
      IOException
    • put

      public Response put(Cluster cluster, String path, org.apache.http.Header[] headers, byte[] content) throws IOException
      Send a PUT request
      Parameters:
      cluster - the cluster definition
      path - the path or URI
      headers - the HTTP headers to include, Content-Type must be supplied
      content - the content bytes
      Returns:
      a Response object with response detail
      Throws:
      IOException
    • post

      public Response post(String path, String contentType, byte[] content) throws IOException
      Send a POST request
      Parameters:
      path - the path or URI
      contentType - the content MIME type
      content - the content bytes
      Returns:
      a Response object with response detail
      Throws:
      IOException
    • post

      public Response post(String path, String contentType, byte[] content, org.apache.http.Header extraHdr) throws IOException
      Send a POST request
      Parameters:
      path - the path or URI
      contentType - the content MIME type
      content - the content bytes
      extraHdr - additional Header to send
      Returns:
      a Response object with response detail
      Throws:
      IOException
    • post

      public Response post(Cluster cluster, String path, String contentType, byte[] content) throws IOException
      Send a POST request
      Parameters:
      cluster - the cluster definition
      path - the path or URI
      contentType - the content MIME type
      content - the content bytes
      Returns:
      a Response object with response detail
      Throws:
      IOException - for error
    • post

      public Response post(Cluster cluster, String path, String contentType, byte[] content, org.apache.http.Header extraHdr) throws IOException
      Send a POST request
      Parameters:
      cluster - the cluster definition
      path - the path or URI
      contentType - the content MIME type
      content - the content bytes
      extraHdr - additional Header to send
      Returns:
      a Response object with response detail
      Throws:
      IOException - for error
    • post

      public Response post(String path, org.apache.http.Header[] headers, byte[] content) throws IOException
      Send a POST request
      Parameters:
      path - the path or URI
      headers - the HTTP headers to include, Content-Type must be supplied
      content - the content bytes
      Returns:
      a Response object with response detail
      Throws:
      IOException
    • post

      public Response post(Cluster cluster, String path, org.apache.http.Header[] headers, byte[] content) throws IOException
      Send a POST request
      Parameters:
      cluster - the cluster definition
      path - the path or URI
      headers - the HTTP headers to include, Content-Type must be supplied
      content - the content bytes
      Returns:
      a Response object with response detail
      Throws:
      IOException
    • delete

      public Response delete(String path) throws IOException
      Send a DELETE request
      Parameters:
      path - the path or URI
      Returns:
      a Response object with response detail
      Throws:
      IOException
    • delete

      public Response delete(String path, org.apache.http.Header extraHdr) throws IOException
      Send a DELETE request
      Parameters:
      path - the path or URI
      extraHdr - additional Header to send
      Returns:
      a Response object with response detail
      Throws:
      IOException
    • delete

      public Response delete(Cluster cluster, String path) throws IOException
      Send a DELETE request
      Parameters:
      cluster - the cluster definition
      path - the path or URI
      Returns:
      a Response object with response detail
      Throws:
      IOException - for error
    • delete

      public Response delete(Cluster cluster, String path, org.apache.http.Header extraHdr) throws IOException
      Send a DELETE request
      Parameters:
      cluster - the cluster definition
      path - the path or URI
      Returns:
      a Response object with response detail
      Throws:
      IOException - for error
    • close

      public void close()