Example usage for org.apache.commons.net.ftp FTPReply isPositiveCompletion

List of usage examples for org.apache.commons.net.ftp FTPReply isPositiveCompletion

Introduction

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

Prototype

public static boolean isPositiveCompletion(int reply) 

Source Link

Document

Determine if a reply code is a positive completion response.

Usage

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

/**
 * Runs the task./*from ww w  .  j  av  a 2  s  .  c  om*/
 *
 * @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);

        /**
         * "verbose" version of <code>FTPClient</code> prints progress indicator
         * See http://evgeny-goldin.com/blog/2010/08/18/ant-ftp-task-progress-indicator-timeout/
         */
        ftp = (verbose
                ? new com.github.carlosrubio.org.apache.tools.ant.taskdefs.optional.net.FTPClient(getProject())
                : new org.apache.commons.net.ftp.FTPClient());

        ftp.setDataTimeout(5 * 60 * 1000); // 5 minutes

        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:com.atomicleopard.thundr.ftp.commons.FTPClient.java

/**
 * Reserve a number of bytes on the server for the next file transfer.
 * <p>//w w  w.j  a  va  2  s  .  c  om
 * @param bytes  The number of bytes which the server should allocate.
 * @return True if successfully completed, false if not.
 * @exception FTPConnectionClosedException
 *      If the FTP server prematurely closes the connection as a result
 *      of the client being idle or some other reason causing the server
 *      to send FTP reply code 421.  This exception may be caught either
 *      as an IOException or independently as itself.
 * @exception IOException  If an I/O error occurs while either sending a
 *      command to the server or receiving a reply from the server.
 */
public boolean allocate(int bytes) throws IOException {
    return FTPReply.isPositiveCompletion(allo(bytes));
}

From source file:com.atomicleopard.thundr.ftp.commons.FTPClient.java

/**
 * Query the server for supported features. The server may reply with a list of server-supported exensions.
 * For example, a typical client-server interaction might be (from RFC 2389):
 * <pre>/*w w  w  .  j a  va2s  . c om*/
C> feat
S> 211-Extensions supported:
S>  MLST size*;create;modify*;perm;media-type
S>  SIZE
S>  COMPRESSION
S>  MDTM
S> 211 END
 * </pre>
 * @see <a href="http://www.faqs.org/rfcs/rfc2389.html">http://www.faqs.org/rfcs/rfc2389.html</a>
 * @return True if successfully completed, false if not.
 * @throws IOException
 * @since 2.2
 */
public boolean features() throws IOException {
    return FTPReply.isPositiveCompletion(feat());
}

From source file:com.atomicleopard.thundr.ftp.commons.FTPClient.java

private boolean initFeatureMap() throws IOException {
    if (__featuresMap == null) {
        // Don't create map here, because next line may throw exception
        boolean success = FTPReply.isPositiveCompletion(feat());
        // we init the map here, so we don't keep trying if we know the command will fail
        __featuresMap = new HashMap<String, Set<String>>();
        if (!success) {
            return false;
        }/*ww  w . j  a  va 2  s .c o  m*/
        for (String l : getReplyStrings()) {
            if (l.startsWith(" ")) { // it's a FEAT entry
                String key;
                String value = "";
                int varsep = l.indexOf(' ', 1);
                if (varsep > 0) {
                    key = l.substring(1, varsep);
                    value = l.substring(varsep + 1);
                } else {
                    key = l.substring(1);
                }
                key = key.toUpperCase(Locale.ENGLISH);
                Set<String> entries = __featuresMap.get(key);
                if (entries == null) {
                    entries = new HashSet<String>();
                    __featuresMap.put(key, entries);
                }
                entries.add(value);
            }
        }
    }
    return true;
}

From source file:com.atomicleopard.thundr.ftp.commons.FTPClient.java

/**
 * Reserve space on the server for the next file transfer.
 * <p>/*from w w w  . j a  va  2  s .co m*/
 * @param bytes  The number of bytes which the server should allocate.
 * @param recordSize  The size of a file record.
 * @return True if successfully completed, false if not.
 * @exception FTPConnectionClosedException
 *      If the FTP server prematurely closes the connection as a result
 *      of the client being idle or some other reason causing the server
 *      to send FTP reply code 421.  This exception may be caught either
 *      as an IOException or independently as itself.
 * @exception IOException  If an I/O error occurs while either sending a
 *      command to the server or receiving a reply from the server.
 */
public boolean allocate(int bytes, int recordSize) throws IOException {
    return FTPReply.isPositiveCompletion(allo(bytes, recordSize));
}

From source file:com.atomicleopard.thundr.ftp.commons.FTPClient.java

/**
 * Issue a command and wait for the reply.
 * <p>/* w ww . j  av  a 2s . c o  m*/
 * Should only be used with commands that return replies on the
 * command channel - do not use for LIST, NLST, MLSD etc.
 * <p>
 * @param command  The command to invoke
 * @param params  The parameters string, may be {@code null}
 * @return True if successfully completed, false if not, in which case
 * call {@link #getReplyCode()} or {@link #getReplyString()}
 * to get the reason.
 *
 * @exception IOException  If an I/O error occurs while either sending a
 *      command to the server or receiving a reply from the server.
 * @since 3.0
 */
public boolean doCommand(String command, String params) throws IOException {
    return FTPReply.isPositiveCompletion(sendCommand(command, params));
}

From source file:com.atomicleopard.thundr.ftp.commons.FTPClient.java

/**
 * Issue a command and wait for the reply, returning it as an array of strings.
 * <p>/*  w  w w .  j  a  va 2  s  .c o  m*/
 * Should only be used with commands that return replies on the
 * command channel - do not use for LIST, NLST, MLSD etc.
 * <p>
 * @param command  The command to invoke
 * @param params  The parameters string, may be {@code null}
 * @return The array of replies, or {@code null} if the command failed, in which case
 * call {@link #getReplyCode()} or {@link #getReplyString()}
 * to get the reason.
 *
 * @exception IOException  If an I/O error occurs while either sending a
 *      command to the server or receiving a reply from the server.
 * @since 3.0
 */
public String[] doCommandAsStrings(String command, String params) throws IOException {
    boolean success = FTPReply.isPositiveCompletion(sendCommand(command, params));
    if (success) {
        return getReplyStrings();
    } else {
        return null;
    }
}

From source file:com.atomicleopard.thundr.ftp.commons.FTPClient.java

/**
 * Get file details using the MLST command
 *
 * @param pathname the file or directory to list, may be {@code} null
 * @return the file details, may be {@code null}
 * @throws IOException/*from  www .ja v  a  2  s  .c  om*/
 * @since 3.0
 */
public FTPFile mlistFile(String pathname) throws IOException {
    boolean success = FTPReply.isPositiveCompletion(sendCommand(FTPCmd.MLST, pathname));
    if (success) {
        String entry = getReplyStrings()[1].substring(1); // skip leading space for parser
        return MLSxEntryParser.parseEntry(entry);
    } else {
        return null;
    }
}

From source file:com.atomicleopard.thundr.ftp.commons.FTPClient.java

/**
 * Renames a remote file.// www . j  a v a 2 s . com
 * <p>
 * @param from  The name of the remote file to rename.
 * @param to    The new name of the remote file.
 * @return True if successfully completed, false if not.
 * @exception FTPConnectionClosedException
 *      If the FTP server prematurely closes the connection as a result
 *      of the client being idle or some other reason causing the server
 *      to send FTP reply code 421.  This exception may be caught either
 *      as an IOException or independently as itself.
 * @exception IOException  If an I/O error occurs while either sending a
 *      command to the server or receiving a reply from the server.
 */
public boolean rename(String from, String to) throws IOException {
    if (!FTPReply.isPositiveIntermediate(rnfr(from))) {
        return false;
    }

    return FTPReply.isPositiveCompletion(rnto(to));
}

From source file:com.atomicleopard.thundr.ftp.commons.FTPClient.java

/**
 * Abort a transfer in progress.//w  w  w .j  av  a 2  s  . c  o  m
 * <p>
 * @return True if successfully completed, false if not.
 * @exception FTPConnectionClosedException
 *      If the FTP server prematurely closes the connection as a result
 *      of the client being idle or some other reason causing the server
 *      to send FTP reply code 421.  This exception may be caught either
 *      as an IOException or independently as itself.
 * @exception IOException  If an I/O error occurs while either sending a
 *      command to the server or receiving a reply from the server.
 */
public boolean abort() throws IOException {
    return FTPReply.isPositiveCompletion(abor());
}