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