Search in sources :

Example 6 with HttpServletResponseDummy

use of lucee.runtime.net.http.HttpServletResponseDummy 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)

Aggregations

HttpServletResponseDummy (lucee.runtime.net.http.HttpServletResponseDummy)6 Charset (java.nio.charset.Charset)3 PageContextImpl (lucee.runtime.PageContextImpl)3 PageException (lucee.runtime.exp.PageException)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 Entry (java.util.Map.Entry)2 Cookie (javax.servlet.http.Cookie)2 DevNullOutputStream (lucee.commons.io.DevNullOutputStream)2 Resource (lucee.commons.io.res.Resource)2 Pair (lucee.commons.lang.Pair)2 HttpServletRequestDummy (lucee.runtime.net.http.HttpServletRequestDummy)2 Key (lucee.runtime.type.Collection.Key)2 StructImpl (lucee.runtime.type.StructImpl)2 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 ZipEntry (java.util.zip.ZipEntry)1 ServletContext (javax.servlet.ServletContext)1