Example usage for org.apache.commons.vfs2.provider.ftp FtpFileSystem getClient

List of usage examples for org.apache.commons.vfs2.provider.ftp FtpFileSystem getClient

Introduction

In this page you can find the example usage for org.apache.commons.vfs2.provider.ftp FtpFileSystem getClient.

Prototype

public FtpClient getClient() throws FileSystemException 

Source Link

Document

Creates an FTP client to use.

Usage

From source file:com.streamsets.pipeline.stage.origin.remote.FTPRemoteDownloadSourceDelegate.java

private void setupModTime() {
    // The FTP protocol's default way to list files gives very inaccurate/ambiguous/inconsistent timestamps (e.g. it's
    // common for many FTP servers to drop the HH:mm on files older than 6 months).  Some FTP servers support the
    // MDTM command, which returns an accurate/correct timestamp, but not all servers support it.  Here, we'll check if
    // MDTM is supported so we can use it later to get proper timestamps.  Unfortunately, VFS does not expose a nice way
    // to use MDTM or to even get to the underlying FTPClient (VFS-257).  We have to use reflection.
    supportsMDTM = false;/*  w w  w  . j  a  va2s.  co m*/
    FtpClient ftpClient = null;
    FtpFileSystem ftpFileSystem = (FtpFileSystem) remoteDir.getFileSystem();
    try {
        ftpClient = ftpFileSystem.getClient();
        getFtpClient = ftpClient.getClass().getDeclaredMethod("getFtpClient");
        getFtpClient.setAccessible(true);
        FTPClient rawFtpClient = (FTPClient) getFtpClient.invoke(ftpClient);
        rawFtpClient.features();
        supportsMDTM = rawFtpClient.getReplyString().contains(FTPCmd.MDTM.getCommand());
    } catch (Exception e) {
        LOG.trace("Ignoring Exception when determining MDTM support", e);
    } finally {
        if (ftpClient != null) {
            ftpFileSystem.putClient(ftpClient);
        }
    }
    LOG.info("Using MDTM for more accurate timestamps: {}", supportsMDTM);
}

From source file:com.streamsets.pipeline.stage.origin.remote.FTPRemoteDownloadSourceDelegate.java

private long getModTime(FileObject fileObject) throws FileSystemException {
    long modTime = fileObject.getContent().getLastModifiedTime();
    if (supportsMDTM) {
        FtpClient ftpClient = null;// w ww .j av  a 2 s.  c  o m
        FtpFileSystem ftpFileSystem = (FtpFileSystem) remoteDir.getFileSystem();
        try {
            ftpClient = ftpFileSystem.getClient();
            FTPClient rawFtpClient = (FTPClient) getFtpClient.invoke(ftpClient);
            String path = fileObject.getName().getPath();
            if (conf.userDirIsRoot && path.startsWith("/")) {
                // Remove the leading slash to turn it into a proper relative path
                path = path.substring(1);
            }
            FTPFile ftpFile = rawFtpClient.mdtmFile(path);
            if (ftpFile != null) {
                modTime = ftpFile.getTimestamp().getTimeInMillis();
            }
        } catch (Exception e) {
            LOG.trace("Ignoring Exception from MDTM command and falling back to basic timestamp", e);
        } finally {
            if (ftpClient != null) {
                ftpFileSystem.putClient(ftpClient);
            }
        }
    }
    return modTime;
}