Search in sources :

Example 21 with Collection

use of lucee.runtime.type.Collection in project Lucee by lucee.

the class InternalRequest method call.

public static Struct call(final PageContext pc, String template, String method, Struct urls, Struct forms, Struct cookies, Struct headers, Object body, String strCharset, boolean addToken) throws PageException {
    // add token
    if (addToken) {
        // if(true) throw new ApplicationException("addtoken==true");
        if (cookies == null)
            cookies = new StructImpl();
        cookies.set(KeyConstants._cfid, pc.getCFID());
        cookies.set(KeyConstants._cftoken, pc.getCFToken());
        String jsessionid = pc.getJSessionId();
        if (jsessionid != null)
            cookies.set("jsessionid", jsessionid);
    }
    // charset
    Charset reqCharset = StringUtil.isEmpty(strCharset) ? pc.getWebCharset() : CharsetUtil.toCharset(strCharset);
    String ext = ResourceUtil.getExtension(template, null);
    // welcome files
    if (StringUtil.isEmpty(ext)) {
        throw new FunctionException(pc, "Invoke", 1, "url", "welcome file listing not supported, please define the template name.");
    }
    // dialect
    int dialect = ((CFMLFactoryImpl) pc.getConfig().getFactory()).toDialect(ext, -1);
    if (dialect == -1)
        dialect = pc.getCurrentTemplateDialect();
    // CFMLEngine.DIALECT_LUCEE
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    byte[] _barr = null;
    if (Decision.isBinary(body))
        _barr = Caster.toBinary(body);
    else if (body != null) {
        Charset cs = null;
        // get charset
        if (headers != null) {
            String strCT = Caster.toString(headers.get(CONTENT_TYPE), null);
            if (strCT != null) {
                ContentType ct = HTTPUtil.toContentType(strCT, null);
                if (ct != null) {
                    String strCS = ct.getCharset();
                    if (!StringUtil.isEmpty(strCS))
                        cs = CharsetUtil.toCharSet(strCS, CharSet.UTF8).toCharset();
                }
            }
        }
        if (cs == null)
            cs = CharsetUtil.UTF8;
        String str = Caster.toString(body);
        _barr = str.getBytes(cs);
    }
    PageContextImpl _pc = createPageContext(pc, template, urls, cookies, headers, _barr, reqCharset, baos);
    fillForm(_pc, forms);
    Collection cookie, request, session = null;
    int status;
    long exeTime;
    boolean isText = false;
    Charset _charset = null;
    try {
        if (CFMLEngine.DIALECT_LUCEE == dialect)
            _pc.execute(template, true, false);
        else
            _pc.executeCFML(template, true, false);
    } finally {
        _pc.flush();
        cookie = _pc.cookieScope().duplicate(false);
        request = _pc.requestScope().duplicate(false);
        session = sessionEnabled(_pc) ? _pc.sessionScope().duplicate(false) : null;
        exeTime = System.currentTimeMillis() - pc.getStartTime();
        // debugging=_pc.getDebugger().getDebuggingData(_pc).duplicate(false);
        HttpServletResponseDummy rsp = (HttpServletResponseDummy) _pc.getHttpServletResponse();
        // headers
        Collection.Key name;
        headers = new StructImpl();
        Iterator<String> it = rsp.getHeaderNames().iterator();
        java.util.Collection<String> values;
        while (it.hasNext()) {
            name = KeyImpl.init(it.next());
            values = rsp.getHeaders(name.getString());
            if (values == null || values.size() == 0)
                continue;
            if (values.size() > 1)
                headers.set(name, Caster.toArray(values));
            else
                headers.set(name, values.iterator().next());
        }
        // status
        status = rsp.getStatus();
        ContentType ct = HTTPUtil.toContentType(rsp.getContentType(), null);
        if (ct != null) {
            isText = HTTPUtil.isTextMimeType(ct.getMimeType());
            if (ct.getCharset() != null)
                _charset = CharsetUtil.toCharset(ct.getCharset(), null);
        }
        releasePageContext(_pc, pc);
    }
    Struct rst = new StructImpl();
    byte[] barr = baos.toByteArray();
    if (isText)
        rst.set(KeyConstants._filecontent, new String(barr, _charset == null ? reqCharset : _charset));
    else
        rst.set(FILECONTENT_BYNARY, barr);
    rst.set(KeyConstants._cookies, cookie);
    rst.set(KeyConstants._request, request);
    if (session != null)
        rst.set(KeyConstants._session, session);
    rst.set(KeyConstants._headers, headers);
    // rst.put(KeyConstants._debugging, debugging);
    rst.set(KeyConstants._executionTime, new Double(exeTime));
    rst.set(KeyConstants._status, new Double(status));
    rst.set(STATUS_CODE, new Double(status));
    return rst;
}
Also used : ContentType(lucee.commons.lang.mimetype.ContentType) FunctionException(lucee.runtime.exp.FunctionException) Charset(java.nio.charset.Charset) ByteArrayOutputStream(java.io.ByteArrayOutputStream) PageContextImpl(lucee.runtime.PageContextImpl) Struct(lucee.runtime.type.Struct) Key(lucee.runtime.type.Collection.Key) StructImpl(lucee.runtime.type.StructImpl) CFMLFactoryImpl(lucee.runtime.CFMLFactoryImpl) Collection(lucee.runtime.type.Collection) HttpServletResponseDummy(lucee.runtime.net.http.HttpServletResponseDummy)

Example 22 with Collection

use of lucee.runtime.type.Collection in project Lucee by lucee.

the class Arch method sizeOf.

private static void sizeOf(Creation creator, Size size, Object obj, Set<Object> parents) throws PageException {
    if (obj == null)
        return;
    Object raw = obj;
    // TODO this is just a patch solution, find a better way to handle this kind of situation (Wrapper classes)
    if (isInstaneOf(obj.getClass(), "lucee.runtime.text.xml.struct.XMLStruct")) {
        try {
            Method toNode = raw.getClass().getMethod("toNode", new Class[0]);
            raw = toNode.invoke(obj, new Object[0]);
        } catch (Exception e) {
            SystemOut.printDate(e);
        }
    }
    if (parents.contains(raw))
        return;
    parents.add(raw);
    try {
        if (obj instanceof Collection) {
            if (obj instanceof Query)
                sizeOf(creator, size, (Query) obj, parents);
            else
                sizeOf(creator, size, ((Collection) obj).valueIterator(), parents);
            return;
        } else // Map
        if (obj instanceof Map) {
            sizeOf(creator, size, ((Map) obj).values().iterator(), parents);
            return;
        } else // List
        if (obj instanceof List) {
            sizeOf(creator, size, ((List) obj).iterator(), parents);
            return;
        } else // String
        if (obj instanceof String) {
            size.size += (CHAR_SIZE * ((String) obj).length()) + REF_SIZE;
        } else // Number
        if (obj instanceof Number) {
            if (obj instanceof Double)
                size.size += DOUBLE_SIZE + REF_SIZE;
            else if (obj instanceof Float)
                size.size += FLOAT_SIZE + REF_SIZE;
            else if (obj instanceof Long)
                size.size += LONG_SIZE + REF_SIZE;
            else if (obj instanceof Integer)
                size.size += INT_SIZE + REF_SIZE;
            else if (obj instanceof Short)
                size.size += SHORT_SIZE + REF_SIZE;
            else if (obj instanceof Byte)
                size.size += BYTE_SIZE + REF_SIZE;
        } else if (obj instanceof Boolean)
            size.size += REF_SIZE + BOOLEAN_SIZE;
        else if (obj instanceof Character)
            size.size += REF_SIZE + CHAR_SIZE;
        else
            size.size += _sizeOf(obj);
        size.count++;
    } finally {
    // parents.remove(raw);// TODO should we not remove, to see if sister is me.
    }
}
Also used : Query(lucee.runtime.type.Query) Method(java.lang.reflect.Method) PageException(lucee.runtime.exp.PageException) Collection(lucee.runtime.type.Collection) List(java.util.List) Map(java.util.Map)

Aggregations

Collection (lucee.runtime.type.Collection)22 List (java.util.List)12 Iterator (java.util.Iterator)8 ExpressionException (lucee.runtime.exp.ExpressionException)8 ArrayList (java.util.ArrayList)7 Map (java.util.Map)7 Key (lucee.runtime.type.Collection.Key)6 Struct (lucee.runtime.type.Struct)6 Node (org.w3c.dom.Node)6 Entry (java.util.Map.Entry)4 XMLException (lucee.runtime.exp.XMLException)4 Array (lucee.runtime.type.Array)4 NodeList (org.w3c.dom.NodeList)4 Enumeration (java.util.Enumeration)3 FunctionException (lucee.runtime.exp.FunctionException)3 Iteratorable (lucee.runtime.type.Iteratorable)3 Query (lucee.runtime.type.Query)3 StructImpl (lucee.runtime.type.StructImpl)3 ListIterator (java.util.ListIterator)2 ExecutorService (java.util.concurrent.ExecutorService)2