Class HttpUtil

java.lang.Object
org.openhab.core.io.net.http.HttpUtil

public class HttpUtil extends Object
Some common methods to be used in HTTP-In-Binding, HTTP-Out-Binding and other bindings For advanced usage direct use of the Jetty client is preferred
Author:
Kai Kreuzer - Initial contribution, Thomas Eichstaedt-Engelen - Initial contribution, Svilen Valkanov - replaced Apache HttpClient with Jetty
  • Constructor Details

    • HttpUtil

      public HttpUtil()
  • Method Details

    • executeUrl

      public static String executeUrl(String httpMethod, String url, int timeout) throws IOException
      Executes the given url with the given httpMethod. Furthermore the http.proxyXXX System variables are read and set into the HttpClient.
      Parameters:
      httpMethod - the HTTP method to use
      url - the url to execute
      timeout - the socket timeout in milliseconds to wait for data
      Returns:
      the response body or NULL when the request went wrong
      Throws:
      IOException - when the request execution failed, timed out or it was interrupted
    • executeUrl

      public static String executeUrl(String httpMethod, String url, InputStream content, String contentType, int timeout) throws IOException
      Executes the given url with the given httpMethod. Furthermore the http.proxyXXX System variables are read and set into the HttpClient.
      Parameters:
      httpMethod - the HTTP method to use
      url - the url to execute
      content - the content to be sent to the given url or null if no content should be sent.
      contentType - the content type of the given content
      timeout - the socket timeout in milliseconds to wait for data
      Returns:
      the response body or NULL when the request went wrong
      Throws:
      IOException - when the request execution failed, timed out or it was interrupted
    • executeUrl

      public static String executeUrl(String httpMethod, String url, Properties httpHeaders, InputStream content, String contentType, int timeout) throws IOException
      Executes the given url with the given httpMethod. Furthermore the http.proxyXXX System variables are read and set into the HttpClient.
      Parameters:
      httpMethod - the HTTP method to use
      url - the url to execute
      httpHeaders - optional http request headers which has to be sent within request
      content - the content to be sent to the given url or null if no content should be sent.
      contentType - the content type of the given content
      timeout - the socket timeout in milliseconds to wait for data
      Returns:
      the response body or NULL when the request went wrong
      Throws:
      IOException - when the request execution failed, timed out or it was interrupted
    • executeUrl

      public static String executeUrl(String httpMethod, String url, Properties httpHeaders, InputStream content, String contentType, int timeout, String proxyHost, Integer proxyPort, String proxyUser, String proxyPassword, String nonProxyHosts) throws IOException
      Executes the given url with the given httpMethod
      Parameters:
      httpMethod - the HTTP method to use
      url - the url to execute
      httpHeaders - optional HTTP headers which has to be set on request
      content - the content to be sent to the given url or null if no content should be sent.
      contentType - the content type of the given content
      timeout - the socket timeout in milliseconds to wait for data
      proxyHost - the hostname of the proxy
      proxyPort - the port of the proxy
      proxyUser - the username to authenticate with the proxy
      proxyPassword - the password to authenticate with the proxy
      nonProxyHosts - the hosts that won't be routed through the proxy
      Returns:
      the response body or NULL when the request went wrong
      Throws:
      IOException - when the request execution failed, timed out or it was interrupted
    • createHttpMethod

      public static org.eclipse.jetty.http.HttpMethod createHttpMethod(String httpMethodString)
      Factory method to create a HttpMethod-object according to the given String httpMethodString
      Parameters:
      httpMethodString - the name of the HttpMethod to create
      Throws:
      IllegalArgumentException - if httpMethod is none of GET, PUT, POST or DELETE
    • downloadImage

      public static RawType downloadImage(String url)
      Download the image data from a URL. If content type is not found in the headers, the data is scanned to determine the content type.
      Parameters:
      url - the URL of the image to be downloaded
      Returns:
      a RawType object containing the image, null if the content type could not be found or the content type is not an image
    • downloadImage

      public static RawType downloadImage(String url, int timeout)
      Download the image data from a URL. If content type is not found in the headers, the data is scanned to determine the content type.
      Parameters:
      url - the URL of the image to be downloaded
      timeout - the socket timeout in milliseconds to wait for data
      Returns:
      a RawType object containing the image, null if the content type could not be found or the content type is not an image
    • downloadImage

      public static RawType downloadImage(String url, boolean scanTypeInContent, long maxContentLength)
      Download the image data from a URL.
      Parameters:
      url - the URL of the image to be downloaded
      scanTypeInContent - true to allow the scan of data to determine the content type if not found in the headers
      maxContentLength - the maximum data size in bytes to trigger the download; any negative value to ignore the data size
      Returns:
      a RawType object containing the image, null if the content type could not be found or the content type is not an image or the data size is too big
    • downloadImage

      public static RawType downloadImage(String url, boolean scanTypeInContent, long maxContentLength, int timeout)
      Download the image data from a URL.
      Parameters:
      url - the URL of the image to be downloaded
      scanTypeInContent - true to allow the scan of data to determine the content type if not found in the headers
      maxContentLength - the maximum data size in bytes to trigger the download; any negative value to ignore the data size
      timeout - the socket timeout in milliseconds to wait for data
      Returns:
      a RawType object containing the image, null if the content type could not be found or the content type is not an image or the data size is too big
    • downloadData

      public static RawType downloadData(String url, String contentTypeRegex, boolean scanTypeInContent, long maxContentLength)
      Download the data from a URL.
      Parameters:
      url - the URL of the data to be downloaded
      contentTypeRegex - the REGEX the content type must match; null to ignore the content type
      scanTypeInContent - true to allow the scan of data to determine the content type if not found in the headers
      maxContentLength - the maximum data size in bytes to trigger the download; any negative value to ignore the data size
      Returns:
      a RawType object containing the downloaded data, null if the content type does not match the expected type or the data size is too big
    • downloadData

      public static RawType downloadData(String url, String contentTypeRegex, boolean scanTypeInContent, long maxContentLength, int timeout)
      Download the data from a URL.
      Parameters:
      url - the URL of the data to be downloaded
      contentTypeRegex - the REGEX the content type must match; null to ignore the content type
      scanTypeInContent - true to allow the scan of data to determine the content type if not found in the headers
      maxContentLength - the maximum data size in bytes to trigger the download; any negative value to ignore the data size
      timeout - the socket timeout in milliseconds to wait for data
      Returns:
      a RawType object containing the downloaded data, null if the content type does not match the expected type or the data size is too big
    • guessContentTypeFromData

      public static String guessContentTypeFromData(byte[] data)
      Determine the content type from the data.
      Parameters:
      data - the data as a buffer of bytes
      Returns:
      the MIME type of the content, null if the content type could not be found
    • setHttpClientFactory

      protected void setHttpClientFactory(HttpClientFactory httpClientFactory)
    • unsetHttpClientFactory

      protected void unsetHttpClientFactory(HttpClientFactory httpClientFactory)