]> git.basschouten.com Git - openhab-addons.git/blob
635a3ca8063bbfb28f7716f1eb6d862eb9a72142
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.THING_TYPE_URL;
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 = new HttpClient(new SslContextFactory.Client());
63         this.insecureClient = new HttpClient(new SslContextFactory.Client(true));
64         try {
65             this.secureClient.start();
66             this.insecureClient.start();
67         } catch (Exception e) {
68             // catching exception is necessary due to the signature of HttpClient.start()
69             logger.warn("Failed to start insecure http client: {}", e.getMessage());
70             throw new IllegalStateException("Could not create insecure HttpClient");
71         }
72         this.httpDynamicStateDescriptionProvider = httpDynamicStateDescriptionProvider;
73     }
74
75     @Deactivate
76     public void deactivate() {
77         try {
78             secureClient.stop();
79             insecureClient.stop();
80         } catch (Exception e) {
81             // catching exception is necessary due to the signature of HttpClient.stop()
82             logger.warn("Failed to stop insecure http client: {}", e.getMessage());
83         }
84     }
85
86     @Override
87     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
88         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
89     }
90
91     @Override
92     protected @Nullable ThingHandler createHandler(Thing thing) {
93         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
94
95         if (THING_TYPE_URL.equals(thingTypeUID)) {
96             return new HttpThingHandler(thing, this, this, httpDynamicStateDescriptionProvider);
97         }
98
99         return null;
100     }
101
102     @Override
103     public ValueTransformation getValueTransformation(@Nullable String pattern) {
104         if (pattern == null || pattern.isEmpty()) {
105             return NoOpValueTransformation.getInstance();
106         }
107         return new CascadedValueTransformationImpl(pattern,
108                 name -> TransformationHelper.getTransformationService(bundleContext, name));
109     }
110
111     @Override
112     public HttpClient getSecureClient() {
113         return secureClient;
114     }
115
116     @Override
117     public HttpClient getInsecureClient() {
118         return insecureClient;
119     }
120 }