Class HttpClientSupport

java.lang.Object
net.solarnetwork.service.support.HttpClientSupport

public class HttpClientSupport extends Object
Basic support for HTTP client actions.
Since:
1.35
Version:
1.2
Author:
matt
  • Field Details

    • ACCEPT_TEXT

      public static final String ACCEPT_TEXT
      A HTTP Accept header value for any text type.
      See Also:
    • ACCEPT_JSON

      public static final String ACCEPT_JSON
      A HTTP Accept header value for a JSON type.
      See Also:
    • DEFAULT_CONNECTION_TIMEOUT

      public static final int DEFAULT_CONNECTION_TIMEOUT
      The default value for the connectionTimeout property.
      See Also:
    • HTTP_METHOD_GET

      public static final String HTTP_METHOD_GET
      The HTTP method GET.
      See Also:
    • HTTP_METHOD_POST

      public static final String HTTP_METHOD_POST
      The HTTP method POST.
      See Also:
    • log

      protected final org.slf4j.Logger log
      A class-level logger.
  • Constructor Details

    • HttpClientSupport

      public HttpClientSupport()
  • Method Details

    • getInputStreamFromURLConnection

      protected InputStream getInputStreamFromURLConnection(URLConnection conn) throws IOException
      Get an InputStream from a URLConnection response, handling compression.

      This method handles decompressing the response if the encoding is set to gzip or deflate.

      Parameters:
      conn - the URLConnection
      Returns:
      the InputStream
      Throws:
      IOException - if any IO error occurs
    • getUnicodeReaderFromURLConnection

      protected Reader getUnicodeReaderFromURLConnection(URLConnection conn) throws IOException
      Get a Reader for a Unicode encoded URL connection response.

      This calls getInputStreamFromURLConnection(URLConnection) so compressed responses are handled appropriately.

      Parameters:
      conn - the URLConnection
      Returns:
      the Reader
      Throws:
      IOException - if an IO error occurs
    • getURLConnection

      protected URLConnection getURLConnection(String url, String httpMethod) throws IOException
      Get a URLConnection for a specific URL and HTTP method.

      This defaults to the ACCEPT_TEXT accept value.

      Parameters:
      url - the URL to connect to
      httpMethod - the HTTP method
      Returns:
      the URLConnection
      Throws:
      IOException - if any IO error occurs
      See Also:
    • getURLConnection

      protected URLConnection getURLConnection(String url, String httpMethod, String accept) throws IOException
      Get a URLConnection for a specific URL and HTTP method.

      If the httpMethod equals POST then the connection's doOutput property will be set to true, otherwise it will be set to false. The doInput property is always set to true.

      This method also sets up the request property Accept-Encoding: gzip,deflate so the response can be compressed. The getInputStreamFromURLConnection(URLConnection) automatically handles compressed responses.

      If the getSslService() property is configured and the URL represents an HTTPS connection, then that factory will be used to for the connection.

      Parameters:
      url - the URL to connect to
      httpMethod - the HTTP method
      accept - the HTTP Accept header value
      Returns:
      the URLConnection
      Throws:
      IOException - if any IO error occurs
    • appendXWWWFormURLEncodedValue

      protected void appendXWWWFormURLEncodedValue(StringBuilder buf, String key, Object value)
      Append a URL-escaped key/value pair to a string buffer.
      Parameters:
      buf - the buffer
      key - the key
      value - the value
    • xWWWFormURLEncoded

      protected String xWWWFormURLEncoded(Map<String,?> data)
      Encode a map of data into a string suitable for posting to a web server as the content type application/x-www-form-urlencoded. Arrays and Collections of values are supported as well.
      Parameters:
      data - the map of data to encode
      Returns:
      the encoded data, or an empty string if nothing to encode
    • postXWWWFormURLEncodedData

      protected URLConnection postXWWWFormURLEncodedData(String url, String accept, Map<String,?> data) throws IOException
      HTTP POST data as application/x-www-form-urlencoded (e.g. a web form) to a URL.
      Parameters:
      url - the URL to post to
      accept - the value to use for the Accept HTTP header
      data - the data to encode and send as the body of the HTTP POST
      Returns:
      the URLConnection after the post data has been sent
      Throws:
      IOException - if any IO error occurs
      RuntimeException - if the HTTP response code is not within the 200 - 299 range
    • postXWWWFormURLEncodedDataForString

      protected String postXWWWFormURLEncodedDataForString(String url, Map<String,?> data) throws IOException
      HTTP POST data as application/x-www-form-urlencoded (e.g. a web form) to a URL and return the response body as a string.

      This method accepts text and JSON responses.

      Parameters:
      url - the URL to post to
      data - the data to encode and send as the body of the HTTP POST
      Returns:
      the response body as a String
      Throws:
      IOException - if any IO error occurs
      RuntimeException - if the HTTP response code is not within the 200 - 299 range
      See Also:
    • setConnectionTimeout

      public void setConnectionTimeout(int connectionTimeout)
      Set the connection timeout.
      Parameters:
      connectionTimeout - the timeout to set, in milliseconds
    • getConnectionTimeout

      public int getConnectionTimeout()
      Get the connection timeout.
      Returns:
      the timeout, in milliseconds
    • getSslService

      public OptionalService<SSLService> getSslService()
      Get the SSL service.
      Returns:
      the service
    • setSslService

      public void setSslService(OptionalService<SSLService> sslService)
      Set the SSL service.
      Parameters:
      sslService - the service to set