use of lucee.runtime.net.http.HttpServletResponseDummy in project Lucee by lucee.
the class InternalRequest method call.
public static Struct call(final PageContext pc, String template, String method, Struct urls, Struct forms, Struct cookies, Struct headers, Object body, String strCharset, boolean addToken) throws PageException {
// add token
if (addToken) {
// if(true) throw new ApplicationException("addtoken==true");
if (cookies == null)
cookies = new StructImpl();
cookies.set(KeyConstants._cfid, pc.getCFID());
cookies.set(KeyConstants._cftoken, pc.getCFToken());
String jsessionid = pc.getJSessionId();
if (jsessionid != null)
cookies.set("jsessionid", jsessionid);
}
// charset
Charset reqCharset = StringUtil.isEmpty(strCharset) ? pc.getWebCharset() : CharsetUtil.toCharset(strCharset);
String ext = ResourceUtil.getExtension(template, null);
// welcome files
if (StringUtil.isEmpty(ext)) {
throw new FunctionException(pc, "Invoke", 1, "url", "welcome file listing not supported, please define the template name.");
}
// dialect
int dialect = ((CFMLFactoryImpl) pc.getConfig().getFactory()).toDialect(ext, -1);
if (dialect == -1)
dialect = pc.getCurrentTemplateDialect();
// CFMLEngine.DIALECT_LUCEE
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] _barr = null;
if (Decision.isBinary(body))
_barr = Caster.toBinary(body);
else if (body != null) {
Charset cs = null;
// get charset
if (headers != null) {
String strCT = Caster.toString(headers.get(CONTENT_TYPE), null);
if (strCT != null) {
ContentType ct = HTTPUtil.toContentType(strCT, null);
if (ct != null) {
String strCS = ct.getCharset();
if (!StringUtil.isEmpty(strCS))
cs = CharsetUtil.toCharSet(strCS, CharSet.UTF8).toCharset();
}
}
}
if (cs == null)
cs = CharsetUtil.UTF8;
String str = Caster.toString(body);
_barr = str.getBytes(cs);
}
PageContextImpl _pc = createPageContext(pc, template, urls, cookies, headers, _barr, reqCharset, baos);
fillForm(_pc, forms);
Collection cookie, request, session = null;
int status;
long exeTime;
boolean isText = false;
Charset _charset = null;
try {
if (CFMLEngine.DIALECT_LUCEE == dialect)
_pc.execute(template, true, false);
else
_pc.executeCFML(template, true, false);
} finally {
_pc.flush();
cookie = _pc.cookieScope().duplicate(false);
request = _pc.requestScope().duplicate(false);
session = sessionEnabled(_pc) ? _pc.sessionScope().duplicate(false) : null;
exeTime = System.currentTimeMillis() - pc.getStartTime();
// debugging=_pc.getDebugger().getDebuggingData(_pc).duplicate(false);
HttpServletResponseDummy rsp = (HttpServletResponseDummy) _pc.getHttpServletResponse();
// headers
Collection.Key name;
headers = new StructImpl();
Iterator<String> it = rsp.getHeaderNames().iterator();
java.util.Collection<String> values;
while (it.hasNext()) {
name = KeyImpl.init(it.next());
values = rsp.getHeaders(name.getString());
if (values == null || values.size() == 0)
continue;
if (values.size() > 1)
headers.set(name, Caster.toArray(values));
else
headers.set(name, values.iterator().next());
}
// status
status = rsp.getStatus();
ContentType ct = HTTPUtil.toContentType(rsp.getContentType(), null);
if (ct != null) {
isText = HTTPUtil.isTextMimeType(ct.getMimeType());
if (ct.getCharset() != null)
_charset = CharsetUtil.toCharset(ct.getCharset(), null);
}
releasePageContext(_pc, pc);
}
Struct rst = new StructImpl();
byte[] barr = baos.toByteArray();
if (isText)
rst.set(KeyConstants._filecontent, new String(barr, _charset == null ? reqCharset : _charset));
else
rst.set(FILECONTENT_BYNARY, barr);
rst.set(KeyConstants._cookies, cookie);
rst.set(KeyConstants._request, request);
if (session != null)
rst.set(KeyConstants._session, session);
rst.set(KeyConstants._headers, headers);
// rst.put(KeyConstants._debugging, debugging);
rst.set(KeyConstants._executionTime, new Double(exeTime));
rst.set(KeyConstants._status, new Double(status));
rst.set(STATUS_CODE, new Double(status));
return rst;
}
Aggregations