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;
}
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;
}
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;
}
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;
}
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;
}
Aggregations