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.binding.hue.internal.connection;
15 import java.io.BufferedReader;
16 import java.io.ByteArrayInputStream;
17 import java.io.Closeable;
18 import java.io.IOException;
19 import java.io.InputStreamReader;
20 import java.io.Reader;
21 import java.net.InetSocketAddress;
22 import java.nio.ByteBuffer;
23 import java.nio.charset.StandardCharsets;
24 import java.time.Duration;
25 import java.time.Instant;
26 import java.util.ArrayList;
27 import java.util.Arrays;
28 import java.util.List;
30 import java.util.Objects;
31 import java.util.Optional;
32 import java.util.Properties;
33 import java.util.concurrent.CompletableFuture;
34 import java.util.concurrent.ConcurrentHashMap;
35 import java.util.concurrent.ExecutionException;
36 import java.util.concurrent.Future;
37 import java.util.concurrent.Semaphore;
38 import java.util.concurrent.TimeUnit;
39 import java.util.concurrent.TimeoutException;
40 import java.util.concurrent.locks.Lock;
41 import java.util.concurrent.locks.ReadWriteLock;
42 import java.util.concurrent.locks.ReentrantReadWriteLock;
43 import java.util.stream.Collectors;
45 import javax.ws.rs.core.MediaType;
47 import org.eclipse.jdt.annotation.NonNullByDefault;
48 import org.eclipse.jdt.annotation.Nullable;
49 import org.eclipse.jetty.client.HttpClient;
50 import org.eclipse.jetty.client.api.ContentResponse;
51 import org.eclipse.jetty.client.api.Request;
52 import org.eclipse.jetty.client.util.StringContentProvider;
53 import org.eclipse.jetty.http.HttpFields;
54 import org.eclipse.jetty.http.HttpHeader;
55 import org.eclipse.jetty.http.HttpMethod;
56 import org.eclipse.jetty.http.HttpStatus;
57 import org.eclipse.jetty.http.HttpURI;
58 import org.eclipse.jetty.http.HttpVersion;
59 import org.eclipse.jetty.http.MetaData;
60 import org.eclipse.jetty.http.MetaData.Response;
61 import org.eclipse.jetty.http2.ErrorCode;
62 import org.eclipse.jetty.http2.api.Session;
63 import org.eclipse.jetty.http2.api.Stream;
64 import org.eclipse.jetty.http2.client.HTTP2Client;
65 import org.eclipse.jetty.http2.frames.DataFrame;
66 import org.eclipse.jetty.http2.frames.GoAwayFrame;
67 import org.eclipse.jetty.http2.frames.HeadersFrame;
68 import org.eclipse.jetty.http2.frames.PingFrame;
69 import org.eclipse.jetty.http2.frames.ResetFrame;
70 import org.eclipse.jetty.util.Callback;
71 import org.eclipse.jetty.util.Promise.Completable;
72 import org.eclipse.jetty.util.ssl.SslContextFactory;
73 import org.openhab.binding.hue.internal.dto.CreateUserRequest;
74 import org.openhab.binding.hue.internal.dto.SuccessResponse;
75 import org.openhab.binding.hue.internal.dto.clip2.BridgeConfig;
76 import org.openhab.binding.hue.internal.dto.clip2.Event;
77 import org.openhab.binding.hue.internal.dto.clip2.Resource;
78 import org.openhab.binding.hue.internal.dto.clip2.ResourceReference;
79 import org.openhab.binding.hue.internal.dto.clip2.Resources;
80 import org.openhab.binding.hue.internal.dto.clip2.enums.ResourceType;
81 import org.openhab.binding.hue.internal.exceptions.ApiException;
82 import org.openhab.binding.hue.internal.exceptions.HttpUnauthorizedException;
83 import org.openhab.binding.hue.internal.handler.Clip2BridgeHandler;
84 import org.openhab.core.io.net.http.HttpClientFactory;
85 import org.openhab.core.io.net.http.HttpUtil;
86 import org.slf4j.Logger;
87 import org.slf4j.LoggerFactory;
89 import com.google.gson.Gson;
90 import com.google.gson.JsonArray;
91 import com.google.gson.JsonElement;
92 import com.google.gson.JsonParseException;
93 import com.google.gson.JsonParser;
94 import com.google.gson.JsonSyntaxException;
97 * This class handles HTTP and SSE connections to/from a Hue Bridge running CLIP 2.
99 * It uses the following connection mechanisms:
102 * <li>The primary communication uses HTTP 2 streams over a shared permanent HTTP 2 session.</li>
103 * <li>The 'registerApplicationKey()' method uses HTTP/1.1 over the OH common Jetty client.</li>
104 * <li>The 'isClip2Supported()' static method uses HTTP/1.1 over the OH common Jetty client via 'HttpUtil'.</li>
107 * @author Andrew Fiddian-Green - Initial Contribution
110 public class Clip2Bridge implements Closeable {
113 * Base (abstract) adapter for listening to HTTP 2 stream events.
115 * It implements a CompletableFuture by means of which the caller can wait for the response data to come in. And
116 * which, in the case of fatal errors, gets completed exceptionally.
118 * It handles the following fatal error events by notifying the containing class:
120 * <li>onHeaders() HTTP unauthorized codes</li>
122 private abstract class BaseStreamListenerAdapter<T> extends Stream.Listener.Adapter {
123 protected final CompletableFuture<T> completable = new CompletableFuture<T>();
124 private String contentType = "UNDEFINED";
127 protected T awaitResult() throws ExecutionException, InterruptedException, TimeoutException {
128 return completable.get(TIMEOUT_SECONDS, TimeUnit.SECONDS);
132 * Return the HTTP content type.
134 * @return content type e.g. 'application/json'
136 protected String getContentType() {
141 * Return the HTTP status code.
143 * @return status code e.g. 200
145 protected int getStatus() {
150 * Handle an HTTP2 error.
152 * @param error the type of error.
153 * @param session the session on which the error occurred.
155 protected void handleHttp2Error(Http2Error error, Session session) {
156 Http2Exception e = new Http2Exception(error);
157 if (Http2Error.UNAUTHORIZED.equals(error)) {
158 // for external error handling, abstract authorization errors into a separate exception
159 completable.completeExceptionally(new HttpUnauthorizedException("HTTP 2 request not authorized"));
161 completable.completeExceptionally(e);
163 fatalErrorDelayed(this, e, session);
167 * Check the reply headers to see whether the request was authorised.
170 public void onHeaders(@Nullable Stream stream, @Nullable HeadersFrame frame) {
171 Objects.requireNonNull(stream);
172 Objects.requireNonNull(frame);
173 MetaData metaData = frame.getMetaData();
174 if (metaData.isResponse()) {
175 Response responseMetaData = (Response) metaData;
176 contentType = responseMetaData.getFields().get(HttpHeader.CONTENT_TYPE).toLowerCase();
177 status = responseMetaData.getStatus();
179 case HttpStatus.UNAUTHORIZED_401:
180 case HttpStatus.FORBIDDEN_403:
181 handleHttp2Error(Http2Error.UNAUTHORIZED, stream.getSession());
189 * Adapter for listening to regular HTTP 2 GET/PUT request stream events.
191 * It assembles the incoming text data into an HTTP 'content' entity. And when the last data frame arrives, it
192 * returns the full content by completing the CompletableFuture with that data.
194 * In addition to those handled by the parent, it handles the following fatal error events by notifying the
197 * <li>onIdleTimeout()</li>
198 * <li>onTimeout()</li>
200 private class ContentStreamListenerAdapter extends BaseStreamListenerAdapter<String> {
201 private final DataFrameCollector content = new DataFrameCollector();
204 public void onData(@Nullable Stream stream, @Nullable DataFrame frame, @Nullable Callback callback) {
205 Objects.requireNonNull(frame);
206 Objects.requireNonNull(callback);
207 synchronized (this) {
208 content.append(frame.getData());
209 if (frame.isEndStream() && !completable.isDone()) {
210 completable.complete(content.contentAsString().trim());
214 callback.succeeded();
218 public boolean onIdleTimeout(@Nullable Stream stream, @Nullable Throwable x) {
219 Objects.requireNonNull(stream);
220 handleHttp2Error(Http2Error.IDLE, stream.getSession());
225 public void onTimeout(@Nullable Stream stream, @Nullable Throwable x) {
226 Objects.requireNonNull(stream);
227 handleHttp2Error(Http2Error.TIMEOUT, stream.getSession());
232 * Class to collect incoming ByteBuffer data from HTTP 2 Data frames.
234 private static class DataFrameCollector {
235 private byte[] buffer = new byte[512];
236 private int usedSize = 0;
238 public void append(ByteBuffer data) {
239 int dataCapacity = data.capacity();
240 int neededSize = usedSize + dataCapacity;
241 if (neededSize > buffer.length) {
242 int newSize = (dataCapacity < 4096) ? neededSize : Math.max(2 * buffer.length, neededSize);
243 buffer = Arrays.copyOf(buffer, newSize);
245 data.get(buffer, usedSize, dataCapacity);
246 usedSize += dataCapacity;
249 public String contentAsString() {
250 return new String(buffer, 0, usedSize, StandardCharsets.UTF_8);
253 public Reader contentStreamReader() {
254 return new InputStreamReader(new ByteArrayInputStream(buffer, 0, usedSize), StandardCharsets.UTF_8);
257 public void reset() {
263 * Adapter for listening to SSE event stream events.
265 * It receives the incoming text lines. Receipt of the first data line causes the CompletableFuture to complete. It
266 * then parses subsequent data according to the SSE specification. If the line starts with a 'data:' message, it
267 * adds the data to the list of strings. And if the line is empty (i.e. the last line of an event), it passes the
268 * full set of strings to the owner via a call-back method.
270 * The stream must be permanently connected, so it ignores onIdleTimeout() events.
272 * The parent class handles most fatal errors, but since the event stream is supposed to be permanently connected,
273 * the following events are also considered as fatal:
275 * <li>onClosed()</li>
278 private class EventStreamListenerAdapter extends BaseStreamListenerAdapter<Boolean> {
279 private final DataFrameCollector eventData = new DataFrameCollector();
282 public void onClosed(@Nullable Stream stream) {
283 Objects.requireNonNull(stream);
284 handleHttp2Error(Http2Error.CLOSED, stream.getSession());
288 public void onData(@Nullable Stream stream, @Nullable DataFrame frame, @Nullable Callback callback) {
289 Objects.requireNonNull(frame);
290 Objects.requireNonNull(callback);
291 synchronized (this) {
292 eventData.append(frame.getData());
293 BufferedReader reader = new BufferedReader(eventData.contentStreamReader());
294 @SuppressWarnings("null")
295 List<String> receivedLines = reader.lines().collect(Collectors.toList());
297 // a blank line marks the end of an SSE message
298 boolean endOfMessage = !receivedLines.isEmpty()
299 && receivedLines.get(receivedLines.size() - 1).isBlank();
303 // receipt of ANY message means the event stream is established
304 if (!completable.isDone()) {
305 completable.complete(Boolean.TRUE);
307 // append any 'data' field values to the event message
308 StringBuilder eventContent = new StringBuilder();
309 for (String receivedLine : receivedLines) {
310 if (receivedLine.startsWith("data:")) {
311 eventContent.append(receivedLine.substring(5).stripLeading());
314 if (eventContent.length() > 0) {
315 onEventData(eventContent.toString().trim());
319 callback.succeeded();
323 public boolean onIdleTimeout(@Nullable Stream stream, @Nullable Throwable x) {
328 public void onReset(@Nullable Stream stream, @Nullable ResetFrame frame) {
329 Objects.requireNonNull(stream);
330 handleHttp2Error(Http2Error.RESET, stream.getSession());
335 * Enum of potential fatal HTTP 2 session/stream errors.
337 private enum Http2Error {
348 * Private exception for handling HTTP 2 stream and session errors.
350 @SuppressWarnings("serial")
351 private static class Http2Exception extends ApiException {
352 public final Http2Error error;
354 public Http2Exception(Http2Error error) {
358 public Http2Exception(Http2Error error, @Nullable Throwable cause) {
359 super("HTTP 2 stream " + error.toString().toLowerCase(), cause);
365 * Adapter for listening to HTTP 2 session status events.
367 * The session must be permanently connected, so it ignores onIdleTimeout() events.
368 * It also handles the following fatal events by notifying the containing class:
371 * <li>onFailure()</li>
372 * <li>onGoAway()</li>
375 private class SessionListenerAdapter extends Session.Listener.Adapter {
378 public void onClose(@Nullable Session session, @Nullable GoAwayFrame frame) {
379 Objects.requireNonNull(session);
380 fatalErrorDelayed(this, new Http2Exception(Http2Error.CLOSED), session);
384 public void onFailure(@Nullable Session session, @Nullable Throwable failure) {
385 Objects.requireNonNull(session);
386 fatalErrorDelayed(this, new Http2Exception(Http2Error.FAILURE), session);
390 * The Hue bridge uses the 'nginx' web server which sends HTTP2 GO_AWAY frames after a certain number (normally
391 * 999) of GET/PUT calls. This is normal behaviour so we just start a new thread to close and reopen the
395 public void onGoAway(@Nullable Session session, @Nullable GoAwayFrame frame) {
396 Objects.requireNonNull(session);
397 if (http2Session == session) {
398 Thread recreateThread = new Thread(() -> recreateSession());
399 Clip2Bridge.this.recreateThread = recreateThread;
400 recreateThread.start();
405 public boolean onIdleTimeout(@Nullable Session session) {
410 public void onPing(@Nullable Session session, @Nullable PingFrame frame) {
411 Objects.requireNonNull(session);
412 Objects.requireNonNull(frame);
413 if (http2Session == session) {
415 if (!frame.isReply()) {
416 session.ping(new PingFrame(true), Callback.NOOP);
422 public void onReset(@Nullable Session session, @Nullable ResetFrame frame) {
423 Objects.requireNonNull(session);
424 fatalErrorDelayed(this, new Http2Exception(Http2Error.RESET), session);
429 * Synchronizer for accessing the HTTP2 session object. This method wraps the 'sessionUseCreateLock' ReadWriteLock
430 * so that GET/PUT methods can access the session on multiple concurrent threads via the 'read' access lock, yet are
431 * forced to wait if the session is being created via its single thread access 'write' lock.
433 private class SessionSynchronizer implements AutoCloseable {
434 private final Optional<Lock> lockOptional;
436 SessionSynchronizer(boolean requireExclusiveAccess) throws InterruptedException {
437 Lock lock = requireExclusiveAccess ? sessionUseCreateLock.writeLock() : sessionUseCreateLock.readLock();
438 lockOptional = lock.tryLock(TIMEOUT_SECONDS, TimeUnit.SECONDS) ? Optional.of(lock) : Optional.empty();
442 public void close() {
443 lockOptional.ifPresent(lock -> lock.unlock());
448 * Enum showing the online state of the session connection.
456 * Session open for HTTP calls only
460 * Session open for HTTP calls and actively receiving SSE events
466 * Class for throttling HTTP GET and PUT requests to prevent overloading the Hue bridge.
468 * The Hue Bridge can get confused if they receive too many HTTP requests in a short period of time (e.g. on start
469 * up), or if too many HTTP sessions are opened at the same time, which cause it to respond with an HTML error page.
470 * So this class a) waits to acquire permitCount (or no more than MAX_CONCURRENT_SESSIONS) stream permits, and b)
471 * throttles the requests to a maximum of one per REQUEST_INTERVAL_MILLISECS.
473 private class Throttler implements AutoCloseable {
474 private final int permitCount;
477 * @param permitCount indicates how many stream permits to be acquired.
478 * @throws InterruptedException
480 Throttler(int permitCount) throws InterruptedException {
481 this.permitCount = permitCount;
482 streamMutex.acquire(permitCount);
484 synchronized (Clip2Bridge.this) {
485 Instant now = Instant.now();
486 delay = lastRequestTime
487 .map(t -> Math.max(0, Duration.between(now, t).toMillis() + REQUEST_INTERVAL_MILLISECS))
489 lastRequestTime = Optional.of(now.plusMillis(delay));
495 public void close() {
496 streamMutex.release(permitCount);
500 private static final Logger LOGGER = LoggerFactory.getLogger(Clip2Bridge.class);
502 private static final String APPLICATION_ID = "org-openhab-binding-hue-clip2";
503 private static final String APPLICATION_KEY = "hue-application-key";
505 private static final String EVENT_STREAM_ID = "eventStream";
506 private static final String FORMAT_URL_CONFIG = "http://%s/api/0/config";
507 private static final String FORMAT_URL_RESOURCE = "https://%s/clip/v2/resource/";
508 private static final String FORMAT_URL_REGISTER = "http://%s/api";
509 private static final String FORMAT_URL_EVENTS = "https://%s/eventstream/clip/v2";
511 private static final long CLIP2_MINIMUM_VERSION = 1948086000L;
513 public static final int TIMEOUT_SECONDS = 10;
514 private static final int CHECK_ALIVE_SECONDS = 300;
515 private static final int REQUEST_INTERVAL_MILLISECS = 50;
516 private static final int MAX_CONCURRENT_STREAMS = 3;
518 private static final ResourceReference BRIDGE = new ResourceReference().setType(ResourceType.BRIDGE);
521 * Static method to attempt to connect to a Hue Bridge, get its software version, and check if it is high enough to
522 * support the CLIP 2 API.
524 * @param hostName the bridge IP address.
525 * @return true if bridge is online and it supports CLIP 2, or false if it is online and does not support CLIP 2.
526 * @throws IOException if unable to communicate with the bridge.
527 * @throws NumberFormatException if the bridge firmware version is invalid.
529 public static boolean isClip2Supported(String hostName) throws IOException {
531 Properties headers = new Properties();
532 headers.put(HttpHeader.ACCEPT, MediaType.APPLICATION_JSON);
533 response = HttpUtil.executeUrl("GET", String.format(FORMAT_URL_CONFIG, hostName), headers, null, null,
534 TIMEOUT_SECONDS * 1000);
535 BridgeConfig config = new Gson().fromJson(response, BridgeConfig.class);
536 if (Objects.nonNull(config)) {
537 String swVersion = config.swversion;
538 if (Objects.nonNull(swVersion)) {
540 if (Long.parseLong(swVersion) >= CLIP2_MINIMUM_VERSION) {
543 } catch (NumberFormatException e) {
544 LOGGER.debug("isClip2Supported() swVersion '{}' is not a number", swVersion);
551 private final HttpClient httpClient;
552 private final HTTP2Client http2Client;
553 private final String hostName;
554 private final String baseUrl;
555 private final String eventUrl;
556 private final String registrationUrl;
557 private final String applicationKey;
558 private final Clip2BridgeHandler bridgeHandler;
559 private final Gson jsonParser = new Gson();
560 private final Semaphore streamMutex = new Semaphore(MAX_CONCURRENT_STREAMS, true); // i.e. fair
561 private final ReadWriteLock sessionUseCreateLock = new ReentrantReadWriteLock(true); // i.e. fair
562 private final Map<Integer, Future<?>> fatalErrorTasks = new ConcurrentHashMap<>();
564 private boolean recreatingSession;
565 private boolean closing;
566 private State onlineState = State.CLOSED;
567 private Optional<Instant> lastRequestTime = Optional.empty();
568 private Instant sessionExpireTime = Instant.MAX;
570 private @Nullable Session http2Session;
571 private @Nullable Thread recreateThread;
572 private @Nullable Future<?> checkAliveTask;
577 * @param httpClientFactory the OH core HttpClientFactory.
578 * @param bridgeHandler the bridge handler.
579 * @param hostName the host name (ip address) of the Hue bridge
580 * @param applicationKey the application key.
581 * @throws ApiException if unable to open Jetty HTTP/2 client.
583 public Clip2Bridge(HttpClientFactory httpClientFactory, Clip2BridgeHandler bridgeHandler, String hostName,
584 String applicationKey) throws ApiException {
585 LOGGER.debug("Clip2Bridge()");
586 httpClient = httpClientFactory.getCommonHttpClient();
587 http2Client = httpClientFactory.createHttp2Client("hue-clip2", httpClient.getSslContextFactory());
588 http2Client.setConnectTimeout(Clip2Bridge.TIMEOUT_SECONDS * 1000);
589 http2Client.setIdleTimeout(-1);
591 this.bridgeHandler = bridgeHandler;
592 this.hostName = hostName;
593 this.applicationKey = applicationKey;
594 baseUrl = String.format(FORMAT_URL_RESOURCE, hostName);
595 eventUrl = String.format(FORMAT_URL_EVENTS, hostName);
596 registrationUrl = String.format(FORMAT_URL_REGISTER, hostName);
600 * Cancel the given task.
602 * @param cancelTask the task to be cancelled (may be null)
603 * @param mayInterrupt allows cancel() to interrupt the thread.
605 private void cancelTask(@Nullable Future<?> cancelTask, boolean mayInterrupt) {
606 if (Objects.nonNull(cancelTask)) {
607 cancelTask.cancel(mayInterrupt);
612 * Send a ping to the Hue bridge to check that the session is still alive.
614 private void checkAlive() {
615 if (onlineState == State.CLOSED) {
618 LOGGER.debug("checkAlive()");
619 Session session = http2Session;
620 if (Objects.nonNull(session)) {
621 session.ping(new PingFrame(false), Callback.NOOP);
622 if (Instant.now().isAfter(sessionExpireTime)) {
623 fatalError(this, new Http2Exception(Http2Error.TIMEOUT), session.hashCode());
629 * Connection is ok, so reschedule the session check alive expire time. Called in response to incoming ping frames
632 protected void checkAliveOk() {
633 LOGGER.debug("checkAliveOk()");
634 sessionExpireTime = Instant.now().plusSeconds(CHECK_ALIVE_SECONDS * 2);
638 * Close the connection.
641 public void close() {
643 Thread recreateThread = this.recreateThread;
644 if (Objects.nonNull(recreateThread) && recreateThread.isAlive()) {
645 recreateThread.interrupt();
650 } catch (ApiException e) {
655 * Private method to close the connection.
657 private void close2() {
658 synchronized (this) {
659 LOGGER.debug("close2()");
660 boolean notifyHandler = onlineState == State.ACTIVE && !closing && !recreatingSession;
661 onlineState = State.CLOSED;
662 synchronized (fatalErrorTasks) {
663 fatalErrorTasks.values().forEach(task -> cancelTask(task, true));
664 fatalErrorTasks.clear();
666 cancelTask(checkAliveTask, true);
667 checkAliveTask = null;
671 bridgeHandler.onConnectionOffline();
677 * Close the event stream(s) if necessary.
679 private void closeEventStream() {
680 Session session = http2Session;
681 if (Objects.nonNull(session)) {
682 final int sessionId = session.hashCode();
683 session.getStreams().stream().filter(s -> Objects.nonNull(s.getAttribute(EVENT_STREAM_ID)) && !s.isReset())
685 int streamId = s.getId();
686 LOGGER.debug("closeEventStream() sessionId:{}, streamId:{}", sessionId, streamId);
687 s.reset(new ResetFrame(streamId, ErrorCode.CANCEL_STREAM_ERROR.code), Callback.NOOP);
693 * Close the HTTP 2 session if necessary.
695 private void closeSession() {
696 Session session = http2Session;
697 if (Objects.nonNull(session)) {
698 LOGGER.debug("closeSession() sessionId:{}, openStreamCount:{}", session.hashCode(),
699 session.getStreams().size());
700 session.close(ErrorCode.NO_ERROR.code, "closeSession", Callback.NOOP);
706 * Close the given stream.
708 * @param stream to be closed.
710 private void closeStream(@Nullable Stream stream) {
711 if (Objects.nonNull(stream) && !stream.isReset()) {
712 stream.reset(new ResetFrame(stream.getId(), ErrorCode.NO_ERROR.code), Callback.NOOP);
717 * Method that is called back in case of fatal stream or session events. The error is only processed if the
718 * connection is online, not in process of closing, and the identities of the current session and the session that
719 * caused the error are the same. In other words it ignores errors relating to expired sessions.
721 * @param listener the entity that caused this method to be called.
722 * @param cause the type of exception that caused the error.
723 * @param sessionId the identity of the session on which the error occurred.
725 private synchronized void fatalError(Object listener, Http2Exception cause, int sessionId) {
726 if (onlineState == State.CLOSED || closing) {
729 Session session = http2Session;
730 if (Objects.isNull(session) || session.hashCode() != sessionId) {
733 String listenerId = listener.getClass().getSimpleName();
734 if (listener instanceof ContentStreamListenerAdapter) {
735 // on GET / PUT requests the caller handles errors and closes the stream; the session is still OK
736 LOGGER.debug("fatalError() listener:{}, sessionId:{}, error:{} => ignoring", listenerId, sessionId,
739 if (LOGGER.isDebugEnabled()) {
740 LOGGER.debug("fatalError() listener:{}, sessionId:{}, error:{} => closing", listenerId, sessionId,
743 LOGGER.warn("Fatal error '{}' from '{}' => closing session.", cause.error, listenerId);
750 * Method that is called back in case of fatal stream or session events. Schedules fatalError() to be called after a
751 * delay in order to prevent sequencing issues.
753 * @param listener the entity that caused this method to be called.
754 * @param cause the type of exception that caused the error.
755 * @param session the session on which the error occurred.
757 protected void fatalErrorDelayed(Object listener, Http2Exception cause, Session session) {
758 synchronized (fatalErrorTasks) {
759 final int index = fatalErrorTasks.size();
760 final int sessionId = session.hashCode();
761 fatalErrorTasks.put(index, bridgeHandler.getScheduler().schedule(() -> {
762 fatalError(listener, cause, sessionId);
763 fatalErrorTasks.remove(index);
764 }, 1, TimeUnit.SECONDS));
769 * HTTP GET a Resources object, for a given resource Reference, from the Hue Bridge. The reference is a class
770 * comprising a resource type and an id. If the id is a specific resource id then only the one specific resource
771 * is returned, whereas if it is null then all resources of the given resource type are returned.
773 * It wraps the getResourcesImpl() method in a try/catch block, and transposes any HttpUnAuthorizedException into an
774 * ApiException. Such transposition should never be required in reality since by the time this method is called, the
775 * connection will surely already have been authorised.
777 * @param reference the Reference class to get.
778 * @return a Resource object containing either a list of Resources or a list of Errors.
779 * @throws ApiException if anything fails.
780 * @throws InterruptedException
782 public Resources getResources(ResourceReference reference) throws ApiException, InterruptedException {
783 if (onlineState == State.CLOSED && !recreatingSession) {
784 throw new ApiException("Connection is closed");
786 return getResourcesImpl(reference);
790 * Internal method to send an HTTP 2 GET request to the Hue Bridge and process its response. Uses a Throttler to
791 * prevent too many concurrent calls, and to prevent too frequent calls on the Hue bridge server. Also uses a
792 * SessionSynchronizer to delay accessing the session while it is being recreated.
794 * @param reference the Reference class to get.
795 * @return a Resource object containing either a list of Resources or a list of Errors.
796 * @throws HttpUnauthorizedException if the request was refused as not authorised or forbidden.
797 * @throws ApiException if the communication failed, or an unexpected result occurred.
798 * @throws InterruptedException
800 private Resources getResourcesImpl(ResourceReference reference)
801 throws HttpUnauthorizedException, ApiException, InterruptedException {
802 // work around for issue #15468 (and similar)
803 ResourceType resourceType = reference.getType();
804 if (resourceType == ResourceType.ERROR) {
805 LOGGER.warn("Resource '{}' type '{}' unknown => GET aborted", reference.getId(), resourceType);
806 return new Resources();
808 Stream stream = null;
809 try (Throttler throttler = new Throttler(1);
810 SessionSynchronizer sessionSynchronizer = new SessionSynchronizer(false)) {
811 Session session = getSession();
812 String url = getUrl(reference);
813 LOGGER.trace("GET {} HTTP/2", url);
814 HeadersFrame headers = prepareHeaders(url, MediaType.APPLICATION_JSON);
815 Completable<@Nullable Stream> streamPromise = new Completable<>();
816 ContentStreamListenerAdapter contentStreamListener = new ContentStreamListenerAdapter();
817 session.newStream(headers, streamPromise, contentStreamListener);
818 // wait for stream to be opened
819 stream = Objects.requireNonNull(streamPromise.get(TIMEOUT_SECONDS, TimeUnit.SECONDS));
820 // wait for HTTP response contents
821 String contentJson = contentStreamListener.awaitResult();
822 String contentType = contentStreamListener.getContentType();
823 int status = contentStreamListener.getStatus();
824 LOGGER.trace("HTTP/2 {} (Content-Type: {}) << {}", status, contentType, contentJson);
825 if (status != HttpStatus.OK_200) {
826 throw new ApiException(String.format("Unexpected HTTP status '%d'", status));
828 if (!MediaType.APPLICATION_JSON.equals(contentType)) {
829 throw new ApiException("Unexpected Content-Type: " + contentType);
832 Resources resources = Objects.requireNonNull(jsonParser.fromJson(contentJson, Resources.class));
833 if (LOGGER.isDebugEnabled()) {
834 resources.getErrors().forEach(error -> LOGGER.debug("Resources error:{}", error));
837 } catch (JsonParseException e) {
838 throw new ApiException("Parsing error", e);
840 } catch (ExecutionException e) {
841 Throwable cause = e.getCause();
842 if (cause instanceof HttpUnauthorizedException) {
843 throw (HttpUnauthorizedException) cause;
845 throw new ApiException("Error sending request", e);
846 } catch (TimeoutException e) {
847 throw new ApiException("Error sending request", e);
854 * Safe access to the session object.
856 * @return the session.
857 * @throws ApiException if session is null or closed.
859 private Session getSession() throws ApiException {
860 Session session = http2Session;
861 if (Objects.isNull(session) || session.isClosed()) {
862 throw new ApiException("HTTP/2 session is null or closed");
868 * Build a full path to a server end point, based on a Reference class instance. If the reference contains only
869 * a resource type, the method returns the end point url to get all resources of the given resource type. Whereas if
870 * it also contains an id, the method returns the end point url to get the specific single resource with that type
873 * @param reference a Reference class instance.
874 * @return the complete end point url.
876 private String getUrl(ResourceReference reference) {
877 String url = baseUrl + reference.getType().name().toLowerCase();
878 String id = reference.getId();
879 return Objects.isNull(id) || id.isEmpty() ? url : url + "/" + id;
883 * The event stream calls this method when it has received text data. It parses the text as JSON into a list of
884 * Event entries, converts the list of events to a list of resources, and forwards that list to the bridge
887 * @param data the incoming (presumed to be JSON) text.
889 protected void onEventData(String data) {
890 if (onlineState != State.ACTIVE && !recreatingSession) {
893 if (LOGGER.isTraceEnabled()) {
894 LOGGER.trace("onEventData() data:{}", data);
896 LOGGER.debug("onEventData() data length:{}", data.length());
898 JsonElement jsonElement;
900 jsonElement = JsonParser.parseString(data);
901 } catch (JsonSyntaxException e) {
902 LOGGER.debug("onEventData() invalid data '{}'", data, e);
905 if (!(jsonElement instanceof JsonArray)) {
906 LOGGER.debug("onEventData() data is not a JsonArray {}", data);
911 events = jsonParser.fromJson(jsonElement, Event.EVENT_LIST_TYPE);
912 } catch (JsonParseException e) {
913 LOGGER.debug("onEventData() parsing error json:{}", data, e);
916 if (Objects.isNull(events) || events.isEmpty()) {
917 LOGGER.debug("onEventData() event list is null or empty");
920 List<Resource> resources = new ArrayList<>();
921 events.forEach(event -> resources.addAll(event.getData()));
922 if (resources.isEmpty()) {
923 LOGGER.debug("onEventData() resource list is empty");
926 resources.forEach(resource -> resource.markAsSparse());
927 bridgeHandler.onResourcesEvent(resources);
931 * Open the HTTP 2 session and the event stream.
933 * @throws ApiException if there was a communication error.
934 * @throws InterruptedException
936 public void open() throws ApiException, InterruptedException {
937 LOGGER.debug("open()");
940 bridgeHandler.onConnectionOnline();
944 * Make the session active, by opening an HTTP 2 SSE event stream (if necessary).
946 * @throws ApiException if an error was encountered.
947 * @throws InterruptedException
949 private void openActive() throws ApiException, InterruptedException {
950 synchronized (this) {
952 onlineState = State.ACTIVE;
957 * Open the check alive task if necessary.
959 private void openCheckAliveTask() {
960 Future<?> task = checkAliveTask;
961 if (Objects.isNull(task) || task.isCancelled() || task.isDone()) {
962 LOGGER.debug("openCheckAliveTask()");
963 cancelTask(checkAliveTask, false);
964 checkAliveTask = bridgeHandler.getScheduler().scheduleWithFixedDelay(() -> checkAlive(),
965 CHECK_ALIVE_SECONDS, CHECK_ALIVE_SECONDS, TimeUnit.SECONDS);
970 * Implementation to open an HTTP 2 SSE event stream if necessary.
972 * @throws ApiException if an error was encountered.
973 * @throws InterruptedException
975 private void openEventStream() throws ApiException, InterruptedException {
976 Session session = getSession();
977 if (session.getStreams().stream().anyMatch(stream -> Objects.nonNull(stream.getAttribute(EVENT_STREAM_ID)))) {
980 LOGGER.trace("GET {} HTTP/2", eventUrl);
981 Stream stream = null;
983 HeadersFrame headers = prepareHeaders(eventUrl, MediaType.SERVER_SENT_EVENTS);
984 Completable<@Nullable Stream> streamPromise = new Completable<>();
985 EventStreamListenerAdapter eventStreamListener = new EventStreamListenerAdapter();
986 session.newStream(headers, streamPromise, eventStreamListener);
987 // wait for stream to be opened
988 stream = Objects.requireNonNull(streamPromise.get(TIMEOUT_SECONDS, TimeUnit.SECONDS));
989 stream.setIdleTimeout(0);
990 stream.setAttribute(EVENT_STREAM_ID, session);
991 // wait for "hi" from the bridge
992 eventStreamListener.awaitResult();
993 LOGGER.debug("openEventStream() sessionId:{} streamId:{}", session.hashCode(), stream.getId());
994 } catch (ExecutionException | TimeoutException e) {
995 if (Objects.nonNull(stream) && !stream.isReset()) {
996 stream.reset(new ResetFrame(stream.getId(), ErrorCode.HTTP_CONNECT_ERROR.code), Callback.NOOP);
998 throw new ApiException("Error opening event stream", e);
1003 * Private method to open the HTTP 2 session in passive mode.
1005 * @throws ApiException if there was a communication error.
1006 * @throws InterruptedException
1008 private void openPassive() throws ApiException, InterruptedException {
1009 synchronized (this) {
1010 LOGGER.debug("openPassive()");
1011 onlineState = State.CLOSED;
1013 openCheckAliveTask();
1014 onlineState = State.PASSIVE;
1019 * Open the HTTP 2 session if necessary.
1021 * @throws ApiException if it was not possible to create and connect the session.
1022 * @throws InterruptedException
1024 private void openSession() throws ApiException, InterruptedException {
1025 Session session = http2Session;
1026 if (Objects.nonNull(session) && !session.isClosed()) {
1030 InetSocketAddress address = new InetSocketAddress(hostName, 443);
1031 SessionListenerAdapter sessionListener = new SessionListenerAdapter();
1032 Completable<@Nullable Session> sessionPromise = new Completable<>();
1033 http2Client.connect(http2Client.getBean(SslContextFactory.class), address, sessionListener, sessionPromise);
1034 // wait for the (SSL) session to be opened
1035 session = Objects.requireNonNull(sessionPromise.get(TIMEOUT_SECONDS, TimeUnit.SECONDS));
1036 LOGGER.debug("openSession() sessionId:{}", session.hashCode());
1037 http2Session = session;
1038 checkAliveOk(); // initialise the session timeout window
1039 } catch (ExecutionException | TimeoutException e) {
1040 throw new ApiException("Error opening HTTP/2 session", e);
1045 * Helper class to create a HeadersFrame for a standard HTTP GET request.
1047 * @param url the server url.
1048 * @param acceptContentType the accepted content type for the response.
1049 * @return the HeadersFrame.
1051 private HeadersFrame prepareHeaders(String url, String acceptContentType) {
1052 return prepareHeaders(url, acceptContentType, "GET", -1, null);
1056 * Helper class to create a HeadersFrame for a more exotic HTTP request.
1058 * @param url the server url.
1059 * @param acceptContentType the accepted content type for the response.
1060 * @param method the HTTP request method.
1061 * @param contentLength the length of the content e.g. for a PUT call.
1062 * @param contentType the respective content type.
1063 * @return the HeadersFrame.
1065 private HeadersFrame prepareHeaders(String url, String acceptContentType, String method, long contentLength,
1066 @Nullable String contentType) {
1067 HttpFields fields = new HttpFields();
1068 fields.put(HttpHeader.ACCEPT, acceptContentType);
1069 if (contentType != null) {
1070 fields.put(HttpHeader.CONTENT_TYPE, contentType);
1072 if (contentLength >= 0) {
1073 fields.putLongField(HttpHeader.CONTENT_LENGTH, contentLength);
1075 fields.put(APPLICATION_KEY, applicationKey);
1076 return new HeadersFrame(new MetaData.Request(method, new HttpURI(url), HttpVersion.HTTP_2, fields), null,
1077 contentLength <= 0);
1081 * Use an HTTP/2 PUT command to send a resource to the server. Uses a Throttler to prevent too many concurrent
1082 * calls, and to prevent too frequent calls on the Hue bridge server. Also uses a SessionSynchronizer to delay
1083 * accessing the session while it is being recreated.
1085 * @param resource the resource to put.
1086 * @return the resource, which may contain errors.
1087 * @throws ApiException if something fails.
1088 * @throws InterruptedException
1090 public Resources putResource(Resource resource) throws ApiException, InterruptedException {
1091 Stream stream = null;
1092 try (Throttler throttler = new Throttler(MAX_CONCURRENT_STREAMS);
1093 SessionSynchronizer sessionSynchronizer = new SessionSynchronizer(false)) {
1094 Session session = getSession();
1095 String requestJson = jsonParser.toJson(resource);
1096 ByteBuffer requestBytes = ByteBuffer.wrap(requestJson.getBytes(StandardCharsets.UTF_8));
1097 String url = getUrl(new ResourceReference().setId(resource.getId()).setType(resource.getType()));
1098 HeadersFrame headers = prepareHeaders(url, MediaType.APPLICATION_JSON, "PUT", requestBytes.capacity(),
1099 MediaType.APPLICATION_JSON);
1100 LOGGER.trace("PUT {} HTTP/2 >> {}", url, requestJson);
1101 Completable<@Nullable Stream> streamPromise = new Completable<>();
1102 ContentStreamListenerAdapter contentStreamListener = new ContentStreamListenerAdapter();
1103 session.newStream(headers, streamPromise, contentStreamListener);
1104 // wait for stream to be opened
1105 stream = Objects.requireNonNull(streamPromise.get(TIMEOUT_SECONDS, TimeUnit.SECONDS));
1106 stream.data(new DataFrame(stream.getId(), requestBytes, true), Callback.NOOP);
1107 // wait for HTTP response
1108 String contentJson = contentStreamListener.awaitResult();
1109 String contentType = contentStreamListener.getContentType();
1110 int status = contentStreamListener.getStatus();
1111 LOGGER.trace("HTTP/2 {} (Content-Type: {}) << {}", status, contentType, contentJson);
1112 if (!HttpStatus.isSuccess(status)) {
1113 throw new ApiException(String.format("Unexpected HTTP status '%d'", status));
1115 if (!MediaType.APPLICATION_JSON.equals(contentType)) {
1116 throw new ApiException("Unexpected Content-Type: " + contentType);
1118 if (contentJson.isEmpty()) {
1119 throw new ApiException("Response payload is empty");
1122 return Objects.requireNonNull(jsonParser.fromJson(contentJson, Resources.class));
1123 } catch (JsonParseException e) {
1124 LOGGER.debug("putResource() parsing error json:{}", contentJson, e);
1125 throw new ApiException("Parsing error", e);
1127 } catch (ExecutionException | TimeoutException e) {
1128 throw new ApiException("Error sending PUT request", e);
1130 closeStream(stream);
1135 * Close and re-open the session. Called when the server sends a GO_AWAY message. Acquires a SessionSynchronizer
1136 * 'write' lock to ensure single thread access while the new session is being created. Therefore it waits for any
1137 * already running GET/PUT method calls, which have a 'read' lock, to complete. And also causes any new GET/PUT
1138 * method calls to wait until this method releases the 'write' lock again. Whereby such GET/PUT calls are postponed
1139 * to the new session.
1141 private synchronized void recreateSession() {
1142 try (SessionSynchronizer sessionSynchronizer = new SessionSynchronizer(true)) {
1143 LOGGER.debug("recreateSession()");
1144 recreatingSession = true;
1145 State onlineState = this.onlineState;
1151 if (onlineState == State.ACTIVE) {
1154 } catch (ApiException | InterruptedException e) {
1155 if (LOGGER.isDebugEnabled()) {
1156 LOGGER.debug("recreateSession() exception", e);
1158 LOGGER.warn("recreateSession() {}: {}", e.getClass().getSimpleName(), e.getMessage());
1161 recreatingSession = false;
1162 LOGGER.debug("recreateSession() done");
1167 * Try to register the application key with the hub. Use the given application key if one is provided; otherwise the
1168 * hub will create a new one. Note: this requires an HTTP 1.1 client call.
1170 * @param oldApplicationKey existing application key if any i.e. may be empty.
1171 * @return the existing or a newly created application key.
1172 * @throws HttpUnauthorizedException if the registration failed.
1173 * @throws ApiException if there was a communications error.
1174 * @throws InterruptedException
1176 public String registerApplicationKey(@Nullable String oldApplicationKey)
1177 throws HttpUnauthorizedException, ApiException, InterruptedException {
1178 LOGGER.debug("registerApplicationKey()");
1179 String json = jsonParser.toJson((Objects.isNull(oldApplicationKey) || oldApplicationKey.isEmpty())
1180 ? new CreateUserRequest(APPLICATION_ID)
1181 : new CreateUserRequest(oldApplicationKey, APPLICATION_ID));
1182 Request httpRequest = httpClient.newRequest(registrationUrl).method(HttpMethod.POST)
1183 .timeout(TIMEOUT_SECONDS, TimeUnit.SECONDS)
1184 .content(new StringContentProvider(json), MediaType.APPLICATION_JSON);
1185 ContentResponse contentResponse;
1187 LOGGER.trace("POST {} HTTP/1.1 >> {}", registrationUrl, json);
1188 contentResponse = httpRequest.send();
1189 } catch (TimeoutException | ExecutionException e) {
1190 throw new ApiException("HTTP processing error", e);
1192 int httpStatus = contentResponse.getStatus();
1193 json = contentResponse.getContentAsString().trim();
1194 LOGGER.trace("HTTP/1.1 {} {} << {}", httpStatus, contentResponse.getReason(), json);
1195 if (httpStatus != HttpStatus.OK_200) {
1196 throw new ApiException(String.format("HTTP bad response '%d'", httpStatus));
1199 List<SuccessResponse> entries = jsonParser.fromJson(json, SuccessResponse.GSON_TYPE);
1200 if (Objects.nonNull(entries) && !entries.isEmpty()) {
1201 SuccessResponse response = entries.get(0);
1202 Map<String, Object> responseSuccess = response.success;
1203 if (Objects.nonNull(responseSuccess)) {
1204 String newApplicationKey = (String) responseSuccess.get("username");
1205 if (Objects.nonNull(newApplicationKey)) {
1206 return newApplicationKey;
1210 } catch (JsonParseException e) {
1211 LOGGER.debug("registerApplicationKey() parsing error json:{}", json, e);
1213 throw new HttpUnauthorizedException("Application key registration failed");
1216 private void startHttp2Client() throws ApiException {
1218 http2Client.start();
1219 } catch (Exception e) {
1220 throw new ApiException("Error starting HTTP/2 client", e);
1224 private void stopHttp2Client() throws ApiException {
1227 } catch (Exception e) {
1228 throw new ApiException("Error stopping HTTP/2 client", e);
1233 * Test the Hue Bridge connection state by attempting to connect and trying to execute a basic command that requires
1236 * @throws HttpUnauthorizedException if it was possible to connect but not to authenticate.
1237 * @throws ApiException if it was not possible to connect.
1238 * @throws InterruptedException
1240 public void testConnectionState() throws HttpUnauthorizedException, ApiException, InterruptedException {
1241 LOGGER.debug("testConnectionState()");
1244 getResourcesImpl(BRIDGE);
1245 } catch (ApiException e) {