Search in sources :

Example 1 with HttpServletRequestDummy

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

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

the class ThreadUtil method cloneHttpServletRequest.

public static HttpServletRequest cloneHttpServletRequest(PageContext pc) {
    Config config = pc.getConfig();
    HttpServletRequest req = pc.getHttpServletRequest();
    HttpServletRequestDummy dest = HttpServletRequestDummy.clone(config, config.getRootDirectory(), req);
    return dest;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletRequestDummy(lucee.runtime.net.http.HttpServletRequestDummy) Config(lucee.runtime.config.Config)

Example 3 with HttpServletRequestDummy

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

the class ThreadUtil method createPageContext.

/**
 * @param config
 * @param os
 * @param serverName
 * @param requestURI
 * @param queryString
 * @param cookies
 * @param headers
 * @param parameters
 * @param attributes
 * @param register
 * @param timeout timeout in ms, if the value is smaller than 1 it is ignored and the value comming from the context is used
 * @return
 */
public static PageContextImpl createPageContext(ConfigWeb config, OutputStream os, String serverName, String requestURI, String queryString, Cookie[] cookies, Pair[] headers, byte[] body, Pair[] parameters, Struct attributes, boolean register, long timeout) {
    CFMLFactory factory = config.getFactory();
    HttpServletRequest req = new HttpServletRequestDummy(config.getRootDirectory(), serverName, requestURI, queryString, cookies, headers, parameters, attributes, null, body);
    req = new HTTPServletRequestWrap(req);
    HttpServletResponse rsp = createHttpServletResponse(os);
    return (PageContextImpl) factory.getLuceePageContext(factory.getServlet(), req, rsp, null, false, -1, false, register, timeout, false, false);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HTTPServletRequestWrap(lucee.runtime.net.http.HTTPServletRequestWrap) HttpServletRequestDummy(lucee.runtime.net.http.HttpServletRequestDummy) HttpServletResponse(javax.servlet.http.HttpServletResponse) CFMLFactory(lucee.runtime.CFMLFactory) PageContextImpl(lucee.runtime.PageContextImpl)

Example 4 with HttpServletRequestDummy

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

HttpServletRequestDummy (lucee.runtime.net.http.HttpServletRequestDummy)4 Cookie (javax.servlet.http.Cookie)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 Resource (lucee.commons.io.res.Resource)2 PageContextImpl (lucee.runtime.PageContextImpl)2 HttpServletResponseDummy (lucee.runtime.net.http.HttpServletResponseDummy)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 Entry (java.util.Map.Entry)1 ZipEntry (java.util.zip.ZipEntry)1 ServletContext (javax.servlet.ServletContext)1 HTTPServletImpl (lucee.cli.servlet.HTTPServletImpl)1 DevNullOutputStream (lucee.commons.io.DevNullOutputStream)1 Pair (lucee.commons.lang.Pair)1 CFMLFactory (lucee.runtime.CFMLFactory)1 Config (lucee.runtime.config.Config)1