Class HttpClientTools


  • public class HttpClientTools
    extends java.lang.Object
    Tools for working with HttpClient
    • Field Detail

      • DEFAULT_REQUEST_CONFIG

        public static final org.apache.http.client.config.RequestConfig DEFAULT_REQUEST_CONFIG
    • Constructor Detail

      • HttpClientTools

        public HttpClientTools()
    • Method Detail

      • createSharedCookiesHttpBuilder

        public static org.apache.http.impl.client.HttpClientBuilder createSharedCookiesHttpBuilder()
        Returns:
        An HttpClientBuilder which uses the same cookie store for all clients
      • createDefaultThreadLocalManager

        public static HttpInterfaceManager createDefaultThreadLocalManager()
        Returns:
        Default HTTP interface manager with thread-local context
      • createCookielessThreadLocalManager

        public static HttpInterfaceManager createCookielessThreadLocalManager()
        Returns:
        HTTP interface manager with thread-local context, ignores cookies
      • getRedirectLocation

        public static java.lang.String getRedirectLocation​(java.lang.String requestUrl,
                                                           org.apache.http.HttpResponse response)
        Parameters:
        requestUrl - URL of the original request.
        response - Response object.
        Returns:
        A redirect location if the status code indicates a redirect and the Location header is present.
      • isSuccessWithContent

        public static boolean isSuccessWithContent​(int statusCode)
        Parameters:
        statusCode - The status code of a response.
        Returns:
        True if this status code indicates a success with a response body
      • assertSuccessWithContent

        public static void assertSuccessWithContent​(org.apache.http.HttpResponse response,
                                                    java.lang.String context)
                                             throws java.io.IOException
        Parameters:
        response - The response.
        context - Additional string to include in exception message.
        Throws:
        java.io.IOException - if this status code indicates an error with a response body
      • assertSuccessWithRedirectContent

        public static void assertSuccessWithRedirectContent​(org.apache.http.HttpResponse response,
                                                            java.lang.String context)
                                                     throws java.io.IOException
        Parameters:
        response - The response.
        context - Additional string to include in exception message.
        Throws:
        java.io.IOException - if this status code indicates an error with a response body
      • getRawContentType

        public static java.lang.String getRawContentType​(org.apache.http.HttpResponse response)
      • hasJsonContentType

        public static boolean hasJsonContentType​(org.apache.http.HttpResponse response)
      • assertJsonContentType

        public static void assertJsonContentType​(org.apache.http.HttpResponse response)
                                          throws java.io.IOException
        Throws:
        java.io.IOException
      • isRetriableNetworkException

        public static boolean isRetriableNetworkException​(java.lang.Throwable exception)
        Parameters:
        exception - Exception to check.
        Returns:
        True if retrying to connect after receiving this exception is likely to succeed.
      • isConnectionResetException

        public static boolean isConnectionResetException​(java.lang.Throwable exception)
      • fetchResponseAsJson

        public static JsonBrowser fetchResponseAsJson​(HttpInterface httpInterface,
                                                      org.apache.http.client.methods.HttpUriRequest request)
                                               throws java.io.IOException
        Executes an HTTP request and returns the response as a JsonBrowser instance.
        Parameters:
        httpInterface - HTTP interface to use for the request.
        request - Request to perform.
        Returns:
        Response as a JsonBrowser instance. null in case of 404.
        Throws:
        java.io.IOException - On network error or for non-200 response code.
      • fetchResponseLines

        public static java.lang.String[] fetchResponseLines​(HttpInterface httpInterface,
                                                            org.apache.http.client.methods.HttpUriRequest request,
                                                            java.lang.String name)
                                                     throws java.io.IOException
        Executes an HTTP request and returns the response as an array of lines.
        Parameters:
        httpInterface - HTTP interface to use for the request.
        request - Request to perform.
        name - Name of the operation to include in exception messages.
        Returns:
        Array of lines from the response
        Throws:
        java.io.IOException - On network error or for non-200 response code.
      • getHeaderValue

        public static java.lang.String getHeaderValue​(org.apache.http.HttpResponse response,
                                                      java.lang.String name)
        Parameters:
        response - Http response to get the header value from.
        name - Name of the header.
        Returns:
        Value if header was present, null otherwise.