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 Gson jsonParser;
80 private HttpClient httpClientTrustingCert;
83 public RemoteopenhabHandlerFactory(final @Reference HttpClientFactory httpClientFactory,
84 final @Reference ClientBuilder clientBuilder, final @Reference SseEventSourceFactory eventSourceFactory,
85 final @Reference RemoteopenhabChannelTypeProvider channelTypeProvider,
86 final @Reference RemoteopenhabStateDescriptionOptionProvider stateDescriptionProvider) {
87 this.httpClient = httpClientFactory.getCommonHttpClient();
88 this.httpClientTrustingCert = httpClientFactory.createHttpClient(RemoteopenhabBindingConstants.BINDING_ID);
89 this.clientBuilder = clientBuilder;
90 this.eventSourceFactory = eventSourceFactory;
91 this.channelTypeProvider = channelTypeProvider;
92 this.stateDescriptionProvider = stateDescriptionProvider;
93 this.jsonParser = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.IDENTITY).create();
96 SSLContext sslContext = SSLContext.getInstance("SSL");
98 TrustManager[] trustAllCerts = new TrustManager[] { new X509ExtendedTrustManager() {
100 public void checkClientTrusted(X509Certificate @Nullable [] chain, @Nullable String authType)
101 throws CertificateException {
105 public void checkServerTrusted(X509Certificate @Nullable [] chain, @Nullable String authType)
106 throws CertificateException {
110 public X509Certificate @Nullable [] getAcceptedIssuers() {
115 public void checkClientTrusted(X509Certificate @Nullable [] chain, @Nullable String authType,
116 @Nullable Socket socket) throws CertificateException {
120 public void checkServerTrusted(X509Certificate @Nullable [] chain, @Nullable String authType,
121 @Nullable Socket socket) throws CertificateException {
125 public void checkClientTrusted(X509Certificate @Nullable [] chain, @Nullable String authType,
126 @Nullable SSLEngine engine) throws CertificateException {
130 public void checkServerTrusted(X509Certificate @Nullable [] chain, @Nullable String authType,
131 @Nullable SSLEngine engine) throws CertificateException {
134 sslContext.init(null, trustAllCerts, null);
136 this.httpClientTrustingCert.getSslContextFactory().setSslContext(sslContext);
137 } catch (NoSuchAlgorithmException e) {
138 logger.warn("An exception occurred while requesting the SSL encryption algorithm : '{}'", e.getMessage(),
140 } catch (KeyManagementException e) {
141 logger.warn("An exception occurred while initialising the SSL context : '{}'", e.getMessage(), e);
146 protected void activate(ComponentContext componentContext) {
147 super.activate(componentContext);
149 httpClientTrustingCert.start();
150 } catch (Exception e) {
151 logger.warn("Unable to start Jetty HttpClient", e);
156 protected void deactivate(ComponentContext componentContext) {
158 httpClientTrustingCert.stop();
159 } catch (Exception e) {
160 logger.warn("Unable to stop Jetty HttpClient", e);
162 super.deactivate(componentContext);
166 * The things this factory supports creating.
169 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
170 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
174 public @Nullable Thing createThing(ThingTypeUID thingTypeUID, Configuration configuration,
175 @Nullable ThingUID thingUID, @Nullable ThingUID bridgeUID) {
176 if (thingTypeUID.equals(RemoteopenhabBindingConstants.BRIDGE_TYPE_SERVER)) {
177 return super.createThing(thingTypeUID, configuration, thingUID, null);
178 } else if (RemoteopenhabBindingConstants.SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID)) {
179 ThingUID newThingUID;
180 if (bridgeUID != null && thingUID != null) {
181 newThingUID = new ThingUID(thingTypeUID, bridgeUID, thingUID.getId());
183 newThingUID = thingUID;
185 return super.createThing(thingTypeUID, configuration, newThingUID, bridgeUID);
187 throw new IllegalArgumentException(
188 "The thing type " + thingTypeUID + " is not supported by the remote openHAB binding.");
192 * Creates a handler for the specific thing.
195 protected @Nullable ThingHandler createHandler(Thing thing) {
196 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
197 if (thingTypeUID.equals(RemoteopenhabBindingConstants.BRIDGE_TYPE_SERVER)) {
198 return new RemoteopenhabBridgeHandler((Bridge) thing, httpClient, httpClientTrustingCert, clientBuilder,
199 eventSourceFactory, channelTypeProvider, stateDescriptionProvider, jsonParser);
200 } else if (RemoteopenhabBindingConstants.SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID)) {
201 return new RemoteopenhabThingHandler(thing);