com.sshtools.ant

Class Sftp


public class Sftp
extends SshSubTask

Basic SFTP client. Performs the following actions:

Nested Class Summary

static class
Sftp.Action
an action to perform, one of "send", "put", "recv", "get", "del", "delete", "list", "mkdir", "chmod"
protected class
Sftp.SftpDirectoryScanner

Field Summary

protected static String[]
ACTION_STRS
protected static int
CHMOD
protected static String[]
COMPLETED_ACTION_STRS
protected static int
DEL_FILES
protected static int
GET_FILES
protected static int
MK_DIR
protected static int
SEND_FILES

Fields inherited from class com.sshtools.ant.SshSubTask

parent, taskString

Method Summary

void
addFileset(FileSet set)
A set of files to upload or download
protected void
checkConfiguration()
Checks to see that all required parameters are set.
protected void
chmod(SftpClient sftp, String filename)
protected void
createParents(SftpClient sftp, String filename)
Creates all parent directories specified in a complete relative pathname.
protected void
delFile(SftpClient sftp, String filename)
Delete a file from the remote host.
void
execute(SshClient ssh)
Runs the task.
protected void
executeSFTPTask(SshClient ssh)
protected void
getFile(SftpClient sftp, String dir, String filename)
Retrieve a single file to the remote host.
protected boolean
isUpToDate(SftpClient sftp, File localFile, String remoteFile)
Checks to see if the remote file is current as compared with the local file.
protected void
makeRemoteDir(SftpClient sftp, String dir)
Create the specified directory on the remote host.
protected String
resolveFile(String file)
Correct a file path to correspond to the remote host requirements.
protected void
sendFile(SftpClient sftp, String dir, String filename)
Sends a single file to the remote host.
void
setAction(Sftp.Action action)
Sets the FTP action to be taken.
void
setChmod(String theMode)
Sets the file permission mode (Unix only) for files sent to the server.
void
setDepends(boolean depends)
Set to true to transmit only files that are new or changed from their remote counterparts.
void
setIgnoreNoncriticalErrors(boolean ignoreNoncriticalErrors)
set the flag to skip errors on directory creation.
void
setNewer(boolean newer)
A synonym for depends.
void
setRemotedir(String remotedir)
Sets the remote working directory
void
setSkipFailedTransfers(boolean skipFailedTransfers)
If true, enables unsuccessful file put, delete and get operations to be skipped with a warning and the remainder of the files still transferred.
void
setVerbose(boolean verbose)
Set to true to receive notification about each file as it is transferred.
protected void
transferFiles(SftpClient sftp)
Sends all files specified by the configured filesets to the remote server.
protected int
transferFiles(SftpClient sftp, FileSet fs)
For each file in the fileset, do the appropriate action: send, get, delete, or list.

Methods inherited from class com.sshtools.ant.SshSubTask

execute, log, log, setParent

Field Details

ACTION_STRS

protected static final String[] ACTION_STRS

CHMOD

protected static final int CHMOD
Field Value:
5

COMPLETED_ACTION_STRS

protected static final String[] COMPLETED_ACTION_STRS

DEL_FILES

protected static final int DEL_FILES
Field Value:
2

GET_FILES

protected static final int GET_FILES
Field Value:
1

MK_DIR

protected static final int MK_DIR
Field Value:
4

SEND_FILES

protected static final int SEND_FILES
Field Value:
0

Method Details

addFileset

public void addFileset(FileSet set)
A set of files to upload or download

checkConfiguration

protected void checkConfiguration()
            throws BuildException
Checks to see that all required parameters are set.

chmod

protected void chmod(SftpClient sftp,
                     String filename)
            throws IOException,
                   BuildException

createParents

protected void createParents(SftpClient sftp,
                             String filename)
            throws IOException,
                   BuildException
Creates all parent directories specified in a complete relative pathname. Attempts to create existing directories will not cause errors.

delFile

protected void delFile(SftpClient sftp,
                       String filename)
            throws IOException,
                   BuildException
Delete a file from the remote host.

execute

public void execute(SshClient ssh)
            throws BuildException
Runs the task.
Overrides:
execute in interface SshSubTask

executeSFTPTask

protected void executeSFTPTask(SshClient ssh)
            throws BuildException

getFile

protected void getFile(SftpClient sftp,
                       String dir,
                       String filename)
            throws IOException,
                   BuildException
Retrieve a single file to the remote host. filename may contain a relative path specification.

The file will then be retreived using the entire relative path spec - no attempt is made to change directories. It is anticipated that this may eventually cause problems with some FTP servers, but it simplifies the coding.


isUpToDate

protected boolean isUpToDate(SftpClient sftp,
                             File localFile,
                             String remoteFile)
            throws IOException,
                   BuildException
Checks to see if the remote file is current as compared with the local file. Returns true if the remote file is up to date.

makeRemoteDir

protected void makeRemoteDir(SftpClient sftp,
                             String dir)
            throws BuildException
Create the specified directory on the remote host.
Parameters:
sftp - The SFTP client connection
dir - The directory to create

resolveFile

protected String resolveFile(String file)
Correct a file path to correspond to the remote host requirements. This implementation currently assumes that the remote end can handle Unix-style paths with forward-slash separators. This can be overridden with the separator task parameter. No attempt is made to determine what syntax is appropriate for the remote host.

sendFile

protected void sendFile(SftpClient sftp,
                        String dir,
                        String filename)
            throws IOException,
                   BuildException
Sends a single file to the remote host. filename may contain a relative path specification. When this is the case, sendFile will attempt to create any necessary parent directories before sending the file. The file will then be sent using the entire relative path spec - no attempt is made to change directories. It is anticipated that this may eventually cause problems with some FTP servers, but it simplifies the coding.

setAction

public void setAction(Sftp.Action action)
            throws BuildException
Sets the FTP action to be taken. Currently accepts "put", "get", "del", "mkdir", "chmod" and "list".

setChmod

public void setChmod(String theMode)
Sets the file permission mode (Unix only) for files sent to the server.

setDepends

public void setDepends(boolean depends)
Set to true to transmit only files that are new or changed from their remote counterparts. The default is to transmit all files.

setIgnoreNoncriticalErrors

public void setIgnoreNoncriticalErrors(boolean ignoreNoncriticalErrors)
set the flag to skip errors on directory creation. (and maybe later other server specific errors)

setNewer

public void setNewer(boolean newer)
A synonym for depends. Set to true to transmit only new or changed files.

setRemotedir

public void setRemotedir(String remotedir)
Sets the remote working directory

setSkipFailedTransfers

public void setSkipFailedTransfers(boolean skipFailedTransfers)
If true, enables unsuccessful file put, delete and get operations to be skipped with a warning and the remainder of the files still transferred.

setVerbose

public void setVerbose(boolean verbose)
Set to true to receive notification about each file as it is transferred.

transferFiles

protected void transferFiles(SftpClient sftp)
            throws IOException,
                   BuildException
Sends all files specified by the configured filesets to the remote server.

transferFiles

protected int transferFiles(SftpClient sftp,
                            FileSet fs)
            throws IOException,
                   BuildException
For each file in the fileset, do the appropriate action: send, get, delete, or list.

Copyright © 2002-2003 Lee David Painter & Contributors. All Rights Reserved.