]> git.basschouten.com Git - openhab-addons.git/blob
fcb60e45021a2edac201613493748e27ca313857
[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.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;
53
54 import com.google.gson.FieldNamingPolicy;
55 import com.google.gson.Gson;
56 import com.google.gson.GsonBuilder;
57
58 /**
59  * The {@link RemoteopenhabHandlerFactory} is responsible for creating things and thing
60  * handlers.
61  *
62  * @author Laurent Garnier - Initial contribution
63  */
64 @NonNullByDefault
65 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.remoteopenhab")
66 public class RemoteopenhabHandlerFactory extends BaseThingHandlerFactory {
67
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());
72
73     private final Logger logger = LoggerFactory.getLogger(RemoteopenhabHandlerFactory.class);
74
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;
84
85     private HttpClient httpClientTrustingCert;
86
87     @Activate
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;
104
105         try {
106             SSLContext sslContext = SSLContext.getInstance("SSL");
107
108             TrustManager[] trustAllCerts = new TrustManager[] { new X509ExtendedTrustManager() {
109                 @Override
110                 public void checkClientTrusted(X509Certificate @Nullable [] chain, @Nullable String authType)
111                         throws CertificateException {
112                 }
113
114                 @Override
115                 public void checkServerTrusted(X509Certificate @Nullable [] chain, @Nullable String authType)
116                         throws CertificateException {
117                 }
118
119                 @Override
120                 public X509Certificate @Nullable [] getAcceptedIssuers() {
121                     return null;
122                 }
123
124                 @Override
125                 public void checkClientTrusted(X509Certificate @Nullable [] chain, @Nullable String authType,
126                         @Nullable Socket socket) throws CertificateException {
127                 }
128
129                 @Override
130                 public void checkServerTrusted(X509Certificate @Nullable [] chain, @Nullable String authType,
131                         @Nullable Socket socket) throws CertificateException {
132                 }
133
134                 @Override
135                 public void checkClientTrusted(X509Certificate @Nullable [] chain, @Nullable String authType,
136                         @Nullable SSLEngine engine) throws CertificateException {
137                 }
138
139                 @Override
140                 public void checkServerTrusted(X509Certificate @Nullable [] chain, @Nullable String authType,
141                         @Nullable SSLEngine engine) throws CertificateException {
142                 }
143             } };
144             sslContext.init(null, trustAllCerts, null);
145
146             this.httpClientTrustingCert.getSslContextFactory().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     }
154
155     @Override
156     protected void activate(ComponentContext componentContext) {
157         super.activate(componentContext);
158         try {
159             httpClientTrustingCert.start();
160         } catch (Exception e) {
161             logger.warn("Unable to start Jetty HttpClient", e);
162         }
163     }
164
165     @Override
166     protected void deactivate(ComponentContext componentContext) {
167         try {
168             httpClientTrustingCert.stop();
169         } catch (Exception e) {
170             logger.warn("Unable to stop Jetty HttpClient", e);
171         }
172         super.deactivate(componentContext);
173     }
174
175     /**
176      * The things this factory supports creating.
177      */
178     @Override
179     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
180         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
181     }
182
183     @Override
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());
192             } else {
193                 newThingUID = thingUID;
194             }
195             return super.createThing(thingTypeUID, configuration, newThingUID, bridgeUID);
196         }
197         throw new IllegalArgumentException(
198                 "The thing type " + thingTypeUID + " is not supported by the remote openHAB binding.");
199     }
200
201     /**
202      * Creates a handler for the specific thing.
203      */
204     @Override
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);
213         }
214         return null;
215     }
216 }