]> git.basschouten.com Git - openhab-addons.git/blob
6c332add640c56abc8894b674f1361e628d73dbd
[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 Gson jsonParser;
79
80     private HttpClient httpClientTrustingCert;
81
82     @Activate
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();
94
95         try {
96             SSLContext sslContext = SSLContext.getInstance("SSL");
97
98             TrustManager[] trustAllCerts = new TrustManager[] { new X509ExtendedTrustManager() {
99                 @Override
100                 public void checkClientTrusted(X509Certificate @Nullable [] chain, @Nullable String authType)
101                         throws CertificateException {
102                 }
103
104                 @Override
105                 public void checkServerTrusted(X509Certificate @Nullable [] chain, @Nullable String authType)
106                         throws CertificateException {
107                 }
108
109                 @Override
110                 public X509Certificate @Nullable [] getAcceptedIssuers() {
111                     return null;
112                 }
113
114                 @Override
115                 public void checkClientTrusted(X509Certificate @Nullable [] chain, @Nullable String authType,
116                         @Nullable Socket socket) throws CertificateException {
117                 }
118
119                 @Override
120                 public void checkServerTrusted(X509Certificate @Nullable [] chain, @Nullable String authType,
121                         @Nullable Socket socket) throws CertificateException {
122                 }
123
124                 @Override
125                 public void checkClientTrusted(X509Certificate @Nullable [] chain, @Nullable String authType,
126                         @Nullable SSLEngine engine) throws CertificateException {
127                 }
128
129                 @Override
130                 public void checkServerTrusted(X509Certificate @Nullable [] chain, @Nullable String authType,
131                         @Nullable SSLEngine engine) throws CertificateException {
132                 }
133             } };
134             sslContext.init(null, trustAllCerts, null);
135
136             this.httpClientTrustingCert.getSslContextFactory().setSslContext(sslContext);
137         } catch (NoSuchAlgorithmException e) {
138             logger.warn("An exception occurred while requesting the SSL encryption algorithm : '{}'", e.getMessage(),
139                     e);
140         } catch (KeyManagementException e) {
141             logger.warn("An exception occurred while initialising the SSL context : '{}'", e.getMessage(), e);
142         }
143     }
144
145     @Override
146     protected void activate(ComponentContext componentContext) {
147         super.activate(componentContext);
148         try {
149             httpClientTrustingCert.start();
150         } catch (Exception e) {
151             logger.warn("Unable to start Jetty HttpClient", e);
152         }
153     }
154
155     @Override
156     protected void deactivate(ComponentContext componentContext) {
157         try {
158             httpClientTrustingCert.stop();
159         } catch (Exception e) {
160             logger.warn("Unable to stop Jetty HttpClient", e);
161         }
162         super.deactivate(componentContext);
163     }
164
165     /**
166      * The things this factory supports creating.
167      */
168     @Override
169     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
170         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
171     }
172
173     @Override
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());
182             } else {
183                 newThingUID = thingUID;
184             }
185             return super.createThing(thingTypeUID, configuration, newThingUID, bridgeUID);
186         }
187         throw new IllegalArgumentException(
188                 "The thing type " + thingTypeUID + " is not supported by the remote openHAB binding.");
189     }
190
191     /**
192      * Creates a handler for the specific thing.
193      */
194     @Override
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);
202         }
203         return null;
204     }
205 }