]> git.basschouten.com Git - openhab-addons.git/blob
4ca427d8ee29f2530dba644e6ad6783cc2497f22
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.http.internal;
14
15 import static org.openhab.binding.http.internal.HttpBindingConstants.*;
16
17 import java.util.Set;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.eclipse.jetty.client.HttpClient;
22 import org.eclipse.jetty.util.ssl.SslContextFactory;
23 import org.openhab.binding.http.internal.transform.CascadedValueTransformationImpl;
24 import org.openhab.binding.http.internal.transform.NoOpValueTransformation;
25 import org.openhab.binding.http.internal.transform.ValueTransformation;
26 import org.openhab.binding.http.internal.transform.ValueTransformationProvider;
27 import org.openhab.core.io.net.http.HttpClientFactory;
28 import org.openhab.core.thing.Thing;
29 import org.openhab.core.thing.ThingTypeUID;
30 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
31 import org.openhab.core.thing.binding.ThingHandler;
32 import org.openhab.core.thing.binding.ThingHandlerFactory;
33 import org.openhab.core.transform.TransformationHelper;
34 import org.osgi.service.component.annotations.Activate;
35 import org.osgi.service.component.annotations.Component;
36 import org.osgi.service.component.annotations.Deactivate;
37 import org.osgi.service.component.annotations.Reference;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41 /**
42  * The {@link HttpHandlerFactory} is responsible for creating things and thing
43  * handlers.
44  *
45  * @author Jan N. Klug - Initial contribution
46  */
47 @NonNullByDefault
48 @Component(configurationPid = "binding.http", service = ThingHandlerFactory.class)
49 public class HttpHandlerFactory extends BaseThingHandlerFactory
50         implements ValueTransformationProvider, HttpClientProvider {
51     private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Set.of(THING_TYPE_URL);
52     private final Logger logger = LoggerFactory.getLogger(HttpHandlerFactory.class);
53
54     private final HttpClient secureClient;
55     private final HttpClient insecureClient;
56
57     private final HttpDynamicStateDescriptionProvider httpDynamicStateDescriptionProvider;
58
59     @Activate
60     public HttpHandlerFactory(@Reference HttpClientFactory httpClientFactory,
61             @Reference HttpDynamicStateDescriptionProvider httpDynamicStateDescriptionProvider) {
62         this.secureClient = httpClientFactory.createHttpClient(BINDING_ID + "-secure", new SslContextFactory.Client());
63         this.insecureClient = httpClientFactory.createHttpClient(BINDING_ID + "-insecure",
64                 new SslContextFactory.Client(true));
65         try {
66             this.secureClient.start();
67             this.insecureClient.start();
68         } catch (Exception e) {
69             // catching exception is necessary due to the signature of HttpClient.start()
70             logger.warn("Failed to start insecure http client: {}", e.getMessage());
71             throw new IllegalStateException("Could not create insecure HttpClient");
72         }
73         this.httpDynamicStateDescriptionProvider = httpDynamicStateDescriptionProvider;
74     }
75
76     @Deactivate
77     public void deactivate() {
78         try {
79             secureClient.stop();
80             insecureClient.stop();
81         } catch (Exception e) {
82             // catching exception is necessary due to the signature of HttpClient.stop()
83             logger.warn("Failed to stop insecure http client: {}", e.getMessage());
84         }
85     }
86
87     @Override
88     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
89         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
90     }
91
92     @Override
93     protected @Nullable ThingHandler createHandler(Thing thing) {
94         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
95
96         if (THING_TYPE_URL.equals(thingTypeUID)) {
97             return new HttpThingHandler(thing, this, this, httpDynamicStateDescriptionProvider);
98         }
99
100         return null;
101     }
102
103     @Override
104     public ValueTransformation getValueTransformation(@Nullable String pattern) {
105         if (pattern == null || pattern.isEmpty()) {
106             return NoOpValueTransformation.getInstance();
107         }
108         return new CascadedValueTransformationImpl(pattern,
109                 name -> TransformationHelper.getTransformationService(bundleContext, name));
110     }
111
112     @Override
113     public HttpClient getSecureClient() {
114         return secureClient;
115     }
116
117     @Override
118     public HttpClient getInsecureClient() {
119         return insecureClient;
120     }
121 }