Search in sources :

Example 66 with FTPFile

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

the class SFTPClientImpl method listFiles.

@Override
public FTPFile[] listFiles(String pathname) throws IOException {
    pathname = cleanPath(pathname);
    List<FTPFile> files = new ArrayList<FTPFile>();
    try {
        Vector list = channelSftp.ls(pathname);
        Iterator<ChannelSftp.LsEntry> it = list.iterator();
        ChannelSftp.LsEntry entry;
        SftpATTRS attrs;
        FTPFile file;
        String fileName;
        while (it.hasNext()) {
            entry = it.next();
            attrs = entry.getAttrs();
            fileName = entry.getFilename();
            if (fileName.equals(".") || fileName.equals(".."))
                continue;
            file = new FTPFile();
            files.add(file);
            // is dir
            file.setType(attrs.isDir() ? FTPFile.DIRECTORY_TYPE : FTPFile.FILE_TYPE);
            file.setTimestamp(Caster.toCalendar(attrs.getMTime() * 1000L, null, Locale.ENGLISH));
            file.setSize(attrs.isDir() ? 0 : attrs.getSize());
            FTPConstant.setPermission(file, attrs.getPermissions());
            file.setName(fileName);
        }
        handleSucess();
    } catch (SftpException e) {
        handleFail(e, stopOnError);
    }
    return files.toArray(new FTPFile[files.size()]);
}
Also used : ChannelSftp(com.jcraft.jsch.ChannelSftp) SftpATTRS(com.jcraft.jsch.SftpATTRS) SftpException(com.jcraft.jsch.SftpException) ArrayList(java.util.ArrayList) FTPFile(org.apache.commons.net.ftp.FTPFile) Vector(java.util.Vector)

Example 67 with FTPFile

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

the class FTPResource method listResources.

@Override
public Resource[] listResources() {
    // new Resource[0];
    if (isFile())
        return null;
    FTPResourceClient client = null;
    try {
        client = provider.getClient(data);
        FTPFile[] files = null;
        String p = getInnerPath();
        if (!StringUtil.endsWith(p, '/'))
            p += "/";
        files = client.listFiles(p);
        if (files == null)
            return new Resource[0];
        List list = new ArrayList();
        String parent = path.concat(name).concat("/");
        String name;
        FTPResource res;
        for (int i = 0; i < files.length; i++) {
            name = files[i].getName();
            if (!".".equals(name) && !"..".equals(name)) {
                res = new FTPResource(provider, data, parent, name);
                client.registerFTPFile(res, files[i]);
                list.add(res);
            }
        }
        return (Resource[]) list.toArray(new FTPResource[list.size()]);
    } catch (IOException ioe) {
        return null;
    } finally {
        provider.returnClient(client);
    }
}
Also used : ArrayList(java.util.ArrayList) FTPFile(org.apache.commons.net.ftp.FTPFile) ArrayList(java.util.ArrayList) List(java.util.List) IOException(java.io.IOException)

Example 68 with FTPFile

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

the class FTPResource method hasPermission.

private Boolean hasPermission(int permission) {
    FTPResourceClient client = null;
    try {
        provider.read(this);
        client = provider.getClient(data);
        FTPFile file = client.getFTPFile(this);
        if (file == null)
            return null;
        return Caster.toBoolean(file.hasPermission(FTPFile.USER_ACCESS, permission) || file.hasPermission(FTPFile.GROUP_ACCESS, permission) || file.hasPermission(FTPFile.WORLD_ACCESS, permission));
    } catch (IOException e) {
        return Boolean.FALSE;
    } finally {
        provider.returnClient(client);
    }
}
Also used : FTPFile(org.apache.commons.net.ftp.FTPFile) IOException(java.io.IOException)

Example 69 with FTPFile

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

the class FTPResource method exists.

@Override
public boolean exists() {
    try {
        provider.read(this);
    } catch (IOException e) {
        return true;
    }
    FTPResourceClient client = null;
    InputStream is = null;
    try {
        // getClient has to be first to check connection
        client = provider.getClient(data);
        if (isRoot())
            return true;
        FTPFile file = client.getFTPFile(this);
        if (file != null) {
            return !file.isUnknown();
        }
        // String pathname = getInnerPath();
        String p = getInnerPath();
        if (!StringUtil.endsWith(p, '/'))
            p += "/";
        if (client.listNames(p) != null)
            return true;
        return false;
    } catch (IOException e) {
        return false;
    } finally {
        IOUtil.closeEL(is);
        provider.returnClient(client);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) FTPFile(org.apache.commons.net.ftp.FTPFile) IOException(java.io.IOException)

Example 70 with FTPFile

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

the class FTPResource method setMode.

@Override
public void setMode(int mode) throws IOException {
    // if(isRoot()) throw new IOException("can't change mode of root");
    FTPResourceClient client = null;
    try {
        provider.lock(this);
        client = provider.getClient(data);
        FTPFile file = client.getFTPFile(this);
        if (file != null) {
            // World
            file.setPermission(FTPFile.WORLD_ACCESS, FTPFile.EXECUTE_PERMISSION, (mode & 01) > 0);
            file.setPermission(FTPFile.WORLD_ACCESS, FTPFile.WRITE_PERMISSION, (mode & 02) > 0);
            file.setPermission(FTPFile.WORLD_ACCESS, FTPFile.READ_PERMISSION, (mode & 04) > 0);
            // Group
            file.setPermission(FTPFile.GROUP_ACCESS, FTPFile.EXECUTE_PERMISSION, (mode & 010) > 0);
            file.setPermission(FTPFile.GROUP_ACCESS, FTPFile.WRITE_PERMISSION, (mode & 020) > 0);
            file.setPermission(FTPFile.GROUP_ACCESS, FTPFile.READ_PERMISSION, (mode & 040) > 0);
            // Owner
            file.setPermission(FTPFile.USER_ACCESS, FTPFile.EXECUTE_PERMISSION, (mode & 0100) > 0);
            file.setPermission(FTPFile.USER_ACCESS, FTPFile.WRITE_PERMISSION, (mode & 0200) > 0);
            file.setPermission(FTPFile.USER_ACCESS, FTPFile.READ_PERMISSION, (mode & 0400) > 0);
            client.unregisterFTPFile(this);
        }
    } catch (IOException e) {
    } finally {
        provider.returnClient(client);
        provider.unlock(this);
    }
}
Also used : 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