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
      Parameters:
      cluster - the cluster definition
    • Client

      public Client(Cluster cluster, boolean sslEnabled)
      Constructor
      Parameters:
      cluster - the cluster definition
      sslEnabled - enable SSL or not
    • Client

      public Client(Cluster cluster, org.apache.hadoop.conf.Configuration conf, boolean sslEnabled)
      Constructor
      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)
      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

      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)
      Parameters:
      cluster - the cluster definition
      conf - 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, Optional<KeyStore> trustStore)
    • 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
      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.
    • 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.
      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