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

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

Introduction

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

Prototype

public FTPListParseEngine initiateListParsing() throws IOException 

Source Link

Document

Using the default autodetect mechanism, initialize an FTPListParseEngine object containing a raw file information for the current working directory on the server This information is obtained through the LIST command.

Usage

From source file:org.eclipse.datatools.connectivity.sample.ftp.internal.FtpContentProvider.java

public Object[] getChildren(Object parent) {
    try {/*from  w ww .  j a v  a 2s  .  c  o m*/
        if (parent instanceof FTPClientObject) {
            FTPClient ftpClient = ((FTPClientObject) parent).getFtpClient();
            if (ftpClient.isConnected()) {
                FTPListParseEngine engine = ftpClient.initiateListParsing();
                FTPFile[] files = engine.getFiles();
                return FTPFileObject.convert(parent, ((FTPClientObject) parent).getProfile(), files);
            }
        } else if (parent instanceof FTPFileObject) {
            FTPFile ftpFile = ((FTPFileObject) parent).getFTPFile();
            FTPClient ftpClient = getFTPClient(parent);
            if (ftpFile.isDirectory() && ftpClient.isConnected()) {
                FTPListParseEngine engine = ftpClient.initiateListParsing(getDirectory((FTPFileObject) parent));
                FTPFile[] files = engine.getFiles();
                return FTPFileObject.convert(parent, ((FTPFileObject) parent).getProfile(), files);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        FTPClient ftpClient = getFTPClient(parent);
        try {
            if (ftpClient != null)
                ftpClient.disconnect();
        } catch (Exception ex) {
        }
    }
    return new Object[0];
}

From source file:org.mule.transport.ftp.FtpMessageReceiver.java

protected FTPFile[] listFiles() throws Exception {
    FTPClient client = null;
    try {//from w  ww  .  j  ava2s . c o m
        client = connector.createFtpClient(endpoint);
        FTPListParseEngine engine = client.initiateListParsing();
        FTPFile[] files = null;
        List<FTPFile> v = new ArrayList<FTPFile>();
        while (engine.hasNext()) {
            if (getLifecycleState().isStopping()) {
                break;
            }
            files = engine.getNext(FTP_LIST_PAGE_SIZE);
            if (files == null || files.length == 0) {
                return files;
            }
            for (FTPFile file : files) {
                if (file.isFile()) {
                    if (filenameFilter == null || filenameFilter.accept(null, file.getName())) {
                        v.add(file);
                    }
                }
            }
        }

        if (!FTPReply.isPositiveCompletion(client.getReplyCode())) {
            throw new IOException("Failed to list files. Ftp error: " + client.getReplyCode());
        }

        return v.toArray(new FTPFile[v.size()]);
    } finally {
        if (client != null) {
            connector.releaseFtp(endpoint.getEndpointURI(), client);
        }
    }
}

From source file:org.mule.transport.ftp.FtpMessageRequester.java

protected FTPFile findFileToProcess(FTPClient client) throws Exception {
    FTPListParseEngine engine = client.initiateListParsing();
    FTPFile[] files = null;//from   w  w  w  .  j  a  v  a  2  s.c  om
    while (engine.hasNext()) {
        files = engine.getNext(FTP_LIST_PAGE_SIZE);
        if (files == null) {
            break;
        }
        FilenameFilter filenameFilter = getFilenameFilter();
        for (int i = 0; i < files.length; i++) {
            FTPFile file = files[i];
            if (file.isFile()) {
                if (filenameFilter.accept(null, file.getName())) {
                    if (connector.validateFile(file)) {
                        // only read the first one
                        return file;
                    }
                }
            }
        }
    }
    if (!FTPReply.isPositiveCompletion(client.getReplyCode())) {
        throw new IOException("Ftp error: " + client.getReplyCode());
    }

    return null;
}