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 if (LOG.isDebugEnabled()) { 068 LOG.debug("Parsing argument String to a List: " + s); 069 } 070 argsList = splitToWhiteSpaceSeparatedTokens(s); 071 } 072 073 File outFile = outFilePath == null ? null : new File(outFilePath); 074 return new ExecCommand(cmd, argsList, dir, timeout, input, outFile, useStderrOnEmptyStdout); 075 } 076 077 public void writeOutput(Exchange exchange, ExecResult result) { 078 ObjectHelper.notNull(exchange, "exchange"); 079 ObjectHelper.notNull(result, "result"); 080 081 if (exchange.getPattern().isOutCapable()) { 082 writeOutputInMessage(exchange.getOut(), result); 083 exchange.getOut().getHeaders().putAll(exchange.getIn().getHeaders()); 084 } else { 085 writeOutputInMessage(exchange.getIn(), result); 086 } 087 } 088 089 /** 090 * Write the {@link ExecResult} in the message body. Write the stderr and 091 * the exit value for convenience in the message headers. <br> 092 * The stdout and/or resultFile should be accessible using a converter or 093 * using the result object directly. 094 * 095 * @param message a Camel message 096 * @param result an {@link ExecResult} instance 097 */ 098 protected void writeOutputInMessage(Message message, ExecResult result) { 099 message.setHeader(EXEC_STDERR, result.getStderr()); 100 message.setHeader(EXEC_EXIT_VALUE, result.getExitValue()); 101 message.setBody(result); 102 } 103 104 /** 105 * Gets and removes the <code> <code>headerName</code> header form the input 106 * <code>message</code> (the header will not be propagated) 107 */ 108 protected <T> T getAndRemoveHeader(Message message, String headerName, T defaultValue, Class<T> headerType) { 109 T h = message.getHeader(headerName, defaultValue, headerType); 110 message.removeHeader(headerName); 111 return h; 112 } 113 }