Search in sources :

Example 1 with CacheHandlerPro

use of lucee.runtime.cache.tag.CacheHandlerPro in project Lucee by lucee.

the class Axis1Client method _callCachedWithin.

private Object _callCachedWithin(PageContext pc, Config secondChanceConfig, String methodName, Struct namedArguments, Object[] arguments) throws PageException, RemoteException, ServiceException {
    // no pc no cache!
    if (pc == null)
        return _call(pc, secondChanceConfig, methodName, namedArguments, arguments);
    Object cachedWithin = getCachedWithin(pc);
    String cacheId = CacheHandlerCollectionImpl.createId(wsdlUrl, username, password, proxyData, methodName, arguments, namedArguments);
    CacheHandler cacheHandler = pc.getConfig().getCacheHandlerCollection(Config.CACHE_TYPE_WEBSERVICE, null).getInstanceMatchingObject(cachedWithin, null);
    if (cacheHandler instanceof CacheHandlerPro) {
        CacheItem cacheItem = ((CacheHandlerPro) cacheHandler).get(pc, cacheId, cachedWithin);
        if (cacheItem instanceof WebserviceCacheItem) {
            WebserviceCacheItem entry = (WebserviceCacheItem) cacheItem;
            return entry.getData();
        }
    } else if (cacheHandler != null) {
        // TODO this else block can be removed when all cache handlers implement CacheHandlerPro
        CacheItem cacheItem = cacheHandler.get(pc, cacheId);
        if (cacheItem instanceof WebserviceCacheItem) {
            WebserviceCacheItem entry = (WebserviceCacheItem) cacheItem;
            return entry.getData();
        }
    }
    // cached item not found, process and cache result if needed
    long start = System.nanoTime();
    Object rtn = _call(pc, secondChanceConfig, methodName, namedArguments, arguments);
    if (cacheHandler != null)
        cacheHandler.set(pc, cacheId, cachedWithin, new WebserviceCacheItem(rtn, wsdlUrl, methodName, System.nanoTime() - start));
    return rtn;
}
Also used : WebserviceCacheItem(lucee.runtime.cache.tag.webservice.WebserviceCacheItem) CacheHandlerPro(lucee.runtime.cache.tag.CacheHandlerPro) WebserviceCacheItem(lucee.runtime.cache.tag.webservice.WebserviceCacheItem) CacheItem(lucee.runtime.cache.tag.CacheItem) CacheHandler(lucee.runtime.cache.tag.CacheHandler)

Example 2 with CacheHandlerPro

use of lucee.runtime.cache.tag.CacheHandlerPro in project Lucee by lucee.

the class Query method doEndTag.

@Override
public int doEndTag() throws PageException {
    if (hasChangedPSQ)
        pageContext.setPsq(orgPSQ);
    String strSQL = bodyContent.getString().trim();
    if (strSQL.isEmpty())
        throw new DatabaseException("no sql string defined, inside query tag", null, null, null);
    try {
        // cannot use attribute params and queryparam tag
        if (!items.isEmpty() && params != null)
            throw new DatabaseException("you cannot use the attribute params and sub tags queryparam at the same time", null, null, null);
        // create SQL
        SQL sql;
        if (params != null) {
            if (params instanceof Argument)
                sql = QueryParamConverter.convert(strSQL, (Argument) params);
            else if (Decision.isArray(params))
                sql = QueryParamConverter.convert(strSQL, Caster.toArray(params));
            else if (Decision.isStruct(params))
                sql = QueryParamConverter.convert(strSQL, Caster.toStruct(params));
            else
                throw new DatabaseException("value of the attribute [params] has to be a struct or a array", null, null, null);
        } else {
            sql = items.isEmpty() ? new SQLImpl(strSQL) : new SQLImpl(strSQL, items.toArray(new SQLItem[items.size()]));
        }
        // lucee.runtime.type.Query query=null;
        QueryResult queryResult = null;
        String cacheHandlerId = null;
        String cacheId = null;
        long exe = 0;
        boolean useCache = (cachedWithin != null) || (cachedAfter != null);
        CacheHandler cacheHandler = null;
        if (useCache) {
            cacheId = CacheHandlerCollectionImpl.createId(sql, datasource != null ? datasource.getName() : null, username, password, returntype);
            CacheHandlerCollectionImpl coll = (CacheHandlerCollectionImpl) pageContext.getConfig().getCacheHandlerCollection(Config.CACHE_TYPE_QUERY, null);
            cacheHandler = coll.getInstanceMatchingObject(cachedWithin, null);
            if (cacheHandler == null && cachedAfter != null)
                cacheHandler = coll.getTimespanInstance(null);
            if (cacheHandler != null) {
                // cacheHandlerId specifies to queryResult the cacheType and therefore whether the query is cached or
                cacheHandlerId = cacheHandler.id();
                if (cacheHandler instanceof CacheHandlerPro) {
                    CacheItem cacheItem = ((CacheHandlerPro) cacheHandler).get(pageContext, cacheId, (cachedWithin != null) ? cachedWithin : cachedAfter);
                    if (cacheItem instanceof QueryResultCacheItem)
                        queryResult = ((QueryResultCacheItem) cacheItem).getQueryResult();
                } else {
                    // FUTURE this else block can be removed when all cache handlers implement CacheHandlerPro
                    CacheItem cacheItem = cacheHandler.get(pageContext, cacheId);
                    if (cacheItem instanceof QueryResultCacheItem) {
                        QueryResultCacheItem queryCachedItem = (QueryResultCacheItem) cacheItem;
                        Date cacheLimit = cachedAfter;
                        if (cacheLimit == null || queryCachedItem.isCachedAfter(cacheLimit))
                            queryResult = queryCachedItem.getQueryResult();
                    }
                }
            } else {
                List<String> patterns = pageContext.getConfig().getCacheHandlerCollection(Config.CACHE_TYPE_QUERY, null).getPatterns();
                throw new ApplicationException("cachedwithin value [" + cachedWithin + "] is invalid, valid values are for example [" + ListUtil.listToList(patterns, ", ") + "]");
            }
        // query=pageContext.getQueryCache().getQuery(pageContext,sql,datasource!=null?datasource.getName():null,username,password,cachedafter);
        }
        // cache not found, process and cache result if needed
        if (queryResult == null) {
            // QoQ
            if ("query".equals(dbtype)) {
                lucee.runtime.type.Query q = executeQoQ(sql);
                if (returntype == RETURN_TYPE_ARRAY)
                    // TODO this should be done in queryExecute itself so we not have to convert afterwards
                    queryResult = QueryArray.toQueryArray(q);
                else if (returntype == RETURN_TYPE_STRUCT) {
                    if (columnName == null)
                        throw new ApplicationException("attribute columnKey is required when return type is set to struct");
                    // TODO this should be done in queryExecute itself so we not have to convert
                    queryResult = QueryStruct.toQueryStruct(q, columnName);
                // afterwards
                } else
                    queryResult = (QueryResult) q;
            } else // ORM and Datasource
            {
                long start = System.nanoTime();
                Object obj;
                if ("orm".equals(dbtype) || "hql".equals(dbtype))
                    obj = executeORM(sql, returntype, ormoptions);
                else
                    obj = executeDatasoure(sql, result != null, pageContext.getTimeZone());
                if (obj instanceof QueryResult) {
                    queryResult = (QueryResult) obj;
                } else {
                    if (setReturnVariable) {
                        rtn = obj;
                    } else if (!StringUtil.isEmpty(name)) {
                        pageContext.setVariable(name, obj);
                    }
                    if (result != null) {
                        Struct sct = new StructImpl();
                        sct.setEL(KeyConstants._cached, Boolean.FALSE);
                        long time = System.nanoTime() - start;
                        sct.setEL(KeyConstants._executionTime, Caster.toDouble(time / 1000000));
                        sct.setEL(KeyConstants._executionTimeNano, Caster.toDouble(time));
                        sct.setEL(KeyConstants._SQL, sql.getSQLString());
                        if (Decision.isArray(obj)) {
                        } else
                            sct.setEL(KeyConstants._RECORDCOUNT, Caster.toDouble(1));
                        pageContext.setVariable(result, sct);
                    } else
                        setExecutionTime((System.nanoTime() - start) / 1000000);
                    return EVAL_PAGE;
                }
            }
            if (cachedWithin != null) {
                CacheItem cacheItem = QueryResultCacheItem.newInstance(queryResult, tags, datasource, null);
                if (cacheItem != null)
                    cacheHandler.set(pageContext, cacheId, cachedWithin, cacheItem);
            }
            exe = queryResult.getExecutionTime();
        } else {
            queryResult.setCacheType(cacheHandlerId);
        }
        if (pageContext.getConfig().debug() && debug) {
            boolean logdb = ((ConfigImpl) pageContext.getConfig()).hasDebugOptions(ConfigImpl.DEBUG_DATABASE);
            if (logdb) {
                boolean debugUsage = DebuggerImpl.debugQueryUsage(pageContext, queryResult);
                DebuggerImpl di = (DebuggerImpl) pageContext.getDebugger();
                di.addQuery(debugUsage ? queryResult : null, datasource != null ? datasource.getName() : null, name, sql, queryResult.getRecordcount(), getPageSource(), exe);
            }
        }
        if (setReturnVariable) {
            rtn = queryResult;
        } else if ((queryResult.getColumncount() + queryResult.getRecordcount()) > 0 && !StringUtil.isEmpty(name)) {
            pageContext.setVariable(name, queryResult);
        }
        // Result
        if (result != null) {
            Struct sct = new StructImpl();
            sct.setEL(KeyConstants._cached, Caster.toBoolean(queryResult.isCached()));
            if ((queryResult.getColumncount() + queryResult.getRecordcount()) > 0) {
                String list = ListUtil.arrayToList(queryResult instanceof lucee.runtime.type.Query ? ((lucee.runtime.type.Query) queryResult).getColumnNamesAsString() : CollectionUtil.toString(queryResult.getColumnNames(), false), ",");
                sct.setEL(KeyConstants._COLUMNLIST, list);
            }
            int rc = queryResult.getRecordcount();
            if (rc == 0)
                rc = queryResult.getUpdateCount();
            sct.setEL(KeyConstants._RECORDCOUNT, Caster.toDouble(rc));
            sct.setEL(KeyConstants._executionTime, Caster.toDouble(queryResult.getExecutionTime() / 1000000));
            sct.setEL(KeyConstants._executionTimeNano, Caster.toDouble(queryResult.getExecutionTime()));
            sct.setEL(KeyConstants._SQL, sql.getSQLString());
            // GENERATED KEYS
            lucee.runtime.type.Query qi = Caster.toQuery(queryResult, null);
            if (qi != null) {
                lucee.runtime.type.Query qryKeys = qi.getGeneratedKeys();
                if (qryKeys != null) {
                    StringBuilder generatedKey = new StringBuilder(), sb;
                    Collection.Key[] columnNames = qryKeys.getColumnNames();
                    QueryColumn column;
                    for (int c = 0; c < columnNames.length; c++) {
                        column = qryKeys.getColumn(columnNames[c]);
                        sb = new StringBuilder();
                        int size = column.size();
                        for (int row = 1; row <= size; row++) {
                            if (row > 1)
                                sb.append(',');
                            sb.append(Caster.toString(column.get(row, null)));
                        }
                        if (sb.length() > 0) {
                            sct.setEL(columnNames[c], sb.toString());
                            if (generatedKey.length() > 0)
                                generatedKey.append(',');
                            generatedKey.append(sb);
                        }
                    }
                    if (generatedKey.length() > 0)
                        sct.setEL(GENERATEDKEY, generatedKey.toString());
                }
            }
            // sqlparameters
            SQLItem[] params = sql.getItems();
            if (params != null && params.length > 0) {
                Array arr = new ArrayImpl();
                sct.setEL(SQL_PARAMETERS, arr);
                for (int i = 0; i < params.length; i++) {
                    arr.append(params[i].getValue());
                }
            }
            pageContext.setVariable(result, sct);
        } else // cfquery.executiontime
        {
            setExecutionTime(exe / 1000000);
        }
        // listener
        ((ConfigWebImpl) pageContext.getConfig()).getActionMonitorCollector().log(pageContext, "query", "Query", exe, queryResult);
        // log
        Log log = pageContext.getConfig().getLog("datasource");
        if (log.getLogLevel() >= Log.LEVEL_INFO) {
            log.info("query tag", "executed [" + sql.toString().trim() + "] in " + DecimalFormat.call(pageContext, exe / 1000000D) + " ms");
        }
    } catch (PageException pe) {
        // log
        pageContext.getConfig().getLog("datasource").error("query tag", pe);
        throw pe;
    } finally {
        ((PageContextImpl) pageContext).setTimestampWithTSOffset(previousLiteralTimestampWithTSOffset);
        if (tmpTZ != null) {
            pageContext.setTimeZone(tmpTZ);
        }
    }
    return EVAL_PAGE;
}
Also used : SQLImpl(lucee.runtime.db.SQLImpl) Argument(lucee.runtime.type.scope.Argument) SimpleQuery(lucee.runtime.type.query.SimpleQuery) ArrayImpl(lucee.runtime.type.ArrayImpl) DebuggerImpl(lucee.runtime.debug.DebuggerImpl) SQLItem(lucee.runtime.db.SQLItem) QueryStruct(lucee.runtime.type.query.QueryStruct) Struct(lucee.runtime.type.Struct) QueryResult(lucee.runtime.type.query.QueryResult) CacheHandlerPro(lucee.runtime.cache.tag.CacheHandlerPro) PageException(lucee.runtime.exp.PageException) Log(lucee.commons.io.log.Log) PageContextImpl(lucee.runtime.PageContextImpl) QueryResultCacheItem(lucee.runtime.cache.tag.query.QueryResultCacheItem) Date(java.util.Date) SQL(lucee.runtime.db.SQL) Array(lucee.runtime.type.Array) QueryArray(lucee.runtime.type.query.QueryArray) ApplicationException(lucee.runtime.exp.ApplicationException) StructImpl(lucee.runtime.type.StructImpl) QueryColumn(lucee.runtime.type.QueryColumn) CacheHandlerCollectionImpl(lucee.runtime.cache.tag.CacheHandlerCollectionImpl) QueryResultCacheItem(lucee.runtime.cache.tag.query.QueryResultCacheItem) CacheItem(lucee.runtime.cache.tag.CacheItem) CacheHandler(lucee.runtime.cache.tag.CacheHandler) DatabaseException(lucee.runtime.exp.DatabaseException) ConfigImpl(lucee.runtime.config.ConfigImpl) Key(lucee.runtime.type.Collection.Key)

Example 3 with CacheHandlerPro

use of lucee.runtime.cache.tag.CacheHandlerPro in project Lucee by lucee.

the class StoredProc method doEndTag.

@Override
public int doEndTag() throws PageException {
    long startNS = System.nanoTime();
    Object ds = datasource;
    if (StringUtil.isEmpty(datasource)) {
        ds = pageContext.getApplicationContext().getDefDataSource();
        if (StringUtil.isEmpty(ds)) {
            boolean isCFML = pageContext.getRequestDialect() == CFMLEngine.DIALECT_CFML;
            throw new ApplicationException("attribute [datasource] is required, when no default datasource is defined", "you can define a default datasource as attribute [defaultdatasource] of the tag " + (isCFML ? Constants.CFML_APPLICATION_TAG_NAME : Constants.LUCEE_APPLICATION_TAG_NAME) + " or as data member of the " + (isCFML ? Constants.CFML_APPLICATION_EVENT_HANDLER : Constants.LUCEE_APPLICATION_EVENT_HANDLER) + " (this.defaultdatasource=\"mydatasource\";)");
        }
    }
    Struct res = new StructImpl();
    DataSourceManager manager = pageContext.getDataSourceManager();
    DatasourceConnection dc = ds instanceof DataSource ? manager.getConnection(pageContext, (DataSource) ds, username, password) : manager.getConnection(pageContext, Caster.toString(ds), username, password);
    // create returnValue
    returnValue(dc);
    String sql = createSQL();
    // add returnValue to params
    if (returnValue != null) {
        params.add(0, returnValue);
    }
    Log log = pageContext.getConfig().getLog("datasource");
    SQLImpl _sql = new SQLImpl(sql);
    CallableStatement callStat = null;
    try {
        if (// log entry added to troubleshoot LDEV-1147
        log.getLogLevel() >= Log.LEVEL_DEBUG)
            log.debug("LDEV1147", sql + " [" + params.size() + " params]");
        callStat = dc.getConnection().prepareCall(sql);
        if (blockfactor > 0)
            callStat.setFetchSize(blockfactor);
        if (timeout > 0)
            DataSourceUtil.setQueryTimeoutSilent(callStat, timeout);
        // set IN register OUT
        Iterator<ProcParamBean> it = params.iterator();
        ProcParamBean param;
        int index = 1;
        while (it.hasNext()) {
            param = it.next();
            param.setIndex(index);
            _sql.addItems(new SQLItemImpl(param.getValue()));
            if (param.getDirection() != ProcParamBean.DIRECTION_OUT) {
                SQLCaster.setValue(pageContext, pageContext.getTimeZone(), callStat, index, param);
            }
            if (param.getDirection() != ProcParamBean.DIRECTION_IN) {
                registerOutParameter(callStat, param);
            }
            index++;
        }
        String dsn = (ds instanceof DataSource) ? ((DataSource) ds).getName() : Caster.toString(ds);
        // cache
        boolean isFromCache = false;
        Object cacheValue = null;
        boolean useCache = (cachedWithin != null) || (cachedafter != null);
        String cacheId = null;
        CacheHandler cacheHandler = null;
        if (useCache) {
            cacheId = CacheHandlerCollectionImpl.createId(_sql, dsn, username, password, Query.RETURN_TYPE_STORED_PROC);
            cacheHandler = pageContext.getConfig().getCacheHandlerCollection(Config.CACHE_TYPE_QUERY, null).getInstanceMatchingObject(cachedWithin, null);
            if (cacheHandler instanceof CacheHandlerPro) {
                CacheItem cacheItem = ((CacheHandlerPro) cacheHandler).get(pageContext, cacheId, cachedWithin);
                if (cacheItem != null)
                    cacheValue = ((StoredProcCacheItem) cacheItem).getStruct();
            } else if (cacheHandler != null) {
                // TODO this else block can be removed when all cache handlers implement CacheHandlerPro
                CacheItem cacheItem = cacheHandler.get(pageContext, cacheId);
                if (cacheItem != null)
                    cacheValue = ((StoredProcCacheItem) cacheItem).getStruct();
            // cacheValue = pageContext.getQueryCache().get(pageContext,_sql,dsn,username,password,cachedafter);
            }
        }
        int count = 0;
        long start = System.currentTimeMillis();
        if (cacheValue == null) {
            // execute
            boolean isResult = callStat.execute();
            Struct cacheStruct = useCache ? new StructImpl() : null;
            // resultsets
            ProcResultBean result;
            index = 1;
            do {
                if (isResult) {
                    ResultSet rs = callStat.getResultSet();
                    if (rs != null) {
                        try {
                            result = (ProcResultBean) results.get(index++, null);
                            if (result != null) {
                                lucee.runtime.type.Query q = new QueryImpl(rs, result.getMaxrows(), result.getName(), pageContext.getTimeZone());
                                count += q.getRecordcount();
                                setVariable(result.getName(), q);
                                if (useCache)
                                    cacheStruct.set(KeyImpl.getInstance(result.getName()), q);
                            }
                        } finally {
                            IOUtil.closeEL(rs);
                        }
                    }
                }
            } while ((isResult = callStat.getMoreResults()) || (callStat.getUpdateCount() != -1));
            // params
            it = params.iterator();
            while (it.hasNext()) {
                param = it.next();
                if (param.getDirection() != ProcParamBean.DIRECTION_IN) {
                    Object value = null;
                    if (!StringUtil.isEmpty(param.getVariable())) {
                        try {
                            value = SQLCaster.toCFType(callStat.getObject(param.getIndex()));
                        } catch (Throwable t) {
                            ExceptionUtil.rethrowIfNecessary(t);
                        }
                        value = emptyIfNull(value);
                        if (param == STATUS_CODE)
                            res.set(STATUSCODE, value);
                        else
                            setVariable(param.getVariable(), value);
                        if (useCache)
                            cacheStruct.set(KeyImpl.getInstance(param.getVariable()), value);
                    }
                }
            }
            if (cacheHandler != null) {
                cacheStruct.set(COUNT, Caster.toDouble(count));
                cacheHandler.set(pageContext, cacheId, cachedWithin, new StoredProcCacheItem(cacheStruct, procedure, System.currentTimeMillis() - start));
            // pageContext.getQueryCache().set(pageContext,_sql,dsn,username,password,cache,cachedbefore);
            }
        } else if (cacheValue instanceof Struct) {
            Struct sctCache = (Struct) cacheValue;
            count = Caster.toIntValue(sctCache.removeEL(COUNT), 0);
            Iterator<Entry<Key, Object>> cit = sctCache.entryIterator();
            Entry<Key, Object> ce;
            while (cit.hasNext()) {
                ce = cit.next();
                if (STATUS_CODE.getVariable().equals(ce.getKey().getString()))
                    res.set(KEY_SC, ce.getValue());
                else
                    setVariable(ce.getKey().getString(), ce.getValue());
            }
            isFromCache = true;
        }
        // result
        long exe;
        setVariable(this.result, res);
        res.set(KeyConstants._executionTime, Caster.toDouble(exe = (System.nanoTime() - startNS)));
        res.set(KeyConstants._cached, Caster.toBoolean(isFromCache));
        if (pageContext.getConfig().debug() && debug) {
            boolean logdb = ((ConfigImpl) pageContext.getConfig()).hasDebugOptions(ConfigImpl.DEBUG_DATABASE);
            if (logdb)
                pageContext.getDebugger().addQuery(null, dsn, procedure, _sql, count, pageContext.getCurrentPageSource(), (int) exe);
        }
        // log
        if (log.getLogLevel() >= Log.LEVEL_INFO) {
            log.info(StoredProc.class.getSimpleName(), "executed [" + sql.trim() + "] in " + DecimalFormat.call(pageContext, exe / 1000000D) + " ms");
        }
    } catch (SQLException e) {
        // log
        log.error(StoredProc.class.getSimpleName(), e);
        throw new DatabaseException(e, new SQLImpl(sql), dc);
    } catch (PageException pe) {
        // log
        log.error(StoredProc.class.getSimpleName(), pe);
        throw pe;
    } finally {
        if (callStat != null) {
            try {
                callStat.close();
            } catch (SQLException e) {
            }
        }
        manager.releaseConnection(pageContext, dc);
    }
    return EVAL_PAGE;
}
Also used : SQLImpl(lucee.runtime.db.SQLImpl) DatasourceConnection(lucee.runtime.db.DatasourceConnection) SQLException(java.sql.SQLException) DataSourceManager(lucee.runtime.db.DataSourceManager) Struct(lucee.runtime.type.Struct) StoredProcCacheItem(lucee.runtime.cache.tag.query.StoredProcCacheItem) QueryImpl(lucee.runtime.type.QueryImpl) Entry(java.util.Map.Entry) CallableStatement(java.sql.CallableStatement) CacheHandlerPro(lucee.runtime.cache.tag.CacheHandlerPro) ResultSet(java.sql.ResultSet) Iterator(java.util.Iterator) SQLItemImpl(lucee.runtime.db.SQLItemImpl) PageException(lucee.runtime.exp.PageException) Log(lucee.commons.io.log.Log) DataSource(lucee.runtime.db.DataSource) ApplicationException(lucee.runtime.exp.ApplicationException) StructImpl(lucee.runtime.type.StructImpl) CacheItem(lucee.runtime.cache.tag.CacheItem) StoredProcCacheItem(lucee.runtime.cache.tag.query.StoredProcCacheItem) CacheHandler(lucee.runtime.cache.tag.CacheHandler) DatabaseException(lucee.runtime.exp.DatabaseException) Key(lucee.runtime.type.Collection.Key) ConfigImpl(lucee.runtime.config.ConfigImpl)

Example 4 with CacheHandlerPro

use of lucee.runtime.cache.tag.CacheHandlerPro in project Lucee by lucee.

the class FileTag method actionRead.

/**
 * read source file
 * @throws PageException
 */
private void actionRead(boolean isBinary) throws PageException {
    if (variable == null)
        throw new ApplicationException("attribute variable is not defined for tag file");
    // check if we can use cache
    if (StringUtil.isEmpty(cachedWithin)) {
        Object tmp = ((PageContextImpl) pageContext).getCachedWithin(ConfigWeb.CACHEDWITHIN_FILE);
        if (tmp != null)
            setCachedwithin(tmp);
    }
    String cacheId = createCacheId(isBinary);
    CacheHandler cacheHandler = null;
    if (cachedWithin != null) {
        cacheHandler = pageContext.getConfig().getCacheHandlerCollection(Config.CACHE_TYPE_FILE, null).getInstanceMatchingObject(cachedWithin, null);
        if (cacheHandler instanceof CacheHandlerPro) {
            CacheItem cacheItem = ((CacheHandlerPro) cacheHandler).get(pageContext, cacheId, cachedWithin);
            if (cacheItem instanceof FileCacheItem) {
                pageContext.setVariable(variable, ((FileCacheItem) cacheItem).getData());
                return;
            }
        } else if (cacheHandler != null) {
            // TODO this else block can be removed when all cache handlers implement CacheHandlerPro
            CacheItem cacheItem = cacheHandler.get(pageContext, cacheId);
            if (cacheItem instanceof FileCacheItem) {
                pageContext.setVariable(variable, ((FileCacheItem) cacheItem).getData());
                return;
            }
        }
    }
    // cache not found, process and cache result if needed
    checkFile(pageContext, securityManager, file, serverPassword, false, false, true, false);
    try {
        long start = System.nanoTime();
        Object data = isBinary ? IOUtil.toBytes(file) : IOUtil.toString(file, CharsetUtil.toCharset(charset));
        pageContext.setVariable(variable, data);
        if (cacheHandler != null)
            cacheHandler.set(pageContext, cacheId, cachedWithin, FileCacheItem.getInstance(file.getAbsolutePath(), data, System.nanoTime() - start));
    } catch (IOException e) {
        throw new ApplicationException("can't read file [" + file.toString() + "]", e.getMessage());
    }
}
Also used : ApplicationException(lucee.runtime.exp.ApplicationException) CacheHandlerPro(lucee.runtime.cache.tag.CacheHandlerPro) FileCacheItem(lucee.runtime.cache.tag.file.FileCacheItem) PageContextImpl(lucee.runtime.PageContextImpl) FileCacheItem(lucee.runtime.cache.tag.file.FileCacheItem) CacheItem(lucee.runtime.cache.tag.CacheItem) IOException(java.io.IOException) CacheHandler(lucee.runtime.cache.tag.CacheHandler)

Example 5 with CacheHandlerPro

use of lucee.runtime.cache.tag.CacheHandlerPro in project Lucee by lucee.

the class HttpGetWithBody method _doEndTag.

private void _doEndTag() throws PageException, IOException {
    long start = System.nanoTime();
    HttpClientBuilder builder = HTTPEngine4Impl.getHttpClientBuilder();
    ssl(builder);
    // redirect
    if (redirect)
        builder.setRedirectStrategy(new DefaultRedirectStrategy());
    else
        builder.disableRedirectHandling();
    // cookies
    BasicCookieStore cookieStore = new BasicCookieStore();
    builder.setDefaultCookieStore(cookieStore);
    ConfigWeb cw = pageContext.getConfig();
    HttpRequestBase req = null;
    HttpContext httpContext = null;
    CacheHandler cacheHandler = null;
    String cacheId = null;
    // HttpRequestBase req = init(pageContext.getConfig(),this,client,params,url,port);
    {
        if (StringUtil.isEmpty(charset, true))
            charset = ((PageContextImpl) pageContext).getWebCharset().name();
        else
            charset = charset.trim();
        // check if has fileUploads
        boolean doUploadFile = false;
        for (int i = 0; i < this.params.size(); i++) {
            if ((this.params.get(i)).getType() == HttpParamBean.TYPE_FILE) {
                doUploadFile = true;
                break;
            }
        }
        // parse url (also query string)
        int len = this.params.size();
        StringBuilder sbQS = new StringBuilder();
        for (int i = 0; i < len; i++) {
            HttpParamBean param = this.params.get(i);
            int type = param.getType();
            // URL
            if (type == HttpParamBean.TYPE_URL) {
                if (sbQS.length() > 0)
                    sbQS.append('&');
                sbQS.append(param.getEncoded() ? urlenc(param.getName(), charset) : param.getName());
                sbQS.append('=');
                sbQS.append(param.getEncoded() ? urlenc(param.getValueAsString(), charset) : param.getValueAsString());
            }
        }
        String host = null;
        HttpHost httpHost;
        try {
            URL _url = HTTPUtil.toURL(url, port, encoded);
            httpHost = new HttpHost(_url.getHost(), _url.getPort());
            host = _url.getHost();
            url = _url.toExternalForm();
            if (sbQS.length() > 0) {
                // no existing QS
                if (StringUtil.isEmpty(_url.getQuery())) {
                    url += "?" + sbQS;
                } else {
                    url += "&" + sbQS;
                }
            }
        } catch (MalformedURLException mue) {
            throw Caster.toPageException(mue);
        }
        // cache
        if (cachedWithin != null) {
            cacheId = createCacheId();
            cacheHandler = pageContext.getConfig().getCacheHandlerCollection(Config.CACHE_TYPE_HTTP, null).getInstanceMatchingObject(cachedWithin, null);
            if (cacheHandler instanceof CacheHandlerPro) {
                CacheItem cacheItem = ((CacheHandlerPro) cacheHandler).get(pageContext, cacheId, cachedWithin);
                if (cacheItem instanceof HTTPCacheItem) {
                    pageContext.setVariable(result, ((HTTPCacheItem) cacheItem).getData());
                    return;
                }
            } else if (cacheHandler != null) {
                // TODO this else block can be removed when all cache handlers implement CacheHandlerPro
                CacheItem cacheItem = cacheHandler.get(pageContext, cacheId);
                if (cacheItem instanceof HTTPCacheItem) {
                    pageContext.setVariable(result, ((HTTPCacheItem) cacheItem).getData());
                    return;
                }
            }
        }
        // cache not found, process and cache result if needed
        // select best matching method (get,post, post multpart (file))
        boolean isBinary = false;
        boolean doMultiPart = doUploadFile || this.multiPart;
        HttpEntityEnclosingRequest eeReqPost = null;
        HttpEntityEnclosingRequest eeReq = null;
        if (this.method == METHOD_GET) {
            req = new HttpGetWithBody(url);
            eeReq = (HttpEntityEnclosingRequest) req;
        } else if (this.method == METHOD_HEAD) {
            req = new HttpHead(url);
        } else if (this.method == METHOD_DELETE) {
            isBinary = true;
            req = new HttpDeleteWithBody(url);
            eeReq = (HttpEntityEnclosingRequest) req;
        } else if (this.method == METHOD_PUT) {
            isBinary = true;
            HttpPut put = new HttpPut(url);
            eeReqPost = put;
            req = put;
            eeReq = put;
        } else if (this.method == METHOD_TRACE) {
            isBinary = true;
            req = new HttpTrace(url);
        } else if (this.method == METHOD_OPTIONS) {
            isBinary = true;
            req = new HttpOptions(url);
        } else if (this.method == METHOD_PATCH) {
            isBinary = true;
            eeReq = HTTPPatchFactory.getHTTPPatch(url);
            req = (HttpRequestBase) eeReq;
        } else {
            isBinary = true;
            eeReqPost = new HttpPost(url);
            req = (HttpPost) eeReqPost;
            eeReq = eeReqPost;
        }
        boolean hasForm = false;
        boolean hasBody = false;
        boolean hasContentType = false;
        // Set http params
        ArrayList<FormBodyPart> parts = new ArrayList<FormBodyPart>();
        StringBuilder acceptEncoding = new StringBuilder();
        java.util.List<NameValuePair> postParam = eeReqPost != null ? new ArrayList<NameValuePair>() : null;
        for (int i = 0; i < len; i++) {
            HttpParamBean param = this.params.get(i);
            int type = param.getType();
            // URL
            if (type == HttpParamBean.TYPE_URL) {
            // listQS.add(new BasicNameValuePair(translateEncoding(param.getName(), http.charset),translateEncoding(param.getValueAsString(),
            // http.charset)));
            } else // Form
            if (type == HttpParamBean.TYPE_FORM) {
                hasForm = true;
                if (this.method == METHOD_GET)
                    throw new ApplicationException("httpparam with type formfield can only be used when the method attribute of the parent http tag is set to post");
                if (eeReqPost != null) {
                    if (doMultiPart) {
                        parts.add(new FormBodyPart(param.getName(), new StringBody(param.getValueAsString(), CharsetUtil.toCharset(charset))));
                    } else {
                        postParam.add(new BasicNameValuePair(param.getName(), param.getValueAsString()));
                    }
                }
            // else if(multi!=null)multi.addParameter(param.getName(),param.getValueAsString());
            } else // CGI
            if (type == HttpParamBean.TYPE_CGI) {
                if (param.getEncoded())
                    req.addHeader(urlenc(param.getName(), charset), urlenc(param.getValueAsString(), charset));
                else
                    req.addHeader(param.getName(), param.getValueAsString());
            } else // Header
            if (type == HttpParamBean.TYPE_HEADER) {
                if (param.getName().equalsIgnoreCase("content-type"))
                    hasContentType = true;
                if (param.getName().equalsIgnoreCase("Content-Length")) {
                } else if (param.getName().equalsIgnoreCase("Accept-Encoding")) {
                    acceptEncoding.append(headerValue(param.getValueAsString()));
                    acceptEncoding.append(", ");
                } else
                    req.addHeader(param.getName(), headerValue(param.getValueAsString()));
            } else // Cookie
            if (type == HttpParamBean.TYPE_COOKIE) {
                HTTPEngine4Impl.addCookie(cookieStore, host, param.getName(), param.getValueAsString(), "/", charset);
            } else // File
            if (type == HttpParamBean.TYPE_FILE) {
                hasForm = true;
                if (this.method == METHOD_GET)
                    throw new ApplicationException("httpparam type file can't only be used, when method of the tag http equal post");
                // if(param.getFile()==null) throw new ApplicationException("httpparam type file can't only be used, when method of the tag http equal
                // post");
                String strCT = getContentType(param);
                ContentType ct = HTTPUtil.toContentType(strCT, null);
                String mt = "text/xml";
                if (ct != null && !StringUtil.isEmpty(ct.getMimeType(), true))
                    mt = ct.getMimeType();
                String cs = charset;
                if (ct != null && !StringUtil.isEmpty(ct.getCharset(), true))
                    cs = ct.getCharset();
                if (doMultiPart) {
                    try {
                        Resource res = param.getFile();
                        parts.add(new FormBodyPart(param.getName(), new ResourceBody(res, mt, res.getName(), cs)));
                    // parts.add(new ResourcePart(param.getName(),new ResourcePartSource(param.getFile()),getContentType(param),_charset));
                    } catch (FileNotFoundException e) {
                        throw new ApplicationException("can't upload file, path is invalid", e.getMessage());
                    }
                }
            } else // XML
            if (type == HttpParamBean.TYPE_XML) {
                ContentType ct = HTTPUtil.toContentType(param.getMimeType(), null);
                String mt = "text/xml";
                if (ct != null && !StringUtil.isEmpty(ct.getMimeType(), true))
                    mt = ct.getMimeType();
                String cs = charset;
                if (ct != null && !StringUtil.isEmpty(ct.getCharset(), true))
                    cs = ct.getCharset();
                hasBody = true;
                hasContentType = true;
                req.addHeader("Content-type", mt + "; charset=" + cs);
                if (eeReq == null)
                    throw new ApplicationException("type xml is only supported for methods get, delete, post, and put");
                HTTPEngine4Impl.setBody(eeReq, param.getValueAsString(), mt, cs);
            } else // Body
            if (type == HttpParamBean.TYPE_BODY) {
                ContentType ct = HTTPUtil.toContentType(param.getMimeType(), null);
                String mt = null;
                if (ct != null && !StringUtil.isEmpty(ct.getMimeType(), true))
                    mt = ct.getMimeType();
                String cs = charset;
                if (ct != null && !StringUtil.isEmpty(ct.getCharset(), true))
                    cs = ct.getCharset();
                hasBody = true;
                if (eeReq == null)
                    throw new ApplicationException("type body is only supported for methods get, delete, post, and put");
                HTTPEngine4Impl.setBody(eeReq, param.getValue(), mt, cs);
            } else {
                throw new ApplicationException("invalid type [" + type + "]");
            }
        }
        // post params
        if (postParam != null && postParam.size() > 0)
            eeReqPost.setEntity(new org.apache.http.client.entity.UrlEncodedFormEntity(postParam, charset));
        if (compression) {
            acceptEncoding.append("gzip");
        } else {
            acceptEncoding.append("deflate;q=0");
            req.setHeader("TE", "deflate;q=0");
        }
        req.setHeader("Accept-Encoding", acceptEncoding.toString());
        // multipart
        if (doMultiPart && eeReq != null) {
            hasContentType = true;
            boolean doIt = true;
            if (!this.multiPart && parts.size() == 1) {
                ContentBody body = parts.get(0).getBody();
                if (body instanceof StringBody) {
                    StringBody sb = (StringBody) body;
                    try {
                        org.apache.http.entity.ContentType ct = org.apache.http.entity.ContentType.create(sb.getMimeType(), sb.getCharset());
                        String str = IOUtil.toString(sb.getReader());
                        StringEntity entity = new StringEntity(str, ct);
                        eeReq.setEntity(entity);
                    } catch (IOException e) {
                        throw Caster.toPageException(e);
                    }
                    doIt = false;
                }
            }
            if (doIt) {
                MultipartEntityBuilder mpeBuilder = MultipartEntityBuilder.create().setStrictMode();
                // enabling the line below will append charset=... to the Content-Type header
                // if (!StringUtil.isEmpty(charset, true))
                // mpeBuilder.setCharset(CharsetUtil.toCharset(charset));
                Iterator<FormBodyPart> it = parts.iterator();
                while (it.hasNext()) {
                    FormBodyPart part = it.next();
                    mpeBuilder.addPart(part);
                }
                eeReq.setEntity(mpeBuilder.build());
            }
        // eem.setRequestEntity(new MultipartRequestEntityFlex(parts.toArray(new Part[parts.size()]), eem.getParams(),http.multiPartType));
        }
        if (hasBody && hasForm)
            throw new ApplicationException("mixing httpparam  type file/formfield and body/XML is not allowed");
        if (!hasContentType) {
            if (isBinary) {
                if (hasBody)
                    req.addHeader("Content-type", "application/octet-stream");
                else
                    req.addHeader("Content-type", "application/x-www-form-urlencoded; charset=" + charset);
            } else {
                if (hasBody)
                    req.addHeader("Content-type", "text/html; charset=" + charset);
            }
        }
        // set User Agent
        if (!hasHeaderIgnoreCase(req, "User-Agent"))
            req.setHeader("User-Agent", this.useragent);
        // set timeout
        setTimeout(builder, checkRemainingTimeout());
        // set Username and Password
        if (this.username != null) {
            if (this.password == null)
                this.password = "";
            if (AUTH_TYPE_NTLM == this.authType) {
                if (StringUtil.isEmpty(this.workStation, true))
                    throw new ApplicationException("attribute workstation is required when authentication type is [NTLM]");
                if (StringUtil.isEmpty(this.domain, true))
                    throw new ApplicationException("attribute domain is required when authentication type is [NTLM]");
                HTTPEngine4Impl.setNTCredentials(builder, this.username, this.password, this.workStation, this.domain);
            } else
                httpContext = HTTPEngine4Impl.setCredentials(builder, httpHost, this.username, this.password, preauth);
        }
        // set Proxy
        ProxyData proxy = null;
        if (!StringUtil.isEmpty(this.proxyserver)) {
            proxy = ProxyDataImpl.getInstance(this.proxyserver, this.proxyport, this.proxyuser, this.proxypassword);
        }
        if (pageContext.getConfig().isProxyEnableFor(host)) {
            proxy = pageContext.getConfig().getProxyData();
        }
        HTTPEngine4Impl.setProxy(builder, req, proxy);
    }
    CloseableHttpClient client = null;
    try {
        if (httpContext == null)
            httpContext = new BasicHttpContext();
        Struct cfhttp = new StructImpl();
        cfhttp.setEL(ERROR_DETAIL, "");
        pageContext.setVariable(result, cfhttp);
        // ///////////////////////////////////////// EXECUTE /////////////////////////////////////////////////
        client = builder.build();
        Executor4 e = new Executor4(pageContext, this, client, httpContext, req, redirect);
        HTTPResponse4Impl rsp = null;
        if (timeout == null || timeout.getMillis() <= 0) {
            try {
                rsp = e.execute(httpContext);
            } catch (Throwable t) {
                ExceptionUtil.rethrowIfNecessary(t);
                if (!throwonerror) {
                    if (t instanceof SocketTimeoutException)
                        setRequestTimeout(cfhttp);
                    else
                        setUnknownHost(cfhttp, t);
                    return;
                }
                throw toPageException(t, rsp);
            }
        } else {
            e.start();
            try {
                synchronized (this) {
                    // print.err(timeout);
                    this.wait(timeout.getMillis());
                }
            } catch (InterruptedException ie) {
                throw Caster.toPageException(ie);
            }
            if (e.t != null) {
                if (!throwonerror) {
                    setUnknownHost(cfhttp, e.t);
                    return;
                }
                throw toPageException(e.t, rsp);
            }
            rsp = e.response;
            if (!e.done) {
                req.abort();
                if (throwonerror)
                    throw new HTTPException("408 Request Time-out", "a timeout occurred in tag http", 408, "Time-out", rsp == null ? null : rsp.getURL());
                setRequestTimeout(cfhttp);
                return;
            // throw new ApplicationException("timeout");
            }
        }
        // ///////////////////////////////////////// EXECUTE /////////////////////////////////////////////////
        Charset responseCharset = CharsetUtil.toCharset(rsp.getCharset());
        int statCode = 0;
        // Write Response Scope
        // String rawHeader=httpMethod.getStatusLine().toString();
        String mimetype = null;
        String contentEncoding = null;
        // status code
        cfhttp.set(STATUSCODE, ((rsp.getStatusCode() + " " + rsp.getStatusText()).trim()));
        cfhttp.set(STATUS_CODE, new Double(statCode = rsp.getStatusCode()));
        cfhttp.set(STATUS_TEXT, (rsp.getStatusText()));
        cfhttp.set(HTTP_VERSION, (rsp.getProtocolVersion()));
        // responseHeader
        lucee.commons.net.http.Header[] headers = rsp.getAllHeaders();
        StringBuffer raw = new StringBuffer(rsp.getStatusLine() + " ");
        Struct responseHeader = new StructImpl();
        Struct cookie;
        Array setCookie = new ArrayImpl();
        Query cookies = new QueryImpl(new String[] { "name", "value", "path", "domain", "expires", "secure", "httpOnly" }, 0, "cookies");
        for (int i = 0; i < headers.length; i++) {
            lucee.commons.net.http.Header header = headers[i];
            // print.ln(header);
            raw.append(header.toString() + " ");
            if (header.getName().equalsIgnoreCase("Set-Cookie")) {
                setCookie.append(header.getValue());
                parseCookie(cookies, header.getValue());
            } else {
                // print.ln(header.getName()+"-"+header.getValue());
                Object value = responseHeader.get(KeyImpl.getInstance(header.getName()), null);
                if (value == null)
                    responseHeader.set(KeyImpl.getInstance(header.getName()), header.getValue());
                else {
                    Array arr = null;
                    if (value instanceof Array) {
                        arr = (Array) value;
                    } else {
                        arr = new ArrayImpl();
                        responseHeader.set(KeyImpl.getInstance(header.getName()), arr);
                        arr.appendEL(value);
                    }
                    arr.appendEL(header.getValue());
                }
            }
            // Content-Type
            if (header.getName().equalsIgnoreCase("Content-Type")) {
                mimetype = header.getValue();
                if (mimetype == null)
                    mimetype = NO_MIMETYPE;
            }
            // Content-Encoding
            if (header.getName().equalsIgnoreCase("Content-Encoding")) {
                contentEncoding = header.getValue();
            }
        }
        cfhttp.set(RESPONSEHEADER, responseHeader);
        cfhttp.set(KeyConstants._cookies, cookies);
        responseHeader.set(STATUS_CODE, new Double(statCode = rsp.getStatusCode()));
        responseHeader.set(EXPLANATION, (rsp.getStatusText()));
        if (setCookie.size() > 0)
            responseHeader.set(SET_COOKIE, setCookie);
        // is text
        boolean isText = mimetype == null || mimetype == NO_MIMETYPE || HTTPUtil.isTextMimeType(mimetype);
        // is multipart
        boolean isMultipart = MultiPartResponseUtils.isMultipart(mimetype);
        cfhttp.set(KeyConstants._text, Caster.toBoolean(isText));
        // boolean responseProvideCharset=false;
        if (!StringUtil.isEmpty(mimetype, true)) {
            if (isText) {
                String[] types = HTTPUtil.splitMimeTypeAndCharset(mimetype, null);
                if (types[0] != null)
                    cfhttp.set(KeyConstants._mimetype, types[0]);
                if (types[1] != null)
                    cfhttp.set(CHARSET, types[1]);
            } else
                cfhttp.set(KeyConstants._mimetype, mimetype);
        } else
            cfhttp.set(KeyConstants._mimetype, NO_MIMETYPE);
        // File
        Resource file = null;
        if (strFile != null && strPath != null) {
            file = ResourceUtil.toResourceNotExisting(pageContext, strPath).getRealResource(strFile);
        } else if (strFile != null) {
            file = ResourceUtil.toResourceNotExisting(pageContext, strFile);
        } else if (strPath != null) {
            file = ResourceUtil.toResourceNotExisting(pageContext, strPath);
            // Resource dir = file.getParentResource();
            if (file.isDirectory()) {
                // TODO was getName()
                file = file.getRealResource(req.getURI().getPath());
            // ->http://hc.apache.org/httpclient-3.x/apidocs/org/apache/commons/httpclient/URI.html#getName()
            }
        }
        if (file != null)
            pageContext.getConfig().getSecurityManager().checkFileLocation(file);
        // filecontent
        InputStream is = null;
        if (isText && getAsBinary != GET_AS_BINARY_YES) {
            String str;
            try {
                // read content
                if (method != METHOD_HEAD) {
                    is = rsp.getContentAsStream();
                    if (is != null && isGzipEncoded(contentEncoding))
                        is = rsp.getStatusCode() != 200 ? new CachingGZIPInputStream(is) : new GZIPInputStream(is);
                }
                try {
                    try {
                        str = is == null ? "" : IOUtil.toString(is, responseCharset, checkRemainingTimeout().getMillis());
                    } catch (EOFException eof) {
                        if (is instanceof CachingGZIPInputStream) {
                            str = IOUtil.toString(is = ((CachingGZIPInputStream) is).getRawData(), responseCharset, checkRemainingTimeout().getMillis());
                        } else
                            throw eof;
                    }
                } catch (UnsupportedEncodingException uee) {
                    str = IOUtil.toString(is, (Charset) null, checkRemainingTimeout().getMillis());
                }
            } catch (IOException ioe) {
                throw Caster.toPageException(ioe);
            } finally {
                IOUtil.closeEL(is);
            }
            if (str == null)
                str = "";
            if (resolveurl) {
                // if(e.redirectURL!=null)url=e.redirectURL.toExternalForm();
                str = new URLResolver().transform(str, e.response.getTargetURL(), false);
            }
            cfhttp.set(KeyConstants._filecontent, str);
            try {
                if (file != null) {
                    IOUtil.write(file, str, ((PageContextImpl) pageContext).getWebCharset(), false);
                }
            } catch (IOException e1) {
            }
            if (name != null) {
                Query qry = CSVParser.toQuery(str, delimiter, textqualifier, columns, firstrowasheaders);
                pageContext.setVariable(name, qry);
            }
        } else // Binary
        {
            byte[] barr = null;
            if (isGzipEncoded(contentEncoding)) {
                if (method != METHOD_HEAD) {
                    is = rsp.getContentAsStream();
                    is = rsp.getStatusCode() != 200 ? new CachingGZIPInputStream(is) : new GZIPInputStream(is);
                }
                try {
                    try {
                        barr = is == null ? new byte[0] : IOUtil.toBytes(is);
                    } catch (EOFException eof) {
                        if (is instanceof CachingGZIPInputStream)
                            barr = IOUtil.toBytes(((CachingGZIPInputStream) is).getRawData());
                        else
                            throw eof;
                    }
                } catch (IOException t) {
                    throw Caster.toPageException(t);
                } finally {
                    IOUtil.closeEL(is);
                }
            } else {
                try {
                    if (method != METHOD_HEAD)
                        barr = rsp.getContentAsByteArray();
                    else
                        barr = new byte[0];
                } catch (IOException t) {
                    throw Caster.toPageException(t);
                }
            }
            // IF Multipart response get file content and parse parts
            if (barr != null) {
                if (isMultipart) {
                    cfhttp.set(KeyConstants._filecontent, MultiPartResponseUtils.getParts(barr, mimetype));
                } else {
                    cfhttp.set(KeyConstants._filecontent, barr);
                }
            } else
                cfhttp.set(KeyConstants._filecontent, "");
            if (file != null) {
                try {
                    if (barr != null)
                        IOUtil.copy(new ByteArrayInputStream(barr), file, true);
                } catch (IOException ioe) {
                    throw Caster.toPageException(ioe);
                }
            }
        }
        // header
        cfhttp.set(KeyConstants._header, raw.toString());
        if (!isStatusOK(rsp.getStatusCode())) {
            String msg = rsp.getStatusCode() + " " + rsp.getStatusText();
            cfhttp.setEL(ERROR_DETAIL, msg);
            if (throwonerror) {
                throw new HTTPException(msg, null, rsp.getStatusCode(), rsp.getStatusText(), rsp.getURL());
            }
        }
        // TODO: check if we can use statCode instead of rsp.getStatusCode() everywhere and cleanup the code
        if (cacheHandler != null && rsp.getStatusCode() == 200) {
            // add to cache
            cacheHandler.set(pageContext, cacheId, cachedWithin, new HTTPCacheItem(cfhttp, url, System.nanoTime() - start));
        }
    } finally {
        if (client != null)
            client.close();
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) HttpRequestBase(org.apache.http.client.methods.HttpRequestBase) MalformedURLException(java.net.MalformedURLException) MultipartEntityBuilder(org.apache.http.entity.mime.MultipartEntityBuilder) ContentType(lucee.commons.lang.mimetype.ContentType) Query(lucee.runtime.type.Query) ArrayImpl(lucee.runtime.type.ArrayImpl) HttpOptions(org.apache.http.client.methods.HttpOptions) ArrayList(java.util.ArrayList) FileNotFoundException(java.io.FileNotFoundException) HttpPut(org.apache.http.client.methods.HttpPut) StringEntity(org.apache.http.entity.StringEntity) ProxyData(lucee.runtime.net.proxy.ProxyData) HttpHost(org.apache.http.HttpHost) HttpEntityEnclosingRequest(org.apache.http.HttpEntityEnclosingRequest) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) CachingGZIPInputStream(lucee.commons.net.http.httpclient.CachingGZIPInputStream) EOFException(java.io.EOFException) ArrayList(java.util.ArrayList) List(java.util.List) Header(lucee.commons.net.http.Header) HTTPCacheItem(lucee.runtime.cache.tag.http.HTTPCacheItem) Resource(lucee.commons.io.res.Resource) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ResourceBody(lucee.commons.net.http.httpclient.ResourceBody) BasicCookieStore(org.apache.http.impl.client.BasicCookieStore) SocketTimeoutException(java.net.SocketTimeoutException) ContentBody(org.apache.http.entity.mime.content.ContentBody) Header(lucee.commons.net.http.Header) ByteArrayInputStream(java.io.ByteArrayInputStream) HTTPResponse4Impl(lucee.commons.net.http.httpclient.HTTPResponse4Impl) CacheHandler(lucee.runtime.cache.tag.CacheHandler) URLResolver(lucee.runtime.util.URLResolver) HTTPException(lucee.runtime.exp.HTTPException) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) URL(java.net.URL) HttpHead(org.apache.http.client.methods.HttpHead) Struct(lucee.runtime.type.Struct) FormBodyPart(org.apache.http.entity.mime.FormBodyPart) GZIPInputStream(java.util.zip.GZIPInputStream) CachingGZIPInputStream(lucee.commons.net.http.httpclient.CachingGZIPInputStream) QueryImpl(lucee.runtime.type.QueryImpl) CacheHandlerPro(lucee.runtime.cache.tag.CacheHandlerPro) Iterator(java.util.Iterator) DefaultRedirectStrategy(org.apache.http.impl.client.DefaultRedirectStrategy) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) GZIPInputStream(java.util.zip.GZIPInputStream) CachingGZIPInputStream(lucee.commons.net.http.httpclient.CachingGZIPInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) HttpContext(org.apache.http.protocol.HttpContext) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) Charset(java.nio.charset.Charset) PageContextImpl(lucee.runtime.PageContextImpl) IOException(java.io.IOException) ConfigWeb(lucee.runtime.config.ConfigWeb) Array(lucee.runtime.type.Array) HttpTrace(org.apache.http.client.methods.HttpTrace) ApplicationException(lucee.runtime.exp.ApplicationException) StructImpl(lucee.runtime.type.StructImpl) StringBody(org.apache.http.entity.mime.content.StringBody) CacheItem(lucee.runtime.cache.tag.CacheItem) HTTPCacheItem(lucee.runtime.cache.tag.http.HTTPCacheItem)

Aggregations

CacheHandler (lucee.runtime.cache.tag.CacheHandler)7 CacheHandlerPro (lucee.runtime.cache.tag.CacheHandlerPro)7 CacheItem (lucee.runtime.cache.tag.CacheItem)7 IOException (java.io.IOException)4 PageContextImpl (lucee.runtime.PageContextImpl)4 ApplicationException (lucee.runtime.exp.ApplicationException)4 Struct (lucee.runtime.type.Struct)3 Iterator (java.util.Iterator)2 BodyContent (javax.servlet.jsp.tagext.BodyContent)2 Log (lucee.commons.io.log.Log)2 ConfigImpl (lucee.runtime.config.ConfigImpl)2 SQLImpl (lucee.runtime.db.SQLImpl)2 DatabaseException (lucee.runtime.exp.DatabaseException)2 PageException (lucee.runtime.exp.PageException)2 Array (lucee.runtime.type.Array)2 ArrayImpl (lucee.runtime.type.ArrayImpl)2 Key (lucee.runtime.type.Collection.Key)2 QueryImpl (lucee.runtime.type.QueryImpl)2 StructImpl (lucee.runtime.type.StructImpl)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1