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.remoteopenhab.internal;
15 import java.net.Socket;
16 import java.security.KeyManagementException;
17 import java.security.NoSuchAlgorithmException;
18 import java.security.cert.CertificateException;
19 import java.security.cert.X509Certificate;
21 import java.util.stream.Collectors;
22 import java.util.stream.Stream;
24 import javax.net.ssl.SSLContext;
25 import javax.net.ssl.SSLEngine;
26 import javax.net.ssl.TrustManager;
27 import javax.net.ssl.X509ExtendedTrustManager;
28 import javax.ws.rs.client.ClientBuilder;
30 import org.eclipse.jdt.annotation.NonNullByDefault;
31 import org.eclipse.jdt.annotation.Nullable;
32 import org.eclipse.jetty.client.HttpClient;
33 import org.openhab.binding.remoteopenhab.internal.handler.RemoteopenhabBridgeHandler;
34 import org.openhab.binding.remoteopenhab.internal.handler.RemoteopenhabThingHandler;
35 import org.openhab.core.config.core.Configuration;
36 import org.openhab.core.i18n.LocaleProvider;
37 import org.openhab.core.i18n.TranslationProvider;
38 import org.openhab.core.io.net.http.HttpClientFactory;
39 import org.openhab.core.thing.Bridge;
40 import org.openhab.core.thing.Thing;
41 import org.openhab.core.thing.ThingTypeUID;
42 import org.openhab.core.thing.ThingUID;
43 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
44 import org.openhab.core.thing.binding.ThingHandler;
45 import org.openhab.core.thing.binding.ThingHandlerFactory;
46 import org.osgi.service.component.ComponentContext;
47 import org.osgi.service.component.annotations.Activate;
48 import org.osgi.service.component.annotations.Component;
49 import org.osgi.service.component.annotations.Reference;
50 import org.osgi.service.jaxrs.client.SseEventSourceFactory;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
54 import com.google.gson.FieldNamingPolicy;
55 import com.google.gson.Gson;
56 import com.google.gson.GsonBuilder;
59 * The {@link RemoteopenhabHandlerFactory} is responsible for creating things and thing
62 * @author Laurent Garnier - Initial contribution
65 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.remoteopenhab")
66 public class RemoteopenhabHandlerFactory extends BaseThingHandlerFactory {
68 private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Stream
69 .concat(RemoteopenhabBindingConstants.SUPPORTED_BRIDGE_TYPES_UIDS.stream(),
70 RemoteopenhabBindingConstants.SUPPORTED_THING_TYPES_UIDS.stream())
71 .collect(Collectors.toSet());
73 private final Logger logger = LoggerFactory.getLogger(RemoteopenhabHandlerFactory.class);
75 private final HttpClient httpClient;
76 private final ClientBuilder clientBuilder;
77 private final SseEventSourceFactory eventSourceFactory;
78 private final RemoteopenhabChannelTypeProvider channelTypeProvider;
79 private final RemoteopenhabStateDescriptionOptionProvider stateDescriptionProvider;
80 private final RemoteopenhabCommandDescriptionOptionProvider commandDescriptionProvider;
81 private final Gson jsonParser;
82 private final TranslationProvider i18nProvider;
83 private final LocaleProvider localeProvider;
85 private HttpClient httpClientTrustingCert;
88 public RemoteopenhabHandlerFactory(final @Reference HttpClientFactory httpClientFactory,
89 final @Reference ClientBuilder clientBuilder, final @Reference SseEventSourceFactory eventSourceFactory,
90 final @Reference RemoteopenhabChannelTypeProvider channelTypeProvider,
91 final @Reference RemoteopenhabStateDescriptionOptionProvider stateDescriptionProvider,
92 final @Reference RemoteopenhabCommandDescriptionOptionProvider commandDescriptionProvider,
93 final @Reference TranslationProvider i18nProvider, final @Reference LocaleProvider localeProvider) {
94 this.httpClient = httpClientFactory.getCommonHttpClient();
95 this.httpClientTrustingCert = httpClientFactory.createHttpClient(RemoteopenhabBindingConstants.BINDING_ID);
96 this.clientBuilder = clientBuilder;
97 this.eventSourceFactory = eventSourceFactory;
98 this.channelTypeProvider = channelTypeProvider;
99 this.stateDescriptionProvider = stateDescriptionProvider;
100 this.commandDescriptionProvider = commandDescriptionProvider;
101 this.jsonParser = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.IDENTITY).create();
102 this.i18nProvider = i18nProvider;
103 this.localeProvider = localeProvider;
106 SSLContext sslContext = SSLContext.getInstance("SSL");
108 TrustManager[] trustAllCerts = new TrustManager[] { new X509ExtendedTrustManager() {
110 public void checkClientTrusted(X509Certificate @Nullable [] chain, @Nullable String authType)
111 throws CertificateException {
115 public void checkServerTrusted(X509Certificate @Nullable [] chain, @Nullable String authType)
116 throws CertificateException {
120 public X509Certificate @Nullable [] getAcceptedIssuers() {
125 public void checkClientTrusted(X509Certificate @Nullable [] chain, @Nullable String authType,
126 @Nullable Socket socket) throws CertificateException {
130 public void checkServerTrusted(X509Certificate @Nullable [] chain, @Nullable String authType,
131 @Nullable Socket socket) throws CertificateException {
135 public void checkClientTrusted(X509Certificate @Nullable [] chain, @Nullable String authType,
136 @Nullable SSLEngine engine) throws CertificateException {
140 public void checkServerTrusted(X509Certificate @Nullable [] chain, @Nullable String authType,
141 @Nullable SSLEngine engine) throws CertificateException {
144 sslContext.init(null, trustAllCerts, null);
146 this.httpClientTrustingCert.getSslContextFactory().setSslContext(sslContext);
147 } catch (NoSuchAlgorithmException e) {
148 logger.warn("An exception occurred while requesting the SSL encryption algorithm : '{}'", e.getMessage(),
150 } catch (KeyManagementException e) {
151 logger.warn("An exception occurred while initialising the SSL context : '{}'", e.getMessage(), e);
156 protected void activate(ComponentContext componentContext) {
157 super.activate(componentContext);
159 httpClientTrustingCert.start();
160 } catch (Exception e) {
161 logger.warn("Unable to start Jetty HttpClient", e);
166 protected void deactivate(ComponentContext componentContext) {
168 httpClientTrustingCert.stop();
169 } catch (Exception e) {
170 logger.warn("Unable to stop Jetty HttpClient", e);
172 super.deactivate(componentContext);
176 * The things this factory supports creating.
179 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
180 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
184 public @Nullable Thing createThing(ThingTypeUID thingTypeUID, Configuration configuration,
185 @Nullable ThingUID thingUID, @Nullable ThingUID bridgeUID) {
186 if (thingTypeUID.equals(RemoteopenhabBindingConstants.BRIDGE_TYPE_SERVER)) {
187 return super.createThing(thingTypeUID, configuration, thingUID, null);
188 } else if (RemoteopenhabBindingConstants.SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID)) {
189 ThingUID newThingUID;
190 if (bridgeUID != null && thingUID != null) {
191 newThingUID = new ThingUID(thingTypeUID, bridgeUID, thingUID.getId());
193 newThingUID = thingUID;
195 return super.createThing(thingTypeUID, configuration, newThingUID, bridgeUID);
197 throw new IllegalArgumentException(
198 "The thing type " + thingTypeUID + " is not supported by the remote openHAB binding.");
202 * Creates a handler for the specific thing.
205 protected @Nullable ThingHandler createHandler(Thing thing) {
206 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
207 if (thingTypeUID.equals(RemoteopenhabBindingConstants.BRIDGE_TYPE_SERVER)) {
208 return new RemoteopenhabBridgeHandler((Bridge) thing, httpClient, httpClientTrustingCert, clientBuilder,
209 eventSourceFactory, channelTypeProvider, stateDescriptionProvider, commandDescriptionProvider,
210 jsonParser, i18nProvider, localeProvider);
211 } else if (RemoteopenhabBindingConstants.SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID)) {
212 return new RemoteopenhabThingHandler(thing);