2 * Copyright (c) 2010-2021 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.io.net.http.HttpClientFactory;
37 import org.openhab.core.thing.Bridge;
38 import org.openhab.core.thing.Thing;
39 import org.openhab.core.thing.ThingTypeUID;
40 import org.openhab.core.thing.ThingUID;
41 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
42 import org.openhab.core.thing.binding.ThingHandler;
43 import org.openhab.core.thing.binding.ThingHandlerFactory;
44 import org.osgi.service.component.ComponentContext;
45 import org.osgi.service.component.annotations.Activate;
46 import org.osgi.service.component.annotations.Component;
47 import org.osgi.service.component.annotations.Reference;
48 import org.osgi.service.jaxrs.client.SseEventSourceFactory;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
52 import com.google.gson.FieldNamingPolicy;
53 import com.google.gson.Gson;
54 import com.google.gson.GsonBuilder;
57 * The {@link RemoteopenhabHandlerFactory} is responsible for creating things and thing
60 * @author Laurent Garnier - Initial contribution
63 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.remoteopenhab")
64 public class RemoteopenhabHandlerFactory extends BaseThingHandlerFactory {
66 private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Stream
67 .concat(RemoteopenhabBindingConstants.SUPPORTED_BRIDGE_TYPES_UIDS.stream(),
68 RemoteopenhabBindingConstants.SUPPORTED_THING_TYPES_UIDS.stream())
69 .collect(Collectors.toSet());
71 private final Logger logger = LoggerFactory.getLogger(RemoteopenhabHandlerFactory.class);
73 private final HttpClient httpClient;
74 private final ClientBuilder clientBuilder;
75 private final SseEventSourceFactory eventSourceFactory;
76 private final RemoteopenhabChannelTypeProvider channelTypeProvider;
77 private final RemoteopenhabStateDescriptionOptionProvider stateDescriptionProvider;
78 private final RemoteopenhabCommandDescriptionOptionProvider commandDescriptionProvider;
79 private final Gson jsonParser;
81 private HttpClient httpClientTrustingCert;
84 public RemoteopenhabHandlerFactory(final @Reference HttpClientFactory httpClientFactory,
85 final @Reference ClientBuilder clientBuilder, final @Reference SseEventSourceFactory eventSourceFactory,
86 final @Reference RemoteopenhabChannelTypeProvider channelTypeProvider,
87 final @Reference RemoteopenhabStateDescriptionOptionProvider stateDescriptionProvider,
88 final @Reference RemoteopenhabCommandDescriptionOptionProvider commandDescriptionProvider) {
89 this.httpClient = httpClientFactory.getCommonHttpClient();
90 this.httpClientTrustingCert = httpClientFactory.createHttpClient(RemoteopenhabBindingConstants.BINDING_ID);
91 this.clientBuilder = clientBuilder;
92 this.eventSourceFactory = eventSourceFactory;
93 this.channelTypeProvider = channelTypeProvider;
94 this.stateDescriptionProvider = stateDescriptionProvider;
95 this.commandDescriptionProvider = commandDescriptionProvider;
96 this.jsonParser = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.IDENTITY).create();
99 SSLContext sslContext = SSLContext.getInstance("SSL");
101 TrustManager[] trustAllCerts = new TrustManager[] { new X509ExtendedTrustManager() {
103 public void checkClientTrusted(X509Certificate @Nullable [] chain, @Nullable String authType)
104 throws CertificateException {
108 public void checkServerTrusted(X509Certificate @Nullable [] chain, @Nullable String authType)
109 throws CertificateException {
113 public X509Certificate @Nullable [] getAcceptedIssuers() {
118 public void checkClientTrusted(X509Certificate @Nullable [] chain, @Nullable String authType,
119 @Nullable Socket socket) throws CertificateException {
123 public void checkServerTrusted(X509Certificate @Nullable [] chain, @Nullable String authType,
124 @Nullable Socket socket) throws CertificateException {
128 public void checkClientTrusted(X509Certificate @Nullable [] chain, @Nullable String authType,
129 @Nullable SSLEngine engine) throws CertificateException {
133 public void checkServerTrusted(X509Certificate @Nullable [] chain, @Nullable String authType,
134 @Nullable SSLEngine engine) throws CertificateException {
137 sslContext.init(null, trustAllCerts, null);
139 this.httpClientTrustingCert.getSslContextFactory().setSslContext(sslContext);
140 } catch (NoSuchAlgorithmException e) {
141 logger.warn("An exception occurred while requesting the SSL encryption algorithm : '{}'", e.getMessage(),
143 } catch (KeyManagementException e) {
144 logger.warn("An exception occurred while initialising the SSL context : '{}'", e.getMessage(), e);
149 protected void activate(ComponentContext componentContext) {
150 super.activate(componentContext);
152 httpClientTrustingCert.start();
153 } catch (Exception e) {
154 logger.warn("Unable to start Jetty HttpClient", e);
159 protected void deactivate(ComponentContext componentContext) {
161 httpClientTrustingCert.stop();
162 } catch (Exception e) {
163 logger.warn("Unable to stop Jetty HttpClient", e);
165 super.deactivate(componentContext);
169 * The things this factory supports creating.
172 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
173 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
177 public @Nullable Thing createThing(ThingTypeUID thingTypeUID, Configuration configuration,
178 @Nullable ThingUID thingUID, @Nullable ThingUID bridgeUID) {
179 if (thingTypeUID.equals(RemoteopenhabBindingConstants.BRIDGE_TYPE_SERVER)) {
180 return super.createThing(thingTypeUID, configuration, thingUID, null);
181 } else if (RemoteopenhabBindingConstants.SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID)) {
182 ThingUID newThingUID;
183 if (bridgeUID != null && thingUID != null) {
184 newThingUID = new ThingUID(thingTypeUID, bridgeUID, thingUID.getId());
186 newThingUID = thingUID;
188 return super.createThing(thingTypeUID, configuration, newThingUID, bridgeUID);
190 throw new IllegalArgumentException(
191 "The thing type " + thingTypeUID + " is not supported by the remote openHAB binding.");
195 * Creates a handler for the specific thing.
198 protected @Nullable ThingHandler createHandler(Thing thing) {
199 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
200 if (thingTypeUID.equals(RemoteopenhabBindingConstants.BRIDGE_TYPE_SERVER)) {
201 return new RemoteopenhabBridgeHandler((Bridge) thing, httpClient, httpClientTrustingCert, clientBuilder,
202 eventSourceFactory, channelTypeProvider, stateDescriptionProvider, commandDescriptionProvider,
204 } else if (RemoteopenhabBindingConstants.SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID)) {
205 return new RemoteopenhabThingHandler(thing);