Updated Branches: refs/heads/camel-2.11.x f9ae70bec -> 3d2b9a89a
CAMEL-6591: Added sendNoop to sftp, which helps keep the connection alive. Thanks to Stephan Siano for the patch. Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/3d2b9a89 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/3d2b9a89 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/3d2b9a89 Branch: refs/heads/camel-2.11.x Commit: 3d2b9a89a47006e64abfc970f5c9f92a3b83800a Parents: f9ae70b Author: Claus Ibsen <[email protected]> Authored: Thu Aug 22 12:32:29 2013 +0200 Committer: Claus Ibsen <[email protected]> Committed: Thu Aug 22 15:01:37 2013 +0200 ---------------------------------------------------------------------- .../component/file/remote/SftpOperations.java | 12 ++++- .../file/remote/sftp/SftpServerTestSupport.java | 10 +++- .../sftp/SftpSimpleProduceDisconnectTest.java | 56 ++++++++++++++++++++ 3 files changed, 75 insertions(+), 3 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/3d2b9a89/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpOperations.java ---------------------------------------------------------------------- diff --git a/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpOperations.java b/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpOperations.java index 22f8ed4..0fac88a 100644 --- a/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpOperations.java +++ b/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpOperations.java @@ -951,8 +951,16 @@ public class SftpOperations implements RemoteFileOperations<ChannelSftp.LsEntry> } public boolean sendNoop() throws GenericFileOperationFailedException { - // is not implemented - return true; + if (isConnected()) { + try { + session.sendIgnore(); + return true; + } catch (Exception e) { + LOG.debug("SFTP session was closed. Ignoring this exception.", e); + return false; + } + } + return false; } public boolean sendSiteCommand(String command) throws GenericFileOperationFailedException { http://git-wip-us.apache.org/repos/asf/camel/blob/3d2b9a89/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/sftp/SftpServerTestSupport.java ---------------------------------------------------------------------- diff --git a/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/sftp/SftpServerTestSupport.java b/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/sftp/SftpServerTestSupport.java index 46e7b0b..72d09f4 100644 --- a/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/sftp/SftpServerTestSupport.java +++ b/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/sftp/SftpServerTestSupport.java @@ -42,13 +42,17 @@ public class SftpServerTestSupport extends BaseServerTestSupport { protected SshServer sshd; protected boolean canTest; - @SuppressWarnings("unchecked") @Override @Before public void setUp() throws Exception { deleteDirectory(FTP_ROOT_DIR); super.setUp(); + setUpServer(); + } + + @SuppressWarnings("unchecked") + protected void setUpServer() throws Exception { canTest = true; try { sshd = SshServer.setUpDefaultServer(); @@ -87,6 +91,10 @@ public class SftpServerTestSupport extends BaseServerTestSupport { public void tearDown() throws Exception { super.tearDown(); + tearDownServer(); + } + + protected void tearDownServer() { if (sshd != null) { try { // stop asap as we may hang forever http://git-wip-us.apache.org/repos/asf/camel/blob/3d2b9a89/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/sftp/SftpSimpleProduceDisconnectTest.java ---------------------------------------------------------------------- diff --git a/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/sftp/SftpSimpleProduceDisconnectTest.java b/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/sftp/SftpSimpleProduceDisconnectTest.java new file mode 100644 index 0000000..fd7a97e --- /dev/null +++ b/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/sftp/SftpSimpleProduceDisconnectTest.java @@ -0,0 +1,56 @@ +/** + * 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.file.remote.sftp; + +import java.io.File; + +import org.apache.camel.Exchange; +import org.junit.Test; + +/** + * @version + */ +public class SftpSimpleProduceDisconnectTest extends SftpServerTestSupport { + + @Override + public boolean isUseRouteBuilder() { + return false; + } + + @Test + public void testSftpSimpleProduce() throws Exception { + if (!canTest()) { + return; + } + + template.sendBodyAndHeader("sftp://localhost:" + getPort() + "/" + FTP_ROOT_DIR + "?username=admin&password=admin", "Hello World", Exchange.FILE_NAME, "hello.txt"); + + File file = new File(FTP_ROOT_DIR + "/hello.txt"); + assertTrue("File should exist: " + file, file.exists()); + assertEquals("Hello World", context.getTypeConverter().convertTo(String.class, file)); + + // restart the SFTP server now + tearDownServer(); + setUpServer(); + + template.sendBodyAndHeader("sftp://localhost:" + getPort() + "/" + FTP_ROOT_DIR + "?username=admin&password=admin", "Hello World", Exchange.FILE_NAME, "hello1.txt"); + + file = new File(FTP_ROOT_DIR + "/hello1.txt"); + assertTrue("File should exist: " + file, file.exists()); + assertEquals("Hello World", context.getTypeConverter().convertTo(String.class, file)); + } +}
