2 * Copyright (c) 2010-2020 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.openhabcloud.internal;
15 import java.io.IOException;
16 import java.net.MalformedURLException;
18 import java.net.URISyntaxException;
20 import java.net.URLEncoder;
21 import java.nio.ByteBuffer;
22 import java.util.Iterator;
23 import java.util.List;
26 import java.util.concurrent.ConcurrentHashMap;
27 import java.util.concurrent.TimeUnit;
29 import org.eclipse.jdt.annotation.Nullable;
30 import org.eclipse.jetty.client.HttpClient;
31 import org.eclipse.jetty.client.api.Request;
32 import org.eclipse.jetty.client.api.Request.FailureListener;
33 import org.eclipse.jetty.client.api.Response;
34 import org.eclipse.jetty.client.api.Response.ContentListener;
35 import org.eclipse.jetty.client.api.Response.HeadersListener;
36 import org.eclipse.jetty.client.api.Result;
37 import org.eclipse.jetty.client.util.BytesContentProvider;
38 import org.eclipse.jetty.http.HttpField;
39 import org.eclipse.jetty.http.HttpFields;
40 import org.eclipse.jetty.http.HttpMethod;
41 import org.eclipse.jetty.http.HttpStatus;
42 import org.eclipse.jetty.util.BufferUtil;
43 import org.eclipse.jetty.util.URIUtil;
44 import org.json.JSONException;
45 import org.json.JSONObject;
46 import org.openhab.core.OpenHAB;
47 import org.openhab.core.common.ThreadPoolManager;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
51 import io.socket.client.IO;
52 import io.socket.client.Manager;
53 import io.socket.client.Socket;
54 import io.socket.emitter.Emitter;
55 import io.socket.engineio.client.Transport;
58 * This class provides communication between openHAB and the openHAB Cloud service.
59 * It also implements async http proxy for serving requests from user to
60 * openHAB through the openHAB Cloud. It uses Socket.IO connection to connect to
61 * the openHAB Cloud service and Jetty Http client to send local http requests to
64 * @author Victor Belov - Initial contribution
65 * @author Kai Kreuzer - migrated code to new Jetty client and ESH APIs
67 public class CloudClient {
69 * Logger for this class
71 private final Logger logger = LoggerFactory.getLogger(CloudClient.class);
74 * This variable holds base URL for the openHAB Cloud connections
76 private final String baseURL;
79 * This variable holds openHAB's UUID for authenticating and connecting to the openHAB Cloud
81 private final String uuid;
84 * This variable holds openHAB's secret for authenticating and connecting to the openHAB Cloud
86 private final String secret;
89 * This variable holds local openHAB's base URL for connecting to the local openHAB instance
91 private final String localBaseUrl;
94 * This variable holds instance of Jetty HTTP client to make requests to local openHAB
96 private final HttpClient jettyClient;
99 * This map holds HTTP requests to local openHAB which are currently running
101 private final Map<Integer, Request> runningRequests = new ConcurrentHashMap<>();
104 * This variable indicates if connection to the openHAB Cloud is currently in an established state
106 private boolean isConnected;
109 * This variable holds version of local openHAB
111 private String openHABVersion;
114 * This variable holds instance of Socket.IO client class which provides communication
115 * with the openHAB Cloud
117 private Socket socket;
120 * The protocol of the openHAB-cloud URL.
122 private String protocol = "https";
125 * This variable holds instance of CloudClientListener which provides callbacks to communicate
126 * certain events from the openHAB Cloud back to openHAB
128 private CloudClientListener listener;
129 private boolean remoteAccessEnabled;
130 private Set<String> exposedItems;
133 * Constructor of CloudClient
135 * @param uuid openHAB's UUID to connect to the openHAB Cloud
136 * @param secret openHAB's Secret to connect to the openHAB Cloud
137 * @param remoteAccessEnabled Allow the openHAB Cloud to be used as a remote proxy
138 * @param exposedItems Items that are made available to apps connected to the openHAB Cloud
140 public CloudClient(HttpClient httpClient, String uuid, String secret, String baseURL, String localBaseUrl,
141 boolean remoteAccessEnabled, Set<String> exposedItems) {
143 this.secret = secret;
144 this.baseURL = baseURL;
145 this.localBaseUrl = localBaseUrl;
146 this.remoteAccessEnabled = remoteAccessEnabled;
147 this.exposedItems = exposedItems;
148 this.jettyClient = httpClient;
152 * Connect to the openHAB Cloud
155 public void connect() {
157 socket = IO.socket(baseURL);
158 URL parsed = new URL(baseURL);
159 protocol = parsed.getProtocol();
160 } catch (URISyntaxException e) {
161 logger.error("Error creating Socket.IO: {}", e.getMessage());
162 } catch (MalformedURLException e) {
163 logger.error("Error parsing baseURL to get protocol, assuming https. Error: {}", e.getMessage());
165 socket.io().on(Manager.EVENT_TRANSPORT, new Emitter.Listener() {
167 public void call(Object... args) {
168 logger.trace("Manager.EVENT_TRANSPORT");
169 Transport transport = (Transport) args[0];
170 transport.on(Transport.EVENT_REQUEST_HEADERS, new Emitter.Listener() {
172 public void call(Object... args) {
173 logger.trace("Transport.EVENT_REQUEST_HEADERS");
174 @SuppressWarnings("unchecked")
175 Map<String, List<String>> headers = (Map<String, List<String>>) args[0];
176 headers.put("uuid", List.of(uuid));
177 headers.put("secret", List.of(secret));
178 headers.put("openhabversion", List.of(OpenHAB.getVersion()));
179 headers.put("clientversion", List.of(CloudService.clientVersion));
180 headers.put("remoteaccess", List.of(((Boolean) remoteAccessEnabled).toString()));
185 socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {
187 public void call(Object... args) {
188 logger.debug("Socket.IO connected");
192 }).on(Socket.EVENT_DISCONNECT, new Emitter.Listener() {
194 public void call(Object... args) {
195 logger.debug("Socket.IO disconnected");
199 }).on(Socket.EVENT_ERROR, new Emitter.Listener() {
201 public void call(Object... args) {
202 logger.error("Error connecting to the openHAB Cloud instance: {}", args[0]);
204 }).on("request", new Emitter.Listener() {
206 public void call(Object... args) {
207 onEvent("request", (JSONObject) args[0]);
209 }).on("cancel", new Emitter.Listener() {
211 public void call(Object... args) {
212 onEvent("cancel", (JSONObject) args[0]);
214 }).on("command", new Emitter.Listener() {
217 public void call(Object... args) {
218 onEvent("command", (JSONObject) args[0]);
225 * Callback method for socket.io client which is called when connection is established
228 public void onConnect() {
229 logger.info("Connected to the openHAB Cloud service (UUID = {}, base URL = {})", this.uuid, this.localBaseUrl);
234 * Callback method for socket.io client which is called when disconnect occurs
237 public void onDisconnect() {
238 logger.info("Disconnected from the openHAB Cloud service (UUID = {}, base URL = {})", this.uuid,
241 // And clean up the list of running requests
242 runningRequests.clear();
246 * Callback method for socket.io client which is called when an error occurs
249 public void onError(IOException error) {
250 logger.debug("{}", error.getMessage());
254 * Callback method for socket.io client which is called when a message is received
257 public void onEvent(String event, JSONObject data) {
258 logger.debug("on(): {}", event);
259 if ("command".equals(event)) {
260 handleCommandEvent(data);
263 if (remoteAccessEnabled) {
264 if ("request".equals(event)) {
265 handleRequestEvent(data);
266 } else if ("cancel".equals(event)) {
267 handleCancelEvent(data);
269 logger.warn("Unsupported event from openHAB Cloud: {}", event);
274 private void handleRequestEvent(JSONObject data) {
276 // Get unique request Id
277 int requestId = data.getInt("id");
278 logger.debug("Got request {}", requestId);
280 String requestPath = data.getString("path");
281 // Get request method
282 String requestMethod = data.getString("method");
284 String requestBody = data.getString("body");
285 // Get JSONObject for request headers
286 JSONObject requestHeadersJson = data.getJSONObject("headers");
287 logger.debug("{}", requestHeadersJson.toString());
288 // Get JSONObject for request query parameters
289 JSONObject requestQueryJson = data.getJSONObject("query");
290 // Create URI builder with base request URI of openHAB and path from request
291 String newPath = URIUtil.addPaths(localBaseUrl, requestPath);
292 Iterator<String> queryIterator = requestQueryJson.keys();
293 // Add query parameters to URI builder, if any
295 while (queryIterator.hasNext()) {
296 String queryName = queryIterator.next();
297 newPath += queryName;
299 newPath += URLEncoder.encode(requestQueryJson.getString(queryName), "UTF-8");
300 if (queryIterator.hasNext()) {
304 // Finally get the future request URI
305 URI requestUri = new URI(newPath);
306 // All preparations which are common for different methods are done
307 // Now perform the request to openHAB
309 logger.debug("Request method is {}", requestMethod);
310 Request request = jettyClient.newRequest(requestUri);
311 setRequestHeaders(request, requestHeadersJson);
312 String proto = protocol;
313 if (data.has("protocol")) {
314 proto = data.getString("protocol");
316 request.header("X-Forwarded-Proto", proto);
318 if (requestMethod.equals("GET")) {
319 request.method(HttpMethod.GET);
320 } else if (requestMethod.equals("POST")) {
321 request.method(HttpMethod.POST);
322 request.content(new BytesContentProvider(requestBody.getBytes()));
323 } else if (requestMethod.equals("PUT")) {
324 request.method(HttpMethod.PUT);
325 request.content(new BytesContentProvider(requestBody.getBytes()));
327 // TODO: Reject unsupported methods
328 logger.warn("Unsupported request method {}", requestMethod);
331 ResponseListener listener = new ResponseListener(requestId);
332 request.onResponseHeaders(listener).onResponseContent(listener).onRequestFailure(listener).send(listener);
333 // If successfully submitted request to http client, add it to the list of currently
334 // running requests to be able to cancel it if needed
335 runningRequests.put(requestId, request);
336 } catch (JSONException | IOException | URISyntaxException e) {
337 logger.debug("{}", e.getMessage());
341 private void setRequestHeaders(Request request, JSONObject requestHeadersJson) {
342 Iterator<String> headersIterator = requestHeadersJson.keys();
343 // Convert JSONObject of headers into Header ArrayList
344 while (headersIterator.hasNext()) {
345 String headerName = headersIterator.next();
348 headerValue = requestHeadersJson.getString(headerName);
349 logger.debug("Jetty set header {} = {}", headerName, headerValue);
350 if (!headerName.equalsIgnoreCase("Content-Length")) {
351 request.header(headerName, headerValue);
353 } catch (JSONException e) {
354 logger.warn("Error processing request headers: {}", e.getMessage());
359 private void handleCancelEvent(JSONObject data) {
361 int requestId = data.getInt("id");
362 logger.debug("Received cancel for request {}", requestId);
363 // Find and abort running request
364 Request request = runningRequests.get(requestId);
365 if (request != null) {
366 request.abort(new InterruptedException());
367 runningRequests.remove(requestId);
369 } catch (JSONException e) {
370 logger.debug("{}", e.getMessage());
374 private void handleCommandEvent(JSONObject data) {
375 String itemName = data.getString("item");
376 if (exposedItems.contains(itemName)) {
378 logger.debug("Received command {} for item {}.", data.getString("command"), itemName);
379 if (this.listener != null) {
380 this.listener.sendCommand(itemName, data.getString("command"));
382 } catch (JSONException e) {
383 logger.debug("{}", e.getMessage());
386 logger.warn("Received command from openHAB Cloud for item '{}', which is not exposed.", itemName);
391 * This method sends notification to the openHAB Cloud
393 * @param userId openHAB Cloud user id
394 * @param message notification message text
395 * @param icon name of the icon for this notification
396 * @param severity severity name for this notification
398 public void sendNotification(String userId, String message, @Nullable String icon, @Nullable String severity) {
400 JSONObject notificationMessage = new JSONObject();
402 notificationMessage.put("userId", userId);
403 notificationMessage.put("message", message);
404 notificationMessage.put("icon", icon);
405 notificationMessage.put("severity", severity);
406 socket.emit("notification", notificationMessage);
407 } catch (JSONException e) {
408 logger.debug("{}", e.getMessage());
411 logger.debug("No connection, notification is not sent");
416 * This method sends log notification to the openHAB Cloud
418 * @param message notification message text
419 * @param icon name of the icon for this notification
420 * @param severity severity name for this notification
422 public void sendLogNotification(String message, @Nullable String icon, @Nullable String severity) {
424 JSONObject notificationMessage = new JSONObject();
426 notificationMessage.put("message", message);
427 notificationMessage.put("icon", icon);
428 notificationMessage.put("severity", severity);
429 socket.emit("lognotification", notificationMessage);
430 } catch (JSONException e) {
431 logger.debug("{}", e.getMessage());
434 logger.debug("No connection, notification is not sent");
439 * This method sends broadcast notification to the openHAB Cloud
441 * @param message notification message text
442 * @param icon name of the icon for this notification
443 * @param severity severity name for this notification
445 public void sendBroadcastNotification(String message, @Nullable String icon, @Nullable String severity) {
447 JSONObject notificationMessage = new JSONObject();
449 notificationMessage.put("message", message);
450 notificationMessage.put("icon", icon);
451 notificationMessage.put("severity", severity);
452 socket.emit("broadcastnotification", notificationMessage);
453 } catch (JSONException e) {
454 logger.debug("{}", e.getMessage());
457 logger.debug("No connection, notification is not sent");
462 * Send item update to openHAB Cloud
464 * @param itemName the name of the item
465 * @param itemState updated item state
468 public void sendItemUpdate(String itemName, String itemState) {
470 logger.debug("Sending update '{}' for item '{}'", itemState, itemName);
471 JSONObject itemUpdateMessage = new JSONObject();
473 itemUpdateMessage.put("itemName", itemName);
474 itemUpdateMessage.put("itemStatus", itemState);
475 socket.emit("itemupdate", itemUpdateMessage);
476 } catch (JSONException e) {
477 logger.debug("{}", e.getMessage());
480 logger.debug("No connection, Item update is not sent");
485 * Returns true if openHAB Cloud connection is active
487 public boolean isConnected() {
492 * Disconnect from openHAB Cloud
494 public void shutdown() {
495 logger.info("Shutting down openHAB Cloud service connection");
499 public String getOpenHABVersion() {
500 return openHABVersion;
503 public void setOpenHABVersion(String openHABVersion) {
504 this.openHABVersion = openHABVersion;
507 public void setListener(CloudClientListener listener) {
508 this.listener = listener;
512 * An internal class which forwards response headers and data back to the openHAB Cloud
514 private class ResponseListener
515 implements Response.CompleteListener, HeadersListener, ContentListener, FailureListener {
517 private static final String THREADPOOL_OPENHABCLOUD = "openhabcloud";
518 private int mRequestId;
519 private boolean mHeadersSent = false;
521 public ResponseListener(int requestId) {
522 mRequestId = requestId;
525 private JSONObject getJSONHeaders(HttpFields httpFields) {
526 JSONObject headersJSON = new JSONObject();
528 for (HttpField field : httpFields) {
529 headersJSON.put(field.getName(), field.getValue());
531 } catch (JSONException e) {
532 logger.warn("Error forming response headers: {}", e.getMessage());
538 public void onComplete(Result result) {
539 // Remove this request from list of running requests
540 runningRequests.remove(mRequestId);
541 if ((result != null && result.isFailed())
542 && (result.getResponse() != null && result.getResponse().getStatus() != HttpStatus.OK_200)) {
543 if (result.getFailure() != null) {
544 logger.warn("Jetty request {} failed: {}", mRequestId, result.getFailure().getMessage());
546 if (result.getRequestFailure() != null) {
547 logger.warn("Request Failure: {}", result.getRequestFailure().getMessage());
549 if (result.getResponseFailure() != null) {
550 logger.warn("Response Failure: {}", result.getResponseFailure().getMessage());
555 * What is this? In some cases where latency is very low the myopenhab service
556 * can receive responseFinished before the headers or content are received and I
557 * cannot find another workaround to prevent it.
559 ThreadPoolManager.getScheduledPool(THREADPOOL_OPENHABCLOUD).schedule(() -> {
560 JSONObject responseJson = new JSONObject();
562 responseJson.put("id", mRequestId);
563 socket.emit("responseFinished", responseJson);
564 logger.debug("Finished responding to request {}", mRequestId);
565 } catch (JSONException e) {
566 logger.debug("{}", e.getMessage());
568 }, 1, TimeUnit.MILLISECONDS);
572 public synchronized void onFailure(Request request, Throwable failure) {
573 JSONObject responseJson = new JSONObject();
575 responseJson.put("id", mRequestId);
576 responseJson.put("responseStatusText", "openHAB connection error: " + failure.getMessage());
577 socket.emit("responseError", responseJson);
578 } catch (JSONException e) {
579 logger.debug("{}", e.getMessage());
584 public void onContent(Response response, ByteBuffer content) {
585 logger.debug("Jetty received response content of size {}", String.valueOf(content.remaining()));
586 JSONObject responseJson = new JSONObject();
588 responseJson.put("id", mRequestId);
589 responseJson.put("body", BufferUtil.toArray(content));
590 socket.emit("responseContentBinary", responseJson);
591 logger.debug("Sent content to request {}", mRequestId);
592 } catch (JSONException e) {
593 logger.debug("{}", e.getMessage());
598 public void onHeaders(Response response) {
600 logger.debug("Jetty finished receiving response header");
601 JSONObject responseJson = new JSONObject();
604 responseJson.put("id", mRequestId);
605 responseJson.put("headers", getJSONHeaders(response.getHeaders()));
606 responseJson.put("responseStatusCode", response.getStatus());
607 responseJson.put("responseStatusText", "OK");
608 socket.emit("responseHeader", responseJson);
609 logger.debug("Sent headers to request {}", mRequestId);
610 logger.debug("{}", responseJson.toString());
611 } catch (JSONException e) {
612 logger.debug("{}", e.getMessage());