Search in sources :

Example 1 with JSONExpressionInterpreter

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;
}
Also used : WDDXConverter(lucee.runtime.converter.WDDXConverter) PageException(lucee.runtime.exp.PageException) InputSource(org.xml.sax.InputSource) JSONExpressionInterpreter(lucee.runtime.interpreter.JSONExpressionInterpreter) ByteArrayInputStream(java.io.ByteArrayInputStream) CFMLExpressionInterpreter(lucee.runtime.interpreter.CFMLExpressionInterpreter) PageException(lucee.runtime.exp.PageException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 2 with JSONExpressionInterpreter

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);
    }
}
Also used : ConverterException(lucee.runtime.converter.ConverterException) HashMap(java.util.HashMap) JSONExpressionInterpreter(lucee.runtime.interpreter.JSONExpressionInterpreter) HTTPResponse(lucee.commons.net.http.HTTPResponse) Charset(java.nio.charset.Charset) PageContext(lucee.runtime.PageContext) ThreadLocalPageContext(lucee.runtime.engine.ThreadLocalPageContext) IOException(java.io.IOException) JSONConverter(lucee.runtime.converter.JSONConverter)

Example 3 with JSONExpressionInterpreter

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;
    }
}
Also used : PageException(lucee.runtime.exp.PageException) JSONExpressionInterpreter(lucee.runtime.interpreter.JSONExpressionInterpreter) Charset(java.nio.charset.Charset) CFMLExpressionInterpreter(lucee.runtime.interpreter.CFMLExpressionInterpreter) MimeType(lucee.commons.lang.mimetype.MimeType) StaticStruct(lucee.runtime.component.StaticStruct) Struct(lucee.runtime.type.Struct)

Example 4 with JSONExpressionInterpreter

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);
    }
}
Also used : PageException(lucee.runtime.exp.PageException) RefBoolean(lucee.commons.lang.types.RefBoolean) ArrayImpl(lucee.runtime.type.ArrayImpl) PageContextImpl(lucee.runtime.PageContextImpl) MimeType(lucee.commons.lang.mimetype.MimeType) PageException(lucee.runtime.exp.PageException) MissingIncludeException(lucee.runtime.exp.MissingIncludeException) IOException(java.io.IOException) Struct(lucee.runtime.type.Struct) PageSource(lucee.runtime.PageSource) Array(lucee.runtime.type.Array) Entry(java.util.Map.Entry) JSONExpressionInterpreter(lucee.runtime.interpreter.JSONExpressionInterpreter) Component(lucee.runtime.Component) RefBooleanImpl(lucee.commons.lang.types.RefBooleanImpl) Key(lucee.runtime.type.Collection.Key)

Aggregations

JSONExpressionInterpreter (lucee.runtime.interpreter.JSONExpressionInterpreter)4 PageException (lucee.runtime.exp.PageException)3 IOException (java.io.IOException)2 Charset (java.nio.charset.Charset)2 MimeType (lucee.commons.lang.mimetype.MimeType)2 CFMLExpressionInterpreter (lucee.runtime.interpreter.CFMLExpressionInterpreter)2 Struct (lucee.runtime.type.Struct)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 HashMap (java.util.HashMap)1 Entry (java.util.Map.Entry)1 RefBoolean (lucee.commons.lang.types.RefBoolean)1 RefBooleanImpl (lucee.commons.lang.types.RefBooleanImpl)1 HTTPResponse (lucee.commons.net.http.HTTPResponse)1 Component (lucee.runtime.Component)1 PageContext (lucee.runtime.PageContext)1 PageContextImpl (lucee.runtime.PageContextImpl)1 PageSource (lucee.runtime.PageSource)1 StaticStruct (lucee.runtime.component.StaticStruct)1 ConverterException (lucee.runtime.converter.ConverterException)1