use of lucee.runtime.type.FunctionArgument in project Lucee by lucee.
the class ComponentUtil method md5.
public static String md5(ComponentSpecificAccess cw) throws IOException {
Key[] keys = cw.keys();
Arrays.sort(keys);
StringBuffer _interface = new StringBuffer();
Object member;
UDF udf;
FunctionArgument[] args;
FunctionArgument arg;
for (int y = 0; y < keys.length; y++) {
member = cw.get(keys[y], null);
if (member instanceof UDF) {
udf = (UDF) member;
// print.out(udf.);
_interface.append(udf.getAccess());
_interface.append(udf.getOutput());
_interface.append(udf.getFunctionName());
_interface.append(udf.getReturnTypeAsString());
args = udf.getFunctionArguments();
for (int i = 0; i < args.length; i++) {
arg = args[i];
_interface.append(arg.isRequired());
_interface.append(arg.getName());
_interface.append(arg.getTypeAsString());
}
}
}
return MD5.getDigestAsString(_interface.toString().toLowerCase());
}
use of lucee.runtime.type.FunctionArgument in project Lucee by lucee.
the class ComponentUtil method getMetaData.
public static Struct getMetaData(PageContext pc, UDFPropertiesBase udf) throws PageException {
StructImpl func = new StructImpl();
pc = ThreadLocalPageContext.get(pc);
// TODO func.set("roles", value);
// TODO func.set("userMetadata", value); neo unterstuetzt irgendwelche a
// meta data
Struct meta = udf.getMeta();
if (meta != null)
StructUtil.copy(meta, func, true);
func.setEL(KeyConstants._closure, Boolean.FALSE);
func.set(KeyConstants._access, ComponentUtil.toStringAccess(udf.getAccess()));
String hint = udf.getHint();
if (!StringUtil.isEmpty(hint))
func.set(KeyConstants._hint, hint);
String displayname = udf.getDisplayName();
if (!StringUtil.isEmpty(displayname))
func.set(KeyConstants._displayname, displayname);
func.set(KeyConstants._name, udf.getFunctionName());
func.set(KeyConstants._output, Caster.toBoolean(udf.getOutput()));
func.set(KeyConstants._returntype, udf.getReturnTypeAsString());
func.set("modifier", udf.getModifier() == Component.MODIFIER_NONE ? "" : ComponentUtil.toModifier(udf.getModifier(), ""));
func.set(KeyConstants._description, udf.getDescription());
if (udf.getLocalMode() != null)
func.set("localMode", AppListenerUtil.toLocalMode(udf.getLocalMode().intValue(), ""));
if (udf.getPageSource() != null)
func.set(KeyConstants._owner, udf.getPageSource().getDisplayPath());
if (udf.getStartLine() > 0 && udf.getEndLine() > 0) {
Struct pos = new StructImpl();
pos.set("start", udf.getStartLine());
pos.set("end", udf.getEndLine());
func.setEL("position", pos);
}
int format = udf.getReturnFormat();
if (format < 0 || format == UDF.RETURN_FORMAT_WDDX)
func.set(KeyConstants._returnFormat, "wddx");
else if (format == UDF.RETURN_FORMAT_PLAIN)
func.set(KeyConstants._returnFormat, "plain");
else if (format == UDF.RETURN_FORMAT_JSON)
func.set(KeyConstants._returnFormat, "json");
else if (format == UDF.RETURN_FORMAT_SERIALIZE)
func.set(KeyConstants._returnFormat, "cfml");
FunctionArgument[] args = udf.getFunctionArguments();
Array params = new ArrayImpl();
// Object defaultValue;
Struct m;
// Object defaultValue;
for (int y = 0; y < args.length; y++) {
StructImpl param = new StructImpl();
param.set(KeyConstants._name, args[y].getName().getString());
param.set(KeyConstants._required, Caster.toBoolean(args[y].isRequired()));
param.set(KeyConstants._type, args[y].getTypeAsString());
displayname = args[y].getDisplayName();
if (!StringUtil.isEmpty(displayname))
param.set(KeyConstants._displayname, displayname);
int defType = args[y].getDefaultType();
if (defType == FunctionArgument.DEFAULT_TYPE_RUNTIME_EXPRESSION) {
param.set(KeyConstants._default, "[runtime expression]");
} else if (defType == FunctionArgument.DEFAULT_TYPE_LITERAL) {
Page p = udf.getPage(pc);
param.set(KeyConstants._default, p.udfDefaultValue(pc, udf.getIndex(), y, null));
}
hint = args[y].getHint();
if (!StringUtil.isEmpty(hint))
param.set(KeyConstants._hint, hint);
// TODO func.set("userMetadata", value); neo unterstuetzt irgendwelche attr, die dann hier ausgebenen werden bloedsinn
// meta data
m = args[y].getMetaData();
if (m != null)
StructUtil.copy(m, param, true);
params.append(param);
}
func.set(KeyConstants._parameters, params);
return func;
}
use of lucee.runtime.type.FunctionArgument in project Lucee by lucee.
the class UDFUtil method argumentCollection.
public static void argumentCollection(Struct values, FunctionArgument[] funcArgs) {
Object value = values.removeEL(KeyConstants._argumentCollection);
if (value != null) {
value = Caster.unwrap(value, value);
if (value instanceof Argument) {
Argument argColl = (Argument) value;
Iterator<Key> it = argColl.keyIterator();
Key k;
int i = -1;
while (it.hasNext()) {
i++;
k = it.next();
if (funcArgs.length > i && k instanceof ArgumentIntKey) {
if (!values.containsKey(funcArgs[i].getName()))
values.setEL(funcArgs[i].getName(), argColl.get(k, Argument.NULL));
else
values.setEL(k, argColl.get(k, Argument.NULL));
} else if (!values.containsKey(k)) {
values.setEL(k, argColl.get(k, Argument.NULL));
}
}
} else if (value instanceof Collection) {
Collection argColl = (Collection) value;
// Collection.Key[] keys = argColl.keys();
Iterator<Key> it = argColl.keyIterator();
Key k;
while (it.hasNext()) {
k = it.next();
if (!values.containsKey(k)) {
values.setEL(k, argColl.get(k, Argument.NULL));
}
}
} else if (value instanceof Map) {
Map map = (Map) value;
Iterator it = map.entrySet().iterator();
Map.Entry entry;
Key key;
while (it.hasNext()) {
entry = (Entry) it.next();
key = Caster.toKey(entry.getKey(), null);
if (!values.containsKey(key)) {
values.setEL(key, entry.getValue());
}
}
} else if (value instanceof java.util.List) {
java.util.List list = (java.util.List) value;
Iterator it = list.iterator();
Object v;
int index = 0;
Key k;
while (it.hasNext()) {
v = it.next();
k = ArgumentIntKey.init(++index);
if (!values.containsKey(k)) {
values.setEL(k, v);
}
}
} else {
values.setEL(KeyConstants._argumentCollection, value);
}
}
}
use of lucee.runtime.type.FunctionArgument in project Lucee by lucee.
the class ComponentImpl method onMissingMethod.
public Object onMissingMethod(PageContext pc, int access, Member member, String name, Object[] _args, Struct _namedArgs, boolean superAccess) throws PageException {
Member ommm = access == -1 ? getMember(pc, KeyConstants._onmissingmethod, false, superAccess) : getMember(access, KeyConstants._onmissingmethod, false, superAccess);
if (ommm instanceof UDF) {
Argument args = new ArgumentImpl();
if (_args != null) {
for (int i = 0; i < _args.length; i++) {
args.setEL(ArgumentIntKey.init(i + 1), _args[i]);
}
} else if (_namedArgs != null) {
UDFUtil.argumentCollection(_namedArgs, new FunctionArgument[] {});
Iterator<Entry<Key, Object>> it = _namedArgs.entryIterator();
Entry<Key, Object> e;
while (it.hasNext()) {
e = it.next();
args.setEL(e.getKey(), e.getValue());
}
}
// Struct newArgs=new StructImpl(StructImpl.TYPE_SYNC);
// newArgs.setEL(MISSING_METHOD_NAME, name);
// newArgs.setEL(MISSING_METHOD_ARGS, args);
Object[] newArgs = new Object[] { name, args };
return _call(pc, KeyConstants._onmissingmethod, (UDFPlus) ommm, null, newArgs);
}
if (member == null)
throw ComponentUtil.notFunction(this, KeyImpl.init(name), null, access);
throw ComponentUtil.notFunction(this, KeyImpl.init(name), member.getValue(), access);
}
use of lucee.runtime.type.FunctionArgument in project Lucee by lucee.
the class Props method _callRest.
private void _callRest(PageContext pc, Component component, UDF udf, String path, Struct variables, Result result, MimeType best, MimeType[] produces, boolean suppressContent, Key methodName) throws PageException, IOException, ConverterException {
FunctionArgument[] fa = udf.getFunctionArguments();
Struct args = new StructImpl(), meta;
Key name;
String restArgName, restArgSource, value;
for (int i = 0; i < fa.length; i++) {
name = fa[i].getName();
meta = fa[i].getMetaData();
restArgSource = meta == null ? "" : Caster.toString(meta.get(KeyConstants._restArgSource, ""), "");
if ("path".equalsIgnoreCase(restArgSource))
setValue(fa[i], args, name, variables.get(name, null));
if ("query".equalsIgnoreCase(restArgSource) || "url".equalsIgnoreCase(restArgSource))
setValue(fa[i], args, name, pc.urlScope().get(name, null));
if ("form".equalsIgnoreCase(restArgSource))
setValue(fa[i], args, name, pc.formScope().get(name, null));
if ("cookie".equalsIgnoreCase(restArgSource))
setValue(fa[i], args, name, pc.cookieScope().get(name, null));
if ("header".equalsIgnoreCase(restArgSource) || "head".equalsIgnoreCase(restArgSource)) {
restArgName = meta == null ? "" : Caster.toString(meta.get(KeyConstants._restArgName, ""), "");
if (StringUtil.isEmpty(restArgName))
restArgName = name.getString();
value = ReqRspUtil.getHeaderIgnoreCase(pc, restArgName, null);
setValue(fa[i], args, name, value);
}
if ("matrix".equalsIgnoreCase(restArgSource))
setValue(fa[i], args, name, result.getMatrix().get(name, null));
if ("body".equalsIgnoreCase(restArgSource) || StringUtil.isEmpty(restArgSource, true)) {
boolean isSimple = CFTypes.isSimpleType(fa[i].getType());
Object body = ReqRspUtil.getRequestBody(pc, true, null);
if (isSimple && !Decision.isSimpleValue(body))
body = ReqRspUtil.getRequestBody(pc, false, null);
setValue(fa[i], args, name, body);
}
}
Object rtn = null;
try {
if (suppressContent)
pc.setSilent();
rtn = component.callWithNamedValues(pc, methodName, args);
} catch (PageException e) {
RestUtil.setStatus(pc, 500, ExceptionUtil.getMessage(e));
} finally {
if (suppressContent)
pc.unsetSilent();
}
// custom response
Struct sct = result.getCustomResponse();
boolean hasContent = false;
if (sct != null) {
HttpServletResponse rsp = pc.getHttpServletResponse();
// status
int status = Caster.toIntValue(sct.get(KeyConstants._status, Constants.DOUBLE_ZERO), 0);
if (status > 0)
rsp.setStatus(status);
// content
Object o = sct.get(KeyConstants._content, null);
if (o != null) {
String content = Caster.toString(o, null);
if (content != null) {
try {
pc.forceWrite(content);
hasContent = true;
} catch (IOException e) {
}
}
}
// headers
Struct headers = Caster.toStruct(sct.get(KeyConstants._headers, null), null);
if (headers != null) {
// Key[] keys = headers.keys();
Iterator<Entry<Key, Object>> it = headers.entryIterator();
Entry<Key, Object> e;
String n, v;
Object tmp;
while (it.hasNext()) {
e = it.next();
n = e.getKey().getString();
tmp = e.getValue();
v = Caster.toString(tmp, null);
if (tmp != null && v == null)
v = tmp.toString();
rsp.setHeader(n, v);
}
}
}
// convert result
if (rtn != null && !hasContent) {
Props props = new Props();
props.format = result.getFormat();
Charset cs = getCharset(pc);
if (result.hasFormatExtension()) {
// setFormat(pc.getHttpServletResponse(), props.format,cs);
_writeOut(pc, props, null, rtn, cs, true);
} else {
if (best != null && !MimeType.ALL.same(best)) {
int f = MimeType.toFormat(best, -1);
if (f != -1) {
props.format = f;
// setFormat(pc.getHttpServletResponse(), f,cs);
_writeOut(pc, props, null, rtn, cs, true);
} else {
writeOut(pc, props, rtn, best);
}
} else {
_writeOut(pc, props, null, rtn, cs, true);
}
}
}
}
Aggregations