Unverified Commit 0eaa74b3 authored by Sebastian Schüpbach's avatar Sebastian Schüpbach
Browse files

add a put method to sftp wrapper


Signed-off-by: Sebastian Schüpbach's avatarSebastian Schüpbach <sebastian.schuepbach@unibas.ch>
parent 56a93d6d
Pipeline #14800 passed with stage
in 2 minutes and 25 seconds
.idea
.gradle
build
\ No newline at end of file
build
out
\ No newline at end of file
......@@ -15,24 +15,20 @@
*/
package org.memobase.sftp
import java.io.Closeable
import java.io.File
import java.net.ConnectException
import java.net.UnknownHostException
import net.schmizz.sshj.SSHClient
import net.schmizz.sshj.sftp.FileAttributes
import net.schmizz.sshj.sftp.FileMode
import net.schmizz.sshj.sftp.OpenMode
import net.schmizz.sshj.sftp.RemoteFile
import net.schmizz.sshj.sftp.SFTPClient
import net.schmizz.sshj.sftp.SFTPException
import net.schmizz.sshj.sftp.*
import net.schmizz.sshj.transport.verification.PromiscuousVerifier
import net.schmizz.sshj.userauth.UserAuthException
import org.apache.logging.log4j.LogManager
import org.memobase.exceptions.SftpClientException
import org.memobase.settings.SftpSettings
import sftp.RemoteInputStream
import java.io.Closeable
import java.io.File
import java.io.IOException
import java.io.InputStream
import java.net.ConnectException
import java.net.UnknownHostException
/**
* A small wrapper around the net.schmizz.sshj.SFTPClient.
......@@ -43,8 +39,10 @@ import java.io.InputStream
class SftpClient(sftpSettings: SftpSettings) : Closeable {
private val log = LogManager.getLogger("SftpClient")
private val ssh = SSHClient()
// is public to make it easier to test workarounds.
private val instance: SFTPClient
init {
try {
ssh.addHostKeyVerifier(PromiscuousVerifier())
......@@ -101,6 +99,22 @@ class SftpClient(sftpSettings: SftpSettings) : Closeable {
}
}
/**
* Saves a file on the server.
*
* @param sourcePath: Path to file on local system
* @param destPath: Path to file on sFTP server
*
* @throws SftpClientException
*/
fun put(sourcePath: String, destPath: String) {
try {
instance.put(sourcePath, destPath)
} catch (ex: IOException) {
throw SftpClientException(ex.localizedMessage)
}
}
/**
* Checks if a file on the server exists and is a regular file (e.g. not a directory).
*
......
......@@ -76,7 +76,7 @@ class TestSftpClient {
val files = sftpClient.listFiles("/memobase/test_record_set_1")
assertThat(files)
.hasSize(1)
.hasSize(1)
}
@Test
......@@ -84,7 +84,14 @@ class TestSftpClient {
val files = sftpClient.listFiles("/memobase/test_record_set_2")
assertThat(files)
.hasSize(3)
.hasSize(3)
}
@Test
fun `test sftp client puts file`() {
sftpClient.put("src/test/resources/data/file1.txt", "/memobase/put_file1.txt")
assertThat(sftpServer.existsFile("/memobase/put_file1.txt")).isTrue()
}
@ExperimentalStdlibApi
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment