Example usage for org.apache.commons.net.ftp FTPListParseEngine getNext

List of usage examples for org.apache.commons.net.ftp FTPListParseEngine getNext

Introduction

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

Prototype

public FTPFile[] getNext(int quantityRequested) 

Source Link

Document

Returns an array of at most quantityRequested FTPFile objects starting at this object's internal iterator's current position.

Usage

From source file:jenkins.plugins.publish_over_ftp.BapFtpClient.java

private void delete() throws IOException {
    final FTPListParseEngine listParser = ftpClient.initiateListParsing();
    if (listParser == null)
        throw new BapPublisherException(Messages.exception_client_listParserNull());
    while (listParser.hasNext())
        delete(listParser.getNext(1)[0]);
}

From source file:jenkins.plugins.publish_over_ftp.BapFtpClientTest.java

private FTPFile expectFtpFile(final FTPListParseEngine mockListEngine, final String filename) {
    expect(mockListEngine.hasNext()).andReturn(true);
    final FTPFile file = mockControl.createMock(FTPFile.class);
    expect(mockListEngine.getNext(1)).andReturn(new FTPFile[] { file });
    expect(file.getName()).andReturn(filename);
    return file;/*from  ww  w .  ja  v a  2 s . c  om*/
}

From source file:com.atomicleopard.thundr.ftp.FtpSession.java

/**
 * List remote contents in batches - includes both files and directories
 * // w  w w  .  j  ava  2 s  .co m
 * @param directory
 * @param batchSize
 * @return
 */
public Iterable<FTPFile[]> listBatch(final String directory, final int batchSize) {
    return timeLogAndCatch("List files in batch", new Callable<Iterable<FTPFile[]>>() {
        @Override
        public Iterable<FTPFile[]> call() throws Exception {
            final FTPListParseEngine engine = preparedClient.initiateListParsing(directory);
            return new Iterable<FTPFile[]>() {
                @Override
                public Iterator<FTPFile[]> iterator() {
                    return new Iterator<FTPFile[]>() {

                        @Override
                        public boolean hasNext() {
                            return engine.hasNext();
                        }

                        @Override
                        public FTPFile[] next() {
                            return engine.getNext(batchSize);
                        }

                        @Override
                        public void remove() {
                            throw new UnsupportedOperationException();
                        }
                    };
                }
            };
        }
    });
}

From source file:org.apache.sqoop.util.MainframeFTPClientUtils.java

public static List<String> listSequentialDatasets(String pdsName, Configuration conf) throws IOException {
    List<String> datasets = new ArrayList<String>();
    String dsName = pdsName;/*  w ww. jav  a  2s .co m*/
    String fileName = "";
    MainframeDatasetPath p = null;
    try {
        p = new MainframeDatasetPath(dsName, conf);
    } catch (Exception e) {
        LOG.error(e.getMessage());
        LOG.error("MainframeDatasetPath helper class incorrectly initialised");
        e.printStackTrace();
    }
    String dsType = conf.get(MainframeConfiguration.MAINFRAME_INPUT_DATASET_TYPE);
    boolean isTape = Boolean.parseBoolean(conf.get(MainframeConfiguration.MAINFRAME_INPUT_DATASET_TAPE));
    boolean isSequentialDs = false;
    boolean isGDG = false;
    if (dsType != null && p != null) {
        isSequentialDs = p.getMainframeDatasetType().toString()
                .equals(MainframeConfiguration.MAINFRAME_INPUT_DATASET_TYPE_SEQUENTIAL);
        isGDG = p.getMainframeDatasetType().toString()
                .equals(MainframeConfiguration.MAINFRAME_INPUT_DATASET_TYPE_GDG);
        pdsName = p.getMainframeDatasetFolder();
        fileName = p.getMainframeDatasetFileName();
    }
    FTPClient ftp = null;
    try {
        ftp = getFTPConnection(conf);
        if (ftp != null) {
            ftp.changeWorkingDirectory("'" + pdsName + "'");
            FTPFile[] ftpFiles = null;
            if (isTape) {
                FTPListParseEngine parser = ftp.initiateListParsing(
                        MainframeConfiguration.MAINFRAME_FTP_FILE_ENTRY_PARSER_CLASSNAME, "");
                List<FTPFile> listing = new ArrayList<FTPFile>();
                while (parser.hasNext()) {
                    FTPFile[] files = parser.getNext(25);
                    for (FTPFile file : files) {
                        if (file != null) {
                            listing.add(file);
                            LOG.info(String.format("Name: %s Type: %s", file.getName(), file.getType()));
                        }
                        // skip nulls returned from parser
                    }
                    ftpFiles = new FTPFile[listing.size()];
                    for (int i = 0; i < listing.size(); i++) {
                        ftpFiles[i] = listing.get(i);
                    }
                    LOG.info("Files returned from mainframe parser:-");
                    for (FTPFile f : ftpFiles) {
                        LOG.info(String.format("Name: %s, Type: %s", f.getName(), f.getType()));
                    }
                }
            } else {
                ftpFiles = ftp.listFiles();
            }
            if (!isGDG) {
                for (FTPFile f : ftpFiles) {
                    LOG.info(String.format("Name: %s Type: %s", f.getName(), f.getType()));
                    if (f.getType() == FTPFile.FILE_TYPE) {
                        // only add datasets if default behaviour of partitioned data sets
                        // or if it is a sequential data set, only add if the file name matches exactly
                        if (!isSequentialDs
                                || isSequentialDs && f.getName().equals(fileName) && !fileName.equals("")) {
                            datasets.add(f.getName());
                        }
                    }
                }
            } else {
                LOG.info("GDG branch. File list:-");
                for (FTPFile f : ftpFiles) {
                    LOG.info(String.format("Name: %s Type: %s", f.getName(), f.getType()));
                }
                if (ftpFiles.length > 0 && ftpFiles[ftpFiles.length - 1].getType() == FTPFile.FILE_TYPE) {
                    // for GDG - add the last file in the collection
                    datasets.add(ftpFiles[ftpFiles.length - 1].getName());
                }
            }
        }
    } catch (IOException ioe) {
        throw new IOException("Could not list datasets from " + pdsName + ":" + ioe.toString());
    } finally {
        if (ftp != null) {
            closeFTPConnection(ftp);
        }
    }
    return datasets;
}

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

protected FTPFile[] listFiles() throws Exception {
    FTPClient client = null;/*  ww w.j  a v  a  2s. c o  m*/
    try {
        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  ww. j  av  a  2 s .  co m
    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;
}

From source file:org.mule.transport.ftps.FtpsMessageReceiver.java

protected FTPFile[] listFiles() throws Exception {
    FTPSClient client = null;/* w  ww  .j  a v a2s  .  co  m*/
    try {
        client = connector.createFTPSClient(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.ftps.FtpsMessageRequester.java

protected FTPFile findFileToProcess(FTPSClient client) throws Exception {
    //Checking if it is a file or a directory
    boolean isFile = connector.isFile(endpoint, client);
    FTPListParseEngine engine = client.initiateListParsing();
    FTPFile[] files = null;//from   w  ww  . ja v  a2s .  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() && isValid(file, filenameFilter)) {
                // only read the first one
                return file;
            }
        }
    }
    if (!FTPReply.isPositiveCompletion(client.getReplyCode())) {
        throw new IOException("Ftp error: " + client.getReplyCode());
    }

    return null;
}

From source file:org.onebusaway.transit_data_federation.impl.realtime.orbcad.OrbcadRecordFtpSource.java

private List<String> getUpdatedFilesToDownload() throws IOException {
    long t1 = System.currentTimeMillis();

    FTPListParseEngine engine = _ftpClient.initiateListParsing(_dataDirectory);

    Set<String> paths = new HashSet<String>();
    List<String> toDownload = new ArrayList<String>();

    while (engine.hasNext()) {
        FTPFile[] files = engine.getNext(25); // "page size" you want
        for (FTPFile file : files) {
            String path = _dataDirectory + "/" + file.getName();
            paths.add(path);// w w w .j  av a  2 s  .  co  m
            if (!_paths.contains(path))
                toDownload.add(path);
        }
    }

    _totalFtpFiles = paths.size();
    _newFtpFiles = toDownload.size();

    long t2 = System.currentTimeMillis();

    if (_log.isDebugEnabled())
        _log.debug("file listing time: " + (t2 - t1) + " totalFiles: " + paths.size() + " newFiles: "
                + toDownload.size());

    _paths = paths;

    if (_maxDownloadCount > 0 && toDownload.size() > _maxDownloadCount) {
        List<String> reduced = new ArrayList<String>(_maxDownloadCount);
        for (int i = 0; i < _maxDownloadCount; i++)
            reduced.add(toDownload.get(toDownload.size() - _maxDownloadCount + i));
        toDownload = reduced;
    }

    return toDownload;
}

From source file:org.paxle.crawler.ftp.impl.FtpUrlConnection.java

private static void formatStdDirlisting(final OutputStream into, final FTPListParseEngine fileParseEngine) {
    final Formatter writer = new Formatter(into);
    FTPFile[] files;/*  ww  w  .j  a v a 2 s  . co m*/
    while (fileParseEngine.hasNext()) {
        files = fileParseEngine.getNext(16);
        for (final FTPFile file : files) {
            if (file == null)
                continue;

            // directory
            char c;
            switch (file.getType()) {
            case FTPFile.DIRECTORY_TYPE:
                c = 'd';
                break;
            case FTPFile.SYMBOLIC_LINK_TYPE:
                c = 's';
                break;
            default:
                c = '-';
                break;
            }
            writer.format("%c", Character.valueOf(c));

            // permissions
            for (final int access : FILE_ACCESS_MODES) {
                writer.format("%c%c%c",
                        Character.valueOf(file.hasPermission(access, FTPFile.READ_PERMISSION) ? 'r' : '-'),
                        Character.valueOf(file.hasPermission(access, FTPFile.WRITE_PERMISSION) ? 'w' : '-'),
                        Character.valueOf(file.hasPermission(access, FTPFile.EXECUTE_PERMISSION) ? 'x' : '-'));
            }

            // other information
            writer.format("  %2d", Integer.valueOf(file.getHardLinkCount()));
            writer.format("  %8s", file.getUser());
            writer.format("  %8s", file.getGroup());
            writer.format("  %12d", Long.valueOf(file.getSize()));
            writer.format("  %1$tY-%1$tm-%1$td %1$tH:%1$tM",
                    Long.valueOf(file.getTimestamp().getTimeInMillis()));
            writer.format("  %s", file.getName());

            writer.format("%s", System.getProperty("line.separator"));
        }
    }

    writer.flush();
}