Search in sources :

Example 1 with ExitScriptException

use of com.servoy.j2db.ExitScriptException in project servoy-client by Servoy.

the class InlineScriptExecutorBehavior method respond.

@Override
protected void respond(AjaxRequestTarget target) {
    Page page = component.getPage();
    String scriptName = RequestCycle.get().getRequest().getParameter("snenc");
    ICrypt urlCrypt = Application.get().getSecuritySettings().getCryptFactory().newCrypt();
    scriptName = urlCrypt.decryptUrlSafe(scriptName).replace("\\\"", "\"");
    String argValue;
    for (String browserArgument : getBrowserArguments(scriptName)) {
        argValue = RequestCycle.get().getRequest().getParameter(browserArgument);
        if (argValue == null)
            argValue = "";
        boolean isString = true;
        try {
            Double.parseDouble(argValue);
            isString = false;
        } catch (NumberFormatException ex) {
        }
        if (isString && ("true".equals(argValue) || "false".equals(argValue)))
            isString = false;
        String browserParamWithArgument = BROWSER_PARAM + browserArgument;
        // make sure we ignore user quoting if isString
        if (isString) {
            scriptName = scriptName.replace("\"" + browserParamWithArgument + "\"", browserParamWithArgument);
            scriptName = scriptName.replace("'" + browserParamWithArgument + "'", browserParamWithArgument);
        }
        scriptName = scriptName.replace(browserParamWithArgument, isString ? "'" + argValue + "'" : argValue);
    }
    WebForm wf = component.findParent(WebForm.class);
    if (wf != null) {
        try {
            wf.getController().eval(scriptName);
        } catch (Exception e) {
            if (!(e instanceof ExitScriptException)) {
                // $NON-NLS-1$
                Debug.error("Exception evaluating: " + scriptName, e);
            }
        }
        WebEventExecutor.generateResponse(target, page);
    }
    // $NON-NLS-1$ //$NON-NLS-2$
    target.appendJavascript("clearDoubleClickId('" + component.getMarkupId() + "')");
}
Also used : ICrypt(org.apache.wicket.util.crypt.ICrypt) WebForm(com.servoy.j2db.server.headlessclient.WebForm) Page(org.apache.wicket.Page) ExitScriptException(com.servoy.j2db.ExitScriptException) ExitScriptException(com.servoy.j2db.ExitScriptException)

Example 2 with ExitScriptException

use of com.servoy.j2db.ExitScriptException in project servoy-client by Servoy.

the class ScriptEngine method eval.

public Object eval(Scriptable scope, String eval_string) {
    String userUidBefore = null;
    if (Context.getCurrentContext() == null) {
        userUidBefore = application.getClientInfo().getUserUid();
    }
    Context cx = Context.enter();
    try {
        Object o = null;
        // $NON-NLS-1$ //$NON-NLS-2$
        Function compileFunction = cx.compileFunction(scope, "function evalFunction(){}", "evalFunction", 0, null);
        if (compileFunction instanceof FunctionWrapper)
            compileFunction = ((FunctionWrapper) compileFunction).getWrappedFunction();
        if (compileFunction instanceof NativeFunction) {
            o = // $NON-NLS-1$
            cx.evaluateString(// $NON-NLS-1$
            ScriptRuntime.createFunctionActivation((NativeFunction) compileFunction, scope, null), // $NON-NLS-1$
            eval_string, // $NON-NLS-1$
            "internal_anon", // $NON-NLS-1$
            1, null);
        } else {
            // $NON-NLS-1$
            o = cx.evaluateString(scope, eval_string, "internal_anon", 1, null);
        }
        if (o instanceof Wrapper) {
            o = ((Wrapper) o).unwrap();
        }
        if (o == Scriptable.NOT_FOUND || o == Undefined.instance) {
            o = null;
        }
        return o;
    } catch (Exception ex) {
        if (ex instanceof ExitScriptException) {
            throw (ExitScriptException) ex;
        } else if (ex.getCause() instanceof ExitScriptException) {
            throw (ExitScriptException) ex.getCause();
        } else if (ex instanceof JavaScriptException && ((JavaScriptException) ex).getValue() instanceof ExitScriptException) {
            throw (ExitScriptException) ((JavaScriptException) ex).getValue();
        } else if (application.getSolution() != null) {
            // $NON-NLS-1$//$NON-NLS-2$
            application.reportError(application.getI18NMessage("servoy.formPanel.error.evalString") + eval_string + "'", ex);
        } else {
            // $NON-NLS-1$
            Debug.trace("Solution already closed, ignoring exception", ex);
        }
    } finally {
        Context.exit();
        testClientUidChange(scope, userUidBefore);
    }
    return null;
}
Also used : Context(org.mozilla.javascript.Context) NativeFunction(org.mozilla.javascript.NativeFunction) Function(org.mozilla.javascript.Function) RenderableWrapper(com.servoy.j2db.ui.RenderableWrapper) Wrapper(org.mozilla.javascript.Wrapper) DataRendererOnRenderWrapper(com.servoy.j2db.ui.DataRendererOnRenderWrapper) NativeFunction(org.mozilla.javascript.NativeFunction) NativeJavaObject(org.mozilla.javascript.NativeJavaObject) ApplicationException(com.servoy.j2db.ApplicationException) ExitScriptException(com.servoy.j2db.ExitScriptException) DataException(com.servoy.j2db.dataprocessing.DataException) ServoyException(com.servoy.j2db.util.ServoyException) JavaScriptException(org.mozilla.javascript.JavaScriptException) RepositoryException(com.servoy.j2db.persistence.RepositoryException) ExitScriptException(com.servoy.j2db.ExitScriptException) JavaScriptException(org.mozilla.javascript.JavaScriptException)

Example 3 with ExitScriptException

use of com.servoy.j2db.ExitScriptException in project servoy-client by Servoy.

the class ServoyWrapFactory method wrap.

/**
 * @see org.mozilla.javascript.WrapFactory#wrap(org.mozilla.javascript.Context, org.mozilla.javascript.Scriptable, java.lang.Object, java.lang.Class)
 */
@Override
public Object wrap(Context cx, Scriptable scope, Object obj, Class staticType) {
    if (application.getSolution() == null) {
        throw new ExitScriptException("killing current script, client/solution already terminated");
    }
    if (obj == null || obj == Undefined.instance || obj instanceof Scriptable || obj instanceof String || obj instanceof CharSequence || obj instanceof Number || obj instanceof Boolean) {
        return obj;
    }
    if (obj instanceof Date) {
        return cx.newObject(scope, "Date", new Object[] { new Double(NativeDate.convertToUTCMillisFromJava(((Date) obj).getTime())) });
    }
    if (obj instanceof DbIdentValue || obj instanceof UUID) {
        return new NativeJavaObject(scope, obj, ScriptObjectRegistry.getJavaMembers(obj.getClass(), null));
    }
    if (cx != null) {
        if (obj instanceof JSConvertedMap<?, ?>) {
            Scriptable newObject = null;
            JSConvertedMap<?, ?> map = (JSConvertedMap<?, ?>) obj;
            if (map.getConstructorName() != null) {
                newObject = cx.newObject(scope, map.getConstructorName());
            } else {
                newObject = cx.newObject(scope);
            }
            Iterator<?> iterator = map.entrySet().iterator();
            while (iterator.hasNext()) {
                Map.Entry<?, ?> next = (Entry<?, ?>) iterator.next();
                Object key = next.getKey();
                Object value = next.getValue();
                if (value != null) {
                    value = wrap(cx, newObject, value, value.getClass());
                }
                if (key instanceof Integer) {
                    newObject.put(((Integer) key).intValue(), newObject, value);
                } else if (key instanceof String) {
                    newObject.put((String) key, newObject, value);
                } else {
                    // $NON-NLS-1$ //$NON-NLS-2$
                    Debug.error("Try to create a JSConvertedMap->NativeObject with a key that isnt a string or integer:" + key + " for value: " + value);
                }
            }
            return newObject;
        }
        if (obj.getClass() == JSONObject.class) {
            JSONObject json = (JSONObject) obj;
            Scriptable newObject = cx.newObject(scope);
            Iterator<String> iterator = json.keys();
            while (iterator.hasNext()) {
                String key = iterator.next();
                Object value = null;
                try {
                    value = ServoyJSONObject.jsonNullToNull(json.get(key));
                } catch (JSONException e) {
                    Debug.error(e);
                }
                if (value != null) {
                    value = wrap(cx, newObject, value, value.getClass());
                }
                newObject.put(key, newObject, value);
            }
            return newObject;
        }
        if (obj.getClass() == JSONArray.class) {
            JSONArray array = (JSONArray) obj;
            Scriptable newObject = cx.newObject(scope, "Array");
            for (int i = 0; i < array.length(); i++) {
                Object value = null;
                try {
                    value = array.get(i);
                } catch (JSONException e) {
                    Debug.error(e);
                }
                if (value != null) {
                    value = wrap(cx, newObject, value, value.getClass());
                }
                newObject.put(i, newObject, value);
            }
            return newObject;
        }
    }
    if (obj instanceof IDataSet) {
        return new JSDataSet(application, (IDataSet) obj);
    }
    if (obj instanceof FormController) {
        throw new IllegalArgumentException(// $NON-NLS-1$
        "Bad practice: FormController cant be wrapped to javascript (for example not usable as argument in Scheduler plugin)");
    }
    // if it is a none primitive array
    if (obj.getClass().isArray() && !obj.getClass().getComponentType().isPrimitive()) {
        // then convert the array to a NativeArray, first convert all the elements of the array itself.
        Object[] source = (Object[]) obj;
        Object[] array = new Object[source.length];
        for (int i = 0; i < source.length; i++) {
            Object src = source[i];
            array[i] = wrap(cx, scope, src, src != null ? src.getClass() : null);
        }
        return cx.newArray(scope, array);
    }
    return super.wrap(cx, scope, obj, staticType);
}
Also used : JSDataSet(com.servoy.j2db.dataprocessing.JSDataSet) Entry(java.util.Map.Entry) DbIdentValue(com.servoy.j2db.dataprocessing.ValueFactory.DbIdentValue) UUID(com.servoy.j2db.util.UUID) NativeJavaObject(org.mozilla.javascript.NativeJavaObject) FormController(com.servoy.j2db.FormController) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) Scriptable(org.mozilla.javascript.Scriptable) ExitScriptException(com.servoy.j2db.ExitScriptException) Date(java.util.Date) NativeDate(org.mozilla.javascript.NativeDate) JSONObject(org.json.JSONObject) ServoyJSONObject(com.servoy.j2db.util.ServoyJSONObject) NativeJavaObject(org.mozilla.javascript.NativeJavaObject) JSONObject(org.json.JSONObject) ServoyJSONObject(com.servoy.j2db.util.ServoyJSONObject) IDataSet(com.servoy.j2db.dataprocessing.IDataSet) Map(java.util.Map)

Example 4 with ExitScriptException

use of com.servoy.j2db.ExitScriptException in project servoy-client by Servoy.

the class FormScope method get.

@Override
public Object get(String name, Scriptable start) {
    if (_fp == null) {
        Debug.warn("Error accessing a form " + formName + "  that is already destroyed for getting: " + name);
        throw new ExitScriptException("killing current script, client/solution already terminated");
    }
    _fp.touch();
    if (// $NON-NLS-1$
    "alldataproviders".equals(name)) {
        List<String> al = new ArrayList<String>();
        Table table = (Table) _fp.getTable();
        if (table != null) {
            al = getDataproviderIdList(table);
        }
        return new NativeJavaArray(this, al.toArray(new String[al.size()]));
    }
    if (// $NON-NLS-1$
    "allmethods".equals(name)) {
        List<String> al = new ArrayList<String>();
        Iterator<ScriptMethod> it = _fp.getForm().getScriptMethods(true);
        while (it.hasNext()) {
            ScriptMethod sm = it.next();
            al.add(sm.getName());
        }
        return new NativeJavaArray(this, al.toArray(new String[al.size()]));
    }
    if (// $NON-NLS-1$
    "allrelations".equals(name)) {
        List<String> al = getFormRelationsIdList(_fp.getForm());
        return new NativeJavaArray(this, al.toArray(new String[al.size()]));
    }
    if (// $NON-NLS-1$
    "allvariables".equals(name)) {
        List<String> al = getAllVariablesIdList(_fp.getForm());
        return new NativeJavaArray(this, al.toArray(new String[al.size()]));
    }
    Object object = super.get(name, start);
    if ((object == null || object == Scriptable.NOT_FOUND) && ("foundset".equals(name) || "elements".equals(name))) {
        Debug.error(Thread.currentThread().getName() + ": For form " + _fp + " the foundset/elements were asked for but that was not (or was no longer) set. ", new RuntimeException());
        if (name.equals("foundset"))
            return _fp.getFormModel();
    }
    return object;
}
Also used : Table(com.servoy.j2db.persistence.Table) ArrayList(java.util.ArrayList) NativeJavaArray(org.mozilla.javascript.NativeJavaArray) ScriptMethod(com.servoy.j2db.persistence.ScriptMethod) ExitScriptException(com.servoy.j2db.ExitScriptException)

Aggregations

ExitScriptException (com.servoy.j2db.ExitScriptException)4 NativeJavaObject (org.mozilla.javascript.NativeJavaObject)2 ApplicationException (com.servoy.j2db.ApplicationException)1 FormController (com.servoy.j2db.FormController)1 DataException (com.servoy.j2db.dataprocessing.DataException)1 IDataSet (com.servoy.j2db.dataprocessing.IDataSet)1 JSDataSet (com.servoy.j2db.dataprocessing.JSDataSet)1 DbIdentValue (com.servoy.j2db.dataprocessing.ValueFactory.DbIdentValue)1 RepositoryException (com.servoy.j2db.persistence.RepositoryException)1 ScriptMethod (com.servoy.j2db.persistence.ScriptMethod)1 Table (com.servoy.j2db.persistence.Table)1 WebForm (com.servoy.j2db.server.headlessclient.WebForm)1 DataRendererOnRenderWrapper (com.servoy.j2db.ui.DataRendererOnRenderWrapper)1 RenderableWrapper (com.servoy.j2db.ui.RenderableWrapper)1 ServoyException (com.servoy.j2db.util.ServoyException)1 ServoyJSONObject (com.servoy.j2db.util.ServoyJSONObject)1 UUID (com.servoy.j2db.util.UUID)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 Map (java.util.Map)1