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);
}
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);
}
}
Aggregations