Search in sources :

Example 6 with UDF

use of lucee.runtime.type.UDF in project Lucee by lucee.

the class ComponentImpl method writeExternal.

public void writeExternal(ObjectOutput out) throws IOException {
    ComponentSpecificAccess cw = new ComponentSpecificAccess(Component.ACCESS_PRIVATE, this);
    Struct _this = new StructImpl();
    Struct _var = new StructImpl();
    // this scope (removing all UDFs)
    Object member;
    {
        Iterator<Entry<Key, Object>> it = cw.entryIterator();
        Entry<Key, Object> e;
        while (it.hasNext()) {
            e = it.next();
            member = e.getValue();
            if (member instanceof UDF)
                continue;
            _this.setEL(e.getKey(), member);
        }
    }
    // variables scope (removing all UDFs and key "this")
    {
        ComponentScope scope = getComponentScope();
        Iterator<Entry<Key, Object>> it = scope.entryIterator();
        Entry<Key, Object> e;
        Key k;
        while (it.hasNext()) {
            e = it.next();
            k = e.getKey();
            if (KeyConstants._THIS.equalsIgnoreCase(k))
                continue;
            member = e.getValue();
            if (member instanceof UDF)
                continue;
            _var.setEL(e.getKey(), member);
        }
    }
    out.writeUTF(getAbsName());
    out.writeUTF(ComponentUtil.md5(cw));
    out.writeObject(_this);
    out.writeObject(_var);
}
Also used : StructImpl(lucee.runtime.type.StructImpl) UDF(lucee.runtime.type.UDF) ComponentIterator(lucee.runtime.type.it.ComponentIterator) ComponentEntryIterator(lucee.runtime.type.cfc.ComponentEntryIterator) StringIterator(lucee.runtime.type.it.StringIterator) Iterator(java.util.Iterator) ComponentValueIterator(lucee.runtime.type.cfc.ComponentValueIterator) ArgumentIntKey(lucee.runtime.type.scope.ArgumentIntKey) Struct(lucee.runtime.type.Struct)

Example 7 with UDF

use of lucee.runtime.type.UDF in project Lucee by lucee.

the class ComponentImpl method metaUDFs.

private static void metaUDFs(PageContext pc, ComponentImpl comp, Struct sct, int access) throws PageException {
    // UDFs
    /*ArrayImpl arr1=new ArrayImpl();
		{
			Page page = comp._getPageSource().loadPage(pc, false);
			// Page page = ((PageSourceImpl)comp._getPageSource()).getPage();
			if(page!=null && page.udfs!=null) {
				for(int i=0;i<page.udfs.length;i++){
					if(page.udfs[i].getAccess()>access) continue;
					print.e(">>"+((UDFPropertiesBase)page.udfs[i]).getFunctionName());
					arr1.append(ComponentUtil.getMetaData(pc,(UDFPropertiesBase) page.udfs[i]));
				}
			}
    	}*/
    ArrayImpl arr = new ArrayImpl();
    if (comp.absFin != null) {
        // we not to add abstract separately because they are not real Methods, more a rule
        if (comp.absFin.hasAbstractUDFs())
            getUDFs(pc, comp.absFin.getAbstractUDFs().values().iterator(), comp, access, arr);
    }
    if (comp._udfs != null)
        getUDFs(pc, comp._udfs.values().iterator(), comp, access, arr);
    // property functions
    Iterator<Entry<Key, UDF>> it = comp._udfs.entrySet().iterator();
    Entry<Key, UDF> entry;
    UDF udf;
    while (it.hasNext()) {
        entry = it.next();
        udf = entry.getValue();
        if (udf.getAccess() > access || !(udf instanceof UDFGSProperty))
            continue;
        if (comp.base != null) {
            if (udf == comp.base.getMember(access, entry.getKey(), true, true))
                continue;
        }
        arr.append(udf.getMetaData(pc));
    }
    if (arr.size() != 0) {
        Collections.sort(arr, new ComparatorImpl());
        sct.set(KeyConstants._functions, arr);
    }
}
Also used : UDF(lucee.runtime.type.UDF) ArrayImpl(lucee.runtime.type.ArrayImpl) UDFGSProperty(lucee.runtime.type.UDFGSProperty) ArgumentIntKey(lucee.runtime.type.scope.ArgumentIntKey)

Example 8 with UDF

use of lucee.runtime.type.UDF in project Lucee by lucee.

the class Props method callRest.

private void callRest(PageContext pc, Component component, String path, Result result, boolean suppressContent) throws IOException, ConverterException {
    String method = pc.getHttpServletRequest().getMethod();
    String[] subPath = result.getPath();
    Struct cMeta;
    try {
        cMeta = component.getMetaData(pc);
    } catch (PageException pe) {
        throw ExceptionUtil.toIOException(pe);
    }
    // Consumes
    MimeType[] cConsumes = null;
    String strMimeType = Caster.toString(cMeta.get(KeyConstants._consumes, null), null);
    if (!StringUtil.isEmpty(strMimeType, true)) {
        cConsumes = MimeType.getInstances(strMimeType, ',');
    }
    // Produces
    MimeType[] cProduces = null;
    strMimeType = Caster.toString(cMeta.get(KeyConstants._produces, null), null);
    if (!StringUtil.isEmpty(strMimeType, true)) {
        cProduces = MimeType.getInstances(strMimeType, ',');
    }
    Iterator<Entry<Key, Object>> it = component.entryIterator();
    Entry<Key, Object> e;
    Object value;
    UDF udf;
    Struct meta;
    int status = 404;
    MimeType bestP, bestC;
    while (it.hasNext()) {
        e = it.next();
        value = e.getValue();
        if (value instanceof UDF) {
            udf = (UDF) value;
            try {
                meta = udf.getMetaData(pc);
                // check if http method match
                String httpMethod = Caster.toString(meta.get(KeyConstants._httpmethod, null), null);
                if (StringUtil.isEmpty(httpMethod) || !httpMethod.equalsIgnoreCase(method))
                    continue;
                // get consumes mimetype
                MimeType[] consumes;
                strMimeType = Caster.toString(meta.get(KeyConstants._consumes, null), null);
                if (!StringUtil.isEmpty(strMimeType, true)) {
                    consumes = MimeType.getInstances(strMimeType, ',');
                } else
                    consumes = cConsumes;
                // get produces mimetype
                MimeType[] produces;
                strMimeType = Caster.toString(meta.get(KeyConstants._produces, null), null);
                if (!StringUtil.isEmpty(strMimeType, true)) {
                    produces = MimeType.getInstances(strMimeType, ',');
                } else
                    produces = cProduces;
                String restPath = Caster.toString(meta.get(KeyConstants._restPath, null), null);
                // no rest path
                if (StringUtil.isEmpty(restPath)) {
                    if (ArrayUtil.isEmpty(subPath)) {
                        bestC = best(consumes, result.getContentType());
                        bestP = best(produces, result.getAccept());
                        if (bestC == null)
                            status = 405;
                        else if (bestP == null)
                            status = 406;
                        else {
                            status = 200;
                            _callRest(pc, component, udf, path, result.getVariables(), result, bestP, produces, suppressContent, e.getKey());
                            break;
                        }
                    }
                } else {
                    Struct var = result.getVariables();
                    int index = RestUtil.matchPath(var, Path.init(restPath), /*TODO cache this*/
                    result.getPath());
                    if (index >= 0 && index + 1 == result.getPath().length) {
                        bestC = best(consumes, result.getContentType());
                        bestP = best(produces, result.getAccept());
                        if (bestC == null)
                            status = 405;
                        else if (bestP == null)
                            status = 406;
                        else {
                            status = 200;
                            _callRest(pc, component, udf, path, var, result, bestP, produces, suppressContent, e.getKey());
                            break;
                        }
                    }
                }
            } catch (PageException pe) {
                pc.getConfig().getLog("rest").error("REST", pe);
            }
        }
    }
    if (status == 404) {
        RestUtil.setStatus(pc, 404, "no rest service for [" + path + "] found");
        pc.getConfig().getLog("rest").error("REST", "404; no rest service for [" + path + "] found");
    } else if (status == 405) {
        RestUtil.setStatus(pc, 405, "Unsupported Media Type");
        pc.getConfig().getLog("rest").error("REST", "405; Unsupported Media Type");
    } else if (status == 406) {
        RestUtil.setStatus(pc, 406, "Not Acceptable");
        pc.getConfig().getLog("rest").error("REST", "406; Not Acceptable");
    }
}
Also used : PageException(lucee.runtime.exp.PageException) MimeType(lucee.commons.lang.mimetype.MimeType) StaticStruct(lucee.runtime.component.StaticStruct) Struct(lucee.runtime.type.Struct) Entry(java.util.Map.Entry) UDF(lucee.runtime.type.UDF) Key(lucee.runtime.type.Collection.Key)

Example 9 with UDF

use of lucee.runtime.type.UDF in project Lucee by lucee.

the class XMLConverter method _serializeComponent.

/**
 * serialize a Component
 * @param component Component to serialize
 * @param done
 * @return serialized component
 * @throws ConverterException
 */
private String _serializeComponent(Component component, Map<Object, String> done) throws ConverterException {
    StringBuilder sb = new StringBuilder();
    Component ca;
    component = new ComponentSpecificAccess(Component.ACCESS_PRIVATE, ca = component);
    boolean isPeristent = ca.isPersistent();
    deep++;
    Object member;
    Iterator<Key> it = component.keyIterator();
    Collection.Key key;
    while (it.hasNext()) {
        key = it.next();
        member = component.get(key, null);
        if (member instanceof UDF)
            continue;
        sb.append(goIn() + "<var scope=\"this\" name=" + del + key.toString() + del + ">");
        sb.append(_serialize(member, done));
        sb.append(goIn() + "</var>");
    }
    Property p;
    Boolean remotingFetch;
    Struct props = ignoreRemotingFetch ? null : ComponentUtil.getPropertiesAsStruct(ca, false);
    ComponentScope scope = ca.getComponentScope();
    it = scope.keyIterator();
    while (it.hasNext()) {
        key = Caster.toKey(it.next(), null);
        if (!ignoreRemotingFetch) {
            p = (Property) props.get(key, null);
            if (p != null) {
                remotingFetch = Caster.toBoolean(p.getDynamicAttributes().get(REMOTING_FETCH, null), null);
                if (remotingFetch == null) {
                    if (isPeristent && ORMUtil.isRelated(p))
                        continue;
                } else if (!remotingFetch.booleanValue())
                    continue;
            }
        }
        member = scope.get(key, null);
        if (member instanceof UDF || key.equals(KeyConstants._this))
            continue;
        sb.append(goIn() + "<var scope=\"variables\" name=" + del + key.toString() + del + ">");
        sb.append(_serialize(member, done));
        sb.append(goIn() + "</var>");
    }
    deep--;
    try {
        // return goIn()+"<struct>"+sb+"</struct>";
        return goIn() + "<component md5=\"" + ComponentUtil.md5(component) + "\" name=\"" + component.getAbsName() + "\">" + sb + "</component>";
    } catch (Exception e) {
        throw toConverterException(e);
    }
}
Also used : PageException(lucee.runtime.exp.PageException) IOException(java.io.IOException) Struct(lucee.runtime.type.Struct) Key(lucee.runtime.type.Collection.Key) ComponentScope(lucee.runtime.ComponentScope) UDF(lucee.runtime.type.UDF) ComponentSpecificAccess(lucee.runtime.ComponentSpecificAccess) Collection(lucee.runtime.type.Collection) Component(lucee.runtime.Component) Property(lucee.runtime.component.Property) Key(lucee.runtime.type.Collection.Key)

Example 10 with UDF

use of lucee.runtime.type.UDF in project Lucee by lucee.

the class ComponentUtil method createMethod.

private static int createMethod(ConstrBytecodeContext constr, java.util.List<LitString> keys, ClassWriter cw, String className, Object member, int max, boolean writeLog, boolean suppressWSbeforeArg, boolean output, boolean returnValue) throws PageException {
    boolean hasOptionalArgs = false;
    if (member instanceof UDF) {
        UDF udf = (UDF) member;
        FunctionArgument[] args = udf.getFunctionArguments();
        Type[] types = new Type[max < 0 ? args.length : max];
        for (int y = 0; y < types.length; y++) {
            types[y] = toType(args[y].getTypeAsString(), true);
            if (!args[y].isRequired())
                hasOptionalArgs = true;
        }
        Type rtnType = toType(udf.getReturnTypeAsString(), true);
        Method method = new Method(udf.getFunctionName(), rtnType, types);
        GeneratorAdapter adapter = new GeneratorAdapter(Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL, method, null, null, cw);
        BytecodeContext bc = new BytecodeContext(null, constr, getPage(constr), keys, cw, className, adapter, method, writeLog, suppressWSbeforeArg, output, returnValue);
        Label start = adapter.newLabel();
        adapter.visitLabel(start);
        // ComponentController.invoke(name, args);
        // name
        adapter.push(udf.getFunctionName());
        // args
        ArrayVisitor av = new ArrayVisitor();
        av.visitBegin(adapter, Types.OBJECT, types.length);
        for (int y = 0; y < types.length; y++) {
            av.visitBeginItem(adapter, y);
            adapter.loadArg(y);
            av.visitEndItem(bc.getAdapter());
        }
        av.visitEnd();
        adapter.invokeStatic(Types.COMPONENT_CONTROLLER, INVOKE);
        adapter.checkCast(rtnType);
        // ASMConstants.NULL(adapter);
        adapter.returnValue();
        Label end = adapter.newLabel();
        adapter.visitLabel(end);
        for (int y = 0; y < types.length; y++) {
            adapter.visitLocalVariable(args[y].getName().getString(), types[y].getDescriptor(), null, start, end, y + 1);
        }
        adapter.endMethod();
        if (hasOptionalArgs) {
            if (max == -1)
                max = args.length - 1;
            else
                max--;
            return max;
        }
    }
    return -1;
}
Also used : Type(org.objectweb.asm.Type) UDF(lucee.runtime.type.UDF) Label(org.objectweb.asm.Label) GeneratorAdapter(org.objectweb.asm.commons.GeneratorAdapter) Method(org.objectweb.asm.commons.Method) ArrayVisitor(lucee.transformer.bytecode.visitor.ArrayVisitor) FunctionArgument(lucee.runtime.type.FunctionArgument) BytecodeContext(lucee.transformer.bytecode.BytecodeContext) ConstrBytecodeContext(lucee.transformer.bytecode.ConstrBytecodeContext)

Aggregations

UDF (lucee.runtime.type.UDF)42 Key (lucee.runtime.type.Collection.Key)14 Struct (lucee.runtime.type.Struct)14 Component (lucee.runtime.Component)9 Entry (java.util.Map.Entry)7 PageException (lucee.runtime.exp.PageException)7 Member (lucee.runtime.component.Member)6 FunctionArgument (lucee.runtime.type.FunctionArgument)6 StructImpl (lucee.runtime.type.StructImpl)6 ArgumentIntKey (lucee.runtime.type.scope.ArgumentIntKey)6 Iterator (java.util.Iterator)5 ComponentScope (lucee.runtime.ComponentScope)5 ComponentSpecificAccess (lucee.runtime.ComponentSpecificAccess)5 PageContextImpl (lucee.runtime.PageContextImpl)5 Property (lucee.runtime.component.Property)5 Array (lucee.runtime.type.Array)5 Collection (lucee.runtime.type.Collection)5 IOException (java.io.IOException)4 Map (java.util.Map)4 HashMap (java.util.HashMap)3