Search in sources :

Example 6 with Mapping

use of lucee.runtime.Mapping in project Lucee by lucee.

the class CustomTagUtil method loadInitFile.

public static InitFile loadInitFile(PageContext pc, String name, InitFile defaultValue) throws PageException {
    ConfigImpl config = (ConfigImpl) pc.getConfig();
    String[] filenames = getFileNames(config, name);
    boolean doCache = config.useCTPathCache();
    boolean doCustomTagDeepSearch = config.doCustomTagDeepSearch();
    PageSource ps = null;
    InitFile initFile;
    // CACHE
    // check local
    String localCacheName = null;
    Mapping[] actms = pc.getApplicationContext().getCustomTagMappings();
    Mapping[] cctms = config.getCustomTagMappings();
    if (doCache) {
        if (pc.getConfig().doLocalCustomTag()) {
            localCacheName = pc.getCurrentPageSource().getDisplayPath().replace('\\', '/');
            localCacheName = "local:" + localCacheName.substring(0, localCacheName.lastIndexOf('/') + 1).concat(name);
            initFile = config.getCTInitFile(pc, localCacheName);
            if (initFile != null)
                return initFile;
        }
        // cache application mapping
        if (actms != null)
            for (int i = 0; i < actms.length; i++) {
                initFile = config.getCTInitFile(pc, "application:" + actms[i].hashCode() + "/" + name);
                if (initFile != null)
                    return initFile;
            }
        // cache config mapping
        if (cctms != null)
            for (int i = 0; i < cctms.length; i++) {
                initFile = config.getCTInitFile(pc, "config:" + cctms[i].hashCode() + "/" + name);
                if (initFile != null)
                    return initFile;
            }
    }
    // search local
    if (pc.getConfig().doLocalCustomTag()) {
        for (int i = 0; i < filenames.length; i++) {
            PageSource[] arr = ((PageContextImpl) pc).getRelativePageSources(filenames[i]);
            // ps=pc.getRelativePageSource(filenames[i]);
            ps = MappingImpl.isOK(arr);
            if (ps != null) {
                initFile = new InitFile(pc, ps, filenames[i]);
                if (doCache)
                    config.putCTInitFile(localCacheName, initFile);
                return initFile;
            }
        }
    }
    // search application custom tag mapping
    if (actms != null) {
        for (int i = 0; i < filenames.length; i++) {
            ps = getMapping(actms, filenames[i], doCustomTagDeepSearch);
            if (ps != null) {
                initFile = new InitFile(pc, ps, filenames[i]);
                if (doCache)
                    config.putCTInitFile("application:" + ps.getMapping().hashCode() + "/" + name, initFile);
                return initFile;
            }
        }
    }
    // search custom tag mappings
    for (int i = 0; i < filenames.length; i++) {
        ps = getMapping(cctms, filenames[i], doCustomTagDeepSearch);
        if (ps != null) {
            initFile = new InitFile(pc, ps, filenames[i]);
            if (doCache)
                config.putCTInitFile("config:" + ps.getMapping().hashCode() + "/" + name, initFile);
            return initFile;
        }
    }
    return defaultValue;
}
Also used : Mapping(lucee.runtime.Mapping) PageContextImpl(lucee.runtime.PageContextImpl) ConfigImpl(lucee.runtime.config.ConfigImpl) PageSource(lucee.runtime.PageSource)

Example 7 with Mapping

use of lucee.runtime.Mapping in project Lucee by lucee.

the class ComponentLoader method _search.

private static Object _search(PageContext pc, PageSource loadingLocation, String rawPath, Boolean searchLocal, Boolean searchRoot, boolean executeConstr, short returnType, PageSource currPS, ImportDefintion[] importDefintions, int dialect, final boolean isExtendedComponent) throws PageException {
    ConfigImpl config = (ConfigImpl) pc.getConfig();
    if (dialect == CFMLEngine.DIALECT_LUCEE && !config.allowLuceeDialect())
        PageContextImpl.notSupported();
    boolean doCache = config.useComponentPathCache();
    String sub = null;
    if (returnType != RETURN_TYPE_PAGE && rawPath.indexOf(':') != -1) {
        int d = rawPath.indexOf(':');
        int s = rawPath.indexOf('.');
        if (d > s) {
            sub = rawPath.substring(d + 1);
            rawPath = rawPath.substring(0, d);
        }
    }
    // app-String appName=pc.getApplicationContext().getName();
    rawPath = rawPath.trim().replace('\\', '/');
    String path = (rawPath.indexOf("./") == -1) ? rawPath.replace('.', '/') : rawPath;
    boolean isRealPath = !StringUtil.startsWith(path, '/');
    // PageSource currPS = pc.getCurrentPageSource();
    // Page currP=currPS.loadPage(pc,false);
    PageSource ps = null;
    CIPage page = null;
    // MUSTMUST improve to handle different extensions
    String pathWithCFC = path.concat("." + (dialect == CFMLEngine.DIALECT_CFML ? Constants.getCFMLComponentExtension() : Constants.getLuceeComponentExtension()));
    // no cache for per application pathes
    Mapping[] acm = pc.getApplicationContext().getComponentMappings();
    if (!ArrayUtil.isEmpty(acm)) {
        Mapping m;
        for (int y = 0; y < acm.length; y++) {
            m = acm[y];
            ps = m.getPageSource(pathWithCFC);
            page = toCIPage(ps.loadPageThrowTemplateException(pc, false, (Page) null));
            if (page != null) {
                return returnType == RETURN_TYPE_PAGE ? page : load(pc, page, trim(path.replace('/', '.')), sub, isRealPath, returnType, isExtendedComponent, executeConstr);
            }
        }
    }
    if (searchLocal == null)
        searchLocal = Caster.toBoolean(rawPath.indexOf('.') == -1 ? true : config.getComponentLocalSearch());
    if (searchRoot == null)
        searchRoot = Caster.toBoolean(config.getComponentRootSearch());
    // CACHE
    // check local in cache
    String localCacheName = null;
    if (searchLocal && isRealPath && currPS != null) {
        localCacheName = currPS.getDisplayPath().replace('\\', '/');
        localCacheName = localCacheName.substring(0, localCacheName.lastIndexOf('/') + 1).concat(pathWithCFC);
        if (doCache) {
            page = config.getCachedPage(pc, localCacheName);
            if (page != null)
                return returnType == RETURN_TYPE_PAGE ? page : load(pc, page, trim(path.replace('/', '.')), sub, isRealPath, returnType, isExtendedComponent, executeConstr);
        }
    }
    // check import cache
    if (doCache && isRealPath) {
        ImportDefintion impDef = config.getComponentDefaultImport();
        ImportDefintion[] impDefs = importDefintions == null ? EMPTY_ID : importDefintions;
        int i = -1;
        do {
            if (impDef.isWildcard() || impDef.getName().equalsIgnoreCase(path)) {
                page = config.getCachedPage(pc, "import:" + impDef.getPackageAsPath() + pathWithCFC);
                if (page != null)
                    return returnType == RETURN_TYPE_PAGE ? page : load(pc, page, trim(path.replace('/', '.')), sub, isRealPath, returnType, isExtendedComponent, executeConstr);
            }
            impDef = ++i < impDefs.length ? impDefs[i] : null;
        } while (impDef != null);
    }
    if (doCache) {
        // check global in cache
        page = config.getCachedPage(pc, pathWithCFC);
        if (page != null)
            return returnType == RETURN_TYPE_PAGE ? page : load(pc, page, trim(path.replace('/', '.')), sub, isRealPath, returnType, isExtendedComponent, executeConstr);
    }
    // search from local
    if (searchLocal && isRealPath) {
        // check realpath
        PageSource[] arr = ((PageContextImpl) pc).getRelativePageSources(pathWithCFC);
        page = toCIPage(PageSourceImpl.loadPage(pc, arr, null));
        if (page != null) {
            if (doCache)
                config.putCachedPageSource(localCacheName, page.getPageSource());
            return returnType == RETURN_TYPE_PAGE ? page : load(pc, page, trim(path.replace('/', '.')), sub, isRealPath, returnType, isExtendedComponent, executeConstr);
        }
    }
    // search with imports
    Mapping[] cMappings = config.getComponentMappings();
    if (isRealPath) {
        ImportDefintion impDef = config.getComponentDefaultImport();
        ImportDefintion[] impDefs = importDefintions == null ? EMPTY_ID : importDefintions;
        PageSource[] arr;
        int i = -1;
        do {
            if (impDef.isWildcard() || impDef.getName().equalsIgnoreCase(path)) {
                // search from local first
                if (searchLocal) {
                    arr = ((PageContextImpl) pc).getRelativePageSources(impDef.getPackageAsPath() + pathWithCFC);
                    page = toCIPage(PageSourceImpl.loadPage(pc, arr, null));
                    if (page != null) {
                        if (doCache)
                            config.putCachedPageSource("import:" + impDef.getPackageAsPath() + pathWithCFC, page.getPageSource());
                        return returnType == RETURN_TYPE_PAGE ? page : load(pc, page, trim(path.replace('/', '.')), sub, isRealPath, returnType, isExtendedComponent, executeConstr);
                    }
                }
                // search mappings and webroot
                page = toCIPage(PageSourceImpl.loadPage(pc, ((PageContextImpl) pc).getPageSources("/" + impDef.getPackageAsPath() + pathWithCFC), null));
                if (page != null) {
                    String key = impDef.getPackageAsPath() + pathWithCFC;
                    if (doCache && !((MappingImpl) page.getPageSource().getMapping()).isAppMapping())
                        config.putCachedPageSource("import:" + key, page.getPageSource());
                    return returnType == RETURN_TYPE_PAGE ? page : load(pc, page, trim(path.replace('/', '.')), sub, isRealPath, returnType, isExtendedComponent, executeConstr);
                }
                // search component mappings
                Mapping m;
                for (int y = 0; y < cMappings.length; y++) {
                    m = cMappings[y];
                    ps = m.getPageSource(impDef.getPackageAsPath() + pathWithCFC);
                    page = toCIPage(ps.loadPageThrowTemplateException(pc, false, (Page) null));
                    if (page != null) {
                        if (doCache)
                            config.putCachedPageSource("import:" + impDef.getPackageAsPath() + pathWithCFC, page.getPageSource());
                        return returnType == RETURN_TYPE_PAGE ? page : load(pc, page, trim(path.replace('/', '.')), sub, isRealPath, returnType, isExtendedComponent, executeConstr);
                    }
                }
            }
            impDef = ++i < impDefs.length ? impDefs[i] : null;
        } while (impDef != null);
    }
    String p;
    if (isRealPath)
        p = '/' + pathWithCFC;
    else
        p = pathWithCFC;
    // search mappings and webroot
    page = toCIPage(PageSourceImpl.loadPage(pc, ((PageContextImpl) pc).getPageSources(p), null));
    if (page != null) {
        String key = pathWithCFC;
        if (doCache && !((MappingImpl) page.getPageSource().getMapping()).isAppMapping())
            config.putCachedPageSource(key, page.getPageSource());
        return returnType == RETURN_TYPE_PAGE ? page : load(pc, page, trim(path.replace('/', '.')), sub, isRealPath, returnType, isExtendedComponent, executeConstr);
    }
    // search component mappings
    Mapping m;
    for (int i = 0; i < cMappings.length; i++) {
        m = cMappings[i];
        ps = m.getPageSource(p);
        page = toCIPage(ps.loadPageThrowTemplateException(pc, false, (Page) null));
        // recursive search
        if (page == null && config.doComponentDeepSearch() && path.indexOf('/') == -1) {
            ps = MappingUtil.searchMappingRecursive(m, pathWithCFC, true);
            if (ps != null) {
                page = toCIPage(ps.loadPageThrowTemplateException(pc, false, (Page) null));
                // do not cache this, it could be ambigous
                if (page != null)
                    doCache = false;
            }
        }
        if (page != null) {
            if (doCache)
                config.putCachedPageSource(pathWithCFC, page.getPageSource());
            return returnType == RETURN_TYPE_PAGE ? page : load(pc, page, trim(path.replace('/', '.')), sub, isRealPath, returnType, isExtendedComponent, executeConstr);
        }
    }
    // search relative to active component (this get not cached because the cache get ambigous if we do)
    if (searchLocal && isRealPath) {
        if (loadingLocation == null) {
            Component c = pc.getActiveComponent();
            if (c != null)
                loadingLocation = c.getPageSource();
        }
        if (loadingLocation != null) {
            ps = loadingLocation.getRealPage(pathWithCFC);
            if (ps != null) {
                page = toCIPage(ps.loadPageThrowTemplateException(pc, false, (Page) null));
                if (page != null) {
                    return returnType == RETURN_TYPE_PAGE ? page : load(pc, page, trim(path.replace('/', '.')), sub, isRealPath, returnType, isExtendedComponent, executeConstr);
                }
            }
        }
    }
    // translate cfide. to org.lucee.cfml
    if (StringUtil.startsWithIgnoreCase(rawPath, "cfide.")) {
        String rpm = Constants.DEFAULT_PACKAGE + "." + rawPath.substring(6);
        try {
            return _search(pc, loadingLocation, rpm, searchLocal, searchRoot, executeConstr, returnType, currPS, importDefintions, dialect, false);
        } catch (ExpressionException ee) {
            return null;
        // throw new ExpressionException("invalid "+toStringType(returnType)+" definition, can't find "+rawPath+" or "+rpm);
        }
    }
    return null;
// throw new ExpressionException("invalid "+toStringType(returnType)+" definition, can't find "+toStringType(returnType)+" ["+rawPath+"]");
}
Also used : CIPage(lucee.runtime.CIPage) Mapping(lucee.runtime.Mapping) PageContextImpl(lucee.runtime.PageContextImpl) MappingImpl(lucee.runtime.MappingImpl) ExpressionException(lucee.runtime.exp.ExpressionException) PageSource(lucee.runtime.PageSource) Component(lucee.runtime.Component) ConfigImpl(lucee.runtime.config.ConfigImpl)

Example 8 with Mapping

use of lucee.runtime.Mapping in project Lucee by lucee.

the class ComponentListPackage method _call.

private static Set<String> _call(PageContext pc, String packageName) throws IOException, ApplicationException {
    PageContextImpl pci = (PageContextImpl) pc;
    ConfigWebImpl config = (ConfigWebImpl) pc.getConfig();
    Set<String> rtn = null;
    // var SEP=server.separator.file;
    // get enviroment configuration
    boolean searchLocal = packageName.indexOf('.') == -1 ? true : config.getComponentLocalSearch();
    boolean searchRoot = config.getComponentRootSearch();
    String path = StringUtil.replace(packageName, ".", File.separator, false);
    // search local
    if (searchLocal) {
        PageSource ps = pci.getRelativePageSourceExisting(path);
        if (ps != null) {
            Mapping mapping = ps.getMapping();
            String _path = ps.getRealpath();
            _path = ListUtil.trim(_path, "\\/");
            String[] list = _listMapping(pc, mapping, _path);
            if (!ArrayUtil.isEmpty(list))
                rtn = add(rtn, list);
        }
    }
    // check mappings (this includes the webroot)
    if (searchRoot) {
        String virtual = "/" + StringUtil.replace(packageName, ".", "/", false);
        Mapping[] mappings = config.getMappings();
        Mapping mapping;
        String _path;
        String[] list;
        for (int i = 0; i < mappings.length; i++) {
            mapping = mappings[i];
            if (StringUtil.startsWithIgnoreCase(virtual, mapping.getVirtual())) {
                _path = ListUtil.trim(virtual.substring(mapping.getVirtual().length()), "\\/").trim();
                _path = StringUtil.replace(_path, "/", File.separator, false);
                list = _listMapping(pc, mapping, _path);
                if (!ArrayUtil.isEmpty(list))
                    rtn = add(rtn, list);
            }
        }
    }
    // check component mappings
    Mapping[] mappings = config.getComponentMappings();
    Mapping mapping;
    String[] list;
    for (int i = 0; i < mappings.length; i++) {
        mapping = mappings[i];
        list = _listMapping(pc, mapping, path);
        if (!ArrayUtil.isEmpty(list))
            rtn = add(rtn, list);
    }
    if (rtn == null)
        throw new ApplicationException("no package with name [" + packageName + "] found");
    return rtn;
}
Also used : ConfigWebImpl(lucee.runtime.config.ConfigWebImpl) ApplicationException(lucee.runtime.exp.ApplicationException) Mapping(lucee.runtime.Mapping) PageContextImpl(lucee.runtime.PageContextImpl) PageSource(lucee.runtime.PageSource)

Example 9 with Mapping

use of lucee.runtime.Mapping in project Lucee by lucee.

the class ConfigImpl method toPageSource.

public PageSource toPageSource(Mapping[] mappings, Resource res, PageSource defaultValue) {
    Mapping mapping;
    String path;
    // app mappings
    if (mappings != null) {
        for (int i = 0; i < mappings.length; i++) {
            mapping = mappings[i];
            // Physical
            if (mapping.hasPhysical()) {
                path = ResourceUtil.getPathToChild(res, mapping.getPhysical());
                if (path != null) {
                    return mapping.getPageSource(path);
                }
            }
            // Archive
            if (mapping.hasArchive() && res.getResourceProvider() instanceof CompressResourceProvider) {
                Resource archive = mapping.getArchive();
                CompressResource cr = ((CompressResource) res);
                if (archive.equals(cr.getCompressResource())) {
                    return mapping.getPageSource(cr.getCompressPath());
                }
            }
        }
    }
    // config mappings
    for (int i = 0; i < this.mappings.length; i++) {
        mapping = this.mappings[i];
        // Physical
        if (mapping.hasPhysical()) {
            path = ResourceUtil.getPathToChild(res, mapping.getPhysical());
            if (path != null) {
                return mapping.getPageSource(path);
            }
        }
        // Archive
        if (mapping.hasArchive() && res.getResourceProvider() instanceof CompressResourceProvider) {
            Resource archive = mapping.getArchive();
            CompressResource cr = ((CompressResource) res);
            if (archive.equals(cr.getCompressResource())) {
                return mapping.getPageSource(cr.getCompressPath());
            }
        }
    }
    // map resource to root mapping when same filesystem
    Mapping rootMapping = this.mappings[this.mappings.length - 1];
    Resource root;
    if (rootMapping.hasPhysical() && res.getResourceProvider().getScheme().equals((root = rootMapping.getPhysical()).getResourceProvider().getScheme())) {
        String realpath = "";
        while (root != null && !ResourceUtil.isChildOf(res, root)) {
            root = root.getParentResource();
            realpath += "../";
        }
        String p2c = ResourceUtil.getPathToChild(res, root);
        if (StringUtil.startsWith(p2c, '/') || StringUtil.startsWith(p2c, '\\'))
            p2c = p2c.substring(1);
        realpath += p2c;
        return rootMapping.getPageSource(realpath);
    }
    // MUST better impl than this
    if (this instanceof ConfigWebImpl) {
        Resource parent = res.getParentResource();
        if (parent != null && !parent.equals(res)) {
            Mapping m = ((ConfigWebImpl) this).getApplicationMapping("application", "/", parent.getAbsolutePath(), null, true, false);
            return m.getPageSource(res.getName());
        }
    }
    // MUST check archive
    return defaultValue;
}
Also used : Resource(lucee.commons.io.res.Resource) CompressResource(lucee.commons.io.res.type.compress.CompressResource) CompressResource(lucee.commons.io.res.type.compress.CompressResource) Mapping(lucee.runtime.Mapping) CompressResourceProvider(lucee.commons.io.res.type.compress.CompressResourceProvider)

Example 10 with Mapping

use of lucee.runtime.Mapping in project Lucee by lucee.

the class ConfigImpl method getPageSources.

@Override
public PageSource[] getPageSources(PageContext pc, Mapping[] mappings, String realPath, boolean onlyTopLevel, boolean useSpecialMappings, boolean useDefaultMapping, boolean useComponentMappings) {
    realPath = realPath.replace('\\', '/');
    String lcRealPath = StringUtil.toLowerCase(realPath) + '/';
    Mapping mapping;
    PageSource ps;
    List<PageSource> list = new ArrayList<PageSource>();
    if (mappings != null) {
        for (int i = 0; i < mappings.length; i++) {
            mapping = mappings[i];
            // print.err(lcRealPath+".startsWith"+(mapping.getStrPhysical()));
            if (lcRealPath.startsWith(mapping.getVirtualLowerCaseWithSlash(), 0)) {
                list.add(mapping.getPageSource(realPath.substring(mapping.getVirtual().length())));
            }
        }
    }
    // / special mappings
    if (useSpecialMappings && lcRealPath.startsWith("/mapping-", 0)) {
        String virtual = "/mapping-tag";
        // tag mappings
        Mapping[] tagMappings = (this instanceof ConfigWebImpl) ? new Mapping[] { ((ConfigWebImpl) this).getServerTagMapping(), getTagMapping() } : new Mapping[] { getTagMapping() };
        if (lcRealPath.startsWith(virtual, 0)) {
            for (int i = 0; i < tagMappings.length; i++) {
                ps = tagMappings[i].getPageSource(realPath.substring(virtual.length()));
                if (ps.exists())
                    list.add(ps);
            }
        }
        // customtag mappings
        tagMappings = getCustomTagMappings();
        virtual = "/mapping-customtag";
        if (lcRealPath.startsWith(virtual, 0)) {
            for (int i = 0; i < tagMappings.length; i++) {
                ps = tagMappings[i].getPageSource(realPath.substring(virtual.length()));
                if (ps.exists())
                    list.add(ps);
            }
        }
    }
    // component mappings (only used for gateway)
    if (useComponentMappings || (pc != null && ((PageContextImpl) pc).isGatewayContext())) {
        boolean isCFC = Constants.isComponentExtension(ResourceUtil.getExtension(realPath, null));
        if (isCFC) {
            Mapping[] cmappings = getComponentMappings();
            for (int i = 0; i < cmappings.length; i++) {
                ps = cmappings[i].getPageSource(realPath);
                if (ps.exists())
                    list.add(ps);
            }
        }
    }
    // config mappings
    for (int i = 0; i < this.mappings.length - 1; i++) {
        mapping = this.mappings[i];
        if ((!onlyTopLevel || mapping.isTopLevel()) && lcRealPath.startsWith(mapping.getVirtualLowerCaseWithSlash(), 0)) {
            list.add(mapping.getPageSource(realPath.substring(mapping.getVirtual().length())));
        }
    }
    if (useDefaultMapping) {
        list.add(this.mappings[this.mappings.length - 1].getPageSource(realPath));
    }
    return list.toArray(new PageSource[list.size()]);
}
Also used : ArrayList(java.util.ArrayList) Mapping(lucee.runtime.Mapping) PageSource(lucee.runtime.PageSource)

Aggregations

Mapping (lucee.runtime.Mapping)30 MappingImpl (lucee.runtime.MappingImpl)11 PageSource (lucee.runtime.PageSource)7 ConfigWebImpl (lucee.runtime.config.ConfigWebImpl)7 ArrayList (java.util.ArrayList)6 PageContextImpl (lucee.runtime.PageContextImpl)6 Resource (lucee.commons.io.res.Resource)5 Struct (lucee.runtime.type.Struct)5 Iterator (java.util.Iterator)4 Entry (java.util.Map.Entry)4 lucee.aprint (lucee.aprint)4 Query (lucee.runtime.type.Query)4 QueryImpl (lucee.runtime.type.QueryImpl)4 Element (org.w3c.dom.Element)4 HashMap (java.util.HashMap)3 LinkedHashMap (java.util.LinkedHashMap)3 ApplicationException (lucee.runtime.exp.ApplicationException)3 ExpressionException (lucee.runtime.exp.ExpressionException)3 Key (lucee.runtime.type.Collection.Key)3 StructImpl (lucee.runtime.type.StructImpl)3