Search in sources :

Example 1 with ResourceFilter

use of lucee.commons.io.res.filter.ResourceFilter in project Lucee by lucee.

the class Directory method actionList.

/**
 * list all files and directories inside a directory
 * @throws PageException
 */
public static Object actionList(PageContext pageContext, Resource directory, String serverPassword, int type, ResourceFilter filter, int listInfo, boolean recurse, String sort) throws PageException {
    // check directory
    SecurityManager securityManager = pageContext.getConfig().getSecurityManager();
    securityManager.checkFileLocation(pageContext.getConfig(), directory, serverPassword);
    if (type != TYPE_ALL) {
        ResourceFilter typeFilter = (type == TYPE_DIR) ? DIRECTORY_FILTER : FILE_FILTER;
        if (filter == null)
            filter = typeFilter;
        else
            filter = new AndResourceFilter(new ResourceFilter[] { typeFilter, filter });
    }
    // create query Object
    String[] names = new String[] { "name", "size", "type", "dateLastModified", "attributes", "mode", "directory" };
    String[] types = new String[] { "VARCHAR", "DOUBLE", "VARCHAR", "DATE", "VARCHAR", "VARCHAR", "VARCHAR" };
    boolean hasMeta = directory instanceof ResourceMetaData;
    if (hasMeta) {
        names = new String[] { "name", "size", "type", "dateLastModified", "attributes", "mode", "directory", "meta" };
        types = new String[] { "VARCHAR", "DOUBLE", "VARCHAR", "DATE", "VARCHAR", "VARCHAR", "VARCHAR", "OBJECT" };
    }
    Array array = null;
    Query query = null;
    Object rtn;
    if (listInfo == LIST_INFO_QUERY_ALL || listInfo == LIST_INFO_QUERY_NAME) {
        boolean listOnlyNames = listInfo == LIST_INFO_QUERY_NAME;
        rtn = query = new QueryImpl(listOnlyNames ? new String[] { "name" } : names, listOnlyNames ? new String[] { "VARCHAR" } : types, 0, "query");
    } else
        rtn = array = new ArrayImpl();
    if (!directory.exists()) {
        if (directory instanceof FileResource)
            return rtn;
        throw new ApplicationException("directory [" + directory.toString() + "] doesn't exist");
    }
    if (!directory.isDirectory()) {
        if (directory instanceof FileResource)
            return rtn;
        throw new ApplicationException("file [" + directory.toString() + "] exists, but isn't a directory");
    }
    if (!directory.isReadable()) {
        if (directory instanceof FileResource)
            return rtn;
        throw new ApplicationException("no access to read directory [" + directory.toString() + "]");
    }
    long startNS = System.nanoTime();
    try {
        // Query All
        if (listInfo == LIST_INFO_QUERY_ALL)
            _fillQueryAll(query, directory, filter, 0, hasMeta, recurse);
        else // Query Name
        if (listInfo == LIST_INFO_QUERY_NAME) {
            if (recurse || type != TYPE_ALL)
                _fillQueryNamesRec("", query, directory, filter, 0, recurse);
            else
                _fillQueryNames(query, directory, filter, 0);
        } else // Array Name/Path
        if (listInfo == LIST_INFO_ARRAY_NAME || listInfo == LIST_INFO_ARRAY_PATH) {
            boolean onlyName = listInfo == LIST_INFO_ARRAY_NAME;
            if (// QueryNamesRec("",query, directory, filter, 0,recurse);
            !onlyName || recurse || type != TYPE_ALL)
                // QueryNamesRec("",query, directory, filter, 0,recurse);
                _fillArrayPathOrName(array, directory, filter, 0, recurse, onlyName);
            else
                _fillArrayName(array, directory, filter, 0);
        }
    } catch (IOException e) {
        throw Caster.toPageException(e);
    }
    // sort
    if (sort != null && query != null) {
        String[] arr = sort.toLowerCase().split(",");
        for (int i = arr.length - 1; i >= 0; i--) {
            try {
                String[] col = arr[i].trim().split("\\s+");
                if (col.length == 1)
                    query.sort(col[0].trim());
                else if (col.length == 2) {
                    String order = col[1].toLowerCase().trim();
                    if (order.equals("asc"))
                        query.sort(col[0], lucee.runtime.type.Query.ORDER_ASC);
                    else if (order.equals("desc"))
                        query.sort(col[0], lucee.runtime.type.Query.ORDER_DESC);
                    else
                        throw new ApplicationException("invalid order type [" + col[1] + "]");
                }
            } catch (Throwable t) {
                ExceptionUtil.rethrowIfNecessary(t);
            }
        }
    }
    if (query != null)
        query.setExecutionTime(System.nanoTime() - startNS);
    return rtn;
}
Also used : SecurityManager(lucee.runtime.security.SecurityManager) Query(lucee.runtime.type.Query) AndResourceFilter(lucee.commons.io.res.filter.AndResourceFilter) ArrayImpl(lucee.runtime.type.ArrayImpl) FileResource(lucee.commons.io.res.type.file.FileResource) IOException(java.io.IOException) Array(lucee.runtime.type.Array) AndResourceFilter(lucee.commons.io.res.filter.AndResourceFilter) NotResourceFilter(lucee.commons.io.res.filter.NotResourceFilter) FileResourceFilter(lucee.commons.io.res.filter.FileResourceFilter) OrResourceFilter(lucee.commons.io.res.filter.OrResourceFilter) ResourceFilter(lucee.commons.io.res.filter.ResourceFilter) DirectoryResourceFilter(lucee.commons.io.res.filter.DirectoryResourceFilter) QueryImpl(lucee.runtime.type.QueryImpl) ApplicationException(lucee.runtime.exp.ApplicationException) ResourceMetaData(lucee.commons.io.res.ResourceMetaData)

Example 2 with ResourceFilter

use of lucee.commons.io.res.filter.ResourceFilter in project Lucee by lucee.

the class Directory method actionCopy.

public static void actionCopy(PageContext pc, Resource directory, String strDestination, String serverPassword, boolean createPath, Object acl, String storage, final ResourceFilter filter, boolean recurse, int nameconflict) throws PageException {
    // check directory
    SecurityManager securityManager = pc.getConfig().getSecurityManager();
    securityManager.checkFileLocation(pc.getConfig(), directory, serverPassword);
    if (!directory.exists())
        throw new ApplicationException("directory [" + directory.toString() + "] doesn't exist");
    if (!directory.isDirectory())
        throw new ApplicationException("file [" + directory.toString() + "] exists, but isn't a directory");
    if (!directory.canRead())
        throw new ApplicationException("no access to read directory [" + directory.toString() + "]");
    if (StringUtil.isEmpty(strDestination))
        throw new ApplicationException("attribute destination is not defined");
    // real to source
    Resource newdirectory = toDestination(pc, strDestination, directory);
    if (nameconflict == NAMECONFLICT_ERROR && newdirectory.exists())
        throw new ApplicationException("new directory [" + newdirectory.toString() + "] already exist");
    securityManager.checkFileLocation(pc.getConfig(), newdirectory, serverPassword);
    try {
        boolean clearEmpty = false;
        // has already a filter
        ResourceFilter f = null;
        if (filter != null) {
            if (!recurse) {
                f = new AndResourceFilter(new ResourceFilter[] { filter, new NotResourceFilter(DirectoryResourceFilter.FILTER) });
            } else {
                clearEmpty = true;
                f = new OrResourceFilter(new ResourceFilter[] { filter, DirectoryResourceFilter.FILTER });
            }
        } else {
            if (!recurse)
                f = new NotResourceFilter(DirectoryResourceFilter.FILTER);
        }
        if (!createPath) {
            Resource p = newdirectory.getParentResource();
            if (p != null && !p.exists())
                throw new ApplicationException("parent directory for [" + newdirectory + "] doesn't exist");
        }
        ResourceUtil.copyRecursive(directory, newdirectory, f);
        if (clearEmpty)
            ResourceUtil.removeEmptyFolders(newdirectory, f == null ? null : new NotResourceFilter(filter));
    } catch (Throwable t) {
        ExceptionUtil.rethrowIfNecessary(t);
        throw new ApplicationException(t.getMessage());
    }
    // set S3 stuff
    setS3Attrs(pc, directory, acl, storage);
}
Also used : AndResourceFilter(lucee.commons.io.res.filter.AndResourceFilter) NotResourceFilter(lucee.commons.io.res.filter.NotResourceFilter) FileResourceFilter(lucee.commons.io.res.filter.FileResourceFilter) OrResourceFilter(lucee.commons.io.res.filter.OrResourceFilter) ResourceFilter(lucee.commons.io.res.filter.ResourceFilter) DirectoryResourceFilter(lucee.commons.io.res.filter.DirectoryResourceFilter) ApplicationException(lucee.runtime.exp.ApplicationException) SecurityManager(lucee.runtime.security.SecurityManager) OrResourceFilter(lucee.commons.io.res.filter.OrResourceFilter) AndResourceFilter(lucee.commons.io.res.filter.AndResourceFilter) Resource(lucee.commons.io.res.Resource) FileResource(lucee.commons.io.res.type.file.FileResource) NotResourceFilter(lucee.commons.io.res.filter.NotResourceFilter)

Example 3 with ResourceFilter

use of lucee.commons.io.res.filter.ResourceFilter in project Lucee by lucee.

the class DirectoryList method _call.

public static Object _call(PageContext pc, String path, boolean recurse, int listInfo, Object oFilter, String sort, int type) throws PageException {
    Resource dir = ResourceUtil.toResourceNotExisting(pc, path);
    ResourceFilter filter = UDFFilter.createResourceAndResourceNameFilter(oFilter);
    return Directory.actionList(pc, dir, null, type, filter, listInfo, recurse, sort);
}
Also used : ResourceFilter(lucee.commons.io.res.filter.ResourceFilter) Resource(lucee.commons.io.res.Resource)

Example 4 with ResourceFilter

use of lucee.commons.io.res.filter.ResourceFilter in project Lucee by lucee.

the class CompressUtil method extractZip.

private static void extractZip(Resource zipFile, Resource targetDir) throws IOException {
    if (!targetDir.exists() || !targetDir.isDirectory())
        throw new IOException(targetDir + " is not a existing directory");
    if (!zipFile.exists())
        throw new IOException(zipFile + " is not a existing file");
    if (zipFile.isDirectory()) {
        Resource[] files = zipFile.listResources(new OrResourceFilter(new ResourceFilter[] { new ExtensionResourceFilter("zip"), new ExtensionResourceFilter("jar"), new ExtensionResourceFilter("war"), new ExtensionResourceFilter("tar"), new ExtensionResourceFilter("ear") }));
        if (files == null)
            throw new IOException("directory " + zipFile + " is empty");
        extract(FORMAT_ZIP, files, targetDir);
        return;
    }
    // read the zip file and build a query from its contents
    unzip(zipFile, targetDir);
/*ZipInputStream zis=null;
        try {
	        zis = new ZipInputStream( IOUtil.toBufferedInputStream(zipFile.getInputStream()) ) ;     
	        ZipEntry entry;
	        while ( ( entry = zis.getNextEntry()) != null ) {
	        	Resource target=targetDir.getRealResource(entry.getName());
	            if(entry.isDirectory()) {
	                target.mkdirs();
	            }
	            else {
	            	Resource parent=target.getParentResource();
	                if(!parent.exists())parent.mkdirs();
	                
	                IOUtil.copy(zis,target,false);
	            }
	            target.setLastModified(entry.getTime());
	           zis.closeEntry() ;
	        }
        }
        finally {
        	IOUtil.closeEL(zis);
        }*/
}
Also used : ExtensionResourceFilter(lucee.commons.io.res.filter.ExtensionResourceFilter) OrResourceFilter(lucee.commons.io.res.filter.OrResourceFilter) ResourceFilter(lucee.commons.io.res.filter.ResourceFilter) OrResourceFilter(lucee.commons.io.res.filter.OrResourceFilter) Resource(lucee.commons.io.res.Resource) ExtensionResourceFilter(lucee.commons.io.res.filter.ExtensionResourceFilter) IOException(java.io.IOException)

Example 5 with ResourceFilter

use of lucee.commons.io.res.filter.ResourceFilter in project Lucee by lucee.

the class Admin method doCreateArchive.

private void doCreateArchive(short mappingType) throws PageException {
    String virtual = getString("admin", action, "virtual").toLowerCase();
    String strFile = getString("admin", action, "file");
    Resource file = ResourceUtil.toResourceNotExisting(pageContext, strFile);
    boolean addCFMLFiles = getBoolV("addCFMLFiles", true);
    boolean addNonCFMLFiles = getBoolV("addNonCFMLFiles", true);
    Boolean ignoreScopes = getBool("ignoreScopes", null);
    // compile
    MappingImpl mapping = (MappingImpl) doCompileMapping(mappingType, virtual, true, ignoreScopes);
    // class files
    if (mapping == null)
        throw new ApplicationException("there is no mapping for [" + virtual + "]");
    if (!mapping.hasPhysical())
        throw new ApplicationException("mapping [" + virtual + "] has no physical directory");
    Resource classRoot = mapping.getClassRootDirectory();
    Resource temp = SystemUtil.getTempDirectory().getRealResource("mani-" + IDGenerator.stringId());
    Resource mani = temp.getRealResource("META-INF/MANIFEST.MF");
    try {
        if (file.exists())
            file.delete();
        if (!file.exists())
            file.createFile(true);
        ResourceFilter filter;
        // include everything, no filter needed
        if (addCFMLFiles && addNonCFMLFiles)
            filter = null;
        else // CFML Files but no other files
        if (addCFMLFiles) {
            if (mappingType == MAPPING_CFC)
                filter = new ExtensionResourceFilter(ArrayUtil.toArray(Constants.getComponentExtensions(), "class", "MF"), true, true);
            else
                filter = new ExtensionResourceFilter(ArrayUtil.toArray(Constants.getExtensions(), "class", "MF"), true, true);
        } else // No CFML Files, but all other files
        if (addNonCFMLFiles) {
            filter = new NotResourceFilter(new ExtensionResourceFilter(Constants.getExtensions(), false, true));
        } else // no files at all
        {
            filter = new ExtensionResourceFilter(new String[] { "class", "MF" }, true, true);
        }
        String id = HashUtil.create64BitHashAsString(mapping.getStrPhysical(), Character.MAX_RADIX);
        // String id = MD5.getDigestAsString(mapping.getStrPhysical());
        String type;
        if (mappingType == MAPPING_CFC)
            type = "cfc";
        else if (mappingType == MAPPING_CT)
            type = "ct";
        else
            type = "regular";
        String token = HashUtil.create64BitHashAsString(System.currentTimeMillis() + "", Character.MAX_RADIX);
        // create manifest
        Manifest mf = new Manifest();
        // StringBuilder manifest=new StringBuilder();
        // Write OSGi specific stuff
        Attributes attrs = mf.getMainAttributes();
        attrs.putValue("Bundle-ManifestVersion", Caster.toString(BundleBuilderFactory.MANIFEST_VERSION));
        attrs.putValue("Bundle-SymbolicName", id);
        attrs.putValue("Bundle-Name", ListUtil.trim(mapping.getVirtual().replace('/', '.'), "."));
        attrs.putValue("Bundle-Description", "this is a " + type + " mapping generated by " + Constants.NAME + ".");
        attrs.putValue("Bundle-Version", "1.0.0." + token);
        // attrs.putValue("Import-Package","lucee.*");
        attrs.putValue("Require-Bundle", "lucee.core");
        // Mapping
        attrs.putValue("mapping-id", id);
        attrs.putValue("mapping-type", type);
        attrs.putValue("mapping-virtual-path", mapping.getVirtual());
        attrs.putValue("mapping-hidden", Caster.toString(mapping.isHidden()));
        attrs.putValue("mapping-physical-first", Caster.toString(mapping.isPhysicalFirst()));
        attrs.putValue("mapping-readonly", Caster.toString(mapping.isReadonly()));
        attrs.putValue("mapping-top-level", Caster.toString(mapping.isTopLevel()));
        attrs.putValue("mapping-inspect", ConfigWebUtil.inspectTemplate(mapping.getInspectTemplateRaw(), ""));
        attrs.putValue("mapping-listener-type", ConfigWebUtil.toListenerType(mapping.getListenerType(), ""));
        attrs.putValue("mapping-listener-mode", ConfigWebUtil.toListenerMode(mapping.getListenerMode(), ""));
        mani.createFile(true);
        IOUtil.write(mani, ManifestUtil.toString(mf, 100, null, null), "UTF-8", false);
        // source files
        Resource[] sources;
        if (!addCFMLFiles && !addNonCFMLFiles)
            sources = new Resource[] { temp, classRoot };
        else
            sources = new Resource[] { temp, mapping.getPhysical(), classRoot };
        CompressUtil.compressZip(ResourceUtil.listResources(sources, filter), file, filter);
        if (getBoolV("append", false)) {
            if (mappingType == MAPPING_CFC) {
                admin.updateComponentMapping(mapping.getVirtual(), mapping.getStrPhysical(), strFile, mapping.isPhysicalFirst() ? "physical" : "archive", mapping.getInspectTemplateRaw());
            } else if (mappingType == MAPPING_CT) {
                admin.updateCustomTag(mapping.getVirtual(), mapping.getStrPhysical(), strFile, mapping.isPhysicalFirst() ? "physical" : "archive", mapping.getInspectTemplateRaw());
            } else
                admin.updateMapping(mapping.getVirtual(), mapping.getStrPhysical(), strFile, mapping.isPhysicalFirst() ? "physical" : "archive", mapping.getInspectTemplateRaw(), mapping.isTopLevel(), mapping.getListenerMode(), mapping.getListenerType(), mapping.isReadonly());
            store();
        }
    } catch (IOException e) {
        throw Caster.toPageException(e);
    } finally {
        ResourceUtil.removeEL(temp, true);
    }
    adminSync.broadcast(attributes, config);
}
Also used : Resource(lucee.commons.io.res.Resource) DynamicAttributes(lucee.runtime.ext.tag.DynamicAttributes) Attributes(java.util.jar.Attributes) IOException(java.io.IOException) Manifest(java.util.jar.Manifest) MappingImpl(lucee.runtime.MappingImpl) ExtensionResourceFilter(lucee.commons.io.res.filter.ExtensionResourceFilter) OrResourceFilter(lucee.commons.io.res.filter.OrResourceFilter) DirectoryResourceFilter(lucee.commons.io.res.filter.DirectoryResourceFilter) NotResourceFilter(lucee.commons.io.res.filter.NotResourceFilter) ResourceFilter(lucee.commons.io.res.filter.ResourceFilter) ApplicationException(lucee.runtime.exp.ApplicationException) NotResourceFilter(lucee.commons.io.res.filter.NotResourceFilter) ExtensionResourceFilter(lucee.commons.io.res.filter.ExtensionResourceFilter)

Aggregations

ResourceFilter (lucee.commons.io.res.filter.ResourceFilter)7 OrResourceFilter (lucee.commons.io.res.filter.OrResourceFilter)6 Resource (lucee.commons.io.res.Resource)5 DirectoryResourceFilter (lucee.commons.io.res.filter.DirectoryResourceFilter)5 IOException (java.io.IOException)3 FileResourceFilter (lucee.commons.io.res.filter.FileResourceFilter)3 NotResourceFilter (lucee.commons.io.res.filter.NotResourceFilter)3 ApplicationException (lucee.runtime.exp.ApplicationException)3 AndResourceFilter (lucee.commons.io.res.filter.AndResourceFilter)2 ExtensionResourceFilter (lucee.commons.io.res.filter.ExtensionResourceFilter)2 FileResource (lucee.commons.io.res.type.file.FileResource)2 MappingImpl (lucee.runtime.MappingImpl)2 SecurityManager (lucee.runtime.security.SecurityManager)2 ArrayList (java.util.ArrayList)1 Attributes (java.util.jar.Attributes)1 Manifest (java.util.jar.Manifest)1 ResourceMetaData (lucee.commons.io.res.ResourceMetaData)1 WildcardPatternFilter (lucee.commons.io.res.util.WildcardPatternFilter)1 Mapping (lucee.runtime.Mapping)1 DynamicAttributes (lucee.runtime.ext.tag.DynamicAttributes)1