public void doFTP()

in src/main/org/apache/tools/ant/taskdefs/optional/net/FTPTaskMirrorImpl.java [1785:1901]


    public void doFTP() throws BuildException {
        FTPClient ftp = null;

        try {
            task.log("Opening FTP connection to " + task.getServer(), Project.MSG_VERBOSE);

            ftp = new FTPClient();
            if (task.isConfigurationSet()) {
                ftp = FTPConfigurator.configure(ftp, task);
            }

            ftp.setRemoteVerificationEnabled(task.getEnableRemoteVerification());
            ftp.connect(task.getServer(), task.getPort());
            if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
                throw new BuildException("FTP connection failed: %s",
                    ftp.getReplyString());
            }

            task.log("connected", Project.MSG_VERBOSE);
            task.log("logging in to FTP server", Project.MSG_VERBOSE);

            if ((task.getAccount() != null && !ftp.login(task.getUserid(), task.getPassword(), task.getAccount()))
                || (task.getAccount() == null && !ftp.login(task.getUserid(), task.getPassword()))) {
                throw new BuildException("Could not login to FTP server");
            }

            task.log("login succeeded", Project.MSG_VERBOSE);

            if (task.isBinary()) {
                ftp.setFileType(org.apache.commons.net.ftp.FTP.BINARY_FILE_TYPE);
            } else {
                ftp.setFileType(org.apache.commons.net.ftp.FTP.ASCII_FILE_TYPE);
            }
            if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
                throw new BuildException("could not set transfer type: %s",
                    ftp.getReplyString());
            }

            if (task.isPassive()) {
                task.log("entering passive mode", Project.MSG_VERBOSE);
                ftp.enterLocalPassiveMode();
                if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
                    throw new BuildException(
                        "could not enter into passive mode: %s",
                        ftp.getReplyString());
                }
            }

            // If an initial command was configured then send it.
            // Some FTP servers offer different modes of operation,
            // E.G. switching between a UNIX file system mode and
            // a legacy file system.
            if (task.getInitialSiteCommand() != null) {
                final FTPClient lftp = ftp;
                executeRetryable(new RetryHandler(task.getRetriesAllowed(), task),
                    () -> doSiteCommand(lftp, task.getInitialSiteCommand()),
                    "initial site command: " + task.getInitialSiteCommand());
            }

            // For a unix ftp server you can set the default mask for all files
            // created.

            if (task.getUmask() != null) {
                final FTPClient lftp = ftp;
                executeRetryable(
                    new RetryHandler(task.getRetriesAllowed(), task),
                    () -> doSiteCommand(lftp, "umask " + task.getUmask()),
                    "umask " + task.getUmask());
            }

            // If the action is MK_DIR, then the specified remote
            // directory is the directory to create.

            if (task.getAction() == FTPTask.MK_DIR) {
                final FTPClient lftp = ftp;
                executeRetryable(
                    new RetryHandler(task.getRetriesAllowed(), task),
                    () -> makeRemoteDir(lftp, task.getRemotedir()),
                    task.getRemotedir());
            } else if (task.getAction() == FTPTask.SITE_CMD) {
                final FTPClient lftp = ftp;
                executeRetryable(
                    new RetryHandler(task.getRetriesAllowed(), task),
                    () -> doSiteCommand(lftp, task.getSiteCommand()),
                    "Site Command: " + task.getSiteCommand());
            } else {
                if (task.getRemotedir() != null) {
                    task.log("changing the remote directory", Project.MSG_VERBOSE);
                    ftp.changeWorkingDirectory(task.getRemotedir());
                    if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
                        throw new BuildException(
                            "could not change remote directory: %s",
                            ftp.getReplyString());
                    }
                }
                if (task.isNewer() && task.isTimeDiffAuto()) {
                    // in this case we want to find how much time span there is between local
                    // and remote
                    task.setTimeDiffMillis(getTimeDiff(ftp));
                }
                task.log(FTPTask.ACTION_STRS[task.getAction()] + " " + FTPTask.ACTION_TARGET_STRS[task.getAction()]);
                transferFiles(ftp);
            }
        } catch (IOException ex) {
            throw new BuildException("error during FTP transfer: " + ex, ex);
        } finally {
            if (ftp != null && ftp.isConnected()) {
                try {
                    task.log("disconnecting", Project.MSG_VERBOSE);
                    ftp.logout();
                    ftp.disconnect();
                } catch (IOException ex) {
                    // ignore it
                }
            }
        }
    }