use of lucee.runtime.converter.ScriptConverter in project Lucee by lucee.
the class HTTPClient method _callWithNamedValues.
private Object _callWithNamedValues(PageContext pc, Key methodName, Struct args) throws PageException {
// prepare request
Map<String, String> formfields = new HashMap<String, String>();
formfields.put("method", methodName.getString());
formfields.put("returnformat", "cfml");
String str;
try {
if (UDF.RETURN_FORMAT_JSON == argumentsCollectionFormat) {
Charset cs = pc.getWebCharset();
str = new JSONConverter(true, cs).serialize(pc, args, false);
formfields.put("argumentCollectionFormat", "json");
} else if (UDF.RETURN_FORMAT_SERIALIZE == argumentsCollectionFormat) {
str = new ScriptConverter().serialize(args);
formfields.put("argumentCollectionFormat", "cfml");
} else {
// Json interpreter also accepts cfscript
str = new ScriptConverter().serialize(args);
}
} catch (ConverterException e) {
throw Caster.toPageException(e);
}
// add aparams to request
formfields.put("argumentCollection", str);
/*
Iterator<Entry<Key, Object>> it = args.entryIterator();
Entry<Key, Object> e;
while(it.hasNext()){
e = it.next();
formfields.put(e.getKey().getString(), Caster.toString(e.getValue()));
}*/
Map<String, String> headers = new HashMap<String, String>();
// application/java disabled for the moment, it is not working when we have different lucee versions
headers.put("accept", "application/cfml,application/json");
HTTPResponse rsp = null;
InputStream is = null;
try {
// call remote cfc
rsp = HTTPEngine.post(url, username, password, -1, false, "UTF-8", createUserAgent(pc), proxyData, headers, formfields);
// read result
Header[] rspHeaders = rsp.getAllHeaders();
MimeType mt = getMimeType(rspHeaders, null);
int format = MimeType.toFormat(mt, -1);
if (format == -1) {
if (rsp.getStatusCode() != 200) {
boolean hasMsg = false;
String msg = rsp.getStatusText();
for (int i = 0; i < rspHeaders.length; i++) {
if (rspHeaders[i].getName().equalsIgnoreCase("exception-message")) {
msg = rspHeaders[i].getValue();
hasMsg = true;
}
}
is = rsp.getContentAsStream();
ApplicationException ae = new ApplicationException("remote component throws the following error:" + msg);
if (!hasMsg)
ae.setAdditional(KeyImpl.init("respone-body"), IOUtil.toString(is, mt.getCharset()));
throw ae;
}
throw new ApplicationException("cannot convert response with mime type [" + mt + "] to a CFML Object");
}
is = rsp.getContentAsStream();
return ReqRspUtil.toObject(pc, IOUtil.toBytes(is, false), format, mt.getCharset(), null);
} catch (IOException ioe) {
throw Caster.toPageException(ioe);
} finally {
IOUtil.closeEL(is);
HTTPEngine.closeEL(rsp);
}
}
use of lucee.runtime.converter.ScriptConverter in project Lucee by lucee.
the class Trace method _doEndTag.
public void _doEndTag() throws IOException, PageException {
PageSource ps = pageContext.getCurrentTemplatePageSource();
// var
String varValue = null;
Object value = null, traceValue = null;
if (!StringUtil.isEmpty(var)) {
try {
if (caller instanceof Scope)
value = VariableInterpreter.getVariable(pageContext, var, (Scope) caller);
else
value = pageContext.getVariable(var);
} catch (PageException e) {
varValue = "(undefined)";
follow = false;
}
if (follow) {
// print.o(1);
if (StringUtil.isEmpty(text, true))
text = var;
// print.o(2);
traceValue = TraceObjectSupport.toTraceObject(pageContext.getDebugger(), value, type, category, text);
if (caller instanceof Scope)
VariableInterpreter.setVariable(pageContext, var, traceValue, (Scope) caller);
else
pageContext.setVariable(var, traceValue);
}
try {
varValue = new ScriptConverter().serialize(value);
} catch (ConverterException e) {
if (value != null)
varValue = "(" + Caster.toTypeName(value) + ")";
}
}
DebugTrace trace = ((DebuggerImpl) pageContext.getDebugger()).addTrace(type, category, text, ps, var, varValue);
DebugTrace[] traces = pageContext.getDebugger().getTraces(pageContext);
String total = "(1st trace)";
if (traces.length > 1) {
long t = 0;
for (int i = 0; i < traces.length; i++) {
t += traces[i].getTime();
}
total = "(" + t + ")";
}
boolean hasCat = !StringUtil.isEmpty(trace.getCategory());
boolean hasText = !StringUtil.isEmpty(trace.getText());
boolean hasVar = !StringUtil.isEmpty(var);
// inline
if (inline) {
lucee.runtime.format.TimeFormat tf = new lucee.runtime.format.TimeFormat(pageContext.getConfig().getLocale());
StringBuffer sb = new StringBuffer();
sb.append("<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" bgcolor=\"white\">");
sb.append("<tr>");
// sb.append("<td><img src=\"/CFIDE/debug/images/Error_16x16.gif\" alt=\"Error type\">");
sb.append("<td>");
sb.append("<font color=\"orange\">");
sb.append("<b>");
sb.append(DebugTraceImpl.toType(trace.getType(), "INFO") + " - ");
sb.append("[CFTRACE " + tf.format(new DateTimeImpl(pageContext.getConfig()), "hh:mm:ss:l") + "]");
sb.append("[" + trace.getTime() + " ms " + total + "]");
sb.append("[" + trace.getTemplate() + " @ line: " + trace.getLine() + "]");
if (hasCat || hasText)
sb.append(" -");
if (hasCat)
sb.append(" [" + trace.getCategory() + "]");
if (hasText)
sb.append(" <i>" + trace.getText() + " </i>");
sb.append("</b>");
sb.append("</font>");
sb.append("</td>");
sb.append("</tr>");
sb.append("</table>");
pageContext.forceWrite(sb.toString());
if (hasVar)
Dump.call(pageContext, value, var);
}
// log
Log log = ((ConfigImpl) pageContext.getConfig()).getLog("trace");
StringBuffer msg = new StringBuffer();
msg.append("[" + trace.getTime() + " ms " + total + "] ");
msg.append("[" + trace.getTemplate() + " @ line: " + trace.getLine() + "]");
if (hasCat || hasText || hasVar)
msg.append("- ");
if (hasCat)
msg.append("[" + trace.getCategory() + "] ");
if (hasVar)
msg.append("[" + var + "=" + varValue + "] ");
if (hasText)
msg.append(" " + trace.getText() + " ");
log.log(trace.getType(), "cftrace", msg.toString());
// abort
if (abort)
throw new Abort(Abort.SCOPE_REQUEST);
}
use of lucee.runtime.converter.ScriptConverter in project Lucee by lucee.
the class Props method _writeOut.
private static void _writeOut(PageContext pc, Props props, Object queryFormat, Object rtn, Charset cs, boolean setFormat) throws ConverterException, PageException, IOException {
// return type XML ignore WDDX
if (props.type == CFTypes.TYPE_XML) {
// if(UDF.RETURN_FORMAT_WDDX==format) format=UDF.RETURN_FORMAT_PLAIN;
rtn = Caster.toString(Caster.toXML(rtn));
} else
// function does no real cast, only check it
rtn = Caster.castTo(pc, (short) props.type, props.strType, rtn);
if (setFormat)
setFormat(pc.getHttpServletResponse(), props.format, cs);
// WDDX
if (UDF.RETURN_FORMAT_WDDX == props.format) {
WDDXConverter converter = new WDDXConverter(pc.getTimeZone(), false, false);
converter.setTimeZone(pc.getTimeZone());
pc.forceWrite(converter.serialize(rtn));
} else // JSON
if (UDF.RETURN_FORMAT_JSON == props.format) {
boolean byColumn = false;
if (queryFormat instanceof String) {
String strQF = ((String) queryFormat).trim();
if (strQF.equalsIgnoreCase("row"))
;
else if (strQF.equalsIgnoreCase("column"))
byColumn = true;
else
throw new ApplicationException("invalid queryformat definition [" + strQF + "], valid formats are [row,column]");
}
JSONConverter converter = new JSONConverter(false, cs);
String prefix = "";
if (props.secureJson) {
prefix = pc.getApplicationContext().getSecureJsonPrefix();
if (prefix == null)
prefix = "";
}
pc.forceWrite(prefix + converter.serialize(pc, rtn, byColumn));
} else // CFML
if (UDF.RETURN_FORMAT_SERIALIZE == props.format) {
ScriptConverter converter = new ScriptConverter(false);
pc.forceWrite(converter.serialize(rtn));
} else // XML
if (UDF.RETURN_FORMAT_XML == props.format) {
XMLConverter converter = new XMLConverter(pc.getTimeZone(), false);
converter.setTimeZone(pc.getTimeZone());
pc.forceWrite(converter.serialize(rtn));
} else // Plain
if (UDF.RETURN_FORMAT_PLAIN == props.format) {
pc.forceWrite(Caster.toString(rtn));
} else // JAVA
if (UDF.RETURN_FORMAT_JAVA == props.format) {
writeOut(pc, rtn, MimeType.APPLICATION_JAVA, new JavaConverter());
} else
throw new IOException("invalid return format defintion:" + props.format);
}
use of lucee.runtime.converter.ScriptConverter in project Lucee by lucee.
the class Props method callCFCMetaData.
private void callCFCMetaData(PageContext pc, Component cfc, int format) throws IOException, PageException, ConverterException {
ComponentSpecificAccess cw = new ComponentSpecificAccess(Component.ACCESS_REMOTE, cfc);
ComponentScope scope = cw.getComponentScope();
Struct udfs = new StructImpl(), sctUDF, sctArg;
Array arrArg;
Iterator<Object> it = scope.valueIterator();
Object v;
UDF udf;
FunctionArgument[] args;
while (it.hasNext()) {
v = it.next();
// UDF
if (v instanceof UDF) {
udf = (UDF) v;
sctUDF = new StructImpl();
arrArg = new ArrayImpl();
udfs.setEL(udf.getFunctionName(), sctUDF);
args = udf.getFunctionArguments();
for (int i = 0; i < args.length; i++) {
sctArg = new StructImpl();
arrArg.appendEL(sctArg);
sctArg.setEL(KeyConstants._name, args[i].getName().getString());
sctArg.setEL(KeyConstants._type, args[i].getTypeAsString());
sctArg.setEL(KeyConstants._required, args[i].isRequired());
if (!StringUtil.isEmpty(args[i].getHint()))
sctArg.setEL(KeyConstants._hint, args[i].getHint());
}
sctUDF.set(KeyConstants._arguments, arrArg);
sctUDF.set(KeyConstants._returntype, udf.getReturnTypeAsString());
}
}
Struct rtn = new StructImpl();
rtn.set(KeyConstants._functions, udfs);
rtn.set(ACCEPT_ARG_COLL_FORMATS, "cfml,json");
InputStream is;
Charset cs = null;
// WDDX
if (UDF.RETURN_FORMAT_WDDX == format) {
WDDXConverter converter = new WDDXConverter(pc.getTimeZone(), false, false);
converter.setTimeZone(pc.getTimeZone());
String str = converter.serialize(rtn);
cs = getCharset(pc);
is = new ByteArrayInputStream(str.getBytes(cs));
} else // JSON
if (UDF.RETURN_FORMAT_JSON == format) {
boolean byColumn = false;
cs = getCharset(pc);
JSONConverter converter = new JSONConverter(false, cs);
String str = converter.serialize(pc, rtn, byColumn);
is = new ByteArrayInputStream(str.getBytes(cs));
} else // CFML
if (UDF.RETURN_FORMAT_SERIALIZE == format) {
ScriptConverter converter = new ScriptConverter(false);
String str = converter.serialize(rtn);
cs = getCharset(pc);
is = new ByteArrayInputStream(str.getBytes(cs));
} else // XML
if (UDF.RETURN_FORMAT_XML == format) {
XMLConverter converter = new XMLConverter(pc.getTimeZone(), false);
converter.setTimeZone(pc.getTimeZone());
String str = converter.serialize(rtn);
cs = getCharset(pc);
is = new ByteArrayInputStream(str.getBytes(cs));
} else // Plain
if (UDF.RETURN_FORMAT_PLAIN == format) {
String str = Caster.toString(rtn);
cs = getCharset(pc);
is = new ByteArrayInputStream(str.getBytes(cs));
} else // Java
if (UDF.RETURN_FORMAT_JAVA == format) {
byte[] bytes = JavaConverter.serializeAsBinary(rtn);
is = new ByteArrayInputStream(bytes);
} else
throw new IOException("invalid format defintion:" + format);
OutputStream os = null;
try {
os = pc.getResponseStream();
setFormat(pc.getHttpServletResponse(), format, cs);
IOUtil.copy(is, os, false, false);
} finally {
IOUtil.flushEL(os);
IOUtil.closeEL(os);
((PageContextImpl) pc).getRootOut().setClosed(true);
}
}
Aggregations