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

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

Introduction

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

Prototype

public String getLink() 

Source Link

Document

If the FTPFile is a symbolic link, this method returns the name of the file being pointed to by the symbolic link.

Usage

From source file:org.apache.flume.source.FTPSource.java

@SuppressWarnings("UnnecessaryContinue")
public void discoverElements(FTPClient ftpClient, String parentDir, String currentDir, int level)
        throws IOException {

    String dirToList = parentDir;
    if (!currentDir.equals("")) {
        dirToList += "/" + currentDir;
    }//from  w w w.  ja va2  s  .  c o  m
    FTPFile[] subFiles = ftpClient.listFiles(dirToList);
    if (subFiles != null && subFiles.length > 0) {

        for (FTPFile aFile : subFiles) {
            String currentFileName = aFile.getName();
            if (currentFileName.equals(".") || currentFileName.equals("..")) {
                log.info("Skip parent directory and directory itself");
                continue;
            }

            if (aFile.isDirectory()) {
                log.info("[" + aFile.getName() + "]");
                ftpClient.changeWorkingDirectory(parentDir);
                discoverElements(ftpClient, dirToList, aFile.getName(), level + 1);
                continue;
            } else if (aFile.isFile()) { //aFile is a regular file
                ftpClient.changeWorkingDirectory(dirToList);
                existFileList.add(dirToList + "/" + aFile.getName()); //control of deleted files in server
                String fileName = aFile.getName();
                if (!(sizeFileList.containsKey(dirToList + "/" + aFile.getName()))) { //new file
                    ftpSourceCounter.incrementFilesCount();
                    InputStream inputStream = null;
                    try {
                        inputStream = ftpClient.retrieveFileStream(aFile.getName());
                        listener.fileStreamRetrieved();
                        readStream(inputStream, 0);
                        boolean success = inputStream != null && ftpClient.completePendingCommand(); //mandatory
                        if (success) {
                            sizeFileList.put(dirToList + "/" + aFile.getName(), aFile.getSize());
                            saveMap(sizeFileList);
                            ftpSourceCounter.incrementFilesProcCount();
                            log.info("discovered: " + fileName + " ," + sizeFileList.size());
                        } else {
                            handleProcessError(fileName);
                        }
                    } catch (FTPConnectionClosedException e) {
                        handleProcessError(fileName);
                        log.error("Ftp server closed connection ", e);
                        continue;
                    }

                    ftpClient.changeWorkingDirectory(dirToList);
                    continue;

                } else { //known file                        
                    long dif = aFile.getSize() - sizeFileList.get(dirToList + "/" + aFile.getName());
                    if (dif > 0) { //known and modified
                        long prevSize = sizeFileList.get(dirToList + "/" + aFile.getName());
                        InputStream inputStream = null;
                        try {
                            inputStream = ftpClient.retrieveFileStream(aFile.getName());
                            listener.fileStreamRetrieved();
                            readStream(inputStream, prevSize);

                            boolean success = inputStream != null && ftpClient.completePendingCommand(); //mandatory
                            if (success) {
                                sizeFileList.put(dirToList + "/" + aFile.getName(), aFile.getSize());
                                saveMap(sizeFileList);
                                ftpSourceCounter.incrementCountModProc();
                                log.info("modified: " + fileName + " ," + sizeFileList.size());
                            } else {
                                handleProcessError(fileName);
                            }
                        } catch (FTPConnectionClosedException e) {
                            log.error("Ftp server closed connection ", e);
                            handleProcessError(fileName);
                            continue;
                        }

                        ftpClient.changeWorkingDirectory(dirToList);
                        continue;
                    } else if (dif < 0) { //known and full modified
                        existFileList.remove(dirToList + "/" + aFile.getName()); //will be rediscovered as new file
                        saveMap(sizeFileList);
                        continue;
                    }
                    ftpClient.changeWorkingDirectory(parentDir);
                    continue;
                }
            } else if (aFile.isSymbolicLink()) {
                log.info(aFile.getName() + " is a link of " + aFile.getLink() + " access denied");
                ftpClient.changeWorkingDirectory(parentDir);
                continue;
            } else if (aFile.isUnknown()) {
                log.info(aFile.getName() + " unknown type of file");
                ftpClient.changeWorkingDirectory(parentDir);
                continue;
            } else {
                ftpClient.changeWorkingDirectory(parentDir);
                continue;
            }

        } //fin de bucle
    } //el listado no es vaco
}

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 .java  2 s.  co  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:ro.kuberam.libs.java.ftclient.FTP.FTP.java

private static void _generateResourceElement(XMLStreamWriter xmlWriter, FTPFile resource, InputStream is,
        String resourceAbsolutePath) throws IOException, Exception {

    String resourceName = resource.getName();
    String resourceType = ((resource.getType() == 1) ? "directory"
            : (((resource.getType() == 0) ? "file" : "link")));
    Calendar resourceTimeStamp = resource.getTimestamp();
    DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ssZ");
    String lastModified = formatter.format(resourceTimeStamp.getTimeInMillis());
    lastModified = lastModified.replace(" ", "T");
    lastModified = lastModified.substring(0, 22) + ":" + lastModified.substring(22, 24);
    long resourceSize = resource.getSize();
    String user = resource.getUser();
    String userGroup = resource.getGroup();
    String permissions = resource.getRawListing().substring(0, 10);
    String linkTo = resource.getLink();

    GenerateResourceElement.run(is, xmlWriter, modulePrefix, moduleNsUri, resourceName, resourceType,
            resourceAbsolutePath, lastModified, resourceSize, user, userGroup, permissions, linkTo);
}