/*
   Copyright 2013 Nationale-Nederlanden

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
package nl.nn.adapterframework.senders;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import nl.nn.adapterframework.core.IPipeLineSession;
import nl.nn.adapterframework.core.ParameterException;
import nl.nn.adapterframework.core.SenderException;
import nl.nn.adapterframework.core.TimeOutException;
import nl.nn.adapterframework.parameters.ParameterValueList;
import nl.nn.adapterframework.stream.Message;
import nl.nn.adapterframework.util.ProcessUtil;

/**
 * Sender that executes either its input or a fixed line, with all parametervalues appended, as a command.
 * <table border="1">
 * <p><b>Parameters:</b>
 * <tr><th>name</th><th>type</th><th>remarks</th></tr>
 * <tr><td>&nbsp;</td><td>the values of all parameters present are appended as arguments to the command</td></tr>
 * </table>
 * </p>
 *
 * @since   4.8
 * @author  Gerrit van Brakel
 */
public class CommandSender extends SenderWithParametersBase {

	private String command;
	private int timeOut = 0;
	private boolean commandWithArguments = false;
	private boolean synchronous=true;

	@Override
	public Message sendMessage(Message message, IPipeLineSession session) throws SenderException, TimeOutException {
		List commandline;
		if (StringUtils.isNotEmpty(getCommand())) {
			commandline = commandToList(getCommand());
		} else {
			try {
				commandline = commandToList(message.asString());
			} catch (IOException e) {
				throw new SenderException(getLogPrefix(),e);
			}
		}
		if (paramList!=null) {
			ParameterValueList pvl;
			try {
				pvl = paramList.getValues(message, session);
			} catch (ParameterException e) {
				throw new SenderException("Could not extract parametervalues",e);
			}
			for (int i=0; i<pvl.size(); i++) {
				commandline.add(pvl.getParameterValue(i).getValue());
			}
		}
		return new Message(ProcessUtil.executeCommand(commandline, timeOut));
	}

	private List commandToList(String command) {
		List list;
		if (commandWithArguments) {
			list=ProcessUtil.splitUpCommandString(command);
		} else {
			list = new ArrayList();
			list.add(command);
		}
		return list;
	}

	@Override
	public boolean isSynchronous() {
		return synchronous;
	}

	public void setCommand(String string) {
		command = string;
	}
	public String getCommand() {
		return command;
	}

	public void setTimeOut(int timeOut) {
		this.timeOut = timeOut;
	}
	public int getTimeOut() {
		return timeOut;
	}

	public void setCommandWithArguments(boolean commandWithArguments) {
		this.commandWithArguments = commandWithArguments;
	}
	public boolean getCommandWithArguments() {
		return commandWithArguments;
	}

}

@@ -35,16 +35,34 @@
 erlanden
+, 2021 WeAreFrank!
 %0A%0A   Lic
@@ -980,32 +980,75 @@
 meOutException;%0A
+import nl.nn.adapterframework.doc.IbisDoc;%0A
 import nl.nn.ada
@@ -2882,239 +2882,808 @@
 %7D%0A%0A%09
-public void setCommand(String string) %7B%0A%09%09command = string;%0A%09%7D%0A%09public String getCommand() %7B%0A%09%09return command;%0A%09%7D%0A%0A%09public void setTimeOut(int timeOut) %7B%0A%09%09this.timeOut = timeOut;%0A%09%7D%0A%09public int getTimeOut() %7B%0A%09%09return timeOut;%0A%09%7D%0A
+@IbisDoc(%7B %221%22, %22The command to be executed. Note: Executing a command in WAS requires %3C%3CALL FILES%3E%3E execute permission to avoid that provide the absolute path of the command. Absolute path can be found with the following command 'which -a %7BcommandName%7D'%22, %22%22 %7D)%0A%09public void setCommand(String string) %7B%0A%09%09command = string;%0A%09%7D%0A%09public String getCommand() %7B%0A%09%09return command;%0A%09%7D%0A%0A%09@IbisDoc(%7B %222%22, %22The number of seconds to execute a command. If the limit is exceeded, a TimeoutException is thrown. A value of 0 means execution time is not limited%22, %220%22 %7D)%0A%09public void setTimeOut(int timeOut) %7B%0A%09%09this.timeOut = timeOut;%0A%09%7D%0A%09public int getTimeOut() %7B%0A%09%09return timeOut;%0A%09%7D%0A%0A%09@IbisDoc(%7B %223%22, %22In case the command that will be executed contains arguments then this flag should be set to true%22, %22false%22 %7D)
 %0A%09pu

Add ibisdoc for commandsender params (#1381)