Search in sources :

Example 71 with FTPFile

use of org.apache.commons.net.ftp.FTPFile in project Lucee by lucee.

the class FTPResource method setLastModified.

@Override
public boolean setLastModified(long time) {
    // if(isRoot()) return false;
    FTPResourceClient client = null;
    try {
        provider.lock(this);
        client = provider.getClient(data);
        PageContext pc = ThreadLocalPageContext.get();
        Calendar c = JREDateTimeUtil.getThreadCalendar();
        if (pc != null)
            c.setTimeZone(pc.getTimeZone());
        c.setTimeInMillis(time);
        FTPFile file = client.getFTPFile(this);
        if (file == null)
            return false;
        file.setTimestamp(c);
        client.unregisterFTPFile(this);
        return true;
    } catch (IOException e) {
    } finally {
        provider.returnClient(client);
        provider.unlock(this);
    }
    return false;
}
Also used : Calendar(java.util.Calendar) PageContext(lucee.runtime.PageContext) ThreadLocalPageContext(lucee.runtime.engine.ThreadLocalPageContext) FTPFile(org.apache.commons.net.ftp.FTPFile) IOException(java.io.IOException)

Example 72 with FTPFile

use of org.apache.commons.net.ftp.FTPFile in project scheduling by ow2-proactive.

the class FTPConnector method downloadDirectory.

private List<String> downloadDirectory(FTPClient ftpClient, String parentDir, String currentDir, String saveDir) throws IOException {
    List<String> filesRelativePathName = new ArrayList<>();
    String dirToList = parentDir;
    if (!currentDir.isEmpty()) {
        dirToList = Paths.get(dirToList, currentDir).toString();
    }
    FTPFile[] subFiles = ftpClient.listFiles(dirToList);
    if (subFiles != null) {
        for (FTPFile aFile : subFiles) {
            String currentFileName = aFile.getName();
            if (currentFileName.equals(".") || currentFileName.equals("..")) {
                // skip parent directory and the directory itself
                continue;
            }
            String remoteFilePath = Paths.get(parentDir, currentDir, currentFileName).toString();
            String savePath = Paths.get(saveDir, parentDir, currentDir, currentFileName).toString();
            if (aFile.isDirectory()) {
                // create the directory savePath inside saveDir
                makeDirectories(savePath);
                // download the sub directory
                filesRelativePathName.addAll(downloadDirectory(ftpClient, dirToList, currentFileName, saveDir));
            } else {
                // download the file
                filesRelativePathName.add(downloadSingleFile(ftpClient, remoteFilePath, savePath));
            }
        }
    }
    return filesRelativePathName;
}
Also used : ArrayList(java.util.ArrayList) FTPFile(org.apache.commons.net.ftp.FTPFile)

Example 73 with FTPFile

use of org.apache.commons.net.ftp.FTPFile in project camel by apache.

the class FtpConsumer method doPollDirectory.

protected boolean doPollDirectory(String absolutePath, String dirName, List<GenericFile<FTPFile>> fileList, int depth) {
    log.trace("doPollDirectory from absolutePath: {}, dirName: {}", absolutePath, dirName);
    depth++;
    // remove trailing /
    dirName = FileUtil.stripTrailingSeparator(dirName);
    // compute dir depending on stepwise is enabled or not
    String dir;
    if (isStepwise()) {
        dir = ObjectHelper.isNotEmpty(dirName) ? dirName : absolutePath;
        operations.changeCurrentDirectory(dir);
    } else {
        dir = absolutePath;
    }
    log.trace("Polling directory: {}", dir);
    List<FTPFile> files = null;
    if (isUseList()) {
        if (isStepwise()) {
            files = operations.listFiles();
        } else {
            files = operations.listFiles(dir);
        }
    } else {
        // we cannot use the LIST command(s) so we can only poll a named file
        // so created a pseudo file with that name
        FTPFile file = new FTPFile();
        file.setType(FTPFile.FILE_TYPE);
        fileExpressionResult = evaluateFileExpression();
        if (fileExpressionResult != null) {
            file.setName(fileExpressionResult);
            files = new ArrayList<FTPFile>(1);
            files.add(file);
        }
    }
    if (files == null || files.isEmpty()) {
        // no files in this directory to poll
        log.trace("No files found in directory: {}", dir);
        return true;
    } else {
        // we found some files
        log.trace("Found {} in directory: {}", files.size(), dir);
    }
    for (FTPFile file : files) {
        if (log.isTraceEnabled()) {
            log.trace("FtpFile[name={}, dir={}, file={}]", new Object[] { file.getName(), file.isDirectory(), file.isFile() });
        }
        // check if we can continue polling in files
        if (!canPollMoreFiles(fileList)) {
            return false;
        }
        if (file.isDirectory()) {
            RemoteFile<FTPFile> remote = asRemoteFile(absolutePath, file, getEndpoint().getCharset());
            if (endpoint.isRecursive() && depth < endpoint.getMaxDepth() && isValidFile(remote, true, files)) {
                // recursive scan and add the sub files and folders
                String subDirectory = file.getName();
                String path = absolutePath + "/" + subDirectory;
                boolean canPollMore = pollSubDirectory(path, subDirectory, fileList, depth);
                if (!canPollMore) {
                    return false;
                }
            }
        } else if (file.isFile()) {
            RemoteFile<FTPFile> remote = asRemoteFile(absolutePath, file, getEndpoint().getCharset());
            if (depth >= endpoint.getMinDepth() && isValidFile(remote, false, files)) {
                // matched file so add
                fileList.add(remote);
            }
        } else {
            log.debug("Ignoring unsupported remote file type: " + file);
        }
    }
    return true;
}
Also used : FTPFile(org.apache.commons.net.ftp.FTPFile)

Example 74 with FTPFile

use of org.apache.commons.net.ftp.FTPFile in project camel by apache.

the class FtpOperations method listFiles.

public List<FTPFile> listFiles(String path) throws GenericFileOperationFailedException {
    log.trace("listFiles({})", path);
    // use current directory if path not given
    if (ObjectHelper.isEmpty(path)) {
        path = ".";
    }
    try {
        final List<FTPFile> list = new ArrayList<FTPFile>();
        FTPFile[] files = client.listFiles(path);
        // can return either null or an empty list depending on FTP servers
        if (files != null) {
            list.addAll(Arrays.asList(files));
        }
        return list;
    } catch (IOException e) {
        throw new GenericFileOperationFailedException(client.getReplyCode(), client.getReplyString(), e.getMessage(), e);
    }
}
Also used : GenericFileOperationFailedException(org.apache.camel.component.file.GenericFileOperationFailedException) ArrayList(java.util.ArrayList) FTPFile(org.apache.commons.net.ftp.FTPFile) IOException(java.io.IOException)

Example 75 with FTPFile

use of org.apache.commons.net.ftp.FTPFile in project camel by apache.

the class FtpOperations method listFiles.

public List<FTPFile> listFiles() throws GenericFileOperationFailedException {
    log.trace("listFiles()");
    try {
        final List<FTPFile> list = new ArrayList<FTPFile>();
        FTPFile[] files = client.listFiles();
        // can return either null or an empty list depending on FTP servers
        if (files != null) {
            list.addAll(Arrays.asList(files));
        }
        return list;
    } catch (IOException e) {
        throw new GenericFileOperationFailedException(client.getReplyCode(), client.getReplyString(), e.getMessage(), e);
    }
}
Also used : GenericFileOperationFailedException(org.apache.camel.component.file.GenericFileOperationFailedException) ArrayList(java.util.ArrayList) FTPFile(org.apache.commons.net.ftp.FTPFile) IOException(java.io.IOException)

Aggregations

FTPFile (org.apache.commons.net.ftp.FTPFile)120 IOException (java.io.IOException)59 FTPClient (org.apache.commons.net.ftp.FTPClient)34 Test (org.junit.Test)32 File (java.io.File)28 InputStream (java.io.InputStream)16 ArrayList (java.util.ArrayList)15 FrameworkException (org.structr.common.error.FrameworkException)15 Tx (org.structr.core.graph.Tx)15 FtpTest (org.structr.web.files.FtpTest)15 FileOutputStream (java.io.FileOutputStream)11 OutputStream (java.io.OutputStream)9 ByteArrayInputStream (java.io.ByteArrayInputStream)8 BuildException (org.apache.tools.ant.BuildException)8 List (java.util.List)7 Matchers.containsString (org.hamcrest.Matchers.containsString)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 BeanFactory (org.springframework.beans.factory.BeanFactory)5 LiteralExpression (org.springframework.expression.common.LiteralExpression)5 HashSet (java.util.HashSet)4