Class UrlUtils

java.lang.Object
net.solarnetwork.io.UrlUtils

public final class UrlUtils extends Object
Utilities for supporting URL related tasks.
Since:
1.54
Version:
2.0
Author:
matt
  • Field Details

  • Constructor Details

    • UrlUtils

      public UrlUtils()
  • Method Details

    • getInputStreamFromURLConnection

      public static 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

      public static 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

      public static URLConnection getURLConnection(String url, String httpMethod, int timeout, SSLService sslService) 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
      timeout - if greater than 0 then set as both the connection timeout and read timeout, in milliseconds
      sslService - if provided and the URL represents an HTTPS URL, then SSLService.getSSLSocketFactory() will be used as the connection's SSLSocketFactory
      Returns:
      the URLConnection
      Throws:
      IOException - if any IO error occurs
      See Also:
    • getURLConnection

      public static URLConnection getURLConnection(String url, String httpMethod, String accept, int timeout, SSLService sslService) throws IOException
      Get a URLConnection for a URL and HTTP method.

      This method is geared towards HTTP (and HTTPS) connections, but can be used for arbitrary URLs by passing null for the HTTP-specific arguments.

      If httpMethod equals PATCH, POST, or PUT 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.

      Parameters:
      url - the URL to connect to
      httpMethod - if provided and the URL is an HTTP URL, the HTTP request method to set
      accept - if provided, an HTTP Accept header value to set
      timeout - if greater than 0 then set as both the connection timeout and read timeout, in milliseconds
      sslService - if provided and the URL represents an HTTPS URL, then SSLService.getSSLSocketFactory() will be used as the connection's SSLSocketFactory
      Returns:
      the URLConnection
      Throws:
      IOException - if any IO error occurs
    • appendURLEncodedValue

      public static <T extends Appendable & CharSequence> void appendURLEncodedValue(T buf, String key, Object value)
      Append a URL-escaped key/value pair to a character buffer.
      Type Parameters:
      T - the buffer type
      Parameters:
      buf - the buffer to append to
      key - the key to append
      value - the value
    • urlEncoded

      public static String urlEncoded(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

      public static URLConnection postXWWWFormURLEncodedData(String url, String accept, Map<String,?> data, int timeout, SSLService sslService) 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
      timeout - if greater than 0 then set as both the connection timeout and read timeout, in milliseconds
      sslService - if provided and the URL represents an HTTPS URL, then SSLService.getSSLSocketFactory() will be used as the connection's SSLSocketFactory
      Returns:
      the URLConnection after the post data has been sent
      Throws:
      IOException - if any IO error occurs
      ResultStatusException - if the URL is the HTTP scheme and the HTTP response code is not within the 200 - 299 range
    • postXWWWFormURLEncodedDataForString

      public static String postXWWWFormURLEncodedDataForString(String url, String accept, Map<String,?> data, int timeout, SSLService sslService) 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.
      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
      timeout - if greater than 0 then set as both the connection timeout and read timeout, in milliseconds
      sslService - if provided and the URL represents an HTTPS URL, then SSLService.getSSLSocketFactory() will be used as the connection's SSLSocketFactory
      Returns:
      the response body as a String
      Throws:
      IOException - if any IO error occurs
      ResultStatusException - if the URL is the HTTP scheme and the HTTP response code is not within the 200 - 299 range
      See Also:
    • getURL

      public static URLConnection getURL(String url, String accept, Map<String,?> queryParameters, int timeout, SSLService sslService) throws IOException
      HTTP GET a URL.
      Parameters:
      url - the URL to post to
      accept - the value to use for the Accept HTTP header
      queryParameters - the data to encode and send as URL query parameters
      timeout - if greater than 0 then set as both the connection timeout and read timeout, in milliseconds
      sslService - if provided and the URL represents an HTTPS URL, then SSLService.getSSLSocketFactory() will be used as the connection's SSLSocketFactory
      Returns:
      the URLConnection after the post data has been sent
      Throws:
      IOException - if any IO error occurs
      ResultStatusException - if the URL is the HTTP scheme and the HTTP response code is not within the 200 - 299 range
    • getURLForString

      public static String getURLForString(String url, String accept, Map<String,?> queryParameters, int timeout, SSLService sslService) throws IOException
      HTTP GET a URL and return the response as a string.
      Parameters:
      url - the URL to post to
      accept - the value to use for the Accept HTTP header
      queryParameters - the data to encode and send as URL query parameters
      timeout - if greater than 0 then set as both the connection timeout and read timeout, in milliseconds
      sslService - if provided and the URL represents an HTTPS URL, then SSLService.getSSLSocketFactory() will be used as the connection's SSLSocketFactory
      Returns:
      the URLConnection after the post data has been sent
      Throws:
      IOException - if any IO error occurs
      ResultStatusException - if the URL is the HTTP scheme and the HTTP response code is not within the 200 - 299 range