Example usage for org.apache.commons.net.ftp FTPFile isFile

List of usage examples for org.apache.commons.net.ftp FTPFile isFile

Introduction

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

Prototype

public boolean isFile() 

Source Link

Document

Determine if the file is a regular file.

Usage

From source file:org.apache.ftpserver.clienttests.ListTest.java

public void testListFileNoArgument() throws Exception {
    TEST_DIR1.mkdirs();/*from   w w w .  j  a v  a2s.co m*/

    FTPFile[] files = client.listFiles();

    assertEquals(1, files.length);

    FTPFile file = getFile(files, TEST_DIR1.getName());
    assertEquals(TEST_DIR1.getName(), file.getName());
    assertEquals(0, file.getSize());
    assertEquals("group", file.getGroup());
    assertEquals("user", file.getUser());
    assertFalse(file.isFile());
    assertTrue(file.isDirectory());
}

From source file:org.apache.ftpserver.clienttests.ListTest.java

public void testListFiles() throws Exception {
    TEST_FILE1.createNewFile();/*from  ww w .  ja v  a 2s.com*/
    TEST_FILE2.createNewFile();
    TEST_DIR1.mkdirs();
    TEST_DIR2.mkdirs();

    TestUtil.writeDataToFile(TEST_FILE1, testData);

    FTPFile[] files = client.listFiles();

    assertEquals(4, files.length);
    FTPFile file = getFile(files, TEST_FILE1.getName());
    assertEquals(TEST_FILE1.getName(), file.getName());
    assertEquals(testData.length, file.getSize());
    assertEquals("group", file.getGroup());
    assertEquals("user", file.getUser());
    assertTrue(file.isFile());
    assertFalse(file.isDirectory());

    file = getFile(files, TEST_FILE2.getName());
    assertEquals(TEST_FILE2.getName(), file.getName());
    assertEquals(0, file.getSize());
    assertEquals("group", file.getGroup());
    assertEquals("user", file.getUser());
    assertTrue(file.isFile());
    assertFalse(file.isDirectory());

    file = getFile(files, TEST_DIR1.getName());
    assertEquals(TEST_DIR1.getName(), file.getName());
    assertEquals(0, file.getSize());
    assertEquals("group", file.getGroup());
    assertEquals("user", file.getUser());
    assertFalse(file.isFile());
    assertTrue(file.isDirectory());

    file = getFile(files, TEST_DIR2.getName());
    assertEquals(TEST_DIR2.getName(), file.getName());
    assertEquals(0, file.getSize());
    assertEquals("group", file.getGroup());
    assertEquals("user", file.getUser());
    assertFalse(file.isFile());
    assertTrue(file.isDirectory());
}

From source file:org.apache.hive.hplsql.Ftp.java

/**
 * Get the list of files to transfer/*from  ww w  .j a  v a 2s.c o  m*/
 */
void retrieveFileList(String dir) {
    if (info) {
        if (dir == null || dir.isEmpty()) {
            info(null, "  Listing the current working FTP directory");
        } else {
            info(null, "  Listing " + dir);
        }
    }
    try {
        FTPFile[] files = ftp.listFiles(dir);
        ArrayList<FTPFile> dirs = new ArrayList<FTPFile>();
        for (FTPFile file : files) {
            String name = file.getName();
            if (file.isFile()) {
                if (filePattern == null || Pattern.matches(filePattern, name)) {
                    if (dir != null && !dir.isEmpty()) {
                        if (dir.endsWith("/")) {
                            name = dir + name;
                        } else {
                            name = dir + "/" + name;
                        }
                    }
                    if (!newOnly || !isTargetExists(name)) {
                        fileCnt++;
                        ftpSizeInBytes += file.getSize();
                        filesQueue.add(name);
                        filesMap.put(name, file);
                    }
                }
            } else {
                if (subdir && !name.equals(".") && !name.equals("..")) {
                    dirCnt++;
                    dirs.add(file);
                }
            }
        }
        if (subdir) {
            for (FTPFile d : dirs) {
                String sd = d.getName();
                if (dir != null && !dir.isEmpty()) {
                    if (dir.endsWith("/")) {
                        sd = dir + sd;
                    } else {
                        sd = dir + "/" + sd;
                    }
                }
                retrieveFileList(sd);
            }
        }
    } catch (IOException e) {
        exec.signal(e);
    }
}

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

private byte[] list2html(List<FTPFile> list, String path, boolean includeDotDot) {

    // StringBuffer x = new
    // StringBuffer("<!doctype html public \"-//ietf//dtd html//en\"><html><head>");
    StringBuffer x = new StringBuffer("<html><head>");
    x.append("<title>Index of " + path + "</title></head>\n");
    x.append("<body><h1>Index of " + path + "</h1><pre>\n");

    if (includeDotDot) {
        x.append("<a href='../'>../</a>\t-\t-\t-\n");
    }//from w w  w  .ja v  a 2  s.  c om

    for (int i = 0; i < list.size(); i++) {
        FTPFile f = (FTPFile) list.get(i);
        String name = f.getName();
        String time = HttpDateFormat.toString(f.getTimestamp());
        if (f.isDirectory()) {
            // some ftp server LIST "." and "..", we skip them here
            if (name.equals(".") || name.equals(".."))
                continue;
            x.append("<a href='" + name + "/" + "'>" + name + "/</a>\t");
            x.append(time + "\t-\n");
        } else if (f.isFile()) {
            x.append("<a href='" + name + "'>" + name + "</a>\t");
            x.append(time + "\t" + f.getSize() + "\n");
        } else {
            // ignore isSymbolicLink()
            // ignore isUnknown()
        }
    }

    x.append("</pre></body></html>\n");

    return new String(x).getBytes();
}

From source file:org.apache.tools.ant.taskdefs.optional.net.FTPTaskMirrorImpl.java

/**
 * check FTPFiles to check whether they function as directories too
 * the FTPFile API seem to make directory and symbolic links incompatible
 * we want to find out if we can cd to a symbolic link
 * @param dir  the parent directory of the file to test
 * @param file the file to test//from   w  ww.j  a v a  2 s. c o  m
 * @return true if it is possible to cd to this directory
 * @since ant 1.6
 */
private boolean isFunctioningAsDirectory(FTPClient ftp, String dir, FTPFile file) {
    boolean result = false;
    String currentWorkingDir = null;
    if (file.isDirectory()) {
        return true;
    } else if (file.isFile()) {
        return false;
    }
    try {
        currentWorkingDir = ftp.printWorkingDirectory();
    } catch (IOException ioe) {
        task.log("could not find current working directory " + dir + " while checking a symlink",
                Project.MSG_DEBUG);
    }
    if (currentWorkingDir != null) {
        try {
            result = ftp.changeWorkingDirectory(file.getLink());
        } catch (IOException ioe) {
            task.log("could not cd to " + file.getLink() + " while checking a symlink", Project.MSG_DEBUG);
        }
        if (result) {
            boolean comeback = false;
            try {
                comeback = ftp.changeWorkingDirectory(currentWorkingDir);
            } catch (IOException ioe) {
                task.log("could not cd back to " + dir + " while checking a symlink", Project.MSG_ERR);
            } finally {
                if (!comeback) {
                    throw new BuildException("could not cd back to " + dir + " while checking a symlink");
                }
            }
        }
    }
    return result;
}

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  a 2s  .  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;
}

From source file:org.fao.fenix.ftp.FTPTask.java

private void checkFTPDirectory(String datasetDirectoryName) {

    try {/*  ww  w . j  a v a  2s.com*/
        if (ftpConnect()) {
            System.out.println("=== amis-ftp === FTPTask: checkFTPDirectory: == INFO == Task executing ...");
            //enter passive mode
            ftpClient.enterLocalPassiveMode();

            //get system name
            System.out.println("=== amis-ftp === FTPTask: checkFTPDirectory: == INFO == Remote system is "
                    + ftpClient.getSystemType());

            //change current directory
            ftpClient.changeWorkingDirectory(remoteDirectory + File.separator + datasetDirectoryName);
            System.out.println("=== amis-ftp === FTPTask: checkFTPDirectory: == INFO == Current directory is "
                    + ftpClient.printWorkingDirectory());

            //get list of filenames
            FTPFile[] ftpFiles = ftpClient.listFiles();

            // Check the directory
            if (ftpFiles != null && ftpFiles.length > 0) {
                ArrayList csvFiles = new ArrayList();

                for (FTPFile file : ftpFiles) {
                    if (file.isFile() && file.getName().endsWith(".csv")) {
                        csvFiles.add(file);
                    }
                }

                FTPFile[] csvFTPFiles = (FTPFile[]) csvFiles.toArray(new FTPFile[csvFiles.size()]);

                System.out.println(
                        "=== amis-ftp === FTPTask: checkFTPDirectory: == INFO ==  Current directory contains  "
                                + csvFTPFiles.length + " CSV File(s)");

                if (csvFTPFiles.length == 1) {
                    validateAndImportFile(csvFTPFiles, datasetDirectoryName);
                } else {
                    System.out.println(
                            "=== amis-ftp === FTPTask: checkFTPDirectory: == ERROR ==  The ftp://ext-ftp.fao.org"
                                    + ftpClient.printWorkingDirectory() + " directory contains "
                                    + ftpFiles.length
                                    + " files, instead of the expected 1. Please remove the redundant files. [THE ERROR EMAIL HAS BEEN SENT TO USER]");
                    //throw new FenixException("** The ftp://ext-ftp.fao.org"+ftpClient.printWorkingDirectory()+" directory contains "+ftpFiles.length+" files, instead of the expected 1. Please remove the redundant files.");
                }
            } else {
                System.out.println(
                        "=== amis-ftp === FTPTask: checkFTPDirectory: == WARNING ==  The ftp://ext-ftp.fao.org"
                                + ftpClient.printWorkingDirectory()
                                + " directory contains no files. No action taken until 1 CSV file is there.");
            }

            ftpClient.logout();
            ftpClient.disconnect();

        } else {
            ftpClient.disconnect();
        }
    } catch (IOException ex) {
        ex.printStackTrace();
    }
    //Email to Developers
    catch (FenixSystemException fse) {
        System.out.println(
                "=== amis-ftp === FTPTask: checkFTPDirectory: == ERROR ==  EMAIL SENT TO AMIS DEVELOPERS FenixSystemException: ");
        emailSender.sendEMailToDevelopers(emailErrorSubject, fse.getMessage());
        //emailSender.sendEMailToContacts(emailErrorSubject, fse.getMessage(), false);
    } finally {
        try {
            if (jdbcConnector.getConn() != null)
                jdbcConnector.getConn().close();

            if (ftpClient.isConnected())
                ftpClient.disconnect();

        } catch (SQLException se) {
            se.printStackTrace();
        } catch (IOException se) {
            se.printStackTrace();
        }
    }
}

From source file:org.martin.ftp.net.FTPLinker.java

public FTPFileFilter getFilter(Filter filter) {

    return (FTPFile ftpf) -> {

        if (filter != null) {
            switch (filter) {
            case FILES_ONLY:
                return ftpf.isFile();
            case DIRECTORIES_ONLY:
                return ftpf.isDirectory();
            default:
                return true;
            }/* ww  w  .ja  va  2  s .  c om*/
        } else
            return true;
    };
}

From source file:org.mousephenotype.dcc.crawler.ZipFileFilter.java

@Override
public boolean accept(FTPFile f) {
    return f != null && f.isFile() && f.getName().endsWith(".zip");
}

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

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