use of lucee.runtime.interpreter.JSONExpressionInterpreter in project Lucee by lucee.
the class ReqRspUtil method toObject.
public static Object toObject(PageContext pc, byte[] data, int format, Charset charset, Object defaultValue) {
switch(format) {
case UDF.RETURN_FORMAT_JSON:
try {
return new JSONExpressionInterpreter().interpret(pc, toString(data, charset));
} catch (PageException pe) {
}
break;
case UDF.RETURN_FORMAT_SERIALIZE:
try {
return new CFMLExpressionInterpreter().interpret(pc, toString(data, charset));
} catch (PageException pe) {
}
break;
case UDF.RETURN_FORMAT_WDDX:
try {
WDDXConverter converter = new WDDXConverter(pc.getTimeZone(), false, true);
converter.setTimeZone(pc.getTimeZone());
return converter.deserialize(toString(data, charset), false);
} catch (Exception pe) {
}
break;
case UDF.RETURN_FORMAT_XML:
try {
InputSource xml = XMLUtil.toInputSource(pc, toString(data, charset));
InputSource validator = null;
return XMLCaster.toXMLStruct(XMLUtil.parse(xml, validator, false), true);
} catch (Exception pe) {
}
break;
case UDF.RETURN_FORMAT_JAVA:
try {
return JavaConverter.deserialize(new ByteArrayInputStream(data));
} catch (Exception pe) {
}
break;
}
return defaultValue;
}
use of lucee.runtime.interpreter.JSONExpressionInterpreter in project Lucee by lucee.
the class SpoolerTaskHTTPCall method execute.
public static final Object execute(RemoteClient client, Config config, String methodName, Struct args) throws PageException {
// return rpc.callWithNamedValues(config, getMethodName(), getArguments());
PageContext pc = ThreadLocalPageContext.get();
// remove wsdl if necessary
String url = client.getUrl();
if (StringUtil.endsWithIgnoreCase(url, "?wsdl"))
url = url.substring(0, url.length() - 5);
// Params
Map<String, String> params = new HashMap<String, String>();
params.put("method", methodName);
params.put("returnFormat", "json");
try {
Charset cs = pc.getWebCharset();
params.put("argumentCollection", new JSONConverter(true, cs).serialize(pc, args, false));
HTTPResponse res = HTTPEngine4Impl.post(HTTPUtil.toURL(url, true), client.getServerUsername(), client.getServerPassword(), -1L, true, pc.getWebCharset().name(), Constants.NAME + " Remote Invocation", client.getProxyData(), null, params);
return new JSONExpressionInterpreter().interpret(pc, res.getContentAsString());
} catch (IOException ioe) {
throw Caster.toPageException(ioe);
} catch (ConverterException ce) {
throw Caster.toPageException(ce);
}
}
use of lucee.runtime.interpreter.JSONExpressionInterpreter in project Lucee by lucee.
the class Props method callWDDX.
private void callWDDX(PageContext pc, Component component, Collection.Key methodName, boolean suppressContent) throws PageException {
try {
// Struct url = StructUtil.duplicate(pc.urlFormScope(),true);
Struct url = StructUtil.merge(new Struct[] { pc.formScope(), pc.urlScope() });
// define args
url.removeEL(KeyConstants._fieldnames);
url.removeEL(KeyConstants._method);
Object args = url.get(KeyConstants._argumentCollection, null);
String strArgCollFormat = Caster.toString(url.get("argumentCollectionFormat", null), null);
// url.returnFormat
int urlReturnFormat = -1;
Object oReturnFormatFromURL = url.get(KeyConstants._returnFormat, null);
if (oReturnFormatFromURL != null)
urlReturnFormat = UDFUtil.toReturnFormat(Caster.toString(oReturnFormatFromURL, null), -1);
// request header "accept"
List<MimeType> accept = ReqRspUtil.getAccept(pc);
int headerReturnFormat = MimeType.toFormat(accept, UDF.RETURN_FORMAT_XML, -1);
Object queryFormat = url.get(KeyConstants._queryFormat, null);
if (args == null) {
args = pc.getHttpServletRequest().getAttribute("argumentCollection");
}
if (StringUtil.isEmpty(strArgCollFormat)) {
strArgCollFormat = Caster.toString(pc.getHttpServletRequest().getAttribute("argumentCollectionFormat"), null);
}
// content-type
Charset cs = getCharset(pc);
Object o = component.get(pc, methodName, null);
// onMissingMethod
if (o == null)
o = component.get(pc, KeyConstants._onmissingmethod, null);
Props props = getProps(pc, o, urlReturnFormat, headerReturnFormat);
// if(!props.output)
setFormat(pc.getHttpServletResponse(), props.format, cs);
Object rtn = null;
try {
if (suppressContent)
pc.setSilent();
if (args == null) {
url = translate(component, methodName.getString(), url);
rtn = component.callWithNamedValues(pc, methodName, url);
} else if (args instanceof String) {
String str = (String) args;
int format = UDFUtil.toReturnFormat(strArgCollFormat, -1);
// CFML
if (UDF.RETURN_FORMAT_SERIALIZE == format) {
// do not catch exception when format is defined
args = new CFMLExpressionInterpreter().interpret(pc, str);
}
// JSON
if (UDF.RETURN_FORMAT_JSON == format) {
// do not catch exception when format is defined
args = new JSONExpressionInterpreter(false).interpret(pc, str);
} else // default
{
// catch exception when format is not defined, then in this case the string can also be a simple argument
try {
args = new JSONExpressionInterpreter(false).interpret(pc, str);
} catch (PageException pe) {
try {
args = new CFMLExpressionInterpreter().interpret(pc, str);
} catch (PageException _pe) {
}
}
}
}
// call
if (args != null) {
if (Decision.isCastableToStruct(args)) {
rtn = component.callWithNamedValues(pc, methodName, Caster.toStruct(args, false));
} else if (Decision.isCastableToArray(args)) {
rtn = component.call(pc, methodName, Caster.toNativeArray(args));
} else {
Object[] ac = new Object[1];
ac[0] = args;
rtn = component.call(pc, methodName, ac);
}
}
} finally {
if (suppressContent)
pc.unsetSilent();
}
// convert result
if (rtn != null) {
if (pc.getHttpServletRequest().getHeader("AMF-Forward") != null) {
pc.variablesScope().setEL("AMF-Forward", rtn);
} else {
_writeOut(pc, props, queryFormat, rtn, cs, false);
}
}
} catch (Throwable t) {
PageException pe = Caster.toPageException(t);
pe.setExposeMessage(true);
throw pe;
}
}
use of lucee.runtime.interpreter.JSONExpressionInterpreter in project Lucee by lucee.
the class ModernAppListener method _onRequest.
protected void _onRequest(PageContext pc, PageSource requestedPage, PageSource appPS, RequestListener rl) throws PageException {
PageContextImpl pci = (PageContextImpl) pc;
pci.setAppListenerType(ApplicationListener.TYPE_MODERN);
if (appPS != null) {
String callPath = appPS.getComponentName();
Component app = ComponentLoader.loadComponent(pci, appPS, callPath, false, false);
// init
ModernApplicationContext appContext = initApplicationContext(pci, app);
apps.put(appContext.getName(), app);
if (!pci.initApplicationContext(this))
return;
if (rl != null) {
requestedPage = rl.execute(pc, requestedPage);
if (requestedPage == null)
return;
}
String targetPage = requestedPage.getRealpathWithVirtual();
RefBoolean goon = new RefBooleanImpl(true);
// onRequestStart
if (app.contains(pc, ON_REQUEST_START)) {
try {
Object rtn = call(app, pci, ON_REQUEST_START, new Object[] { targetPage }, false);
if (!Caster.toBooleanValue(rtn, true))
return;
} catch (PageException pe) {
pe = handlePageException(pci, app, pe, requestedPage, targetPage, goon);
if (pe != null)
throw pe;
}
}
// onRequest
if (goon.toBooleanValue()) {
boolean isComp = isComponent(pc, requestedPage);
Object method;
if (isComp && app.contains(pc, ON_CFCREQUEST) && (method = pc.urlFormScope().get(KeyConstants._method, null)) != null) {
Struct url = (Struct) Duplicator.duplicate(pc.urlFormScope(), true);
url.removeEL(KeyConstants._fieldnames);
url.removeEL(KeyConstants._method);
Object args = url.get(KeyConstants._argumentCollection, null);
// url returnFormat
Object oReturnFormat = url.removeEL(KeyConstants._returnFormat);
int urlReturnFormat = -1;
if (oReturnFormat != null)
urlReturnFormat = UDFUtil.toReturnFormat(Caster.toString(oReturnFormat, null), -1);
// request header accept
List<MimeType> accept = ReqRspUtil.getAccept(pc);
int headerReturnFormat = MimeType.toFormat(accept, -1, -1);
Object queryFormat = url.removeEL(KeyConstants._queryFormat);
if (args == null) {
args = pc.getHttpServletRequest().getAttribute("argumentCollection");
}
if (args instanceof String) {
args = new JSONExpressionInterpreter().interpret(pc, (String) args);
}
if (args != null) {
if (Decision.isCastableToStruct(args)) {
Struct sct = Caster.toStruct(args, false);
// Key[] keys = url.keys();
Iterator<Entry<Key, Object>> it = url.entryIterator();
Entry<Key, Object> e;
while (it.hasNext()) {
e = it.next();
sct.setEL(e.getKey(), e.getValue());
}
args = sct;
} else if (Decision.isCastableToArray(args)) {
args = Caster.toArray(args);
} else {
Array arr = new ArrayImpl();
arr.appendEL(args);
args = arr;
}
} else
args = url;
Object rtn = call(app, pci, ON_CFCREQUEST, new Object[] { requestedPage.getComponentName(), method, args }, true);
if (rtn != null) {
if (pc.getHttpServletRequest().getHeader("AMF-Forward") != null) {
pc.variablesScope().setEL("AMF-Forward", rtn);
// ThreadLocalWDDXResult.set(rtn);
} else {
try {
ComponentPageImpl.writeToResponseStream(pc, app, method.toString(), urlReturnFormat, headerReturnFormat, queryFormat, rtn);
} catch (Exception e) {
throw Caster.toPageException(e);
}
}
}
} else {
// TODO impl die nicht so generisch ist
try {
if (!isComp && app.contains(pc, ON_REQUEST))
call(app, pci, ON_REQUEST, new Object[] { targetPage }, false);
else
pci._doInclude(new PageSource[] { requestedPage }, false, null);
} catch (PageException pe) {
pe = handlePageException(pci, app, pe, requestedPage, targetPage, goon);
if (pe != null)
throw pe;
}
}
}
// onRequestEnd
if (goon.toBooleanValue() && app.contains(pc, ON_REQUEST_END)) {
try {
call(app, pci, ON_REQUEST_END, new Object[] { targetPage }, false);
} catch (PageException pe) {
pe = handlePageException(pci, app, pe, requestedPage, targetPage, goon);
if (pe != null)
throw pe;
}
}
} else {
apps.put(pc.getApplicationContext().getName(), null);
if (rl != null) {
requestedPage = rl.execute(pc, requestedPage);
if (requestedPage == null)
return;
}
pci._doInclude(new PageSource[] { requestedPage }, false, null);
}
}
Aggregations