This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch camel-4.8.x
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/camel-4.8.x by this push:
new a9157593e80 CAMEL-22059: camel-ssh - Calling 2nd time does not keep
correct exit value header
a9157593e80 is described below
commit a9157593e80dd8191fa767bc671206b25c7edac3
Author: Claus Ibsen <[email protected]>
AuthorDate: Tue May 13 08:14:53 2025 +0200
CAMEL-22059: camel-ssh - Calling 2nd time does not keep correct exit value
header
---
.../apache/camel/component/ssh/SshProducer.java | 7 ++-
.../component/ssh/ExitCodeCommandFactory.java | 47 +++++++++++++++
.../component/ssh/SshProducerExitCodeTest.java | 68 ++++++++++++++++++++++
3 files changed, 120 insertions(+), 2 deletions(-)
diff --git
a/components/camel-ssh/src/main/java/org/apache/camel/component/ssh/SshProducer.java
b/components/camel-ssh/src/main/java/org/apache/camel/component/ssh/SshProducer.java
index a9232cf8021..0fd9cccd171 100644
---
a/components/camel-ssh/src/main/java/org/apache/camel/component/ssh/SshProducer.java
+++
b/components/camel-ssh/src/main/java/org/apache/camel/component/ssh/SshProducer.java
@@ -80,6 +80,11 @@ public class SshProducer extends DefaultProducer {
endpoint.isFailOnUnknownHost()));
}
SshResult result = SshHelper.sendExecCommand(headers, command,
endpoint, client);
+
+ // propagate headers
+ exchange.getOut().getHeaders().putAll(in.getHeaders());
+
+ // store result
exchange.getOut().setBody(result.getStdout());
exchange.getOut().setHeader(SshConstants.EXIT_VALUE,
result.getExitValue());
exchange.getOut().setHeader(SshConstants.STDERR,
result.getStderr());
@@ -87,7 +92,5 @@ public class SshProducer extends DefaultProducer {
throw new CamelExchangeException("Cannot execute command: " +
command, exchange, e);
}
- // propagate headers
- exchange.getOut().getHeaders().putAll(in.getHeaders());
}
}
diff --git
a/components/camel-ssh/src/test/java/org/apache/camel/component/ssh/ExitCodeCommandFactory.java
b/components/camel-ssh/src/test/java/org/apache/camel/component/ssh/ExitCodeCommandFactory.java
new file mode 100644
index 00000000000..8b3f81d562c
--- /dev/null
+++
b/components/camel-ssh/src/test/java/org/apache/camel/component/ssh/ExitCodeCommandFactory.java
@@ -0,0 +1,47 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.camel.component.ssh;
+
+import java.util.concurrent.atomic.AtomicInteger;
+
+import org.apache.sshd.server.ExitCallback;
+import org.apache.sshd.server.channel.ChannelSession;
+import org.apache.sshd.server.command.Command;
+
+public class ExitCodeCommandFactory extends EchoCommandFactory {
+
+ @Override
+ public Command createCommand(ChannelSession channelSession, String
command) {
+ return new ExitCodeCommand(command);
+ }
+
+ public static class ExitCodeCommand extends EchoCommand {
+
+ private static final AtomicInteger exit = new AtomicInteger();
+
+ public ExitCodeCommand(String command) {
+ super(command);
+ }
+
+ @Override
+ public void setExitCallback(ExitCallback callback) {
+ super.setExitCallback((i, s, b) -> {
+ callback.onExit(exit.incrementAndGet());
+ });
+ }
+ }
+}
diff --git
a/components/camel-ssh/src/test/java/org/apache/camel/component/ssh/SshProducerExitCodeTest.java
b/components/camel-ssh/src/test/java/org/apache/camel/component/ssh/SshProducerExitCodeTest.java
new file mode 100644
index 00000000000..3e0fe9b2323
--- /dev/null
+++
b/components/camel-ssh/src/test/java/org/apache/camel/component/ssh/SshProducerExitCodeTest.java
@@ -0,0 +1,68 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.camel.component.ssh;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.junit.jupiter.api.Test;
+
+public class SshProducerExitCodeTest extends SshComponentTestSupport {
+
+ @Override
+ public void doPreSetup() throws Exception {
+ super.doPreSetup();
+ sshd.setCommandFactory(new ExitCodeCommandFactory());
+ }
+
+ @Test
+ public void testExitCode() throws Exception {
+ MockEndpoint mockEndpoint = getMockEndpoint("mock:result");
+ mockEndpoint.expectedHeaderReceived(SshConstants.EXIT_VALUE, 2);
+
+ Map<String, Object> headers = new HashMap<>();
+ headers.put(SshConstants.USERNAME_HEADER, "smx");
+ headers.put(SshConstants.PASSWORD_HEADER, "smx");
+
+ template.sendBodyAndHeaders("direct:start", "Hello", headers);
+
+ mockEndpoint.assertIsSatisfied();
+ }
+
+ @Override
+ protected RouteBuilder createRouteBuilder() throws Exception {
+ return new RouteBuilder() {
+ @Override
+ public void configure() {
+ // 1st try (expected: ExitValue=1)
+ from("direct:start")
+ .setBody().simple("exit 1")
+ .to("ssh:localhost:" + port)
+ .log("1st try: ExitValue=${header.CamelSshExitValue}")
+
+ // 2nd try (expected: ExitValue=2)
+ .setBody().simple("exit 2")
+ .to("ssh:localhost:" + port)
+ .log("2nd try: ExitValue=${header.CamelSshExitValue}")
+
+ .to("mock:result");
+ }
+ };
+ }
+}