]> git.basschouten.com Git - openhab-addons.git/blob
93e3177c738ac3283d244f2dc1f79b116563f43d
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.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;
51
52 import com.google.gson.FieldNamingPolicy;
53 import com.google.gson.Gson;
54 import com.google.gson.GsonBuilder;
55
56 /**
57  * The {@link RemoteopenhabHandlerFactory} is responsible for creating things and thing
58  * handlers.
59  *
60  * @author Laurent Garnier - Initial contribution
61  */
62 @NonNullByDefault
63 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.remoteopenhab")
64 public class RemoteopenhabHandlerFactory extends BaseThingHandlerFactory {
65
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());
70
71     private final Logger logger = LoggerFactory.getLogger(RemoteopenhabHandlerFactory.class);
72
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;
80
81     private HttpClient httpClientTrustingCert;
82
83     @Activate
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();
97
98         try {
99             SSLContext sslContext = SSLContext.getInstance("SSL");
100
101             TrustManager[] trustAllCerts = new TrustManager[] { new X509ExtendedTrustManager() {
102                 @Override
103                 public void checkClientTrusted(X509Certificate @Nullable [] chain, @Nullable String authType)
104                         throws CertificateException {
105                 }
106
107                 @Override
108                 public void checkServerTrusted(X509Certificate @Nullable [] chain, @Nullable String authType)
109                         throws CertificateException {
110                 }
111
112                 @Override
113                 public X509Certificate @Nullable [] getAcceptedIssuers() {
114                     return null;
115                 }
116
117                 @Override
118                 public void checkClientTrusted(X509Certificate @Nullable [] chain, @Nullable String authType,
119                         @Nullable Socket socket) throws CertificateException {
120                 }
121
122                 @Override
123                 public void checkServerTrusted(X509Certificate @Nullable [] chain, @Nullable String authType,
124                         @Nullable Socket socket) throws CertificateException {
125                 }
126
127                 @Override
128                 public void checkClientTrusted(X509Certificate @Nullable [] chain, @Nullable String authType,
129                         @Nullable SSLEngine engine) throws CertificateException {
130                 }
131
132                 @Override
133                 public void checkServerTrusted(X509Certificate @Nullable [] chain, @Nullable String authType,
134                         @Nullable SSLEngine engine) throws CertificateException {
135                 }
136             } };
137             sslContext.init(null, trustAllCerts, null);
138
139             this.httpClientTrustingCert.getSslContextFactory().setSslContext(sslContext);
140         } catch (NoSuchAlgorithmException e) {
141             logger.warn("An exception occurred while requesting the SSL encryption algorithm : '{}'", e.getMessage(),
142                     e);
143         } catch (KeyManagementException e) {
144             logger.warn("An exception occurred while initialising the SSL context : '{}'", e.getMessage(), e);
145         }
146     }
147
148     @Override
149     protected void activate(ComponentContext componentContext) {
150         super.activate(componentContext);
151         try {
152             httpClientTrustingCert.start();
153         } catch (Exception e) {
154             logger.warn("Unable to start Jetty HttpClient", e);
155         }
156     }
157
158     @Override
159     protected void deactivate(ComponentContext componentContext) {
160         try {
161             httpClientTrustingCert.stop();
162         } catch (Exception e) {
163             logger.warn("Unable to stop Jetty HttpClient", e);
164         }
165         super.deactivate(componentContext);
166     }
167
168     /**
169      * The things this factory supports creating.
170      */
171     @Override
172     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
173         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
174     }
175
176     @Override
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());
185             } else {
186                 newThingUID = thingUID;
187             }
188             return super.createThing(thingTypeUID, configuration, newThingUID, bridgeUID);
189         }
190         throw new IllegalArgumentException(
191                 "The thing type " + thingTypeUID + " is not supported by the remote openHAB binding.");
192     }
193
194     /**
195      * Creates a handler for the specific thing.
196      */
197     @Override
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,
203                     jsonParser);
204         } else if (RemoteopenhabBindingConstants.SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID)) {
205             return new RemoteopenhabThingHandler(thing);
206         }
207         return null;
208     }
209 }