Example usage for org.apache.commons.net.ftp FTPFileFilters NON_NULL

List of usage examples for org.apache.commons.net.ftp FTPFileFilters NON_NULL

Introduction

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

Prototype

FTPFileFilter NON_NULL

To view the source code for org.apache.commons.net.ftp FTPFileFilters NON_NULL.

Click Source Link

Document

Accepts all non-null FTPFile entries.

Usage

From source file:nl.esciencecenter.xenon.adaptors.filesystems.ftp.FtpFileSystem.java

@Override
protected List<PathAttributes> listDirectory(Path path) throws XenonException {
    assertIsOpen();//w w w .j  a v a 2  s  .c o m
    assertDirectoryExists(path);

    try {
        ArrayList<PathAttributes> result = new ArrayList<>();

        for (FTPFile f : ftpClient.listFiles(path.toString(), FTPFileFilters.NON_NULL)) {
            result.add(convertAttributes(path.resolve(f.getName()), f));
        }

        return result;
    } catch (IOException e) {
        throw new XenonException(ADAPTOR_NAME, "Failed to retrieve directory listing of " + path, e);
    }
}

From source file:org.codelibs.fess.crawler.client.ftp.FtpClient.java

protected ResponseData getResponseData(final String uri, final boolean includeContent) {
    final ResponseData responseData = new ResponseData();
    FTPClient client = null;// w ww  .  j  a  v  a2s  . c  o  m
    try {
        responseData.setMethod(Constants.GET_METHOD);

        final FtpInfo ftpInfo = new FtpInfo(uri);
        responseData.setUrl(ftpInfo.toUrl());

        client = getClient(ftpInfo);

        FTPFile file = null;
        client.changeWorkingDirectory(ftpInfo.getParent());
        validateRequest(client);

        if (ftpInfo.getName() == null) {
            // root directory
            final Set<RequestData> requestDataSet = new HashSet<>();
            if (includeContent) {
                try {
                    final FTPFile[] files = client.listFiles(ftpInfo.getParent(), FTPFileFilters.NON_NULL);
                    validateRequest(client);
                    for (final FTPFile f : files) {
                        final String chileUri = ftpInfo.toUrl(f.getName());
                        requestDataSet.add(RequestDataBuilder.newRequestData().get().url(chileUri).build());
                    }
                } catch (final IOException e) {
                    throw new CrawlingAccessException("Could not access " + uri, e);
                }
            }
            ftpClientQueue.offer(client);
            throw new ChildUrlsException(requestDataSet,
                    this.getClass().getName() + "#getResponseData(String, boolean)");
        }

        final FTPFile[] files = client.listFiles(null, FTPFileFilters.NON_NULL);
        validateRequest(client);
        for (final FTPFile f : files) {
            if (ftpInfo.getName().equals(f.getName())) {
                file = f;
                break;
            }
        }

        if (file == null) {
            responseData.setHttpStatusCode(Constants.NOT_FOUND_STATUS_CODE);
            responseData.setCharSet(charset);
            responseData.setContentLength(0);
        } else if (file.isFile()) {
            responseData.setHttpStatusCode(Constants.OK_STATUS_CODE);
            responseData.setCharSet(Constants.UTF_8);
            responseData.setLastModified(file.getTimestamp().getTime());

            // check file size
            responseData.setContentLength(file.getSize());
            checkMaxContentLength(responseData);

            if (includeContent) {
                File tempFile = null;
                File outputFile = null;
                try {
                    tempFile = File.createTempFile("ftp-", ".tmp");
                    try (OutputStream out = new BufferedOutputStream(new FileOutputStream(tempFile))) {
                        if (!client.retrieveFile(ftpInfo.getName(), out)) {
                            throw new CrawlingAccessException("Failed to retrieve: " + ftpInfo.toUrl());
                        }
                    }

                    final MimeTypeHelper mimeTypeHelper = crawlerContainer.getComponent("mimeTypeHelper");
                    try (InputStream is = new FileInputStream(tempFile)) {
                        responseData.setMimeType(mimeTypeHelper.getContentType(is, file.getName()));
                    } catch (final Exception e) {
                        responseData.setMimeType(mimeTypeHelper.getContentType(null, file.getName()));
                    }

                    if (contentLengthHelper != null) {
                        final long maxLength = contentLengthHelper.getMaxLength(responseData.getMimeType());
                        if (responseData.getContentLength() > maxLength) {
                            throw new MaxLengthExceededException(
                                    "The content length (" + responseData.getContentLength() + " byte) is over "
                                            + maxLength + " byte. The url is " + uri);
                        }
                    }

                    responseData.setCharSet(geCharSet(tempFile));

                    if (tempFile.length() < maxCachedContentSize) {
                        try (InputStream contentStream = new BufferedInputStream(
                                new FileInputStream(tempFile))) {
                            responseData.setResponseBody(InputStreamUtil.getBytes(contentStream));
                        }
                    } else {
                        outputFile = File.createTempFile("crawler-FileSystemClient-", ".out");
                        CopyUtil.copy(tempFile, outputFile);
                        responseData.setResponseBody(outputFile, true);
                    }
                } catch (final Exception e) {
                    logger.warn("I/O Exception.", e);
                    responseData.setHttpStatusCode(Constants.SERVER_ERROR_STATUS_CODE);
                } finally {
                    if (tempFile != null && !tempFile.delete()) {
                        logger.warn("Could not delete " + tempFile.getAbsolutePath());
                    }
                    if (outputFile != null && !outputFile.delete()) {
                        logger.warn("Could not delete " + outputFile.getAbsolutePath());
                    }
                }
            }
        } else if (file.isDirectory()) {
            final Set<RequestData> requestDataSet = new HashSet<>();
            if (includeContent) {
                try {
                    final FTPFile[] ftpFiles = client.listFiles(ftpInfo.getName(), FTPFileFilters.NON_NULL);
                    validateRequest(client);
                    for (final FTPFile f : ftpFiles) {
                        final String chileUri = ftpInfo.toUrl(f.getName());
                        requestDataSet.add(RequestDataBuilder.newRequestData().get().url(chileUri).build());
                    }
                } catch (final IOException e) {
                    throw new CrawlingAccessException("Could not access " + uri, e);
                }
            }
            ftpClientQueue.offer(client);
            throw new ChildUrlsException(requestDataSet,
                    this.getClass().getName() + "#getResponseData(String, boolean)");
        } else {
            responseData.setHttpStatusCode(Constants.NOT_FOUND_STATUS_CODE);
            responseData.setCharSet(charset);
            responseData.setContentLength(0);
        }
        ftpClientQueue.offer(client);
    } catch (final CrawlerSystemException e) {
        IOUtils.closeQuietly(responseData);
        throw e;
    } catch (final Exception e) {
        IOUtils.closeQuietly(responseData);
        throw new CrawlingAccessException("Could not access " + uri, e);
    }

    return responseData;
}