Class HTTPUtils


  • public class HTTPUtils
    extends java.lang.Object
    Contains utility methods related to HTTP. This class has two categories of methods. The first set of methods provide access to a pooled set of http client connections. The pool uses a centralized connection manager which is configured with the default properties identified in HttpClientTransport. This pool of clients share authorization and the credentials provider (if any) and will automatically configure preemptive authentication. Unless noted, these methods do not require any action on the user to clean up resources.

    The class also defines a set of single-use client methods, all starting with singleUse (for example singleUseClient(). When using the single-use methods, the caller is responsible for closing the returned CloseableHttpClient to clean up resources.

    • Constructor Summary

      Constructors 
      Constructor Description
      HTTPUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static org.apache.http.client.methods.CloseableHttpResponse execute​(org.apache.http.client.methods.HttpUriRequest method)
      Executes the method and returns the response.
      static byte[] getAsBytes​(java.lang.String url)
      Executes a GET and returns results as byte array.
      static int getAsBytes​(java.lang.String url, byte[] bytes)
      Executes a GET and populates results in the provided byte array.
      static java.io.InputStream getAsStream​(java.lang.String url)
      Executes an HTTP get and returns the response back as a stream using the default client.
      static java.io.InputStream getAsStream​(java.lang.String url, boolean close)
      Executes an HTTP get and returns the response back as a stream using the default client.
      static java.lang.String getAsText​(java.lang.String url)
      Executes a GET and returns results as a string in the default encoding.
      static java.io.InputStream getConnectionClosingStream​(org.apache.http.client.methods.HttpRequestBase m)
      Gets a stream from the HTTP method that will be automatically closed when the stream is closed.
      static org.apache.http.pool.PoolStats getConnectionManagerTotalStats()  
      static long getContentLength​(java.lang.String url)
      Executes a HTTP header request and returns the content length
      static org.apache.http.client.CredentialsProvider getCredentialsProvider()  
      static int getSizeReturned​(java.lang.String url)
      Executes a GET and returns the number of size in bytes of the result returned.
      static void getToStream​(java.lang.String url, java.io.OutputStream out)
      Reads the full contents of a URL.
      static boolean isAuthRequired()  
      static long lastModified​(java.lang.String url)
      Executes a HTTP header request and returns the lastModified header as a unix timestamp
      static java.lang.String post​(InputStreamBuilder in, java.lang.String url)
      Executes an HTTP request and gets a response back for inspection.
      static <T> T post​(java.lang.Class<? extends T> clazz, org.apache.http.entity.AbstractHttpEntity entity, java.lang.String url, boolean close)  
      static java.lang.String postAndGetText​(java.lang.String request, java.lang.String uri, org.apache.http.entity.ContentType contentType)
      Executes an HTTP request and gets the text response.
      static java.lang.String postAndGetText​(java.lang.String request, java.lang.String uri, org.apache.http.entity.ContentType contentType, java.lang.String encoding)
      Executes an HTTP request and gets the text response.
      static <T> T put​(java.lang.Class<? extends T> clazz, org.apache.http.HttpEntity entity, java.lang.String url)  
      static void resetConnectionManager()  
      static void setCredentialsProvider​(org.apache.http.client.CredentialsProvider provider)
      Specifies the credentials provider that gets called for servers returning 401, authentication required, requests.
      static org.apache.http.impl.client.CloseableHttpClient singleUseClient()  
      static org.apache.http.impl.client.CloseableHttpClient singleUseClient​(java.lang.String username, java.lang.String password)
      Returns a HttpClient that must be closed after use.
      static org.apache.http.impl.client.CloseableHttpClient singleUseClient​(org.apache.http.auth.AuthScope authScope, org.apache.http.client.CredentialsProvider provider, java.lang.String username, java.lang.String password)
      Returns a HttpClient that must be closed after use.
      static org.apache.http.impl.client.CloseableHttpClient singleUseClient​(org.apache.http.auth.AuthScope authScope, org.apache.http.client.CredentialsProvider provider, java.lang.String username, java.lang.String password, java.lang.String keyStorePath, java.lang.String keyStoreType)
      Returns a HttpClient that must be closed after use.
      static HttpResponseStream stream​(java.lang.String url)
      Initialize a streaming client connection.
      • Methods inherited from class java.lang.Object

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

      • HTTPUtils

        public HTTPUtils()
    • Method Detail

      • resetConnectionManager

        public static void resetConnectionManager()
      • getCredentialsProvider

        public static org.apache.http.client.CredentialsProvider getCredentialsProvider()
      • setCredentialsProvider

        public static void setCredentialsProvider​(org.apache.http.client.CredentialsProvider provider)
        Specifies the credentials provider that gets called for servers returning 401, authentication required, requests.
        Parameters:
        provider - credentials provider. null removes authentication from client requests.
      • post

        public static <T> T post​(java.lang.Class<? extends T> clazz,
                                 org.apache.http.entity.AbstractHttpEntity entity,
                                 java.lang.String url,
                                 boolean close)
                          throws AttivioException
        Throws:
        AttivioException
      • put

        public static <T> T put​(java.lang.Class<? extends T> clazz,
                                org.apache.http.HttpEntity entity,
                                java.lang.String url)
                         throws AttivioException
        Throws:
        AttivioException
      • getAsStream

        public static java.io.InputStream getAsStream​(java.lang.String url)
                                               throws AttivioException
        Executes an HTTP get and returns the response back as a stream using the default client. The caller is responsible for closing the input stream after use.
        Throws:
        AttivioException
      • getAsStream

        public static java.io.InputStream getAsStream​(java.lang.String url,
                                                      boolean close)
                                               throws AttivioException
        Executes an HTTP get and returns the response back as a stream using the default client. The caller is responsible for closing the input stream after use.
        Parameters:
        url -
        close - ??
        Throws:
        AttivioException
      • getConnectionClosingStream

        public static java.io.InputStream getConnectionClosingStream​(org.apache.http.client.methods.HttpRequestBase m)
                                                              throws AttivioException
        Gets a stream from the HTTP method that will be automatically closed when the stream is closed.
        Throws:
        AttivioException
      • getContentLength

        public static long getContentLength​(java.lang.String url)
                                     throws AttivioException
        Executes a HTTP header request and returns the content length
        Throws:
        AttivioException
      • lastModified

        public static long lastModified​(java.lang.String url)
                                 throws AttivioException
        Executes a HTTP header request and returns the lastModified header as a unix timestamp
        Throws:
        AttivioException
      • post

        public static java.lang.String post​(InputStreamBuilder in,
                                            java.lang.String url)
                                     throws AttivioException
        Executes an HTTP request and gets a response back for inspection.
        Parameters:
        in - input stream to read from and then write to the http output stream
        url - URL to send request to
        Returns:
        The response as a String
        Throws:
        AttivioException - if an error occurred
      • postAndGetText

        public static java.lang.String postAndGetText​(java.lang.String request,
                                                      java.lang.String uri,
                                                      org.apache.http.entity.ContentType contentType)
                                               throws AttivioException
        Executes an HTTP request and gets the text response.
        Parameters:
        request - string to POST
        uri - URI to send request to
        contentType - the type of request
        Returns:
        the string output from the POST. returns the error stream if a HTTP >400 is returned
        Throws:
        AttivioException - if an error occurred
      • postAndGetText

        public static java.lang.String postAndGetText​(java.lang.String request,
                                                      java.lang.String uri,
                                                      org.apache.http.entity.ContentType contentType,
                                                      java.lang.String encoding)
                                               throws AttivioException
        Executes an HTTP request and gets the text response.
        Parameters:
        request - string to POST
        uri - URI to send request to
        contentType - the type of request
        encoding -
        Returns:
        the string output from the POST. returns the error stream if a HTTP >400 is returned
        Throws:
        AttivioException - if an error occurred
      • getToStream

        public static void getToStream​(java.lang.String url,
                                       java.io.OutputStream out)
                                throws AttivioException
        Reads the full contents of a URL.
        Parameters:
        url - uri to read
        out - OutputStream to write content to
        Throws:
        AttivioException - if an error occurred
      • getAsText

        public static java.lang.String getAsText​(java.lang.String url)
                                          throws AttivioException
        Executes a GET and returns results as a string in the default encoding. HTTP status codes other than SC_OK will trigger an AttivioException. SC_NOT_FOUND and HttpStatus.SC_NO_CONTENT are mapped to KernelError.NOT_FOUND in the thrown exception.
        Parameters:
        url - url to GET
        Returns:
        string in the default encoding
        Throws:
        AttivioException - if an error occurred.
      • getAsBytes

        public static byte[] getAsBytes​(java.lang.String url)
                                 throws AttivioException
        Executes a GET and returns results as byte array.
        Parameters:
        url - url to GET
        Returns:
        byte array
        Throws:
        java.io.IOException - if an error occurred
        AttivioException
      • getAsBytes

        public static int getAsBytes​(java.lang.String url,
                                     byte[] bytes)
                              throws AttivioException
        Executes a GET and populates results in the provided byte array. This allows to control the amount of data read
        Parameters:
        url - url to GET
        bytes - to populate
        Returns:
        byte number of bytes actually read
        Throws:
        AttivioException - if an error occurred
      • getSizeReturned

        public static int getSizeReturned​(java.lang.String url)
                                   throws AttivioException
        Executes a GET and returns the number of size in bytes of the result returned. The full stream of bytes is consumed to determine the length.
        Parameters:
        url - url to GET
        Returns:
        size in bytes of the return
        Throws:
        java.io.IOException - if an error occured
        AttivioException
      • execute

        public static org.apache.http.client.methods.CloseableHttpResponse execute​(org.apache.http.client.methods.HttpUriRequest method)
                                                                            throws AttivioException
        Executes the method and returns the response. Compressed responses are accepted. If the response is unauthorized, AttivioException is thrown. Otherwise if the response is not ok an AttivioException with the response text decoded is thrown.
        Parameters:
        method - method to execute
        Returns:
        the response, which must be closed when finished
        Throws:
        AttivioException - if unauthorized or the response is not ok.
      • isAuthRequired

        public static boolean isAuthRequired()
        Returns:
      • singleUseClient

        public static org.apache.http.impl.client.CloseableHttpClient singleUseClient()
      • singleUseClient

        public static org.apache.http.impl.client.CloseableHttpClient singleUseClient​(java.lang.String username,
                                                                                      java.lang.String password)
        Returns a HttpClient that must be closed after use. Only a single outbound connection will created and maintained. Pre-emptive authentication not supported.
        Parameters:
        username - if null no authentication is configured
        password - if null no authentication is configured
      • singleUseClient

        public static org.apache.http.impl.client.CloseableHttpClient singleUseClient​(org.apache.http.auth.AuthScope authScope,
                                                                                      org.apache.http.client.CredentialsProvider provider,
                                                                                      java.lang.String username,
                                                                                      java.lang.String password)
        Returns a HttpClient that must be closed after use. Only a single outbound connection will created and maintained. Pre-emptive authentication not supported.
        Parameters:
        authScope -
        provider -
        username - if null no authentication is configured
        password - if null no authentication is configured
      • singleUseClient

        public static org.apache.http.impl.client.CloseableHttpClient singleUseClient​(org.apache.http.auth.AuthScope authScope,
                                                                                      org.apache.http.client.CredentialsProvider provider,
                                                                                      java.lang.String username,
                                                                                      java.lang.String password,
                                                                                      java.lang.String keyStorePath,
                                                                                      java.lang.String keyStoreType)
        Returns a HttpClient that must be closed after use. Only a single outbound connection will created and maintained. Pre-emptive authentication not supported. Note!! If keyStorePath not provided, the returned client will trust any https connection.
        Parameters:
        authScope -
        provider -
        username - if null no authentication is configured
        password - if null no authentication is configured
        keyStorePath - if null any host is trusted, otherwise an SSL context is set up for the connection
        keyStoreType - if null defaults to jks
      • getConnectionManagerTotalStats

        public static org.apache.http.pool.PoolStats getConnectionManagerTotalStats()