]> git.basschouten.com Git - openhab-addons.git/blob
4a743a6ac5dbc3559469fd2b59828b05c8444570
[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.transform.exec.internal;
14
15 import java.time.Duration;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.core.io.net.exec.ExecUtil;
20 import org.openhab.core.transform.TransformationException;
21 import org.openhab.core.transform.TransformationService;
22 import org.osgi.service.component.annotations.Activate;
23 import org.osgi.service.component.annotations.Component;
24 import org.osgi.service.component.annotations.Reference;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 /**
29  * The implementation of {@link TransformationService} which transforms the
30  * input by command line.
31  *
32  * @author Pauli Anttila - Initial contribution
33  * @author Jan N. Klug - added command whitelist service
34  */
35 @NonNullByDefault
36 @Component(property = { "openhab.transform=EXEC" })
37 public class ExecTransformationService implements TransformationService {
38     private final Logger logger = LoggerFactory.getLogger(ExecTransformationService.class);
39     private final ExecTransformationWhitelistWatchService execTransformationWhitelistWatchService;
40
41     @Activate
42     public ExecTransformationService(
43             @Reference ExecTransformationWhitelistWatchService execTransformationWhitelistWatchService) {
44         this.execTransformationWhitelistWatchService = execTransformationWhitelistWatchService;
45     }
46
47     /**
48      * Transforms the input <code>source</code> by the command line.
49      *
50      * @param commandLine the command to execute. Command line should contain %s string, which will be replaced by the
51      *            input data.
52      * @param source the input to transform
53      */
54     @Override
55     public @Nullable String transform(String commandLine, String source) throws TransformationException {
56         if (commandLine == null || source == null) {
57             throw new TransformationException("the given parameters 'commandLine' and 'source' must not be null");
58         }
59
60         if (!execTransformationWhitelistWatchService.isWhitelisted(commandLine)) {
61             logger.warn("Tried to execute '{}', but it is not contained in whitelist.", commandLine);
62             return null;
63         }
64         logger.debug("about to transform '{}' by the commandline '{}'", source, commandLine);
65
66         long startTime = System.currentTimeMillis();
67
68         String formattedCommandLine = String.format(commandLine, source);
69         String result = ExecUtil.executeCommandLineAndWaitResponse(Duration.ofSeconds(5),
70                 formattedCommandLine.split(" "));
71         logger.trace("command line execution elapsed {} ms", System.currentTimeMillis() - startTime);
72
73         return result;
74     }
75 }