2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.io.hueemulation.internal.automation;
17 import java.util.concurrent.ExecutionException;
18 import java.util.concurrent.TimeUnit;
19 import java.util.concurrent.TimeoutException;
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.eclipse.jetty.client.HttpClient;
24 import org.eclipse.jetty.client.api.Request;
25 import org.eclipse.jetty.client.util.StringContentProvider;
26 import org.eclipse.jetty.http.HttpMethod;
27 import org.openhab.core.automation.Action;
28 import org.openhab.core.automation.handler.ActionHandler;
29 import org.openhab.core.automation.handler.BaseModuleHandler;
30 import org.openhab.core.io.net.http.HttpClientFactory;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
35 * The action module type handled by this class allows to execute a http request (GET, POST, PUT, etc)
36 * on a given address. Relative addresses are supported.
38 * The optional mimetype and body configuration parameters allow to send arbitrary data with a request.
40 * @author David Graeff - Initial contribution
43 public class HttpActionHandler extends BaseModuleHandler<Action> implements ActionHandler {
44 private final Logger logger = LoggerFactory.getLogger(HttpActionHandler.class);
46 public static final String MODULE_TYPE_ID = "rules.HttpAction";
47 public static final String CALLBACK_CONTEXT_NAME = "CALLBACK";
48 public static final String MODULE_CONTEXT_NAME = "MODULE";
50 public static final String CFG_METHOD = "method";
51 public static final String CFG_URL = "url";
52 public static final String CFG_BODY = "body";
53 public static final String CFG_MIMETYPE = "mimetype";
54 public static final String CFG_TIMEOUT = "timeout";
56 private static class Config {
57 HttpMethod method = HttpMethod.GET;
60 String mimetype = "application/json";
64 private Config config = new Config();
65 private HttpClient httpClient;
67 public HttpActionHandler(final Action module, HttpClientFactory httpFactory) {
70 this.config = module.getConfiguration().as(Config.class);
71 if (config.url.isEmpty()) {
72 throw new IllegalArgumentException("URL not set!");
74 // convert relative path to absolute one
75 String url = config.url;
76 if (url.startsWith("/")) {
77 config.url = "http://localhost:" + Integer.getInteger("org.osgi.service.http.port", 8080).toString() + url;
80 httpClient = httpFactory.createHttpClient("HttpActionHandler_" + module.getId());
84 public @Nullable Map<String, Object> execute(Map<String, Object> context) {
86 Request request = httpClient.newRequest(URI.create(config.url)).method(config.method)
87 .timeout(config.timeout, TimeUnit.SECONDS);
88 if (config.method == HttpMethod.POST || config.method == HttpMethod.PUT) {
89 request.content(new StringContentProvider(config.body), config.mimetype);
92 } catch (InterruptedException | TimeoutException | ExecutionException e) {
93 logger.warn("Failed to send http request", e);