View Javadoc
1   package org.argeo.ssh;
2   
3   import java.io.IOException;
4   import java.net.URI;
5   import java.nio.file.FileSystem;
6   import java.nio.file.Path;
7   
8   import org.apache.sshd.client.subsystem.sftp.fs.SftpFileSystem;
9   
10  /** Create an SFTP {@link FileSystem}. */
11  public class Sftp extends AbstractSsh {
12  	private URI uri;
13  
14  	private SftpFileSystem fileSystem;
15  
16  	public Sftp(String username, String host, int port) {
17  		this(AbstractSsh.toUri(username, host, port));
18  	}
19  
20  	public Sftp(URI uri) {
21  		this.uri = uri;
22  		openSession(uri);
23  	}
24  
25  	public FileSystem getFileSystem() {
26  		if (fileSystem == null) {
27  			try {
28  				authenticate();
29  				fileSystem = getSftpFileSystemProvider().newFileSystem(getSession());
30  			} catch (IOException e) {
31  				throw new IllegalStateException(e);
32  			}
33  		}
34  		return fileSystem;
35  	}
36  
37  	public Path getBasePath() {
38  		String p = uri.getPath() != null ? uri.getPath() : "/";
39  		return getFileSystem().getPath(p);
40  	}
41  
42  }