Package com.adaptris.sftp
Class SftpClient
- java.lang.Object
-
- com.adaptris.filetransfer.FileTransferClientImp
-
- com.adaptris.sftp.SftpClient
-
- All Implemented Interfaces:
FileTransferClient
,java.io.Closeable
,java.lang.AutoCloseable
public class SftpClient extends FileTransferClientImp
Provides SSH File Transfer Protocol implementation of FileTransferClient- See Also:
FileTransferClient
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.String
NO_KERBEROS_AUTH
static java.lang.String
SSH_PREFERRED_AUTHENTICATIONS
-
Fields inherited from class com.adaptris.filetransfer.FileTransferClientImp
logR
-
-
Constructor Summary
Constructors Constructor Description SftpClient(java.lang.String host)
Constructor assuming the default SSH port.SftpClient(java.lang.String host, int port, int timeout)
Constructor.SftpClient(java.lang.String host, int port, int timeout, java.io.File knownHostsFile, ConfigBuilder configBuilder)
Constructor.SftpClient(java.net.InetAddress addr)
Constructor assuming the default SSH port.SftpClient(java.net.InetAddress addr, int port, int timeout)
Constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
chdir(java.lang.String dir)
Change the remote working directory to that suppliedvoid
connect(java.lang.String user, byte[] prvKey, byte[] prvKeyPwd)
Connect and login into an account on the SFTP server.void
connect(java.lang.String user, java.lang.String password)
Connect and login into an account on the FTP server.void
delete(java.lang.String remoteFile)
Delete the specified remote filejava.lang.String[]
dir(java.lang.String dirname, boolean full)
List a directory's contents as an array of strings.java.lang.String[]
dir(java.lang.String directory, java.io.FileFilter filter)
List a directory's contentsvoid
disconnect()
Quit the FTP sessionvoid
get(java.io.OutputStream destStream, java.lang.String remoteFile)
Get data from the FTP server.byte[]
get(java.lang.String remoteFile)
Get data from the FTP server.long
getKeepAliveTimeout()
Get the time to wait between sending control connection keepalive messages.boolean
isConnected()
Check if this client is still connected to its target Any errors return a false This is used to check cached connections are still workingboolean
isDirectory(java.lang.String path)
long
lastModified(java.lang.String remoteFile)
Return the last modified time for the given path.java.util.Date
lastModifiedDate(java.lang.String remoteFile)
Return the last modified date for the given path.void
mkdir(java.lang.String dir)
Create the specified remote working directoryvoid
put(java.io.InputStream srcStream, java.lang.String remoteFile, boolean append)
Put a stream of data onto the FTP server.void
rename(java.lang.String from, java.lang.String to)
Rename a file or directoryvoid
rmdir(java.lang.String dir)
Delete the specified remote working directoryvoid
setKeepAliveTimeout(long seconds)
Set the time to wait between sending control connection keepalive messages when processing file upload or download.void
setKnownHosts(java.lang.String knownHostsFilename)
SftpClient
withAdditionalDebug(boolean onoff)
SftpClient
withKeepAliveTimeout(long seconds)
-
Methods inherited from class com.adaptris.filetransfer.FileTransferClientImp
ensureNotNull, isAdditionaDebug, log, setAdditionalDebug
-
-
-
-
Field Detail
-
NO_KERBEROS_AUTH
public static final java.lang.String NO_KERBEROS_AUTH
- See Also:
- Constant Field Values
-
SSH_PREFERRED_AUTHENTICATIONS
public static final java.lang.String SSH_PREFERRED_AUTHENTICATIONS
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
SftpClient
public SftpClient(java.lang.String host) throws SftpException
Constructor assuming the default SSH port.- Parameters:
host
- the remote ssh host.- Throws:
SftpException
-
SftpClient
public SftpClient(java.net.InetAddress addr) throws SftpException
Constructor assuming the default SSH port.- Parameters:
addr
- the remote ssh host.- Throws:
SftpException
-
SftpClient
public SftpClient(java.net.InetAddress addr, int port, int timeout) throws SftpException
Constructor.- Parameters:
addr
- the remote ssh host.port
- the ssh port.timeout
- the timeout;- Throws:
SftpException
-
SftpClient
public SftpClient(java.lang.String host, int port, int timeout) throws SftpException
Constructor.- Parameters:
host
- the hostport
- the porttimeout
- the timeout;- Throws:
SftpException
-
SftpClient
public SftpClient(java.lang.String host, int port, int timeout, java.io.File knownHostsFile, ConfigBuilder configBuilder) throws SftpException
Constructor.- Parameters:
host
- the hostport
- the porttimeout
- the timeout;configBuilder
- any required behaviour for this client;- Throws:
SftpException
-
-
Method Detail
-
connect
public void connect(java.lang.String user, java.lang.String password) throws java.io.IOException, FileTransferException
Description copied from interface:FileTransferClient
Connect and login into an account on the FTP server. This completes the entire login process- Parameters:
user
- user namepassword
- user's password- Throws:
FileTransferException
- if an FTP specific exception occursjava.io.IOException
- if a comms error occurs- See Also:
FileTransferClient.connect(java.lang.String, java.lang.String)
-
connect
public void connect(java.lang.String user, byte[] prvKey, byte[] prvKeyPwd) throws FileTransferException
Connect and login into an account on the SFTP server. This completes the entire login process- Parameters:
user
- user nameprvKey
- private key as part of public/private key pairprvKeyPwd
- the password for accessing the private key.- Throws:
FileTransferException
- if an FTP specific exception occurs
-
dir
public java.lang.String[] dir(java.lang.String directory, java.io.FileFilter filter) throws FileTransferException, java.io.IOException
Description copied from interface:FileTransferClient
List a directory's contentsNote that although we use a standard
FileFilter
interface here operating onFile
; it actually usesRemoteFile
instead which overrides information that can be obtained from the remote server. Other standardFile
operations will not be supported, and may ultimately cause a runtime exception.- Parameters:
directory
- the directory to list.filter
- the filefilter mask to use- Returns:
- an array of strings containing the listing
- Throws:
FileTransferException
- on FTP Specific exception error.java.io.IOException
- on comms error.
-
dir
public java.lang.String[] dir(java.lang.String dirname, boolean full) throws java.io.IOException, FileTransferException
Description copied from interface:FileTransferClient
List a directory's contents as an array of strings.A detailed listing is available, otherwise just filenames are provided. The detailed listing varies in details depending on OS and FTP server. Note that a full listing can be used on a file name to obtain information about a file. The special files "." and ".." are ignored.
- Parameters:
dirname
- name of directory (not a file mask)full
- true if detailed listing required false otherwise- Returns:
- an array of directory listing strings
- Throws:
FileTransferException
- if an FTP specific exception occursjava.io.IOException
- if a comms error occurs- See Also:
FileTransferClient.dir(java.lang.String, boolean)
-
disconnect
public void disconnect() throws java.io.IOException, FileTransferException
Description copied from interface:FileTransferClient
Quit the FTP session- Throws:
FileTransferException
- if an FTP specific exception occursjava.io.IOException
- if a comms error occurs- See Also:
FileTransferClient.disconnect()
-
put
public void put(java.io.InputStream srcStream, java.lang.String remoteFile, boolean append) throws java.io.IOException, FileTransferException
Description copied from interface:FileTransferClient
Put a stream of data onto the FTP server.- Parameters:
srcStream
- input stream of data to putremoteFile
- name of remote fileappend
- true if appending, false otherwise- Throws:
FileTransferException
- if an FTP specific exception occursjava.io.IOException
- if a comms error occurs- See Also:
FileTransferClient.put(java.io.InputStream, java.lang.String, boolean)
-
get
public void get(java.io.OutputStream destStream, java.lang.String remoteFile) throws java.io.IOException, FileTransferException
Description copied from interface:FileTransferClient
Get data from the FTP server.- Parameters:
destStream
- data stream to write data toremoteFile
- name of remote file- Throws:
FileTransferException
- if an FTP specific exception occursjava.io.IOException
- if a comms error occurs- See Also:
FileTransferClient.get(java.io.OutputStream, java.lang.String)
-
get
public byte[] get(java.lang.String remoteFile) throws java.io.IOException, FileTransferException
Description copied from interface:FileTransferClient
Get data from the FTP server.- Parameters:
remoteFile
- name of remote file- Returns:
- a byte array.
- Throws:
FileTransferException
- if an FTP specific exception occursjava.io.IOException
- if a comms error occurs- See Also:
FileTransferClient.get(java.lang.String)
-
delete
public void delete(java.lang.String remoteFile) throws java.io.IOException, FileTransferException
Description copied from interface:FileTransferClient
Delete the specified remote file- Parameters:
remoteFile
- name of remote file to delete- Throws:
FileTransferException
- if an FTP specific exception occursjava.io.IOException
- if a comms error occurs- See Also:
FileTransferClient.delete(java.lang.String)
-
rename
public void rename(java.lang.String from, java.lang.String to) throws java.io.IOException, FileTransferException
Description copied from interface:FileTransferClient
Rename a file or directory- Parameters:
from
- name of file or directory to renameto
- intended name- Throws:
FileTransferException
- if an FTP specific exception occursjava.io.IOException
- if a comms error occurs
-
rmdir
public void rmdir(java.lang.String dir) throws java.io.IOException, FileTransferException
Description copied from interface:FileTransferClient
Delete the specified remote working directory- Parameters:
dir
- name of remote directory to delete- Throws:
FileTransferException
- if an FTP specific exception occursjava.io.IOException
- if a comms error occurs
-
mkdir
public void mkdir(java.lang.String dir) throws java.io.IOException, FileTransferException
Description copied from interface:FileTransferClient
Create the specified remote working directory- Parameters:
dir
- name of remote directory to create- Throws:
FileTransferException
- if an FTP specific exception occursjava.io.IOException
- if a comms error occurs
-
chdir
public void chdir(java.lang.String dir) throws java.io.IOException, FileTransferException
Description copied from interface:FileTransferClient
Change the remote working directory to that supplied- Parameters:
dir
- name of remote directory to change to- Throws:
FileTransferException
- if an FTP specific exception occursjava.io.IOException
- if a comms error occurs
-
isDirectory
public boolean isDirectory(java.lang.String path) throws java.io.IOException
- Throws:
java.io.IOException
-
lastModifiedDate
public java.util.Date lastModifiedDate(java.lang.String remoteFile) throws java.io.IOException, FileTransferException
Description copied from interface:FileTransferClient
Return the last modified date for the given path.- Parameters:
remoteFile
- the path to the filename.- Returns:
- the date.
- Throws:
FileTransferException
- if an FTP specific exception occursjava.io.IOException
- if a comms error occurs- See Also:
FileTransferClient.lastModifiedDate(java.lang.String)
-
lastModified
public long lastModified(java.lang.String remoteFile) throws java.io.IOException, FileTransferException
Description copied from interface:FileTransferClient
Return the last modified time for the given path.- Parameters:
remoteFile
- the path to the filename.- Returns:
- the last modified time
- Throws:
FileTransferException
- if an FTP specific exception occursjava.io.IOException
- if a comms error occurs- See Also:
FileTransferClient.lastModified(java.lang.String)
-
getKeepAliveTimeout
public long getKeepAliveTimeout() throws FtpException
Description copied from interface:FileTransferClient
Get the time to wait between sending control connection keepalive messages.- Returns:
- time in seconds
- Throws:
FtpException
-
setKeepAliveTimeout
public void setKeepAliveTimeout(long seconds) throws FtpException
Description copied from interface:FileTransferClient
Set the time to wait between sending control connection keepalive messages when processing file upload or download.- Parameters:
seconds
- time in seconds- Throws:
FtpException
-
withKeepAliveTimeout
public SftpClient withKeepAliveTimeout(long seconds) throws FtpException
- Throws:
FtpException
-
withAdditionalDebug
public SftpClient withAdditionalDebug(boolean onoff)
-
isConnected
public boolean isConnected()
Description copied from interface:FileTransferClient
Check if this client is still connected to its target Any errors return a false This is used to check cached connections are still working- Returns:
- true = connected, false = not connected or some error
-
setKnownHosts
public void setKnownHosts(java.lang.String knownHostsFilename) throws SftpException
- Throws:
SftpException
-
-