Search in sources :

Example 1 with HTTPServletImpl

use of lucee.cli.servlet.HTTPServletImpl 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 2 with HTTPServletImpl

use of lucee.cli.servlet.HTTPServletImpl in project Lucee by lucee.

the class PageContextUtil method getPageContext.

public static PageContext getPageContext(Config config, ServletConfig servletConfig, File contextRoot, String host, String scriptName, String queryString, Cookie[] cookies, Map<String, Object> headers, Map<String, String> parameters, Map<String, Object> attributes, OutputStream os, boolean register, long timeout, boolean ignoreScopes) throws ServletException {
    boolean callOnStart = ThreadLocalPageContext.callOnStart.get();
    try {
        ThreadLocalPageContext.callOnStart.set(false);
        if (contextRoot == null)
            contextRoot = new File(".");
        // Engine
        CFMLEngine engine = null;
        try {
            engine = CFMLEngineFactory.getInstance();
        } catch (Throwable t) {
            ExceptionUtil.rethrowIfNecessary(t);
        }
        if (engine == null)
            throw new ServletException("there is no ServletContext");
        if (headers == null)
            headers = new HashMap<String, Object>();
        if (parameters == null)
            parameters = new HashMap<String, String>();
        if (attributes == null)
            attributes = new HashMap<String, Object>();
        // Request
        HttpServletRequest req = CreationImpl.getInstance(engine).createHttpServletRequest(contextRoot, host, scriptName, queryString, cookies, headers, parameters, attributes, null);
        // Response
        HttpServletResponse rsp = CreationImpl.getInstance(engine).createHttpServletResponse(os);
        if (config == null)
            config = ThreadLocalPageContext.getConfig();
        CFMLFactory factory = null;
        HttpServlet servlet;
        if (config instanceof ConfigWeb) {
            ConfigWeb cw = (ConfigWeb) config;
            factory = cw.getFactory();
            servlet = factory.getServlet();
        } else {
            if (servletConfig == null) {
                ServletConfig[] configs = engine.getServletConfigs();
                String rootDir = contextRoot.getAbsolutePath();
                for (ServletConfig conf : configs) {
                    if (lucee.commons.io.SystemUtil.arePathsSame(rootDir, conf.getServletContext().getRealPath("/"))) {
                        servletConfig = conf;
                        break;
                    }
                }
                if (servletConfig == null)
                    servletConfig = configs[0];
            }
            factory = engine.getCFMLFactory(servletConfig, req);
            servlet = new HTTPServletImpl(servletConfig, servletConfig.getServletContext(), servletConfig.getServletName());
        }
        return factory.getLuceePageContext(servlet, req, rsp, null, false, -1, false, register, timeout, false, ignoreScopes);
    } finally {
        ThreadLocalPageContext.callOnStart.set(callOnStart);
    }
}
Also used : HashMap(java.util.HashMap) HttpServlet(javax.servlet.http.HttpServlet) ServletConfig(javax.servlet.ServletConfig) HttpServletResponse(javax.servlet.http.HttpServletResponse) CFMLFactory(lucee.runtime.CFMLFactory) ConfigWeb(lucee.runtime.config.ConfigWeb) ServletException(javax.servlet.ServletException) HttpServletRequest(javax.servlet.http.HttpServletRequest) CFMLEngine(lucee.loader.engine.CFMLEngine) HTTPServletImpl(lucee.cli.servlet.HTTPServletImpl) File(java.io.File)

Aggregations

HttpServletResponse (javax.servlet.http.HttpServletResponse)2 HTTPServletImpl (lucee.cli.servlet.HTTPServletImpl)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 IOException (java.io.IOException)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 HashMap (java.util.HashMap)1 Entry (java.util.Map.Entry)1 ZipEntry (java.util.zip.ZipEntry)1 ServletConfig (javax.servlet.ServletConfig)1 ServletContext (javax.servlet.ServletContext)1 ServletException (javax.servlet.ServletException)1 Cookie (javax.servlet.http.Cookie)1 HttpServlet (javax.servlet.http.HttpServlet)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 Resource (lucee.commons.io.res.Resource)1 Pair (lucee.commons.lang.Pair)1 CFMLEngine (lucee.loader.engine.CFMLEngine)1 CFMLFactory (lucee.runtime.CFMLFactory)1