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
031 import static org.apache.camel.component.exec.impl.ExecParseUtils.splitToWhiteSpaceSeparatedTokens;
032
033 /**
034 * Default implementation of {@link ExecBinding}.
035 *
036 * @see DefaultExecBinding#writeOutputInMessage(Message, ExecResult)
037 */
038 public class DefaultExecBinding implements ExecBinding {
039
040 @SuppressWarnings("unchecked")
041 public ExecCommand readInput(Exchange exchange, ExecEndpoint endpoint) {
042 ObjectHelper.notNull(exchange, "exchange");
043 ObjectHelper.notNull(endpoint, "endpoint");
044
045 // do not convert args as we do that manually later
046 Object args = exchange.getIn().removeHeader(EXEC_COMMAND_ARGS);
047 String cmd = getAndRemoveHeader(exchange.getIn(), EXEC_COMMAND_EXECUTABLE, endpoint.getExecutable(), String.class);
048 String dir = getAndRemoveHeader(exchange.getIn(), EXEC_COMMAND_WORKING_DIR, endpoint.getWorkingDir(), String.class);
049 long timeout = getAndRemoveHeader(exchange.getIn(), EXEC_COMMAND_TIMEOUT, endpoint.getTimeout(), Long.class);
050 String outFilePath = getAndRemoveHeader(exchange.getIn(), EXEC_COMMAND_OUT_FILE, endpoint.getOutFile(), String.class);
051 boolean useStderrOnEmptyStdout = getAndRemoveHeader(exchange.getIn(), EXEC_USE_STDERR_ON_EMPTY_STDOUT, endpoint.isUseStderrOnEmptyStdout(), Boolean.class);
052 InputStream input = exchange.getIn().getBody(InputStream.class);
053
054 // try to convert args to list at fist
055 List<String> argsList = exchange.getContext().getTypeConverter().convertTo(List.class, exchange, args);
056 if (argsList == null) {
057 // no we could not do that, then parse it as a string to a list
058 String s = endpoint.getArgs();
059 if (args != null) {
060 // use args from header instead from endpoint
061 s = exchange.getContext().getTypeConverter().convertTo(String.class, exchange, args);
062 }
063 argsList = splitToWhiteSpaceSeparatedTokens(s);
064 }
065
066 File outFile = outFilePath == null ? null : new File(outFilePath);
067 return new ExecCommand(cmd, argsList, dir, timeout, input, outFile, useStderrOnEmptyStdout);
068 }
069
070 public void writeOutput(Exchange exchange, ExecResult result) {
071 ObjectHelper.notNull(exchange, "exchange");
072 ObjectHelper.notNull(result, "result");
073
074 if (exchange.getPattern().isOutCapable()) {
075 writeOutputInMessage(exchange.getOut(), result);
076 exchange.getOut().getHeaders().putAll(exchange.getIn().getHeaders());
077 } else {
078 writeOutputInMessage(exchange.getIn(), result);
079 }
080 }
081
082 /**
083 * Write the {@link ExecResult} in the message body. Write the stderr and
084 * the exit value for convenience in the message headers. <br>
085 * The stdout and/or resultFile should be accessible using a converter or
086 * using the result object directly.
087 *
088 * @param message a Camel message
089 * @param result an {@link ExecResult} instance
090 */
091 protected void writeOutputInMessage(Message message, ExecResult result) {
092 message.setHeader(EXEC_STDERR, result.getStderr());
093 message.setHeader(EXEC_EXIT_VALUE, result.getExitValue());
094 message.setBody(result);
095 }
096
097 /**
098 * Gets and removes the <code> <code>headerName</code> header form the input
099 * <code>message</code> (the header will not be propagated)
100 */
101 protected <T> T getAndRemoveHeader(Message message, String headerName, T defaultValue, Class<T> headerType) {
102 T h = message.getHeader(headerName, defaultValue, headerType);
103 message.removeHeader(headerName);
104 return h;
105 }
106 }