]> git.basschouten.com Git - openhab-addons.git/blob
9d012df1b526fe5a3d85ac3352185f13eefa2293
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.remoteopenhab.internal;
14
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;
20 import java.util.Set;
21 import java.util.stream.Collectors;
22 import java.util.stream.Stream;
23
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;
29
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;
54
55 import com.google.gson.FieldNamingPolicy;
56 import com.google.gson.Gson;
57 import com.google.gson.GsonBuilder;
58
59 /**
60  * The {@link RemoteopenhabHandlerFactory} is responsible for creating things and thing
61  * handlers.
62  *
63  * @author Laurent Garnier - Initial contribution
64  */
65 @NonNullByDefault
66 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.remoteopenhab")
67 public class RemoteopenhabHandlerFactory extends BaseThingHandlerFactory {
68
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());
73
74     private final Logger logger = LoggerFactory.getLogger(RemoteopenhabHandlerFactory.class);
75
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;
85
86     private HttpClient httpClientTrustingCert;
87
88     @Activate
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;
104
105         SslContextFactory sslContextFactory = new SslContextFactory.Client();
106         try {
107             SSLContext sslContext = SSLContext.getInstance("SSL");
108
109             TrustManager[] trustAllCerts = new TrustManager[] { new X509ExtendedTrustManager() {
110                 @Override
111                 public void checkClientTrusted(X509Certificate @Nullable [] chain, @Nullable String authType)
112                         throws CertificateException {
113                 }
114
115                 @Override
116                 public void checkServerTrusted(X509Certificate @Nullable [] chain, @Nullable String authType)
117                         throws CertificateException {
118                 }
119
120                 @Override
121                 public X509Certificate @Nullable [] getAcceptedIssuers() {
122                     return null;
123                 }
124
125                 @Override
126                 public void checkClientTrusted(X509Certificate @Nullable [] chain, @Nullable String authType,
127                         @Nullable Socket socket) throws CertificateException {
128                 }
129
130                 @Override
131                 public void checkServerTrusted(X509Certificate @Nullable [] chain, @Nullable String authType,
132                         @Nullable Socket socket) throws CertificateException {
133                 }
134
135                 @Override
136                 public void checkClientTrusted(X509Certificate @Nullable [] chain, @Nullable String authType,
137                         @Nullable SSLEngine engine) throws CertificateException {
138                 }
139
140                 @Override
141                 public void checkServerTrusted(X509Certificate @Nullable [] chain, @Nullable String authType,
142                         @Nullable SSLEngine engine) throws CertificateException {
143                 }
144             } };
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(),
149                     e);
150         } catch (KeyManagementException e) {
151             logger.warn("An exception occurred while initialising the SSL context : '{}'", e.getMessage(), e);
152         }
153         this.httpClientTrustingCert = httpClientFactory.createHttpClient(RemoteopenhabBindingConstants.BINDING_ID,
154                 sslContextFactory);
155     }
156
157     @Override
158     protected void activate(ComponentContext componentContext) {
159         super.activate(componentContext);
160         try {
161             httpClientTrustingCert.start();
162         } catch (Exception e) {
163             logger.warn("Unable to start Jetty HttpClient", e);
164         }
165     }
166
167     @Override
168     protected void deactivate(ComponentContext componentContext) {
169         try {
170             httpClientTrustingCert.stop();
171         } catch (Exception e) {
172             logger.warn("Unable to stop Jetty HttpClient", e);
173         }
174         super.deactivate(componentContext);
175     }
176
177     /**
178      * The things this factory supports creating.
179      */
180     @Override
181     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
182         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
183     }
184
185     @Override
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());
194             } else {
195                 newThingUID = thingUID;
196             }
197             return super.createThing(thingTypeUID, configuration, newThingUID, bridgeUID);
198         }
199         throw new IllegalArgumentException(
200                 "The thing type " + thingTypeUID + " is not supported by the remote openHAB binding.");
201     }
202
203     /**
204      * Creates a handler for the specific thing.
205      */
206     @Override
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);
215         }
216         return null;
217     }
218 }