Package org.openhab.core.io.net.http
Class HttpUtil
java.lang.Object
org.openhab.core.io.net.http.HttpUtil
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 Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic org.eclipse.jetty.http.HttpMethod
createHttpMethod
(String httpMethodString) Factory method to create aHttpMethod
-object according to the given StringhttpMethodString
static RawType
downloadData
(String url, String contentTypeRegex, boolean scanTypeInContent, long maxContentLength) Download the data from a URL.static RawType
downloadData
(String url, String contentTypeRegex, boolean scanTypeInContent, long maxContentLength, int timeout) Download the data from a URL.static RawType
downloadImage
(String url) Download the image data from a URL.static RawType
downloadImage
(String url, boolean scanTypeInContent, long maxContentLength) Download the image data from a URL.static RawType
downloadImage
(String url, boolean scanTypeInContent, long maxContentLength, int timeout) Download the image data from a URL.static RawType
downloadImage
(String url, int timeout) Download the image data from a URL.static String
executeUrl
(String httpMethod, String url, int timeout) Executes the givenurl
with the givenhttpMethod
.static String
executeUrl
(String httpMethod, String url, InputStream content, String contentType, int timeout) Executes the givenurl
with the givenhttpMethod
.static String
executeUrl
(String httpMethod, String url, Properties httpHeaders, InputStream content, String contentType, int timeout) Executes the givenurl
with the givenhttpMethod
.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) Executes the givenurl
with the givenhttpMethod
static String
guessContentTypeFromData
(byte[] data) Determine the content type from the data.protected void
setHttpClientFactory
(HttpClientFactory httpClientFactory) protected void
unsetHttpClientFactory
(HttpClientFactory httpClientFactory)
-
Constructor Details
-
HttpUtil
public HttpUtil()
-
-
Method Details
-
executeUrl
Executes the givenurl
with the givenhttpMethod
. Furthermore thehttp.proxyXXX
System variables are read and set into theHttpClient
.- Parameters:
httpMethod
- the HTTP method to useurl
- the url to executetimeout
- 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 givenurl
with the givenhttpMethod
. Furthermore thehttp.proxyXXX
System variables are read and set into theHttpClient
.- Parameters:
httpMethod
- the HTTP method to useurl
- the url to executecontent
- the content to be sent to the givenurl
ornull
if no content should be sent.contentType
- the content type of the givencontent
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 givenurl
with the givenhttpMethod
. Furthermore thehttp.proxyXXX
System variables are read and set into theHttpClient
.- Parameters:
httpMethod
- the HTTP method to useurl
- the url to executehttpHeaders
- optional http request headers which has to be sent within requestcontent
- the content to be sent to the givenurl
ornull
if no content should be sent.contentType
- the content type of the givencontent
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 givenurl
with the givenhttpMethod
- Parameters:
httpMethod
- the HTTP method to useurl
- the url to executehttpHeaders
- optional HTTP headers which has to be set on requestcontent
- the content to be sent to the givenurl
ornull
if no content should be sent.contentType
- the content type of the givencontent
timeout
- the socket timeout in milliseconds to wait for dataproxyHost
- the hostname of the proxyproxyPort
- the port of the proxyproxyUser
- the username to authenticate with the proxyproxyPassword
- the password to authenticate with the proxynonProxyHosts
- 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
Factory method to create aHttpMethod
-object according to the given StringhttpMethodString
- Parameters:
httpMethodString
- the name of theHttpMethod
to create- Throws:
IllegalArgumentException
- ifhttpMethod
is none ofGET
,PUT
,POST or
DELETE
-
downloadImage
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
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 downloadedtimeout
- 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
Download the image data from a URL.- Parameters:
url
- the URL of the image to be downloadedscanTypeInContent
- true to allow the scan of data to determine the content type if not found in the headersmaxContentLength
- 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 downloadedscanTypeInContent
- true to allow the scan of data to determine the content type if not found in the headersmaxContentLength
- the maximum data size in bytes to trigger the download; any negative value to ignore the data sizetimeout
- 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 downloadedcontentTypeRegex
- the REGEX the content type must match; null to ignore the content typescanTypeInContent
- true to allow the scan of data to determine the content type if not found in the headersmaxContentLength
- 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 downloadedcontentTypeRegex
- the REGEX the content type must match; null to ignore the content typescanTypeInContent
- true to allow the scan of data to determine the content type if not found in the headersmaxContentLength
- the maximum data size in bytes to trigger the download; any negative value to ignore the data sizetimeout
- 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
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
-
unsetHttpClientFactory
-