import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
-import org.eclipse.jetty.client.HttpClient;
import org.openhab.binding.vizio.internal.appdb.VizioAppDbService;
import org.openhab.binding.vizio.internal.handler.VizioHandler;
import org.openhab.core.io.net.http.HttpClientFactory;
@Component(service = ThingHandlerFactory.class, configurationPid = "binding.vizio")
public class VizioHandlerFactory extends BaseThingHandlerFactory {
- private final HttpClient httpClient;
+ private final HttpClientFactory httpClientFactory;
private final VizioStateDescriptionOptionProvider stateDescriptionProvider;
private final String vizioAppsJson;
public VizioHandlerFactory(final @Reference HttpClientFactory httpClientFactory,
final @Reference VizioStateDescriptionOptionProvider provider,
final @Reference VizioAppDbService vizioAppDbService) {
- this.httpClient = httpClientFactory.getCommonHttpClient();
+ this.httpClientFactory = httpClientFactory;
this.stateDescriptionProvider = provider;
this.vizioAppsJson = vizioAppDbService.getVizioAppsJson();
}
ThingTypeUID thingTypeUID = thing.getThingTypeUID();
if (SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID)) {
- VizioHandler handler = new VizioHandler(thing, httpClient, stateDescriptionProvider, vizioAppsJson);
+ VizioHandler handler = new VizioHandler(thing, httpClientFactory, stateDescriptionProvider, vizioAppsJson);
return handler;
}
+++ /dev/null
-/**
- * Copyright (c) 2010-2023 Contributors to the openHAB project
- *
- * See the NOTICE file(s) distributed with this work for additional
- * information.
- *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License 2.0 which is available at
- * http://www.eclipse.org/legal/epl-2.0
- *
- * SPDX-License-Identifier: EPL-2.0
- */
-package org.openhab.binding.vizio.internal.communication;
-
-import java.net.MalformedURLException;
-import java.security.cert.CertificateException;
-
-import javax.net.ssl.X509ExtendedTrustManager;
-
-import org.eclipse.jdt.annotation.NonNullByDefault;
-import org.openhab.core.io.net.http.PEMTrustManager;
-import org.openhab.core.io.net.http.TlsTrustManagerProvider;
-import org.openhab.core.io.net.http.TrustAllTrustManager;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-/**
- * Provides a {@link PEMTrustManager} to allow secure connections to a Vizio TV that uses self signed
- * certificates.
- *
- * @author Christoph Weitkamp - Initial Contribution
- * @author Michael Lobstein - Adapted for Vizio binding
- */
-@NonNullByDefault
-public class VizioTlsTrustManagerProvider implements TlsTrustManagerProvider {
- private final String hostname;
-
- private final Logger logger = LoggerFactory.getLogger(VizioTlsTrustManagerProvider.class);
-
- public VizioTlsTrustManagerProvider(String hostname) {
- this.hostname = hostname;
- }
-
- @Override
- public String getHostName() {
- return hostname;
- }
-
- @Override
- public X509ExtendedTrustManager getTrustManager() {
- try {
- logger.trace("Use self-signed certificate downloaded from Vizio TV.");
- return PEMTrustManager.getInstanceFromServer("https://" + getHostName());
- } catch (CertificateException | MalformedURLException e) {
- logger.debug("An unexpected exception occurred - returning a TrustAllTrustManager: {}", e.getMessage(), e);
- }
- return TrustAllTrustManager.getInstance();
- }
-}
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.jetty.client.HttpClient;
+import org.eclipse.jetty.util.ssl.SslContextFactory;
import org.openhab.binding.vizio.internal.VizioConfiguration;
import org.openhab.binding.vizio.internal.VizioException;
import org.openhab.binding.vizio.internal.VizioStateDescriptionOptionProvider;
import org.openhab.binding.vizio.internal.communication.VizioCommunicator;
-import org.openhab.binding.vizio.internal.communication.VizioTlsTrustManagerProvider;
import org.openhab.binding.vizio.internal.dto.app.CurrentApp;
import org.openhab.binding.vizio.internal.dto.applist.VizioApp;
import org.openhab.binding.vizio.internal.dto.applist.VizioApps;
import org.openhab.binding.vizio.internal.dto.power.PowerMode;
import org.openhab.binding.vizio.internal.enums.KeyCommand;
import org.openhab.core.config.core.Configuration;
-import org.openhab.core.io.net.http.TlsTrustManagerProvider;
+import org.openhab.core.io.net.http.HttpClientFactory;
import org.openhab.core.library.types.NextPreviousType;
import org.openhab.core.library.types.OnOffType;
import org.openhab.core.library.types.PercentType;
import org.openhab.core.thing.ThingStatus;
import org.openhab.core.thing.ThingStatusDetail;
import org.openhab.core.thing.binding.BaseThingHandler;
+import org.openhab.core.thing.util.ThingWebClientUtil;
import org.openhab.core.types.Command;
import org.openhab.core.types.RefreshType;
import org.openhab.core.types.StateOption;
import org.openhab.core.types.UnDefType;
-import org.osgi.framework.FrameworkUtil;
-import org.osgi.framework.ServiceRegistration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@NonNullByDefault
public class VizioHandler extends BaseThingHandler {
private final Logger logger = LoggerFactory.getLogger(VizioHandler.class);
- private final HttpClient httpClient;
+ private final HttpClientFactory httpClientFactory;
+ private @Nullable HttpClient httpClient;
private final VizioStateDescriptionOptionProvider stateDescriptionProvider;
private final String dbAppsJson;
- private @Nullable ServiceRegistration<?> serviceRegistration;
private @Nullable ScheduledFuture<?> refreshJob;
private @Nullable ScheduledFuture<?> metadataRefreshJob;
private boolean powerOn = false;
private boolean debounce = true;
- public VizioHandler(Thing thing, HttpClient httpClient,
+ public VizioHandler(Thing thing, HttpClientFactory httpClientFactory,
VizioStateDescriptionOptionProvider stateDescriptionProvider, String vizioAppsJson) {
super(thing);
- this.httpClient = httpClient;
+ this.httpClientFactory = httpClientFactory;
this.stateDescriptionProvider = stateDescriptionProvider;
this.dbAppsJson = vizioAppsJson;
- this.communicator = new VizioCommunicator(httpClient, EMPTY, -1, EMPTY);
+ this.communicator = new VizioCommunicator(httpClientFactory.getCommonHttpClient(), EMPTY, -1, EMPTY);
}
@Override
host = "[" + host + "]";
}
- this.communicator = new VizioCommunicator(httpClient, host, config.port, authToken != null ? authToken : EMPTY);
-
- // register trustmanager service to allow httpClient to accept self signed cert from the Vizio TV
- VizioTlsTrustManagerProvider tlsTrustManagerProvider = new VizioTlsTrustManagerProvider(
- host + ":" + config.port);
- serviceRegistration = FrameworkUtil.getBundle(getClass()).getBundleContext()
- .registerService(TlsTrustManagerProvider.class.getName(), tlsTrustManagerProvider, null);
+ final String httpClientName = ThingWebClientUtil.buildWebClientConsumerName(thing.getUID(), null);
+ try {
+ httpClient = httpClientFactory.createHttpClient(httpClientName, new SslContextFactory.Client(true));
+ final HttpClient localHttpClient = this.httpClient;
+ if (localHttpClient != null) {
+ localHttpClient.start();
+ this.communicator = new VizioCommunicator(localHttpClient, host, config.port,
+ authToken != null ? authToken : EMPTY);
+ }
+ } catch (Exception e) {
+ logger.error(
+ "Long running HttpClient for Vizio handler {} cannot be started. Creating Handler failed. Exception: {}",
+ httpClientName, e.getMessage(), e);
+ updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR);
+ return;
+ }
if (authToken == null) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_PENDING,
this.metadataRefreshJob = null;
}
- ServiceRegistration<?> localServiceRegistration = serviceRegistration;
- if (localServiceRegistration != null) {
- // remove trustmanager service
- localServiceRegistration.unregister();
- serviceRegistration = null;
+ try {
+ HttpClient localHttpClient = this.httpClient;
+ if (localHttpClient != null) {
+ localHttpClient.stop();
+ }
+ this.httpClient = null;
+ } catch (Exception e) {
+ logger.debug("Unable to stop Vizio httpClient. Exception: {}", e.getMessage(), e);
}
}