public abstract class AbstractSession<T extends AbstractHost<?>> extends Object implements Closeable
close()
the session after use.
A session is not thread-safe and need external synchronization if
used with multiple threads.Modifier and Type | Class and Description |
---|---|
(package private) static class |
AbstractSession.CloseResourceOutputStream
Wrapper around the outputstream so that we can
close the related resource when the stream is closed.
|
(package private) static class |
AbstractSession.UploadSourceWrapper
Wraps an UploadSource implementation with a LocalSourceFile
implementation so that the file data can be sent to the remote
server with SSHj.
|
Modifier and Type | Field and Description |
---|---|
private static org.slf4j.Logger |
logger |
private T |
remote |
private net.schmizz.sshj.sftp.SFTPClient |
sftp |
private net.schmizz.sshj.SSHClient |
ssh |
Modifier | Constructor and Description |
---|---|
protected |
AbstractSession(T remote,
net.schmizz.sshj.SSHClient ssh)
Session are typically created from
RemoteHost.connect(int) . |
Modifier and Type | Method and Description |
---|---|
void |
close() |
void |
downloadFile(String fromPath,
DownloadTarget target)
Download a remote file to a local destination.
|
protected <R> CmdResult<R> |
execute(String cmd,
int timeout) |
CmdResult<String> |
executeCmd(String cmd,
int timeout)
Execute a command on the remote host.
|
T |
getHost()
Get the host that created this session.
|
private InputStream |
getRemoteInputStream(String fromPath,
FileMetaData metadata) |
private OutputStream |
getRemoteOutputStream(String toPath,
boolean overwrite,
FileMetaData metadata,
FilePermission permission) |
private net.schmizz.sshj.sftp.SFTPClient |
getSFTPClient() |
boolean |
isClosed()
Check if the session has been closed or not.
|
CmdResult<String> |
mkdirs(FilePermission permission,
String... paths)
Create directories on the remote server if they do not already
exists.
|
InputStream |
readFile(String fromPath,
FileMetaData metadata)
Read from a file on the remote server.
|
void |
uploadFile(UploadSource source,
String toPath,
FilePermission permission)
Upload a file with SFTP.
|
OutputStream |
writeFile(String toPath,
boolean overwrite,
FileMetaData metadata,
FilePermission permission)
Write to a file on the remote server.
|
private static final org.slf4j.Logger logger
private final T extends AbstractHost<?> remote
private final net.schmizz.sshj.SSHClient ssh
private net.schmizz.sshj.sftp.SFTPClient sftp
protected AbstractSession(T remote, net.schmizz.sshj.SSHClient ssh)
RemoteHost.connect(int)
.public boolean isClosed()
public void close() throws IOException
close
in interface Closeable
close
in interface AutoCloseable
IOException
public T getHost()
public CmdResult<String> executeCmd(String cmd, int timeout)
CmdResult
object.cmd
- The command to executetimeout
- Timeout in seconds to wait for the command to finishprivate net.schmizz.sshj.sftp.SFTPClient getSFTPClient() throws IOException
IOException
public CmdResult<String> mkdirs(FilePermission permission, String... paths)
permission
- The permission to give to directories that
are created. Null to use default permissions. Already
existing directories are not affected.paths
- The paths to createpublic void uploadFile(UploadSource source, String toPath, FilePermission permission)
public OutputStream writeFile(String toPath, boolean overwrite, FileMetaData metadata, FilePermission permission)
toPath
- Absolute path where the uploaded file should be storedoverwrite
- TRUE to overwrite existing filesmetadata
- Information about the file (or null if not important)permission
- Permission to set on the filepublic void downloadFile(String fromPath, DownloadTarget target)
fromPath
- The path of the remote filetarget
- The local target the remote file is downloaded topublic InputStream readFile(String fromPath, FileMetaData metadata)
fromPath
- The path of the remote filemetadata
- A metadata instance for collecting metadata
about the remote file or null if not important. The metadata
will be set before the file download is startedprivate InputStream getRemoteInputStream(String fromPath, FileMetaData metadata) throws IOException
IOException
private OutputStream getRemoteOutputStream(String toPath, boolean overwrite, FileMetaData metadata, FilePermission permission) throws IOException
IOException