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.eclipse.jetty.util.ssl.SslContextFactory;
34 import org.openhab.binding.remoteopenhab.internal.handler.RemoteopenhabBridgeHandler;
35 import org.openhab.binding.remoteopenhab.internal.handler.RemoteopenhabThingHandler;
36 import org.openhab.core.config.core.Configuration;
37 import org.openhab.core.i18n.LocaleProvider;
38 import org.openhab.core.i18n.TranslationProvider;
39 import org.openhab.core.io.net.http.HttpClientFactory;
40 import org.openhab.core.thing.Bridge;
41 import org.openhab.core.thing.Thing;
42 import org.openhab.core.thing.ThingTypeUID;
43 import org.openhab.core.thing.ThingUID;
44 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
45 import org.openhab.core.thing.binding.ThingHandler;
46 import org.openhab.core.thing.binding.ThingHandlerFactory;
47 import org.osgi.service.component.ComponentContext;
48 import org.osgi.service.component.annotations.Activate;
49 import org.osgi.service.component.annotations.Component;
50 import org.osgi.service.component.annotations.Reference;
51 import org.osgi.service.jaxrs.client.SseEventSourceFactory;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
55 import com.google.gson.FieldNamingPolicy;
56 import com.google.gson.Gson;
57 import com.google.gson.GsonBuilder;
60 * The {@link RemoteopenhabHandlerFactory} is responsible for creating things and thing
63 * @author Laurent Garnier - Initial contribution
66 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.remoteopenhab")
67 public class RemoteopenhabHandlerFactory extends BaseThingHandlerFactory {
69 private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Stream
70 .concat(RemoteopenhabBindingConstants.SUPPORTED_BRIDGE_TYPES_UIDS.stream(),
71 RemoteopenhabBindingConstants.SUPPORTED_THING_TYPES_UIDS.stream())
72 .collect(Collectors.toSet());
74 private final Logger logger = LoggerFactory.getLogger(RemoteopenhabHandlerFactory.class);
76 private final HttpClient httpClient;
77 private final ClientBuilder clientBuilder;
78 private final SseEventSourceFactory eventSourceFactory;
79 private final RemoteopenhabChannelTypeProvider channelTypeProvider;
80 private final RemoteopenhabStateDescriptionOptionProvider stateDescriptionProvider;
81 private final RemoteopenhabCommandDescriptionOptionProvider commandDescriptionProvider;
82 private final Gson jsonParser;
83 private final TranslationProvider i18nProvider;
84 private final LocaleProvider localeProvider;
86 private HttpClient httpClientTrustingCert;
89 public RemoteopenhabHandlerFactory(final @Reference HttpClientFactory httpClientFactory,
90 final @Reference ClientBuilder clientBuilder, final @Reference SseEventSourceFactory eventSourceFactory,
91 final @Reference RemoteopenhabChannelTypeProvider channelTypeProvider,
92 final @Reference RemoteopenhabStateDescriptionOptionProvider stateDescriptionProvider,
93 final @Reference RemoteopenhabCommandDescriptionOptionProvider commandDescriptionProvider,
94 final @Reference TranslationProvider i18nProvider, final @Reference LocaleProvider localeProvider) {
95 this.httpClient = httpClientFactory.getCommonHttpClient();
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;
105 SslContextFactory sslContextFactory = new SslContextFactory.Client();
107 SSLContext sslContext = SSLContext.getInstance("SSL");
109 TrustManager[] trustAllCerts = new TrustManager[] { new X509ExtendedTrustManager() {
111 public void checkClientTrusted(X509Certificate @Nullable [] chain, @Nullable String authType)
112 throws CertificateException {
116 public void checkServerTrusted(X509Certificate @Nullable [] chain, @Nullable String authType)
117 throws CertificateException {
121 public X509Certificate @Nullable [] getAcceptedIssuers() {
126 public void checkClientTrusted(X509Certificate @Nullable [] chain, @Nullable String authType,
127 @Nullable Socket socket) throws CertificateException {
131 public void checkServerTrusted(X509Certificate @Nullable [] chain, @Nullable String authType,
132 @Nullable Socket socket) throws CertificateException {
136 public void checkClientTrusted(X509Certificate @Nullable [] chain, @Nullable String authType,
137 @Nullable SSLEngine engine) throws CertificateException {
141 public void checkServerTrusted(X509Certificate @Nullable [] chain, @Nullable String authType,
142 @Nullable SSLEngine engine) throws CertificateException {
145 sslContext.init(null, trustAllCerts, null);
146 sslContextFactory.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);
153 this.httpClientTrustingCert = httpClientFactory.createHttpClient(RemoteopenhabBindingConstants.BINDING_ID,
158 protected void activate(ComponentContext componentContext) {
159 super.activate(componentContext);
161 httpClientTrustingCert.start();
162 } catch (Exception e) {
163 logger.warn("Unable to start Jetty HttpClient", e);
168 protected void deactivate(ComponentContext componentContext) {
170 httpClientTrustingCert.stop();
171 } catch (Exception e) {
172 logger.warn("Unable to stop Jetty HttpClient", e);
174 super.deactivate(componentContext);
178 * The things this factory supports creating.
181 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
182 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
186 public @Nullable Thing createThing(ThingTypeUID thingTypeUID, Configuration configuration,
187 @Nullable ThingUID thingUID, @Nullable ThingUID bridgeUID) {
188 if (thingTypeUID.equals(RemoteopenhabBindingConstants.BRIDGE_TYPE_SERVER)) {
189 return super.createThing(thingTypeUID, configuration, thingUID, null);
190 } else if (RemoteopenhabBindingConstants.SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID)) {
191 ThingUID newThingUID;
192 if (bridgeUID != null && thingUID != null) {
193 newThingUID = new ThingUID(thingTypeUID, bridgeUID, thingUID.getId());
195 newThingUID = thingUID;
197 return super.createThing(thingTypeUID, configuration, newThingUID, bridgeUID);
199 throw new IllegalArgumentException(
200 "The thing type " + thingTypeUID + " is not supported by the remote openHAB binding.");
204 * Creates a handler for the specific thing.
207 protected @Nullable ThingHandler createHandler(Thing thing) {
208 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
209 if (thingTypeUID.equals(RemoteopenhabBindingConstants.BRIDGE_TYPE_SERVER)) {
210 return new RemoteopenhabBridgeHandler((Bridge) thing, httpClient, httpClientTrustingCert, clientBuilder,
211 eventSourceFactory, channelTypeProvider, stateDescriptionProvider, commandDescriptionProvider,
212 jsonParser, i18nProvider, localeProvider);
213 } else if (RemoteopenhabBindingConstants.SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID)) {
214 return new RemoteopenhabThingHandler(thing);