use of lucee.commons.lang.Pair in project Lucee by lucee.
the class HttpUtil method cloneParameters.
public static Pair<String, String>[] cloneParameters(HttpServletRequest req) {
List<Pair<String, String>> parameters = new ArrayList<Pair<String, String>>();
Enumeration e = req.getParameterNames();
String[] values;
String name;
while (e.hasMoreElements()) {
name = (String) e.nextElement();
values = req.getParameterValues(name);
if (values == null && ReqRspUtil.needEncoding(name, false))
values = req.getParameterValues(ReqRspUtil.encode(name, ReqRspUtil.getCharacterEncoding(null, req)));
if (values == null) {
PageContext pc = ThreadLocalPageContext.get();
if (pc != null && ReqRspUtil.identical(pc.getHttpServletRequest(), req)) {
values = HTTPServletRequestWrap.getParameterValues(ThreadLocalPageContext.get(), name);
}
}
if (values != null)
for (int i = 0; i < values.length; i++) {
parameters.add(new Pair<String, String>(name, values[i]));
}
}
return parameters.toArray(new Pair[parameters.size()]);
}
use of lucee.commons.lang.Pair in project Lucee by lucee.
the class HttpUtil method getAttributes.
public static Pair<String, Object>[] getAttributes(HttpServletRequest req) {
List<Pair<String, Object>> attributes = new ArrayList<Pair<String, Object>>();
Enumeration e = req.getAttributeNames();
String name;
while (e.hasMoreElements()) {
name = (String) e.nextElement();
attributes.add(new Pair<String, Object>(name, req.getAttribute(name)));
}
return attributes.toArray(new Pair[attributes.size()]);
}
use of lucee.commons.lang.Pair in project Lucee by lucee.
the class TagUtil method addTagMetaData.
/**
* load metadata from cfc based custom tags and add the info to the tag
* @param cs
* @param config
*/
public static void addTagMetaData(ConfigWebImpl cw) {
if (true)
return;
PageContextImpl pc = null;
try {
pc = ThreadUtil.createPageContext(cw, DevNullOutputStream.DEV_NULL_OUTPUT_STREAM, "localhost", "/", "", new Cookie[0], new Pair[0], null, new Pair[0], new StructImpl(), false, -1);
} catch (Throwable t) {
ExceptionUtil.rethrowIfNecessary(t);
return;
}
PageContext orgPC = ThreadLocalPageContext.get();
try {
ThreadLocalPageContext.register(pc);
// MUST MOST of them are the same, so this is a huge overhead
_addTagMetaData(pc, cw, CFMLEngine.DIALECT_CFML);
_addTagMetaData(pc, cw, CFMLEngine.DIALECT_LUCEE);
} catch (Throwable t) {
ExceptionUtil.rethrowIfNecessary(t);
} finally {
pc.getConfig().getFactory().releaseLuceePageContext(pc, true);
ThreadLocalPageContext.register(orgPC);
}
}
use of lucee.commons.lang.Pair 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;
}
use of lucee.commons.lang.Pair 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);
}
Aggregations