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 String cmd = getAndRemoveHeader(exchange.getIn(), EXEC_COMMAND_EXECUTABLE, endpoint.getExecutable(), String.class);
046 List<String> argsList = getAndRemoveHeader(exchange.getIn(), EXEC_COMMAND_ARGS, null, List.class);
047 String dir = getAndRemoveHeader(exchange.getIn(), EXEC_COMMAND_WORKING_DIR, endpoint.getWorkingDir(), String.class);
048 long timeout = getAndRemoveHeader(exchange.getIn(), EXEC_COMMAND_TIMEOUT, endpoint.getTimeout(), Long.class);
049 String outFilePath = getAndRemoveHeader(exchange.getIn(), EXEC_COMMAND_OUT_FILE, endpoint.getOutFile(), String.class);
050 boolean useStderrOnEmptyStdout = getAndRemoveHeader(exchange.getIn(), EXEC_USE_STDERR_ON_EMPTY_STDOUT, endpoint.isUseStderrOnEmptyStdout(), Boolean.class);
051 InputStream input = exchange.getIn().getBody(InputStream.class);
052
053 if (argsList == null) {
054 // do the URI parsing, only if the arguments are not set
055 argsList = splitToWhiteSpaceSeparatedTokens(endpoint.getArgs());
056 }
057
058 File outFile = outFilePath == null ? null : new File(outFilePath);
059 return new ExecCommand(cmd, argsList, dir, timeout, input, outFile, useStderrOnEmptyStdout);
060 }
061
062 public void writeOutput(Exchange exchange, ExecResult result) {
063 ObjectHelper.notNull(exchange, "exchange");
064 ObjectHelper.notNull(result, "result");
065
066 if (exchange.getPattern().isOutCapable()) {
067 writeOutputInMessage(exchange.getOut(), result);
068 exchange.getOut().getHeaders().putAll(exchange.getIn().getHeaders());
069 } else {
070 writeOutputInMessage(exchange.getIn(), result);
071 }
072 }
073
074 /**
075 * Write the {@link ExecResult} in the message body. Write the stderr and
076 * the exit value for convenience in the message headers. <br>
077 * The stdout and/or resultFile should be accessible using a converter or
078 * using the result object directly.
079 *
080 * @param message a Camel message
081 * @param result an {@link ExecResult} instance
082 */
083 protected void writeOutputInMessage(Message message, ExecResult result) {
084 message.setHeader(EXEC_STDERR, result.getStderr());
085 message.setHeader(EXEC_EXIT_VALUE, result.getExitValue());
086 message.setBody(result);
087 }
088
089 /**
090 * Gets and removes the <code> <code>headerName</code> header form the input
091 * <code>message</code> (the header will not be propagated)
092 */
093 protected <T> T getAndRemoveHeader(Message message, String headerName, T defaultValue, Class<T> headerType) {
094 T h = message.getHeader(headerName, defaultValue, headerType);
095 message.removeHeader(headerName);
096 return h;
097 }
098 }