use of org.structr.core.GraphObject in project structr by structr.
the class Scripting method evaluateJavascript.
public static Object evaluateJavascript(final ActionContext actionContext, final GraphObject entity, final Snippet snippet) throws FrameworkException {
final String entityName = entity != null ? entity.getProperty(AbstractNode.name) : null;
final String entityDescription = entity != null ? (StringUtils.isNotBlank(entityName) ? "\"" + entityName + "\":" : "") + entity.getUuid() : "anonymous";
final Context scriptingContext = Scripting.setupJavascriptContext();
try {
// enable some optimizations..
scriptingContext.setLanguageVersion(Context.VERSION_1_2);
scriptingContext.setOptimizationLevel(9);
scriptingContext.setInstructionObserverThreshold(0);
scriptingContext.setGenerateObserverCount(false);
scriptingContext.setGeneratingDebug(true);
final Scriptable scope = scriptingContext.initStandardObjects();
final StructrScriptable scriptable = new StructrScriptable(actionContext, entity, scriptingContext);
scriptable.setParentScope(scope);
// register Structr scriptable
scope.put("Structr", scope, scriptable);
// clear output buffer
actionContext.clear();
// compile or use provided script
Script compiledScript = snippet.getCompiledScript();
if (compiledScript == null) {
final String sourceLocation = snippet.getName() + " [" + entityDescription + "], line ";
final String embeddedSourceCode = embedInFunction(actionContext, snippet.getSource());
compiledScript = compileOrGetCached(scriptingContext, embeddedSourceCode, sourceLocation, 1);
}
Object extractedValue = compiledScript.exec(scriptingContext, scope);
if (scriptable.hasException()) {
throw scriptable.getException();
}
// prioritize written output over result returned from method
final String output = actionContext.getOutput();
if (output != null && !output.isEmpty()) {
extractedValue = output;
}
if (extractedValue == null || extractedValue == Undefined.instance) {
extractedValue = scriptable.unwrap(scope.get("_structrMainResult", scope));
}
if (extractedValue == null || extractedValue == Undefined.instance) {
extractedValue = "";
}
return extractedValue;
} catch (final FrameworkException fex) {
fex.printStackTrace();
// just throw the FrameworkException so we dont lose the information contained
throw fex;
} catch (final Throwable t) {
t.printStackTrace();
// if any other kind of Throwable is encountered throw a new FrameworkException and be done with it
throw new FrameworkException(422, t.getMessage());
} finally {
Scripting.destroyJavascriptContext();
}
}
use of org.structr.core.GraphObject in project structr by structr.
the class StructrScriptEngine method eval.
@Override
public Object eval(final String script, final ScriptContext context) throws ScriptException {
try {
final ActionContext actionContext = (ActionContext) get("_actionContext");
final GraphObject entity = (GraphObject) get("_entity");
return Functions.evaluate(actionContext, entity, script);
} catch (UnlicensedException | FrameworkException fex) {
// wrap FrameworkException in ScriptException and re-throw
throw new ScriptException(fex);
}
}
use of org.structr.core.GraphObject in project structr by structr.
the class Actions method execute.
public static Object execute(final SecurityContext securityContext, final GraphObject entity, final String source, final Map<String, Object> parameters, final String methodName) throws FrameworkException, UnlicensedException {
final ActionContext context = new ActionContext(securityContext, parameters);
final Object result = Scripting.evaluate(context, entity, source, methodName);
// check for errors raised by scripting
if (context.hasError()) {
throw new FrameworkException(422, "Server-side scripting error", context.getErrorBuffer());
}
return result;
}
use of org.structr.core.GraphObject in project structr by structr.
the class Function method toGraphObject.
public static Object toGraphObject(final Object sourceObject, final Integer outputDepth) {
if (sourceObject instanceof GraphObject) {
return sourceObject;
} else if (sourceObject instanceof List) {
final List list = (List) sourceObject;
final List<GraphObject> res = new ArrayList<>();
for (final Object o : list) {
if (o instanceof Map) {
final GraphObjectMap newObj = new GraphObjectMap();
Function.recursivelyConvertMapToGraphObjectMap(newObj, (Map) o, outputDepth);
res.add(newObj);
} else if (o instanceof GraphObject) {
res.add((GraphObject) o);
} else if (o instanceof String) {
res.add(Function.wrapStringInGraphObjectMap((String) o));
}
}
return res;
} else if (sourceObject instanceof Map) {
final GraphObjectMap map = new GraphObjectMap();
Function.recursivelyConvertMapToGraphObjectMap(map, (Map) sourceObject, outputDepth);
return map;
} else if (sourceObject instanceof String[]) {
final List<GraphObject> res = new ArrayList<>();
for (final String s : (String[]) sourceObject) {
res.add(Function.wrapStringInGraphObjectMap(s));
}
return res;
} else if (sourceObject instanceof String) {
return Function.wrapStringInGraphObjectMap((String) sourceObject);
}
return null;
}
use of org.structr.core.GraphObject in project structr by structr.
the class RelationshipResource method doGet.
@Override
public Result doGet(final PropertyKey sortKey, final boolean sortDescending, final int pageSize, final int page) throws FrameworkException {
// fetch all results, paging is applied later
final List<? extends GraphObject> results = wrappedResource.doGet(null, false, NodeFactory.DEFAULT_PAGE_SIZE, NodeFactory.DEFAULT_PAGE).getResults();
final App app = StructrApp.getInstance();
if (results != null && !results.isEmpty()) {
try {
final List<GraphObject> resultList = new LinkedList<>();
for (GraphObject obj : results) {
if (obj instanceof AbstractNode) {
final List<? extends RelationshipInterface> relationships = Direction.INCOMING.equals(direction) ? Iterables.toList(((AbstractNode) obj).getIncomingRelationships()) : Iterables.toList(((AbstractNode) obj).getOutgoingRelationships());
if (relationships != null) {
boolean filterInternalRelationshipTypes = false;
if (securityContext != null && securityContext.getRequest() != null) {
final String filterInternal = securityContext.getRequest().getParameter(REQUEST_PARAMETER_FILTER_INTERNAL_RELATIONSHIP_TYPES);
if (filterInternal != null) {
filterInternalRelationshipTypes = "true".equals(filterInternal);
}
}
// the result set using the request parameter "filterInternal=true"
if (filterInternalRelationshipTypes) {
for (final RelationshipInterface rel : relationships) {
if (!rel.isInternal()) {
resultList.add(rel);
}
}
} else {
resultList.addAll(relationships);
}
}
}
}
final int rawResultCount = resultList.size();
return new Result(PagingHelper.subList(resultList, pageSize, page), rawResultCount, true, false);
} catch (Throwable t) {
logger.warn("Exception while fetching relationships", t);
}
} else {
logger.info("No results from parent..");
}
throw new IllegalPathException(getResourceSignature() + " can only be applied to a non-empty resource");
}
Aggregations