001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.camel.component.exec.impl;
018    
019    import java.io.File;
020    import java.io.InputStream;
021    import java.util.List;
022    
023    import org.apache.camel.Exchange;
024    import org.apache.camel.Message;
025    import org.apache.camel.component.exec.ExecBinding;
026    import org.apache.camel.component.exec.ExecCommand;
027    import org.apache.camel.component.exec.ExecEndpoint;
028    import org.apache.camel.component.exec.ExecResult;
029    import org.apache.camel.util.ObjectHelper;
030    import org.slf4j.Logger;
031    import org.slf4j.LoggerFactory;
032    
033    import static org.apache.camel.component.exec.impl.ExecParseUtils.splitToWhiteSpaceSeparatedTokens;
034    
035    /**
036     * Default implementation of {@link ExecBinding}.
037     * 
038     * @see DefaultExecBinding#writeOutputInMessage(Message, ExecResult)
039     */
040    public class DefaultExecBinding implements ExecBinding {
041    
042        private static final Logger LOG = LoggerFactory.getLogger(DefaultExecBinding.class);
043    
044        @SuppressWarnings("unchecked")
045        public ExecCommand readInput(Exchange exchange, ExecEndpoint endpoint) {
046            ObjectHelper.notNull(exchange, "exchange");
047            ObjectHelper.notNull(endpoint, "endpoint");
048    
049            // do not convert args as we do that manually later
050            Object args = exchange.getIn().removeHeader(EXEC_COMMAND_ARGS);
051            String cmd = getAndRemoveHeader(exchange.getIn(), EXEC_COMMAND_EXECUTABLE, endpoint.getExecutable(), String.class);
052            String dir = getAndRemoveHeader(exchange.getIn(), EXEC_COMMAND_WORKING_DIR, endpoint.getWorkingDir(), String.class);
053            long timeout = getAndRemoveHeader(exchange.getIn(), EXEC_COMMAND_TIMEOUT, endpoint.getTimeout(), Long.class);
054            String outFilePath = getAndRemoveHeader(exchange.getIn(), EXEC_COMMAND_OUT_FILE, endpoint.getOutFile(), String.class);
055            boolean useStderrOnEmptyStdout = getAndRemoveHeader(exchange.getIn(), EXEC_USE_STDERR_ON_EMPTY_STDOUT, endpoint.isUseStderrOnEmptyStdout(), Boolean.class);
056            InputStream input = exchange.getIn().getBody(InputStream.class);
057    
058            // try to convert args to list at fist
059            List<String> argsList = exchange.getContext().getTypeConverter().convertTo(List.class, exchange, args);
060            if (argsList == null) {
061                // no we could not do that, then parse it as a string to a list
062                String s = endpoint.getArgs();
063                if (args != null) {
064                    // use args from header instead from endpoint
065                    s = exchange.getContext().getTypeConverter().convertTo(String.class, exchange, args);
066                }
067                LOG.debug("Parsing argument String to a List: {}", s);
068                argsList = splitToWhiteSpaceSeparatedTokens(s);
069            }
070    
071            File outFile = outFilePath == null ? null : new File(outFilePath);
072            return new ExecCommand(cmd, argsList, dir, timeout, input, outFile, useStderrOnEmptyStdout);
073        }
074    
075        public void writeOutput(Exchange exchange, ExecResult result) {
076            ObjectHelper.notNull(exchange, "exchange");
077            ObjectHelper.notNull(result, "result");
078    
079            if (exchange.getPattern().isOutCapable()) {
080                writeOutputInMessage(exchange.getOut(), result);
081                exchange.getOut().getHeaders().putAll(exchange.getIn().getHeaders());
082            } else {
083                writeOutputInMessage(exchange.getIn(), result);
084            }
085        }
086    
087        /**
088         * Write the {@link ExecResult} in the message body. Write the stderr and
089         * the exit value for convenience in the message headers. <br>
090         * The stdout and/or resultFile should be accessible using a converter or
091         * using the result object directly.
092         * 
093         * @param message a Camel message
094         * @param result an {@link ExecResult} instance
095         */
096        protected void writeOutputInMessage(Message message, ExecResult result) {
097            message.setHeader(EXEC_STDERR, result.getStderr());
098            message.setHeader(EXEC_EXIT_VALUE, result.getExitValue());
099            message.setBody(result);
100        }
101    
102        /**
103         * Gets and removes the <code> <code>headerName</code> header form the input
104         * <code>message</code> (the header will not be propagated)
105         */
106        protected <T> T getAndRemoveHeader(Message message, String headerName, T defaultValue, Class<T> headerType) {
107            T h = message.getHeader(headerName, defaultValue, headerType);
108            message.removeHeader(headerName);
109            return h;
110        }
111    }