Example usage for org.apache.commons.net.ftp FTPClient getReplyCode

List of usage examples for org.apache.commons.net.ftp FTPClient getReplyCode

Introduction

In this page you can find the example usage for org.apache.commons.net.ftp FTPClient getReplyCode.

Prototype

public int getReplyCode() 

Source Link

Document

Returns the integer value of the reply code of the last FTP reply.

Usage

From source file:org.apache.tools.ant.taskdefs.optional.net.FTP.java

/**
 * Sends a single file to the remote host. <code>filename</code> may
 * contain a relative path specification. When this is the case, <code>sendFile</code>
 * 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.//from w ww  . ja  v  a2 s .  c  o m
 * @param ftp ftp client
 * @param dir base directory of the file to be sent (local)
 * @param filename relative path of the file to be send
 *        locally relative to dir
 *        remotely relative to the remotedir attribute
 * @throws IOException  in unknown circumstances
 * @throws BuildException in unknown circumstances
 */
protected void sendFile(FTPClient ftp, String dir, String filename) throws IOException, BuildException {
    InputStream instream = null;

    try {
        // TODO - why not simply new File(dir, filename)?
        File file = getProject().resolveFile(new File(dir, filename).getPath());

        if (newerOnly && isUpToDate(ftp, file, resolveFile(filename))) {
            return;
        }

        if (verbose) {
            log("transferring " + file.getAbsolutePath());
        }

        instream = new BufferedInputStream(new FileInputStream(file));

        createParents(ftp, filename);

        ftp.storeFile(resolveFile(filename), instream);

        boolean success = FTPReply.isPositiveCompletion(ftp.getReplyCode());

        if (!success) {
            String s = "could not put file: " + ftp.getReplyString();

            if (skipFailedTransfers) {
                log(s, Project.MSG_WARN);
                skipped++;
            } else {
                throw new BuildException(s);
            }

        } else {
            // see if we should issue a chmod command
            if (chmod != null) {
                doSiteCommand(ftp, "chmod " + chmod + " " + resolveFile(filename));
            }
            log("File " + file.getAbsolutePath() + " copied to " + server, Project.MSG_VERBOSE);
            transferred++;
        }
    } finally {
        FileUtils.close(instream);
    }
}

From source file:org.apache.tools.ant.taskdefs.optional.net.FTP.java

/**
 * Create the specified directory on the remote host.
 *
 * @param ftp The FTP client connection/*from   ww w. j  a  v  a2s .c  om*/
 * @param dir The directory to create (format must be correct for host
 *      type)
 * @throws IOException  in unknown circumstances
 * @throws BuildException if ignoreNoncriticalErrors has not been set to true
 *         and a directory could not be created, for instance because it was
 *         already existing. Precisely, the codes 521, 550 and 553 will trigger
 *         a BuildException
 */
protected void makeRemoteDir(FTPClient ftp, String dir) throws IOException, BuildException {
    String workingDirectory = ftp.printWorkingDirectory();
    if (verbose) {
        if (dir.startsWith("/") || workingDirectory == null) {
            log("Creating directory: " + dir + " in /");
        } else {
            log("Creating directory: " + dir + " in " + workingDirectory);
        }
    }
    if (dir.startsWith("/")) {
        ftp.changeWorkingDirectory("/");
    }
    String subdir = "";
    StringTokenizer st = new StringTokenizer(dir, "/");
    while (st.hasMoreTokens()) {
        subdir = st.nextToken();
        log("Checking " + subdir, Project.MSG_DEBUG);
        if (!ftp.changeWorkingDirectory(subdir)) {
            if (!ftp.makeDirectory(subdir)) {
                // codes 521, 550 and 553 can be produced by FTP Servers
                //  to indicate that an attempt to create a directory has
                //  failed because the directory already exists.
                int rc = ftp.getReplyCode();
                if (!(ignoreNoncriticalErrors && (rc == CODE_550 || rc == CODE_553 || rc == CODE_521))) {
                    throw new BuildException("could not create directory: " + ftp.getReplyString());
                }
                if (verbose) {
                    log("Directory already exists");
                }
            } else {
                if (verbose) {
                    log("Directory created OK");
                }
                ftp.changeWorkingDirectory(subdir);
            }
        }
    }
    if (workingDirectory != null) {
        ftp.changeWorkingDirectory(workingDirectory);
    }
}

From source file:org.apache.tools.ant.taskdefs.optional.net.FTP.java

/**
 * look at the response for a failed mkdir action, decide whether
 * it matters or not. If it does, we throw an exception
 * @param ftp current ftp connection//from   w  w w  .j a  v  a 2  s . c om
 * @throws BuildException if this is an error to signal
 */
private void handleMkDirFailure(FTPClient ftp) throws BuildException {
    int rc = ftp.getReplyCode();
    if (!(ignoreNoncriticalErrors && (rc == CODE_550 || rc == CODE_553 || rc == CODE_521))) {
        throw new BuildException("could not create directory: " + ftp.getReplyString());
    }
}

From source file:org.apache.tools.ant.taskdefs.optional.net.FTP.java

/**
 * Runs the task./*from  w  ww.j  a  v a 2 s .  c  o  m*/
 *
 * @throws BuildException if the task fails or is not configured
 *         correctly.
 */
public void execute() throws BuildException {
    checkAttributes();

    FTPClient ftp = null;

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

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

        ftp.setRemoteVerificationEnabled(enableRemoteVerification);
        ftp.connect(server, port);
        if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
            throw new BuildException("FTP connection failed: " + ftp.getReplyString());
        }

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

        if ((this.account != null && !ftp.login(userid, password, account))
                || (this.account == null && !ftp.login(userid, password))) {
            throw new BuildException("Could not login to FTP server");
        }

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

        if (binary) {
            ftp.setFileType(org.apache.commons.net.ftp.FTP.BINARY_FILE_TYPE);
            if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
                throw new BuildException("could not set transfer type: " + ftp.getReplyString());
            }
        } 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: " + ftp.getReplyString());
            }
        }

        if (passive) {
            log("entering passive mode", Project.MSG_VERBOSE);
            ftp.enterLocalPassiveMode();
            if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
                throw new BuildException("could not enter into passive " + "mode: " + 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 (this.initialSiteCommand != null) {
            RetryHandler h = new RetryHandler(this.retriesAllowed, this);
            final FTPClient lftp = ftp;
            executeRetryable(h, new Retryable() {
                public void execute() throws IOException {
                    doSiteCommand(lftp, FTP.this.initialSiteCommand);
                }
            }, "initial site command: " + this.initialSiteCommand);
        }

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

        if (umask != null) {
            RetryHandler h = new RetryHandler(this.retriesAllowed, this);
            final FTPClient lftp = ftp;
            executeRetryable(h, new Retryable() {
                public void execute() throws IOException {
                    doSiteCommand(lftp, "umask " + umask);
                }
            }, "umask " + umask);
        }

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

        if (action == MK_DIR) {
            RetryHandler h = new RetryHandler(this.retriesAllowed, this);
            final FTPClient lftp = ftp;
            executeRetryable(h, new Retryable() {
                public void execute() throws IOException {
                    makeRemoteDir(lftp, remotedir);
                }
            }, remotedir);
        } else if (action == SITE_CMD) {
            RetryHandler h = new RetryHandler(this.retriesAllowed, this);
            final FTPClient lftp = ftp;
            executeRetryable(h, new Retryable() {
                public void execute() throws IOException {
                    doSiteCommand(lftp, FTP.this.siteCommand);
                }
            }, "Site Command: " + this.siteCommand);
        } else {
            if (remotedir != null) {
                log("changing the remote directory to " + remotedir, Project.MSG_VERBOSE);
                ftp.changeWorkingDirectory(remotedir);
                if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
                    throw new BuildException("could not change remote " + "directory: " + ftp.getReplyString());
                }
            }
            if (newerOnly && timeDiffAuto) {
                // in this case we want to find how much time span there is between local
                // and remote
                timeDiffMillis = getTimeDiff(ftp);
            }
            log(ACTION_STRS[action] + " " + ACTION_TARGET_STRS[action]);
            transferFiles(ftp);
        }

    } catch (IOException ex) {
        throw new BuildException("error during FTP transfer: " + ex, ex);
    } finally {
        if (ftp != null && ftp.isConnected()) {
            try {
                log("disconnecting", Project.MSG_VERBOSE);
                ftp.logout();
                ftp.disconnect();
            } catch (IOException ex) {
                // ignore it
            }
        }
    }
}

From source file:org.apache.tools.ant.taskdefs.optional.net.FTPTaskMirrorImpl.java

/**
 * auto find the time difference between local and remote
 * @param ftp handle to ftp client//from  w  w w  . j av  a  2  s  .c om
 * @return number of millis to add to remote time to make it comparable to local time
 * @since ant 1.6
 */
private long getTimeDiff(FTPClient ftp) {
    long returnValue = 0;
    File tempFile = findFileName(ftp);
    try {
        // create a local temporary file
        FILE_UTILS.createNewFile(tempFile);
        long localTimeStamp = tempFile.lastModified();
        BufferedInputStream instream = new BufferedInputStream(new FileInputStream(tempFile));
        ftp.storeFile(tempFile.getName(), instream);
        instream.close();
        boolean success = FTPReply.isPositiveCompletion(ftp.getReplyCode());
        if (success) {
            FTPFile[] ftpFiles = ftp.listFiles(tempFile.getName());
            if (ftpFiles.length == 1) {
                long remoteTimeStamp = ftpFiles[0].getTimestamp().getTime().getTime();
                returnValue = localTimeStamp - remoteTimeStamp;
            }
            ftp.deleteFile(ftpFiles[0].getName());
        }
        // delegate the deletion of the local temp file to the delete task
        // because of race conditions occurring on Windows
        Delete mydelete = new Delete();
        mydelete.bindToOwner(task);
        mydelete.setFile(tempFile.getCanonicalFile());
        mydelete.execute();
    } catch (Exception e) {
        throw new BuildException(e, task.getLocation());
    }
    return returnValue;
}

From source file:org.apache.tools.ant.taskdefs.optional.net.FTPTaskMirrorImpl.java

/**
 * Sends a single file to the remote host. <code>filename</code> may
 * contain a relative path specification. When this is the case, <code>sendFile</code>
 * 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.//w  w w . ja  v  a 2s.c o m
 * @param ftp ftp client
 * @param dir base directory of the file to be sent (local)
 * @param filename relative path of the file to be send
 *        locally relative to dir
 *        remotely relative to the remotedir attribute
 * @throws IOException  in unknown circumstances
 * @throws BuildException in unknown circumstances
 */
protected void sendFile(FTPClient ftp, String dir, String filename) throws IOException, BuildException {
    InputStream instream = null;

    try {
        // TODO - why not simply new File(dir, filename)?
        File file = task.getProject().resolveFile(new File(dir, filename).getPath());

        if (task.isNewer() && isUpToDate(ftp, file, resolveFile(filename))) {
            return;
        }

        if (task.isVerbose()) {
            task.log("transferring " + file.getAbsolutePath());
        }

        instream = new BufferedInputStream(new FileInputStream(file));

        createParents(ftp, filename);

        ftp.storeFile(resolveFile(filename), instream);

        boolean success = FTPReply.isPositiveCompletion(ftp.getReplyCode());

        if (!success) {
            String s = "could not put file: " + ftp.getReplyString();

            if (task.isSkipFailedTransfers()) {
                task.log(s, Project.MSG_WARN);
                skipped++;
            } else {
                throw new BuildException(s);
            }

        } else {
            // see if we should issue a chmod command
            if (task.getChmod() != null) {
                doSiteCommand(ftp, "chmod " + task.getChmod() + " " + resolveFile(filename));
            }
            task.log("File " + file.getAbsolutePath() + " copied to " + task.getServer(), Project.MSG_VERBOSE);
            transferred++;
        }
    } finally {
        FileUtils.close(instream);
    }
}

From source file:org.apache.tools.ant.taskdefs.optional.net.FTPTaskMirrorImpl.java

/**
 * Retrieve a single file from the remote host. <code>filename</code> may
 * contain a relative path specification. <p>
 *
 * 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.</p>//from  w ww  . j a  v  a2s . co  m
 * @param ftp the ftp client
 * @param dir local base directory to which the file should go back
 * @param filename relative path of the file based upon the ftp remote directory
 *        and/or the local base directory (dir)
 * @throws IOException  in unknown circumstances
 * @throws BuildException if skipFailedTransfers is false
 * and the file cannot be retrieved.
 */
protected void getFile(FTPClient ftp, String dir, String filename) throws IOException, BuildException {
    OutputStream outstream = null;
    try {
        File file = task.getProject().resolveFile(new File(dir, filename).getPath());

        if (task.isNewer() && isUpToDate(ftp, file, resolveFile(filename))) {
            return;
        }

        if (task.isVerbose()) {
            task.log("transferring " + filename + " to " + file.getAbsolutePath());
        }

        File pdir = file.getParentFile();

        if (!pdir.exists()) {
            pdir.mkdirs();
        }
        outstream = new BufferedOutputStream(new FileOutputStream(file));
        ftp.retrieveFile(resolveFile(filename), outstream);

        if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
            String s = "could not get file: " + ftp.getReplyString();

            if (task.isSkipFailedTransfers()) {
                task.log(s, Project.MSG_WARN);
                skipped++;
            } else {
                throw new BuildException(s);
            }

        } else {
            task.log("File " + file.getAbsolutePath() + " copied from " + task.getServer(),
                    Project.MSG_VERBOSE);
            transferred++;
            if (task.isPreserveLastModified()) {
                outstream.close();
                outstream = null;
                FTPFile[] remote = ftp.listFiles(resolveFile(filename));
                if (remote.length > 0) {
                    FILE_UTILS.setFileLastModified(file, remote[0].getTimestamp().getTime().getTime());
                }
            }
        }
    } finally {
        FileUtils.close(outstream);
    }
}

From source file:org.apache.tools.ant.taskdefs.optional.net.FTPTaskMirrorImpl.java

/**
 * Create the specified directory on the remote host.
 *
 * @param ftp The FTP client connection/*from ww w.ja  v  a  2 s  .c om*/
 * @param dir The directory to create (format must be correct for host
 *      type)
 * @throws IOException  in unknown circumstances
 * @throws BuildException if ignoreNoncriticalErrors has not been set to true
 *         and a directory could not be created, for instance because it was
 *         already existing. Precisely, the codes 521, 550 and 553 will trigger
 *         a BuildException
 */
protected void makeRemoteDir(FTPClient ftp, String dir) throws IOException, BuildException {
    String workingDirectory = ftp.printWorkingDirectory();
    if (task.isVerbose()) {
        if (dir.startsWith("/") || workingDirectory == null) {
            task.log("Creating directory: " + dir + " in /");
        } else {
            task.log("Creating directory: " + dir + " in " + workingDirectory);
        }
    }
    if (dir.startsWith("/")) {
        ftp.changeWorkingDirectory("/");
    }
    String subdir = "";
    StringTokenizer st = new StringTokenizer(dir, "/");
    while (st.hasMoreTokens()) {
        subdir = st.nextToken();
        task.log("Checking " + subdir, Project.MSG_DEBUG);
        if (!ftp.changeWorkingDirectory(subdir)) {
            if (!ftp.makeDirectory(subdir)) {
                // codes 521, 550 and 553 can be produced by FTP Servers
                //  to indicate that an attempt to create a directory has
                //  failed because the directory already exists.
                int rc = ftp.getReplyCode();
                if (!(task.isIgnoreNoncriticalErrors()
                        && (rc == CODE_550 || rc == CODE_553 || rc == CODE_521))) {
                    throw new BuildException("could not create directory: " + ftp.getReplyString());
                }
                if (task.isVerbose()) {
                    task.log("Directory already exists");
                }
            } else {
                if (task.isVerbose()) {
                    task.log("Directory created OK");
                }
                ftp.changeWorkingDirectory(subdir);
            }
        }
    }
    if (workingDirectory != null) {
        ftp.changeWorkingDirectory(workingDirectory);
    }
}

From source file:org.apache.tools.ant.taskdefs.optional.net.FTPTaskMirrorImpl.java

/**
 * look at the response for a failed mkdir action, decide whether
 * it matters or not. If it does, we throw an exception
 * @param ftp current ftp connection//from  w  w w. ja  v a2  s.co m
 * @throws BuildException if this is an error to signal
 */
private void handleMkDirFailure(FTPClient ftp) throws BuildException {
    int rc = ftp.getReplyCode();
    if (!(task.isIgnoreNoncriticalErrors() && (rc == CODE_550 || rc == CODE_553 || rc == CODE_521))) {
        throw new BuildException("could not create directory: " + ftp.getReplyString());
    }
}

From source file:org.apache.tools.ant.taskdefs.optional.net.FTPTaskMirrorImpl.java

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

    try {/*  w w  w . ja  v  a  2 s .c o  m*/
        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: " + 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);
            if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
                throw new BuildException("could not set transfer type: " + ftp.getReplyString());
            }
        } 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: " + 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: " + 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) {
            RetryHandler h = new RetryHandler(task.getRetriesAllowed(), task);
            final FTPClient lftp = ftp;
            executeRetryable(h, new Retryable() {
                public void execute() throws IOException {
                    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) {
            RetryHandler h = new RetryHandler(task.getRetriesAllowed(), task);
            final FTPClient lftp = ftp;
            executeRetryable(h, new Retryable() {
                public void execute() throws IOException {
                    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) {
            RetryHandler h = new RetryHandler(task.getRetriesAllowed(), task);
            final FTPClient lftp = ftp;
            executeRetryable(h, new Retryable() {
                public void execute() throws IOException {
                    makeRemoteDir(lftp, task.getRemotedir());
                }
            }, task.getRemotedir());
        } else if (task.getAction() == FTPTask.SITE_CMD) {
            RetryHandler h = new RetryHandler(task.getRetriesAllowed(), task);
            final FTPClient lftp = ftp;
            executeRetryable(h, new Retryable() {
                public void execute() throws IOException {
                    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: " + 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
            }
        }
    }
}