Search in sources :

Example 31 with NativeJavaObject

use of org.mozilla.javascript.NativeJavaObject in project convertigo by convertigo.

the class HTTPStatement method makeQuery.

private String makeQuery(com.twinsoft.convertigo.engine.Context context, String methodToAnalyse) throws EngineException {
    String variable, httpVariable, httpVariableValue, method, query = "";
    int len = numberOfVariables();
    String urlEncodingCharset = getUrlEncodingCharset();
    if (urlEncodingCharset == null || urlEncodingCharset.length() == 0) {
        urlEncodingCharset = getParentTransaction().getComputedUrlEncodingCharset();
    }
    try {
        for (int i = 0; i < len; i++) {
            HttpStatementVariable httpStatementVariable = (HttpStatementVariable) getVariable(i);
            if (httpStatementVariable != null) {
                variable = httpStatementVariable.getName();
                method = httpStatementVariable.getHttpMethod();
                httpVariable = httpStatementVariable.getHttpName();
                if (method.equals(methodToAnalyse)) {
                    if (query.length() != 0) {
                        query += "&";
                    }
                    try {
                        // evaluate method can throw EngineException
                        // try catch to get the default value in this case
                        evaluate(javascriptContext, scope, variable, httpVariable, false);
                        Engine.logBeans.debug("Javascript evaluation of httpVariable named '" + httpVariable + "' executed");
                        // if no Engine Exception has been thown until here, normal execution
                        if (evaluated != null) {
                            if (evaluated instanceof NativeJavaArray) {
                                Object object = ((NativeJavaArray) evaluated).unwrap();
                                List<Object> list = Arrays.asList((Object[]) object);
                                for (int j = 0; j < list.size(); j++) {
                                    Object item = list.get(j);
                                    httpVariableValue = item.toString();
                                    query = addVariableToQuery(methodToAnalyse, httpVariable, httpVariableValue, query, urlEncodingCharset, j != 0);
                                }
                            } else if (evaluated instanceof NativeJavaObject) {
                                NativeJavaObject nativeJavaObject = (NativeJavaObject) evaluated;
                                Object javaObject = nativeJavaObject.unwrap();
                                if (javaObject instanceof List) {
                                    Vector<String> v = GenericUtils.cast(javaObject);
                                    for (int j = 0; j < v.size(); j++) {
                                        httpVariableValue = v.get(j);
                                        query = addVariableToQuery(methodToAnalyse, httpVariable, httpVariableValue, query, urlEncodingCharset, j != 0);
                                    }
                                } else {
                                    httpVariableValue = (String) nativeJavaObject.getDefaultValue(String.class);
                                    query = addVariableToQuery(methodToAnalyse, httpVariable, httpVariableValue, query, urlEncodingCharset, false);
                                }
                            } else if (evaluated instanceof NativeArray) {
                                NativeArray array = (NativeArray) evaluated;
                                for (int j = 0; j < array.getLength(); j++) {
                                    Object item = array.get(j, array);
                                    httpVariableValue = item.toString();
                                    query = addVariableToQuery(methodToAnalyse, httpVariable, httpVariableValue, query, urlEncodingCharset, j != 0);
                                }
                            } else if (evaluated instanceof List) {
                                Vector<String> v = GenericUtils.cast(evaluated);
                                for (int j = 0; j < v.size(); j++) {
                                    httpVariableValue = v.get(j);
                                    query = addVariableToQuery(methodToAnalyse, httpVariable, httpVariableValue, query, urlEncodingCharset, j != 0);
                                }
                            } else if (evaluated instanceof Undefined) {
                                throw new EngineException("Undefined");
                            } else {
                                httpVariableValue = evaluated.toString();
                                query = addVariableToQuery(methodToAnalyse, httpVariable, httpVariableValue, query, urlEncodingCharset, false);
                            }
                        }
                    } catch (EngineException e) {
                        // Engine Exception has been thrown ==> get variable default value
                        Object value = getVariableValue(variable);
                        if (value != null) {
                            if (value instanceof Collection) {
                                List<String> list = GenericUtils.toString((Collection<?>) value);
                                for (String val : list) {
                                    httpVariableValue = val;
                                    query = addVariableToQuery(methodToAnalyse, httpVariable, httpVariableValue, query, urlEncodingCharset, false);
                                }
                            } else {
                                httpVariableValue = value.toString();
                                query = addVariableToQuery(methodToAnalyse, httpVariable, httpVariableValue, query, urlEncodingCharset, false);
                            }
                        }
                    }
                }
            }
        }
    } catch (UnsupportedEncodingException e) {
        throw new EngineException("UTF-8 encoding is not supported.", e);
    }
    return query;
}
Also used : NativeArray(org.mozilla.javascript.NativeArray) Undefined(org.mozilla.javascript.Undefined) EngineException(com.twinsoft.convertigo.engine.EngineException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) HttpStatementVariable(com.twinsoft.convertigo.beans.variables.HttpStatementVariable) NativeJavaArray(org.mozilla.javascript.NativeJavaArray) Collection(java.util.Collection) DatabaseObject(com.twinsoft.convertigo.beans.core.DatabaseObject) NativeJavaObject(org.mozilla.javascript.NativeJavaObject) ArrayList(java.util.ArrayList) NodeList(org.w3c.dom.NodeList) List(java.util.List) NativeJavaObject(org.mozilla.javascript.NativeJavaObject) XMLVector(com.twinsoft.convertigo.beans.common.XMLVector) Vector(java.util.Vector)

Example 32 with NativeJavaObject

use of org.mozilla.javascript.NativeJavaObject in project convertigo by convertigo.

the class RequestableObject method includeInScope.

public static Object includeInScope(org.mozilla.javascript.Context cx, Scriptable thisObj, Object[] args, Function funObj) {
    Object res = Scriptable.NOT_FOUND;
    if (args.length < 1) {
        return res;
    }
    String path = (String) args[0];
    Context context = (Context) ((NativeJavaObject) thisObj.get("context", thisObj)).unwrap();
    Project project = context.project;
    File js = new File(project.getDirPath(), path);
    if (js.exists()) {
        try {
            res = RhinoUtils.evalCachedJavascript(cx, thisObj, FileUtils.readFileToString(js, "UTF-8"), path, 1, null);
        } catch (IOException e) {
            Engine.logBeans.warn("Cannot include '" + js + "' because of a read failure!", e);
        }
    } else {
        Engine.logBeans.warn("Cannot include '" + js + "' because it doesn't exist!");
    }
    return res;
}
Also used : Context(com.twinsoft.convertigo.engine.Context) NativeJavaObject(org.mozilla.javascript.NativeJavaObject) FunctionObject(org.mozilla.javascript.FunctionObject) IOException(java.io.IOException) File(java.io.File)

Example 33 with NativeJavaObject

use of org.mozilla.javascript.NativeJavaObject in project convertigo by convertigo.

the class RequestableObject method useInScope.

public static Object useInScope(org.mozilla.javascript.Context cx, Scriptable thisObj, Object[] args, Function funObj) {
    if (args.length < 1) {
        return null;
    }
    String key = (String) args[0];
    String mapkey = "__convertigo_use_" + key;
    Scriptable ctx = (Scriptable) thisObj.get("context", thisObj);
    Project project = (Project) ((NativeJavaObject) ctx.get("project", ctx)).unwrap();
    Object res = project.get(mapkey);
    if (res == null) {
        try {
            res = RhinoUtils.evalCachedJavascript(cx, thisObj, key, "use", 1, null);
            project.set(mapkey, res);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return res;
}
Also used : NativeJavaObject(org.mozilla.javascript.NativeJavaObject) FunctionObject(org.mozilla.javascript.FunctionObject) Scriptable(org.mozilla.javascript.Scriptable) NoSuchElementException(java.util.NoSuchElementException) StepException(com.twinsoft.convertigo.beans.steps.StepException) IOException(java.io.IOException) EngineException(com.twinsoft.convertigo.engine.EngineException)

Example 34 with NativeJavaObject

use of org.mozilla.javascript.NativeJavaObject in project convertigo by convertigo.

the class RequestableObject method synchronize.

public static Object synchronize(org.mozilla.javascript.Context cx, Scriptable thisObj, Object[] args, Function funObj) {
    if (args.length < 2) {
        return null;
    }
    Object lock = ((NativeJavaObject) args[0]).unwrap();
    Function fun = (Function) args[1];
    Object res;
    synchronized (lock) {
        res = fun.call(cx, thisObj, thisObj, null);
    }
    return res;
}
Also used : Function(org.mozilla.javascript.Function) NativeJavaObject(org.mozilla.javascript.NativeJavaObject) FunctionObject(org.mozilla.javascript.FunctionObject) NativeJavaObject(org.mozilla.javascript.NativeJavaObject)

Example 35 with NativeJavaObject

use of org.mozilla.javascript.NativeJavaObject in project servoy-client by Servoy.

the class WebServiceScriptable method compileServerScript.

/**
 * Compiles the server side script, enabled debugging if possible.
 * It returns the $scope object
 */
public static Scriptable compileServerScript(URL serverScript, Scriptable model, INGApplication app) {
    Scriptable scopeObject = null;
    Context context = Context.enter();
    try {
        Scriptable topLevel = ScriptableObject.getTopLevelScope(model);
        if (topLevel == null) {
            // This should not really happen anymore.
            Debug.log("toplevel object not found for creating serverside script: " + serverScript);
            topLevel = context.initStandardObjects();
        }
        Scriptable apiObject = null;
        Scriptable execScope = context.newObject(topLevel);
        execScope.setParentScope(topLevel);
        scopeObject = context.newObject(execScope);
        apiObject = context.newObject(execScope);
        scopeObject.put("api", scopeObject, apiObject);
        scopeObject.put("model", scopeObject, model);
        execScope.put("$scope", execScope, scopeObject);
        execScope.put("console", execScope, new NativeJavaObject(execScope, new ConsoleObject(app), ScriptObjectRegistry.getJavaMembers(ConsoleObject.class, execScope)));
        execScope.put("servoyApi", execScope, new NativeJavaObject(execScope, new ServoyApiObject(app), ScriptObjectRegistry.getJavaMembers(ServoyApiObject.class, execScope)));
        getScript(context, serverScript, app).exec(context, execScope);
        apiObject.setPrototype(model);
    } catch (Exception ex) {
        Debug.error("error creating server side scripting object: " + serverScript, ex);
    } finally {
        Context.exit();
    }
    return scopeObject;
}
Also used : IWebObjectContext(org.sablo.IWebObjectContext) Context(org.mozilla.javascript.Context) Scriptable(org.mozilla.javascript.Scriptable) NativeJavaObject(org.mozilla.javascript.NativeJavaObject) URISyntaxException(java.net.URISyntaxException) JSONException(org.json.JSONException) IOException(java.io.IOException)

Aggregations

NativeJavaObject (org.mozilla.javascript.NativeJavaObject)45 Scriptable (org.mozilla.javascript.Scriptable)16 Context (org.mozilla.javascript.Context)11 NativeArray (org.mozilla.javascript.NativeArray)9 ScriptableObject (org.mozilla.javascript.ScriptableObject)9 NativeJavaArray (org.mozilla.javascript.NativeJavaArray)8 ArrayList (java.util.ArrayList)7 List (java.util.List)6 IOException (java.io.IOException)5 Test (org.junit.Test)4 NativeObject (org.mozilla.javascript.NativeObject)4 Undefined (org.mozilla.javascript.Undefined)4 NodeList (org.w3c.dom.NodeList)4 IScriptableProvider (com.servoy.j2db.scripting.IScriptableProvider)3 SolutionScope (com.servoy.j2db.scripting.SolutionScope)3 EngineException (com.twinsoft.convertigo.engine.EngineException)3 Collection (java.util.Collection)3 Vector (java.util.Vector)3 FunctionObject (org.mozilla.javascript.FunctionObject)3 Node (org.w3c.dom.Node)3