2 * Copyright (c) 2010-2022 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.http.internal;
15 import static org.openhab.binding.http.internal.HttpBindingConstants.THING_TYPE_URL;
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;
42 * The {@link HttpHandlerFactory} is responsible for creating things and thing
45 * @author Jan N. Klug - Initial contribution
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);
54 private final HttpClient secureClient;
55 private final HttpClient insecureClient;
57 private final HttpDynamicStateDescriptionProvider httpDynamicStateDescriptionProvider;
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));
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");
72 this.httpDynamicStateDescriptionProvider = httpDynamicStateDescriptionProvider;
76 public void deactivate() {
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());
87 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
88 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
92 protected @Nullable ThingHandler createHandler(Thing thing) {
93 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
95 if (THING_TYPE_URL.equals(thingTypeUID)) {
96 return new HttpThingHandler(thing, this, this, httpDynamicStateDescriptionProvider);
103 public ValueTransformation getValueTransformation(@Nullable String pattern) {
104 if (pattern == null || pattern.isEmpty()) {
105 return NoOpValueTransformation.getInstance();
107 return new CascadedValueTransformationImpl(pattern,
108 name -> TransformationHelper.getTransformationService(bundleContext, name));
112 public HttpClient getSecureClient() {
117 public HttpClient getInsecureClient() {
118 return insecureClient;