2 * Copyright (c) 2010-2023 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.transform;
15 import java.lang.ref.WeakReference;
16 import java.util.Optional;
17 import java.util.function.Function;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.core.transform.TransformationException;
22 import org.openhab.core.transform.TransformationService;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
27 * A transformation for a value used in {@link HttpChannel}.
29 * @author David Graeff - Initial contribution
30 * @author Jan N. Klug - adapted from MQTT binding to HTTP binding
33 public class SingleValueTransformation implements ValueTransformation {
34 private final Logger logger = LoggerFactory.getLogger(SingleValueTransformation.class);
35 private final Function<String, @Nullable TransformationService> transformationServiceSupplier;
36 private WeakReference<@Nullable TransformationService> transformationService = new WeakReference<>(null);
37 private final String pattern;
38 private final String serviceName;
41 * Creates a new channel state transformer.
43 * @param pattern A transformation pattern, starting with the transformation service
44 * name, followed by a colon and the transformation itself.
45 * @param transformationServiceSupplier
47 public SingleValueTransformation(String pattern,
48 Function<String, @Nullable TransformationService> transformationServiceSupplier) {
49 this.transformationServiceSupplier = transformationServiceSupplier;
50 int index = pattern.indexOf(':');
52 throw new IllegalArgumentException(
53 "The transformation pattern must consist of the type and the pattern separated by a colon");
55 this.serviceName = pattern.substring(0, index).toUpperCase();
56 this.pattern = pattern.substring(index + 1);
60 public Optional<String> apply(String value) {
61 TransformationService transformationService = this.transformationService.get();
62 if (transformationService == null) {
63 transformationService = transformationServiceSupplier.apply(serviceName);
64 if (transformationService == null) {
65 logger.warn("Transformation service {} for pattern {} not found!", serviceName, pattern);
66 return Optional.empty();
68 this.transformationService = new WeakReference<>(transformationService);
72 String result = transformationService.transform(pattern, value);
74 logger.debug("Transformation {} returned empty result when applied to {}.", this, value);
75 return Optional.empty();
77 return Optional.of(result);
78 } catch (TransformationException e) {
79 logger.warn("Executing transformation {} failed: {}", this, e.getMessage());
82 return Optional.empty();
86 public String toString() {
87 return "ChannelStateTransformation{pattern='" + pattern + "', serviceName='" + serviceName + "'}";