Search in sources :

Example 1 with HttpServletResponseDummy

use of lucee.runtime.net.http.HttpServletResponseDummy in project Lucee by lucee.

the class ChildThreadImpl method execute.

public PageException execute(Config config) {
    PageContext oldPc = ThreadLocalPageContext.get();
    Page p = page;
    PageContextImpl pc = null;
    try {
        // deamon
        if (this.pc != null) {
            pc = this.pc;
            ThreadLocalPageContext.register(pc);
        } else // task
        {
            ConfigWebImpl cwi;
            try {
                cwi = (ConfigWebImpl) config;
                DevNullOutputStream os = DevNullOutputStream.DEV_NULL_OUTPUT_STREAM;
                pc = ThreadUtil.createPageContext(cwi, os, serverName, requestURI, queryString, SerializableCookie.toCookies(cookies), headers, null, parameters, attributes, true, -1);
                pc.setRequestTimeout(requestTimeout);
                p = PageSourceImpl.loadPage(pc, cwi.getPageSources(oldPc == null ? pc : oldPc, null, template, false, false, true));
            // p=cwi.getPageSources(oldPc,null, template, false,false,true).loadPage(cwi);
            } catch (PageException e) {
                return e;
            }
            pc.addPageSource(p.getPageSource(), true);
        }
        threadScope = pc.getThreadScope(KeyConstants._cfthread, null);
        pc.setCurrentThreadScope(new ThreadsImpl(this));
        pc.setThread(Thread.currentThread());
        // String encodings = pc.getHttpServletRequest().getHeader("Accept-Encoding");
        Undefined undefined = pc.us();
        Argument newArgs = new ArgumentThreadImpl((Struct) Duplicator.duplicate(attrs, false));
        LocalImpl newLocal = pc.getScopeFactory().getLocalInstance();
        // Key[] keys = attrs.keys();
        Iterator<Entry<Key, Object>> it = attrs.entryIterator();
        Entry<Key, Object> e;
        while (it.hasNext()) {
            e = it.next();
            newArgs.setEL(e.getKey(), e.getValue());
        }
        newLocal.setEL(KEY_ATTRIBUTES, newArgs);
        Argument oldArgs = pc.argumentsScope();
        Local oldLocal = pc.localScope();
        int oldMode = undefined.setMode(Undefined.MODE_LOCAL_OR_ARGUMENTS_ALWAYS);
        pc.setFunctionScopes(newLocal, newArgs);
        try {
            p.threadCall(pc, threadIndex);
        } catch (Throwable t) {
            ExceptionUtil.rethrowIfNecessary(t);
            if (!Abort.isSilentAbort(t)) {
                ConfigWeb c = pc.getConfig();
                if (c instanceof ConfigImpl) {
                    ConfigImpl ci = (ConfigImpl) c;
                    Log log = ci.getLog("thread");
                    if (log != null)
                        LogUtil.log(log, Log.LEVEL_ERROR, this.getName(), t);
                }
                PageException pe = Caster.toPageException(t);
                if (!serializable)
                    catchBlock = pe.getCatchBlock(pc.getConfig());
                return pe;
            }
        } finally {
            completed = true;
            pc.setFunctionScopes(oldLocal, oldArgs);
            undefined.setMode(oldMode);
            // pc.getScopeFactory().recycle(newArgs);
            pc.getScopeFactory().recycle(pc, newLocal);
            if (pc.getHttpServletResponse() instanceof HttpServletResponseDummy) {
                HttpServletResponseDummy rsp = (HttpServletResponseDummy) pc.getHttpServletResponse();
                pc.flush();
                contentType = rsp.getContentType();
                Pair<String, Object>[] _headers = rsp.getHeaders();
                if (_headers != null)
                    for (int i = 0; i < _headers.length; i++) {
                        if (_headers[i].getName().equalsIgnoreCase("Content-Encoding"))
                            contentEncoding = Caster.toString(_headers[i].getValue(), null);
                    }
            }
        }
    } finally {
        pc.getConfig().getFactory().releaseLuceePageContext(pc, true);
        pc = null;
        if (oldPc != null)
            ThreadLocalPageContext.register(oldPc);
    }
    return null;
}
Also used : Argument(lucee.runtime.type.scope.Argument) Page(lucee.runtime.Page) Entry(java.util.Map.Entry) ThreadLocalPageContext(lucee.runtime.engine.ThreadLocalPageContext) PageContext(lucee.runtime.PageContext) LocalImpl(lucee.runtime.type.scope.LocalImpl) Pair(lucee.commons.lang.Pair) PageException(lucee.runtime.exp.PageException) Undefined(lucee.runtime.type.scope.Undefined) Log(lucee.commons.io.log.Log) Local(lucee.runtime.type.scope.Local) PageContextImpl(lucee.runtime.PageContextImpl) ConfigWeb(lucee.runtime.config.ConfigWeb) DevNullOutputStream(lucee.commons.io.DevNullOutputStream) ArgumentThreadImpl(lucee.runtime.type.scope.ArgumentThreadImpl) ConfigWebImpl(lucee.runtime.config.ConfigWebImpl) HttpServletResponseDummy(lucee.runtime.net.http.HttpServletResponseDummy) Key(lucee.runtime.type.Collection.Key) ConfigImpl(lucee.runtime.config.ConfigImpl)

Example 2 with HttpServletResponseDummy

use of lucee.runtime.net.http.HttpServletResponseDummy in project Lucee by lucee.

the class CFMLEngineImpl method cli.

@Override
public void cli(Map<String, String> config, ServletConfig servletConfig) throws IOException, JspException, ServletException {
    ServletContext servletContext = servletConfig.getServletContext();
    HTTPServletImpl servlet = new HTTPServletImpl(servletConfig, servletContext, servletConfig.getServletName());
    // webroot
    String strWebroot = config.get("webroot");
    if (StringUtil.isEmpty(strWebroot, true))
        throw new IOException("missing webroot configuration");
    Resource root = ResourcesImpl.getFileResourceProvider().getResource(strWebroot);
    root.mkdirs();
    // serverName
    String serverName = config.get("server-name");
    if (StringUtil.isEmpty(serverName, true))
        serverName = "localhost";
    // uri
    String strUri = config.get("uri");
    if (StringUtil.isEmpty(strUri, true))
        throw new IOException("missing uri configuration");
    URI uri;
    try {
        uri = lucee.commons.net.HTTPUtil.toURI(strUri);
    } catch (URISyntaxException e) {
        throw Caster.toPageException(e);
    }
    // cookie
    Cookie[] cookies;
    String strCookie = config.get("cookie");
    if (StringUtil.isEmpty(strCookie, true))
        cookies = new Cookie[0];
    else {
        Map<String, String> mapCookies = HTTPUtil.parseParameterList(strCookie, false, null);
        int index = 0;
        cookies = new Cookie[mapCookies.size()];
        Entry<String, String> entry;
        Iterator<Entry<String, String>> it = mapCookies.entrySet().iterator();
        Cookie c;
        while (it.hasNext()) {
            entry = it.next();
            c = ReqRspUtil.toCookie(entry.getKey(), entry.getValue(), null);
            if (c != null)
                cookies[index++] = c;
            else
                throw new IOException("cookie name [" + entry.getKey() + "] is invalid");
        }
    }
    // header
    Pair[] headers = new Pair[0];
    // parameters
    Pair[] parameters = new Pair[0];
    // attributes
    StructImpl attributes = new StructImpl();
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    HttpServletRequestDummy req = new HttpServletRequestDummy(root, serverName, uri.getPath(), uri.getQuery(), cookies, headers, parameters, attributes, null, null);
    req.setProtocol("CLI/1.0");
    HttpServletResponse rsp = new HttpServletResponseDummy(os);
    serviceCFML(servlet, req, rsp);
    String res = os.toString(ReqRspUtil.getCharacterEncoding(null, rsp).name());
    System.out.println(res);
}
Also used : Cookie(javax.servlet.http.Cookie) Resource(lucee.commons.io.res.Resource) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) URI(java.net.URI) ZipEntry(java.util.zip.ZipEntry) Entry(java.util.Map.Entry) StructImpl(lucee.runtime.type.StructImpl) HttpServletRequestDummy(lucee.runtime.net.http.HttpServletRequestDummy) ServletContext(javax.servlet.ServletContext) HttpServletResponseDummy(lucee.runtime.net.http.HttpServletResponseDummy) HTTPServletImpl(lucee.cli.servlet.HTTPServletImpl) Pair(lucee.commons.lang.Pair)

Example 3 with HttpServletResponseDummy

use of lucee.runtime.net.http.HttpServletResponseDummy in project Lucee by lucee.

the class CallerResponseStreamResult method call.

@Override
public final String call() throws PageException {
    ThreadLocalPageContext.register(pc);
    // make sure content is not compressed
    pc.getRootOut().setAllowCompression(false);
    String str = null;
    try {
        _call(parent, pc);
    } finally {
        try {
            HttpServletResponseDummy rsp = (HttpServletResponseDummy) pc.getHttpServletResponse();
            Charset cs = ReqRspUtil.getCharacterEncoding(pc, rsp);
            // if(enc==null) enc="ISO-8859-1";
            // make sure content is flushed
            pc.getOut().flush();
            pc.getConfig().getFactory().releasePageContext(pc);
            // TODO add support for none string content
            str = IOUtil.toString((new ByteArrayInputStream(baos.toByteArray())), cs);
        } catch (Exception e) {
            SystemOut.printDate(e);
        }
    }
    return str;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) Charset(java.nio.charset.Charset) HttpServletResponseDummy(lucee.runtime.net.http.HttpServletResponseDummy) PageException(lucee.runtime.exp.PageException)

Example 4 with HttpServletResponseDummy

use of lucee.runtime.net.http.HttpServletResponseDummy in project Lucee by lucee.

the class UDFCaller2 method call.

@Override
public final Data<P> call() throws PageException {
    ThreadLocalPageContext.register(pc);
    // make sure content is not compressed
    pc.getRootOut().setAllowCompression(false);
    String str = null;
    Object result = null;
    try {
        if (namedArguments != null)
            result = udf.callWithNamedValues(pc, namedArguments, doIncludePath);
        else
            result = udf.call(pc, arguments, doIncludePath);
    } finally {
        try {
            HttpServletResponseDummy rsp = (HttpServletResponseDummy) pc.getHttpServletResponse();
            Charset cs = ReqRspUtil.getCharacterEncoding(pc, rsp);
            // if(enc==null) enc="ISO-8859-1";
            // make sure content is flushed
            pc.getOut().flush();
            pc.getConfig().getFactory().releasePageContext(pc);
            // TODO add support for none string content
            str = IOUtil.toString((new ByteArrayInputStream(baos.toByteArray())), cs);
        } catch (Exception e) {
            SystemOut.printDate(e);
        }
    }
    return new Data<P>(str, result, passed);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) Charset(java.nio.charset.Charset) HttpServletResponseDummy(lucee.runtime.net.http.HttpServletResponseDummy) PageException(lucee.runtime.exp.PageException)

Example 5 with HttpServletResponseDummy

use of lucee.runtime.net.http.HttpServletResponseDummy in project Lucee by lucee.

the class ModernAppListener method createPageContext.

private PageContextImpl createPageContext(CFMLFactory factory, Component app, String applicationName, String cfid, Collection.Key methodName, boolean register, long timeout) throws PageException {
    Resource root = factory.getConfig().getRootDirectory();
    String path = app.getPageSource().getRealpathWithVirtual();
    // Request
    HttpServletRequestDummy req = new HttpServletRequestDummy(root, "localhost", path, "", null, null, null, null, null, null);
    if (!StringUtil.isEmpty(cfid))
        req.setCookies(new Cookie[] { new Cookie("cfid", cfid), new Cookie("cftoken", "0") });
    // Response
    OutputStream os = DevNullOutputStream.DEV_NULL_OUTPUT_STREAM;
    // File based output stream
    /*try {
			Resource out = factory.getConfig().getConfigDir().getRealResource("output/"+methodName.getString()+".out");
			out.getParentResource().mkdirs();
			os = out.getOutputStream(false);
		} 
		catch (IOException e) {}*/
    HttpServletResponseDummy rsp = new HttpServletResponseDummy(os);
    // PageContext
    PageContextImpl pc = (PageContextImpl) factory.getLuceePageContext(factory.getServlet(), req, rsp, null, false, -1, false, register, timeout, true, false);
    // ApplicationContext
    ClassicApplicationContext ap = new ClassicApplicationContext(factory.getConfig(), applicationName, false, app == null ? null : ResourceUtil.getResource(pc, app.getPageSource(), null));
    initApplicationContext(pc, app);
    ap.setName(applicationName);
    ap.setSetSessionManagement(true);
    // if(!ap.hasName())ap.setName("Controler")
    // Base
    pc.setBase(app.getPageSource());
    return pc;
}
Also used : Cookie(javax.servlet.http.Cookie) HttpServletRequestDummy(lucee.runtime.net.http.HttpServletRequestDummy) DevNullOutputStream(lucee.commons.io.DevNullOutputStream) OutputStream(java.io.OutputStream) Resource(lucee.commons.io.res.Resource) HttpServletResponseDummy(lucee.runtime.net.http.HttpServletResponseDummy) PageContextImpl(lucee.runtime.PageContextImpl)

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