Example usage for org.apache.commons.net.ftp FTPCommand LIST

List of usage examples for org.apache.commons.net.ftp FTPCommand LIST

Introduction

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

Prototype

int LIST

To view the source code for org.apache.commons.net.ftp FTPCommand LIST.

Click Source Link

Usage

From source file:ch.cyberduck.core.ftp.FTPPath.java

@Override
public AttributedList<Path> list(final AttributedList<Path> children) {
    try {/*from  www .  ja  v  a 2  s.c  o  m*/
        this.getSession().check();
        this.getSession().message(MessageFormat
                .format(Locale.localizedString("Listing directory {0}", "Status"), this.getName()));

        // Cached file parser determined from SYST response with the timezone set from the bookmark
        final FTPFileEntryParser parser = this.getSession().getFileParser();
        boolean success = false;
        try {
            if (this.getSession().isStatListSupportedEnabled()) {
                int response = this.getSession().getClient().stat(this.getAbsolute());
                if (FTPReply.isPositiveCompletion(response)) {
                    String[] reply = this.getSession().getClient().getReplyStrings();
                    final List<String> result = new ArrayList<String>(reply.length);
                    for (final String line : reply) {
                        //Some servers include the status code for every line.
                        if (line.startsWith(String.valueOf(response))) {
                            try {
                                result.add(line.substring(line.indexOf(response) + line.length() + 1).trim());
                            } catch (IndexOutOfBoundsException e) {
                                log.error(String.format("Failed parsing line %s", line), e);
                            }
                        } else {
                            result.add(StringUtils.stripStart(line, null));
                        }
                    }
                    success = this.parseListResponse(children, parser, result);
                } else {
                    this.getSession().setStatListSupportedEnabled(false);
                }
            }
        } catch (IOException e) {
            log.warn("Command STAT failed with I/O error:" + e.getMessage());
            this.getSession().interrupt();
            this.getSession().check();
        }
        if (!success || children.isEmpty()) {
            success = this.data(new DataConnectionAction() {
                @Override
                public boolean run() throws IOException {
                    if (!getSession().getClient().changeWorkingDirectory(getAbsolute())) {
                        throw new FTPException(getSession().getClient().getReplyString());
                    }
                    if (!getSession().getClient().setFileType(FTPClient.ASCII_FILE_TYPE)) {
                        // Set transfer type for traditional data socket file listings. The data transfer is over the
                        // data connection in type ASCII or type EBCDIC.
                        throw new FTPException(getSession().getClient().getReplyString());
                    }
                    boolean success = false;
                    // STAT listing failed or empty
                    if (getSession().isMlsdListSupportedEnabled()
                            // Note that there is no distinct FEAT output for MLSD.
                            // The presence of the MLST feature indicates that both MLST and MLSD are supported.
                            && getSession().getClient().isFeatureSupported(FTPCommand.MLST)) {
                        success = parseMlsdResponse(children, getSession().getClient().list(FTPCommand.MLSD));
                        if (!success) {
                            getSession().setMlsdListSupportedEnabled(false);
                        }
                    }
                    if (!success) {
                        // MLSD listing failed or not enabled
                        if (getSession().isExtendedListEnabled()) {
                            try {
                                success = parseListResponse(children, parser,
                                        getSession().getClient().list(FTPCommand.LIST, "-a"));
                            } catch (FTPException e) {
                                getSession().setExtendedListEnabled(false);
                            }
                        }
                        if (!success) {
                            // LIST -a listing failed or not enabled
                            success = parseListResponse(children, parser,
                                    getSession().getClient().list(FTPCommand.LIST));
                        }
                    }
                    return success;
                }
            });
        }
        for (Path child : children) {
            if (child.attributes().isSymbolicLink()) {
                if (this.getSession().getClient().changeWorkingDirectory(child.getAbsolute())) {
                    child.attributes().setType(SYMBOLIC_LINK_TYPE | DIRECTORY_TYPE);
                } else {
                    // Try if CWD to symbolic link target succeeds
                    if (this.getSession().getClient()
                            .changeWorkingDirectory(child.getSymlinkTarget().getAbsolute())) {
                        // Workdir change succeeded
                        child.attributes().setType(SYMBOLIC_LINK_TYPE | DIRECTORY_TYPE);
                    } else {
                        child.attributes().setType(SYMBOLIC_LINK_TYPE | FILE_TYPE);
                    }
                }
            }
        }
        if (!success) {
            // LIST listing failed
            log.error("No compatible file listing method found");
        }
    } catch (IOException e) {
        log.warn("Listing directory failed:" + e.getMessage());
        children.attributes().setReadable(false);
        if (!session.cache().containsKey(this.getReference())) {
            this.error(e.getMessage(), e);
        }
    }
    return children;
}

From source file:org.apache.nutch.protocol.ftp.Client.java

/**
 * retrieve list reply for path//from  ww w .j a v a2 s.  c  o  m
 * 
 * @param path
 * @param entries
 * @param limit
 * @param parser
 * @throws IOException
 * @throws FtpExceptionCanNotHaveDataConnection
 * @throws FtpExceptionUnknownForcedDataClose
 * @throws FtpExceptionControlClosedByForcedDataClose
 */
public void retrieveList(String path, List<FTPFile> entries, int limit, FTPFileEntryParser parser)
        throws IOException, FtpExceptionCanNotHaveDataConnection, FtpExceptionUnknownForcedDataClose,
        FtpExceptionControlClosedByForcedDataClose {
    Socket socket = __openPassiveDataConnection(FTPCommand.LIST, path);

    if (socket == null)
        throw new FtpExceptionCanNotHaveDataConnection("LIST " + ((path == null) ? "" : path));

    BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));

    // force-close data channel socket, when download limit is reached
    // boolean mandatory_close = false;

    // List entries = new LinkedList();
    int count = 0;
    String line = parser.readNextEntry(reader);
    while (line != null) {
        FTPFile ftpFile = parser.parseFTPEntry(line);
        // skip non-formatted lines
        if (ftpFile == null) {
            line = parser.readNextEntry(reader);
            continue;
        }
        entries.add(ftpFile);
        count += line.length();
        // impose download limit if limit >= 0, otherwise no limit
        // here, cut off is up to the line when total bytes is just over limit
        if (limit >= 0 && count > limit) {
            // mandatory_close = true;
            break;
        }
        line = parser.readNextEntry(reader);
    }

    // if (mandatory_close)
    // you always close here, no matter mandatory_close or not.
    // however different ftp servers respond differently, see below.
    socket.close();

    // scenarios:
    // (1) mandatory_close is false, download limit not reached
    // no special care here
    // (2) mandatory_close is true, download limit is reached
    // different servers have different reply codes:

    try {
        int reply = getReply();
        if (!_notBadReply(reply))
            throw new FtpExceptionUnknownForcedDataClose(getReplyString());
    } catch (FTPConnectionClosedException e) {
        // some ftp servers will close control channel if data channel socket
        // is closed by our end before all data has been read out. Check:
        // tux414.q-tam.hp.com FTP server (hp.com version whp02)
        // so must catch FTPConnectionClosedException thrown by getReply() above
        // disconnect();
        throw new FtpExceptionControlClosedByForcedDataClose(e.getMessage());
    }

}