Search in sources :

Example 36 with GraphObject

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();
    }
}
Also used : SecurityContext(org.structr.common.SecurityContext) Context(org.mozilla.javascript.Context) ScriptContext(javax.script.ScriptContext) ActionContext(org.structr.schema.action.ActionContext) Script(org.mozilla.javascript.Script) FrameworkException(org.structr.common.error.FrameworkException) GraphObject(org.structr.core.GraphObject) Scriptable(org.mozilla.javascript.Scriptable)

Example 37 with GraphObject

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);
    }
}
Also used : ScriptException(javax.script.ScriptException) UnlicensedException(org.structr.common.error.UnlicensedException) FrameworkException(org.structr.common.error.FrameworkException) ActionContext(org.structr.schema.action.ActionContext) GraphObject(org.structr.core.GraphObject)

Example 38 with GraphObject

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;
}
Also used : FrameworkException(org.structr.common.error.FrameworkException) GraphObject(org.structr.core.GraphObject)

Example 39 with GraphObject

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;
}
Also used : GraphObjectMap(org.structr.core.GraphObjectMap) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) GraphObject(org.structr.core.GraphObject) GraphObject(org.structr.core.GraphObject) Map(java.util.Map) GraphObjectMap(org.structr.core.GraphObjectMap)

Example 40 with GraphObject

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");
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) IllegalPathException(org.structr.rest.exception.IllegalPathException) AbstractNode(org.structr.core.entity.AbstractNode) GraphObject(org.structr.core.GraphObject) LinkedList(java.util.LinkedList) Result(org.structr.core.Result) RelationshipInterface(org.structr.core.graph.RelationshipInterface)

Aggregations

GraphObject (org.structr.core.GraphObject)151 FrameworkException (org.structr.common.error.FrameworkException)58 PropertyKey (org.structr.core.property.PropertyKey)39 LinkedList (java.util.LinkedList)35 SecurityContext (org.structr.common.SecurityContext)25 App (org.structr.core.app.App)25 StructrApp (org.structr.core.app.StructrApp)24 Tx (org.structr.core.graph.Tx)23 List (java.util.List)22 PropertyConverter (org.structr.core.converter.PropertyConverter)22 AbstractNode (org.structr.core.entity.AbstractNode)18 GraphObjectMap (org.structr.core.GraphObjectMap)17 NodeInterface (org.structr.core.graph.NodeInterface)17 LinkedHashSet (java.util.LinkedHashSet)15 PropertyMap (org.structr.core.property.PropertyMap)13 Map (java.util.Map)12 RestMethodResult (org.structr.rest.RestMethodResult)11 ConfigurationProvider (org.structr.schema.ConfigurationProvider)10 Result (org.structr.core.Result)9 ArrayList (java.util.ArrayList)8