use of org.openhab.binding.http.HttpBindingProvider in project openhab1-addons by openhab.
the class HttpBinding method formatAndExecute.
/**
* Finds the corresponding binding provider, replaces formatting markers
* in the url (@see java.util.Formatter for further information) and executes
* the formatted url.
*
* @param itemName the item context
* @param command the executed command or one of the virtual commands
* (see {@link HttpGenericBindingProvider})
* @param value the value to be used by the String.format method
*/
private void formatAndExecute(String itemName, Command command, Type value) {
HttpBindingProvider provider = findFirstMatchingBindingProvider(itemName, command);
if (provider == null) {
logger.trace("Couldn't find matching binding provider [itemName={}, command={}]", itemName, command);
return;
}
String httpMethod = provider.getHttpMethod(itemName, command);
String url = provider.getUrl(itemName, command);
if (format) {
url = String.format(url, Calendar.getInstance().getTime(), value);
}
String body = provider.getBody(itemName, command);
InputStream stream = null;
if (StringUtils.isNotBlank(httpMethod) && StringUtils.isNotBlank(url)) {
if (httpMethod.equals("POST") && StringUtils.isNotBlank(body)) {
try {
stream = IOUtils.toInputStream(body, "UTF-8");
} catch (IOException ioe) {
logger.error("Failed to convert the specified body into an acceptable input stream.", ioe);
logger.error("Body contents: {}", body);
}
logger.debug("Executing url '{}' via method {}, with body content '{}'", url, httpMethod, body);
HttpUtil.executeUrl(httpMethod, url, provider.getHttpHeaders(itemName, command), stream, "text/plain", timeout);
} else {
logger.debug("Executing url '{}' via method {}", url, httpMethod);
HttpUtil.executeUrl(httpMethod, url, provider.getHttpHeaders(itemName, command), null, null, timeout);
}
} else {
if (StringUtils.isBlank(httpMethod)) {
logger.error("The HTTP method specified was empty.");
}
if (StringUtils.isBlank(url)) {
logger.error("The URL specified was empty.");
}
}
}
use of org.openhab.binding.http.HttpBindingProvider in project openhab1-addons by openhab.
the class HttpBinding method execute.
/**
* @{inheritDoc}
*/
@Override
public void execute() {
for (HttpBindingProvider provider : providers) {
for (String itemName : provider.getInBindingItemNames()) {
String url = provider.getUrl(itemName);
if (format) {
url = String.format(url, Calendar.getInstance().getTime());
}
Properties headers = provider.getHttpHeaders(itemName);
int refreshInterval = provider.getRefreshInterval(itemName);
String transformation = provider.getTransformation(itemName);
Long lastUpdateTimeStamp = lastUpdateMap.get(itemName);
if (lastUpdateTimeStamp == null) {
lastUpdateTimeStamp = 0L;
}
long age = System.currentTimeMillis() - lastUpdateTimeStamp;
boolean needsUpdate = age >= refreshInterval;
if (needsUpdate) {
String response = null;
// cache rather than directly from server
if (isCacheConfig(url)) {
logger.debug("item '{}' is fetched from cache", itemName);
response = getCacheData(url);
} else if (isValidUrl(url)) {
logger.debug("item '{}' is about to be refreshed now", itemName);
response = HttpUtil.executeUrl("GET", url, headers, null, null, timeout);
} else {
logger.debug("item '{}' is not a valid URL or is a cache id yet to be initialised ({})", itemName, url);
continue;
}
if (response == null) {
logger.error("No response received from '{}'", url);
} else {
String transformedResponse;
try {
String[] parts = splitTransformationConfig(transformation);
String transformationType = parts[0];
String transformationFunction = parts[1];
TransformationService transformationService = TransformationHelper.getTransformationService(HttpActivator.getContext(), transformationType);
if (transformationService != null) {
transformedResponse = transformationService.transform(transformationFunction, response);
} else {
transformedResponse = response;
logger.warn("Couldn't transform response because transformationService of type '{}' is unavailable", transformationType);
}
} catch (TransformationException te) {
logger.error("Transformation '{}' threw an exception. [response={}]", transformation, response, te);
// in case of an error we return the response without any
// transformation
transformedResponse = response;
}
logger.debug("transformed response is '{}'", transformedResponse);
State state = provider.getState(itemName, transformedResponse);
if (state != null) {
eventPublisher.postUpdate(itemName, state);
} else {
logger.debug("Couldn't create state for item '{}' from string '{}'", itemName, transformedResponse);
}
}
lastUpdateMap.put(itemName, System.currentTimeMillis());
}
}
}
}
Aggregations