Search in sources :

Example 16 with ConfigImpl

use of lucee.runtime.config.ConfigImpl 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 17 with ConfigImpl

use of lucee.runtime.config.ConfigImpl in project Lucee by lucee.

the class FormImpl method initializeMultiPart.

private void initializeMultiPart(PageContext pc, boolean scriptProteced) {
    // get temp directory
    Resource tempDir = ((ConfigImpl) pc.getConfig()).getTempDirectory();
    Resource tempFile;
    // Create a new file upload handler
    final String encoding = getEncoding();
    FileItemFactory factory = tempDir instanceof File ? new DiskFileItemFactory(DiskFileItemFactory.DEFAULT_SIZE_THRESHOLD, (File) tempDir) : new DiskFileItemFactory();
    ServletFileUpload upload = new ServletFileUpload(factory);
    upload.setHeaderEncoding(encoding);
    // ServletRequestContext c = new ServletRequestContext(pc.getHttpServletRequest());
    HttpServletRequest req = pc.getHttpServletRequest();
    ServletRequestContext context = new ServletRequestContext(req) {

        @Override
        public String getCharacterEncoding() {
            return encoding;
        }
    };
    // Parse the request
    try {
        FileItemIterator iter = upload.getItemIterator(context);
        // byte[] value;
        InputStream is;
        ArrayList<URLItem> list = new ArrayList<URLItem>();
        String fileName;
        while (iter.hasNext()) {
            FileItemStream item = iter.next();
            is = IOUtil.toBufferedInputStream(item.openStream());
            if (item.getContentType() == null || StringUtil.isEmpty(item.getName())) {
                list.add(new URLItem(item.getFieldName(), new String(IOUtil.toBytes(is), encoding), false));
            } else {
                fileName = getFileName();
                tempFile = tempDir.getRealResource(fileName);
                _fileItems.put(fileName, new Item(tempFile, item.getContentType(), item.getName(), item.getFieldName()));
                String value = tempFile.toString();
                IOUtil.copy(is, tempFile, true);
                list.add(new URLItem(item.getFieldName(), value, false));
            }
        }
        raw = list.toArray(new URLItem[list.size()]);
        fillDecoded(raw, encoding, scriptProteced, pc.getApplicationContext().getSameFieldAsArray(SCOPE_FORM));
    } catch (Exception e) {
        SystemOut.printDate(e);
        // throw new PageRuntimeException(Caster.toPageException(e));
        fillDecodedEL(new URLItem[0], encoding, scriptProteced, pc.getApplicationContext().getSameFieldAsArray(SCOPE_FORM));
        initException = e;
    }
}
Also used : ServletInputStream(javax.servlet.ServletInputStream) InputStream(java.io.InputStream) Resource(lucee.commons.io.res.Resource) ArrayList(java.util.ArrayList) ServletRequestContext(org.apache.commons.fileupload.servlet.ServletRequestContext) DiskFileItemFactory(org.apache.commons.fileupload.disk.DiskFileItemFactory) FileItemFactory(org.apache.commons.fileupload.FileItemFactory) DiskFileItemFactory(org.apache.commons.fileupload.disk.DiskFileItemFactory) PageException(lucee.runtime.exp.PageException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) HttpServletRequest(javax.servlet.http.HttpServletRequest) DiskFileItem(org.apache.commons.fileupload.disk.DiskFileItem) URLItem(lucee.commons.net.URLItem) URLItem(lucee.commons.net.URLItem) ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) FileItemStream(org.apache.commons.fileupload.FileItemStream) File(java.io.File) FileItemIterator(org.apache.commons.fileupload.FileItemIterator) ConfigImpl(lucee.runtime.config.ConfigImpl)

Example 18 with ConfigImpl

use of lucee.runtime.config.ConfigImpl in project Lucee by lucee.

the class IKHandlerDatasource method loadData.

@Override
public IKStorageValue loadData(PageContext pc, String appName, String name, String strType, int type, Log log) throws PageException {
    ConfigImpl config = (ConfigImpl) ThreadLocalPageContext.getConfig(pc);
    DatasourceConnectionPool pool = config.getDatasourceConnectionPool();
    DatasourceConnection dc = pool.getDatasourceConnection(config, pc.getDataSource(name), null, null);
    SQLExecutor executor = SQLExecutionFactory.getInstance(dc);
    Query query;
    try {
        if (!dc.getDatasource().isStorage())
            throw new ApplicationException("storage usage for this datasource is disabled, you can enable this in the Lucee administrator.");
        query = executor.select(config, pc.getCFID(), pc.getApplicationContext().getName(), dc, type, log, true);
    } catch (SQLException se) {
        throw Caster.toPageException(se);
    } finally {
        if (dc != null)
            pool.releaseDatasourceConnection(dc);
    }
    if (query != null && config.debug()) {
        boolean debugUsage = DebuggerUtil.debugQueryUsage(pc, query);
        pc.getDebugger().addQuery(debugUsage ? query : null, name, "", query.getSql(), query.getRecordcount(), ((PageContextImpl) pc).getCurrentPageSource(null), query.getExecutionTime());
    }
    boolean _isNew = query.getRecordcount() == 0;
    if (_isNew) {
        ScopeContext.info(log, "create new " + strType + " scope for " + pc.getApplicationContext().getName() + "/" + pc.getCFID() + " in datasource [" + name + "]");
        return null;
    }
    String str = Caster.toString(query.getAt(KeyConstants._data, 1));
    if (str.startsWith("struct:"))
        return null;
    try {
        IKStorageValue data = (IKStorageValue) JavaConverter.deserialize(str);
        ScopeContext.info(log, "load existing data from [" + name + "." + PREFIX + "_" + strType + "_data] to create " + strType + " scope for " + pc.getApplicationContext().getName() + "/" + pc.getCFID());
        return data;
    } catch (Exception e) {
        ScopeContext.error(log, e);
        return null;
    // throw Caster.toPageException(e);
    }
}
Also used : ApplicationException(lucee.runtime.exp.ApplicationException) DatasourceConnectionPool(lucee.runtime.db.DatasourceConnectionPool) DatasourceConnection(lucee.runtime.db.DatasourceConnection) Query(lucee.runtime.type.Query) SQLExecutor(lucee.runtime.type.scope.storage.db.SQLExecutor) SQLException(java.sql.SQLException) ConfigImpl(lucee.runtime.config.ConfigImpl) SQLException(java.sql.SQLException) ApplicationException(lucee.runtime.exp.ApplicationException) PageException(lucee.runtime.exp.PageException)

Example 19 with ConfigImpl

use of lucee.runtime.config.ConfigImpl in project Lucee by lucee.

the class IKHandlerDatasource method store.

@Override
public void store(IKStorageScopeSupport storageScope, PageContext pc, String appName, final String name, String cfid, MapPro<Key, IKStorageScopeItem> data, Log log) {
    DatasourceConnection dc = null;
    ConfigImpl ci = (ConfigImpl) ThreadLocalPageContext.getConfig(pc);
    DatasourceConnectionPool pool = ci.getDatasourceConnectionPool();
    try {
        pc = ThreadLocalPageContext.get(pc);
        DataSource ds;
        if (pc != null)
            ds = pc.getDataSource(name);
        else
            ds = ci.getDataSource(name);
        dc = pool.getDatasourceConnection(null, ds, null, null);
        SQLExecutor executor = SQLExecutionFactory.getInstance(dc);
        IKStorageValue existingVal = loadData(pc, appName, name, storageScope.getTypeAsString(), storageScope.getType(), log);
        IKStorageValue sv = new IKStorageValue(IKStorageScopeSupport.prepareToStore(data, existingVal, storageScope.lastModified()));
        executor.update(ci, cfid, appName, dc, storageScope.getType(), sv, storageScope.getTimeSpan(), log);
    } catch (Throwable t) {
        ExceptionUtil.rethrowIfNecessary(t);
        ScopeContext.error(log, t);
    } finally {
        if (dc != null)
            pool.releaseDatasourceConnection(dc);
    }
}
Also used : DatasourceConnection(lucee.runtime.db.DatasourceConnection) DatasourceConnectionPool(lucee.runtime.db.DatasourceConnectionPool) SQLExecutor(lucee.runtime.type.scope.storage.db.SQLExecutor) ConfigImpl(lucee.runtime.config.ConfigImpl) DataSource(lucee.runtime.db.DataSource)

Example 20 with ConfigImpl

use of lucee.runtime.config.ConfigImpl in project Lucee by lucee.

the class IKHandlerDatasource method unstore.

@Override
public void unstore(IKStorageScopeSupport storageScope, PageContext pc, String appName, String name, String cfid, Log log) {
    ConfigImpl ci = (ConfigImpl) ThreadLocalPageContext.getConfig(pc);
    DatasourceConnection dc = null;
    DatasourceConnectionPool pool = ci.getDatasourceConnectionPool();
    try {
        // FUTURE change method interface
        pc = ThreadLocalPageContext.get(pc);
        DataSource ds;
        if (pc != null)
            ds = pc.getDataSource(name);
        else
            ds = ci.getDataSource(name);
        dc = pool.getDatasourceConnection(null, ds, null, null);
        SQLExecutor executor = SQLExecutionFactory.getInstance(dc);
        executor.delete(ci, cfid, appName, dc, storageScope.getType(), log);
    } catch (Throwable t) {
        ExceptionUtil.rethrowIfNecessary(t);
        ScopeContext.error(log, t);
    } finally {
        if (dc != null)
            pool.releaseDatasourceConnection(dc);
    }
}
Also used : DatasourceConnection(lucee.runtime.db.DatasourceConnection) DatasourceConnectionPool(lucee.runtime.db.DatasourceConnectionPool) SQLExecutor(lucee.runtime.type.scope.storage.db.SQLExecutor) ConfigImpl(lucee.runtime.config.ConfigImpl) DataSource(lucee.runtime.db.DataSource)

Aggregations

ConfigImpl (lucee.runtime.config.ConfigImpl)47 PageException (lucee.runtime.exp.PageException)15 Log (lucee.commons.io.log.Log)14 DatasourceConnection (lucee.runtime.db.DatasourceConnection)10 ApplicationException (lucee.runtime.exp.ApplicationException)8 Struct (lucee.runtime.type.Struct)8 StructImpl (lucee.runtime.type.StructImpl)8 DataSource (lucee.runtime.db.DataSource)7 DatasourceConnectionPool (lucee.runtime.db.DatasourceConnectionPool)7 SQLExecutor (lucee.runtime.type.scope.storage.db.SQLExecutor)7 SQLException (java.sql.SQLException)6 Resource (lucee.commons.io.res.Resource)6 PageContextImpl (lucee.runtime.PageContextImpl)6 PageSource (lucee.runtime.PageSource)6 QueryImpl (lucee.runtime.type.QueryImpl)5 ArrayList (java.util.ArrayList)4 ConfigWebImpl (lucee.runtime.config.ConfigWebImpl)4 DatabaseException (lucee.runtime.exp.DatabaseException)4 Key (lucee.runtime.type.Collection.Key)4 IOException (java.io.IOException)3