X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;f=cms%2Forg.argeo.cms.integration%2Fsrc%2Forg%2Fargeo%2Fssh%2FSftp.java;fp=cms%2Forg.argeo.cms.integration%2Fsrc%2Forg%2Fargeo%2Fssh%2FSftp.java;h=da10b961fd3792fbedf14f959c1fb1dce853d411;hb=d3bee9f6a2c9aea9bc9ab631e935794dcba39b03;hp=0000000000000000000000000000000000000000;hpb=ecc22e604e47533c79de9cecdcdeacbc752cbff1;p=gpl%2Fargeo-slc.git diff --git a/cms/org.argeo.cms.integration/src/org/argeo/ssh/Sftp.java b/cms/org.argeo.cms.integration/src/org/argeo/ssh/Sftp.java new file mode 100644 index 000000000..da10b961f --- /dev/null +++ b/cms/org.argeo.cms.integration/src/org/argeo/ssh/Sftp.java @@ -0,0 +1,42 @@ +package org.argeo.ssh; + +import java.io.IOException; +import java.net.URI; +import java.nio.file.FileSystem; +import java.nio.file.Path; + +import org.apache.sshd.client.subsystem.sftp.fs.SftpFileSystem; + +/** Create an SFTP {@link FileSystem}. */ +public class Sftp extends AbstractSsh { + private URI uri; + + private SftpFileSystem fileSystem; + + public Sftp(String username, String host, int port) { + this(AbstractSsh.toUri(username, host, port)); + } + + public Sftp(URI uri) { + this.uri = uri; + openSession(uri); + } + + public FileSystem getFileSystem() { + if (fileSystem == null) { + try { + authenticate(); + fileSystem = getSftpFileSystemProvider().newFileSystem(getSession()); + } catch (IOException e) { + throw new IllegalStateException(e); + } + } + return fileSystem; + } + + public Path getBasePath() { + String p = uri.getPath() != null ? uri.getPath() : "/"; + return getFileSystem().getPath(p); + } + +}