]> git.basschouten.com Git - openhab-addons.git/blob
77a9661a5a2d6ce7e8250eb3b686056d48c16408
[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.daikin.internal;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.eclipse.jetty.client.HttpClient;
18 import org.eclipse.jetty.util.ssl.SslContextFactory;
19 import org.openhab.core.io.net.http.HttpClientFactory;
20 import org.osgi.service.component.annotations.Activate;
21 import org.osgi.service.component.annotations.Component;
22 import org.osgi.service.component.annotations.Deactivate;
23 import org.osgi.service.component.annotations.Reference;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 /**
28  * Factory class to create Jetty web clients
29  *
30  * Some Daikin controllers communicate via https using a custom common name,
31  * and they are accessed using an ip address.
32  *
33  * The core HttpClientFactory creates a HttpClient that will fail because of this.
34  * This factory creates a HttpClient with SslContextFactory(true)
35  * which will accept any ssl certificate without checking for common name mismatches.
36  *
37  * @author Jimmy Tanagra - Initial contribution
38  */
39 @Component
40 @NonNullByDefault
41 public class DaikinHttpClientFactoryImpl implements DaikinHttpClientFactory {
42
43     private final Logger logger = LoggerFactory.getLogger(DaikinHttpClientFactoryImpl.class);
44
45     private HttpClient httpClient;
46
47     @Activate
48     public DaikinHttpClientFactoryImpl(@Reference HttpClientFactory httpClientFactory) {
49         this.httpClient = httpClientFactory.createHttpClient(DaikinBindingConstants.BINDING_ID,
50                 new SslContextFactory.Client(true));
51         try {
52             httpClient.start();
53             logger.debug("Daikin http client started");
54         } catch (Exception e) {
55             logger.warn("Could not start Daikin http client", e);
56         }
57     }
58
59     @Deactivate
60     protected void deactivate() {
61         try {
62             httpClient.stop();
63             logger.debug("Daikin http client stopped");
64         } catch (Exception e) {
65             logger.debug("error while stopping Daikin http client", e);
66         }
67     }
68
69     @Override
70     public @Nullable HttpClient getHttpClient() {
71         return httpClient;
72     }
73 }