Search in sources :

Example 1 with ExpressionSyntaxException

use of com.evolveum.midpoint.repo.common.expression.ExpressionSyntaxException in project midpoint by Evolveum.

the class ScriptExecutor method execute.

@Override
public PipelineData execute(ActionExpressionType expression, PipelineData input, ExecutionContext context, OperationResult globalResult) throws ScriptExecutionException {
    checkRootAuthorization(globalResult, NAME);
    ScriptExpressionEvaluatorType script = expressionHelper.getSingleArgumentValue(expression.getParameter(), PARAM_SCRIPT, true, true, NAME, input, context, ScriptExpressionEvaluatorType.class, globalResult);
    String outputItem = expressionHelper.getSingleArgumentValue(expression.getParameter(), PARAM_OUTPUT_ITEM, false, false, NAME, input, context, String.class, globalResult);
    ItemDefinition<?> outputDefinition = getItemDefinition(outputItem);
    ScriptExpression scriptExpression;
    try {
        scriptExpression = scriptExpressionFactory.createScriptExpression(script, outputDefinition, "script");
    } catch (ExpressionSyntaxException e) {
        throw new ScriptExecutionException("Couldn't parse script expression: " + e.getMessage(), e);
    }
    PipelineData output = PipelineData.createEmpty();
    for (PipelineItem item : input.getData()) {
        PrismValue value = item.getValue();
        OperationResult result = operationsHelper.createActionResult(item, this, context, globalResult);
        context.checkTaskStop();
        String valueDescription;
        long started;
        if (value instanceof PrismObjectValue) {
            started = operationsHelper.recordStart(context, asObjectType(value));
            valueDescription = asObjectType(value).asPrismObject().toString();
        } else {
            started = 0;
            valueDescription = value.toHumanReadableString();
        }
        Throwable exception = null;
        try {
            Object outObject = executeScript(scriptExpression, value, context, result);
            if (outObject != null) {
                addToData(outObject, item.getResult(), output);
            } else {
                output.add(item);
            }
            if (value instanceof PrismObjectValue) {
                operationsHelper.recordEnd(context, asObjectType(value), started, null);
            }
        } catch (Throwable ex) {
            if (value instanceof PrismObjectValue) {
                operationsHelper.recordEnd(context, asObjectType(value), started, ex);
            }
            exception = processActionException(ex, NAME, value, context);
        }
        context.println((exception != null ? "Attempted to execute " : "Executed ") + "script on " + valueDescription + exceptionSuffix(exception));
        operationsHelper.trimAndCloneResult(result, globalResult, context);
    }
    return output;
}
Also used : ScriptExecutionException(com.evolveum.midpoint.model.api.ScriptExecutionException) PipelineData(com.evolveum.midpoint.model.impl.scripting.PipelineData) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) ScriptExpressionEvaluatorType(com.evolveum.midpoint.xml.ns._public.common.common_3.ScriptExpressionEvaluatorType) ScriptExpression(com.evolveum.midpoint.model.common.expression.script.ScriptExpression) ExpressionSyntaxException(com.evolveum.midpoint.repo.common.expression.ExpressionSyntaxException) PipelineItem(com.evolveum.midpoint.model.api.PipelineItem)

Example 2 with ExpressionSyntaxException

use of com.evolveum.midpoint.repo.common.expression.ExpressionSyntaxException in project midpoint by Evolveum.

the class LazyXPathVariableResolver method resolveVariable.

@Override
public Object resolveVariable(QName name) {
    if (variables == null) {
        return null;
    }
    if (name != null && (name.getNamespaceURI() == null || name.getNamespaceURI().isEmpty())) {
        LOGGER.warn("Using variable without a namespace (" + name + "), possible namespace problem (e.g. missing namespace prefix declaration) in " + contextDescription);
    }
    // Note: null is a legal variable name here. It corresponds to the root node
    Object variableValue = variables.get(name);
    if (variableValue == null) {
        // TODO: warning ???
        return null;
    }
    QName type = null;
    // Attempt to resolve object reference
    if (objectResolver != null && variableValue instanceof ObjectReferenceType) {
        ObjectReferenceType ref = (ObjectReferenceType) variableValue;
        if (ref.getOid() == null) {
            SchemaException newEx = new SchemaException("Null OID in reference in variable " + name + " in " + contextDescription, name);
            throw new TunnelException(newEx);
        } else {
            type = ref.getType();
            try {
                // TODO task
                variableValue = objectResolver.resolve(ref, ObjectType.class, null, contextDescription, null, result);
            } catch (ObjectNotFoundException e) {
                ObjectNotFoundException newEx = new ObjectNotFoundException("Object not found during variable " + name + " resolution in " + contextDescription + ": " + e.getMessage(), e, ref.getOid());
                // We have no other practical way how to handle the error
                throw new TunnelException(newEx);
            } catch (SchemaException e) {
                ExpressionSyntaxException newEx = new ExpressionSyntaxException("Schema error during variable " + name + " resolution in " + contextDescription + ": " + e.getMessage(), e, name);
                throw new TunnelException(newEx);
            }
        }
    }
    try {
        return convertToXml(variableValue, name, prismContext, contextDescription);
    } catch (SchemaException e) {
        throw new TunnelException(e);
    }
}
Also used : SchemaException(com.evolveum.midpoint.util.exception.SchemaException) TunnelException(com.evolveum.midpoint.util.exception.TunnelException) ObjectType(com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectType) ObjectReferenceType(com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectReferenceType) QName(javax.xml.namespace.QName) ObjectNotFoundException(com.evolveum.midpoint.util.exception.ObjectNotFoundException) PrismObject(com.evolveum.midpoint.prism.PrismObject) ExpressionSyntaxException(com.evolveum.midpoint.repo.common.expression.ExpressionSyntaxException)

Example 3 with ExpressionSyntaxException

use of com.evolveum.midpoint.repo.common.expression.ExpressionSyntaxException in project midpoint by Evolveum.

the class XPathScriptEvaluator method evaluate.

private Object evaluate(QName returnType, String code, ExpressionVariables variables, ObjectResolver objectResolver, Collection<FunctionLibrary> functions, String contextDescription, OperationResult result) throws ExpressionEvaluationException, ObjectNotFoundException, ExpressionSyntaxException {
    XPathExpressionCodeHolder codeHolder = new XPathExpressionCodeHolder(code);
    //System.out.println("code " + code);
    XPath xpath = factory.newXPath();
    XPathVariableResolver variableResolver = new LazyXPathVariableResolver(variables, objectResolver, contextDescription, prismContext, result);
    xpath.setXPathVariableResolver(variableResolver);
    xpath.setNamespaceContext(new MidPointNamespaceContext(codeHolder.getNamespaceMap()));
    xpath.setXPathFunctionResolver(getFunctionResolver(functions));
    XPathExpression expr;
    try {
        expr = xpath.compile(codeHolder.getExpressionAsString());
    } catch (Exception e) {
        Throwable originalException = ExceptionUtil.lookForTunneledException(e);
        if (originalException != null && originalException instanceof ObjectNotFoundException) {
            throw (ObjectNotFoundException) originalException;
        }
        if (originalException != null && originalException instanceof ExpressionSyntaxException) {
            throw (ExpressionSyntaxException) originalException;
        }
        if (e instanceof XPathExpressionException) {
            throw createExpressionEvaluationException(e, contextDescription);
        }
        if (e instanceof RuntimeException) {
            throw (RuntimeException) e;
        }
        throw new SystemException(e.getMessage(), e);
    }
    Object rootNode;
    try {
        rootNode = determineRootNode(variableResolver, contextDescription);
    } catch (SchemaException e) {
        throw new ExpressionSyntaxException(e.getMessage(), e);
    }
    Object evaluatedExpression;
    try {
        evaluatedExpression = expr.evaluate(rootNode, returnType);
    } catch (Exception e) {
        Throwable originalException = ExceptionUtil.lookForTunneledException(e);
        if (originalException != null && originalException instanceof ObjectNotFoundException) {
            throw (ObjectNotFoundException) originalException;
        }
        if (originalException != null && originalException instanceof ExpressionSyntaxException) {
            throw (ExpressionSyntaxException) originalException;
        }
        if (e instanceof XPathExpressionException) {
            throw createExpressionEvaluationException(e, contextDescription);
        }
        if (e instanceof RuntimeException) {
            throw (RuntimeException) e;
        }
        throw new SystemException(e.getMessage(), e);
    }
    if (evaluatedExpression == null) {
        return null;
    }
    return evaluatedExpression;
}
Also used : SchemaException(com.evolveum.midpoint.util.exception.SchemaException) SchemaException(com.evolveum.midpoint.util.exception.SchemaException) ExpressionEvaluationException(com.evolveum.midpoint.util.exception.ExpressionEvaluationException) ExpressionSyntaxException(com.evolveum.midpoint.repo.common.expression.ExpressionSyntaxException) ObjectNotFoundException(com.evolveum.midpoint.util.exception.ObjectNotFoundException) SystemException(com.evolveum.midpoint.util.exception.SystemException) ExpressionSyntaxException(com.evolveum.midpoint.repo.common.expression.ExpressionSyntaxException) SystemException(com.evolveum.midpoint.util.exception.SystemException) ObjectNotFoundException(com.evolveum.midpoint.util.exception.ObjectNotFoundException)

Example 4 with ExpressionSyntaxException

use of com.evolveum.midpoint.repo.common.expression.ExpressionSyntaxException in project midpoint by Evolveum.

the class AbstractValueTransformationExpressionEvaluator method evaluateScriptExpression.

private Collection<V> evaluateScriptExpression(Collection<Source<?, ?>> sources, ExpressionVariables variables, String contextDescription, boolean useNew, ExpressionEvaluationContext params, Task task, OperationResult result) throws ExpressionEvaluationException, ObjectNotFoundException, SchemaException {
    ExpressionVariables scriptVariables = new ExpressionVariables();
    if (useNew) {
        scriptVariables.addVariableDefinitionsNew(variables);
    } else {
        scriptVariables.addVariableDefinitionsOld(variables);
    }
    if (sources != null) {
        // Add sources to variables
        for (Source<?, ?> source : sources) {
            LOGGER.trace("source: {}", source);
            QName name = source.getName();
            if (name == null) {
                if (sources.size() == 1) {
                    name = ExpressionConstants.VAR_INPUT;
                } else {
                    throw new ExpressionSyntaxException("No name definition for source in " + contextDescription);
                }
            }
            Object value = null;
            if (useNew) {
                value = getRealContent(source.getItemNew(), source.getResidualPath());
            } else {
                value = getRealContent(source.getItemOld(), source.getResidualPath());
            }
            scriptVariables.addVariableDefinition(name, value);
        }
    }
    List<V> scriptResults = transformSingleValue(scriptVariables, null, useNew, params, (useNew ? "(new) " : "(old) ") + contextDescription, task, result);
    if (scriptResults == null || scriptResults.isEmpty()) {
        return null;
    }
    Collection<V> outputSet = new ArrayList<V>(scriptResults.size());
    for (V pval : scriptResults) {
        if (pval instanceof PrismPropertyValue<?>) {
            if (((PrismPropertyValue<?>) pval).getValue() == null) {
                continue;
            }
            Object realValue = ((PrismPropertyValue<?>) pval).getValue();
            if (realValue instanceof String) {
                if (((String) realValue).isEmpty()) {
                    continue;
                }
            }
            if (realValue instanceof PolyString) {
                if (((PolyString) realValue).isEmpty()) {
                    continue;
                }
            }
        }
        outputSet.add(pval);
    }
    return outputSet;
}
Also used : ExpressionVariables(com.evolveum.midpoint.repo.common.expression.ExpressionVariables) QName(javax.xml.namespace.QName) PolyString(com.evolveum.midpoint.prism.polystring.PolyString) ObjectDeltaObject(com.evolveum.midpoint.repo.common.expression.ObjectDeltaObject) PolyString(com.evolveum.midpoint.prism.polystring.PolyString) ExpressionSyntaxException(com.evolveum.midpoint.repo.common.expression.ExpressionSyntaxException) PrismPropertyValue(com.evolveum.midpoint.prism.PrismPropertyValue)

Aggregations

ExpressionSyntaxException (com.evolveum.midpoint.repo.common.expression.ExpressionSyntaxException)4 ObjectNotFoundException (com.evolveum.midpoint.util.exception.ObjectNotFoundException)2 SchemaException (com.evolveum.midpoint.util.exception.SchemaException)2 QName (javax.xml.namespace.QName)2 PipelineItem (com.evolveum.midpoint.model.api.PipelineItem)1 ScriptExecutionException (com.evolveum.midpoint.model.api.ScriptExecutionException)1 ScriptExpression (com.evolveum.midpoint.model.common.expression.script.ScriptExpression)1 PipelineData (com.evolveum.midpoint.model.impl.scripting.PipelineData)1 PrismObject (com.evolveum.midpoint.prism.PrismObject)1 PrismPropertyValue (com.evolveum.midpoint.prism.PrismPropertyValue)1 PolyString (com.evolveum.midpoint.prism.polystring.PolyString)1 ExpressionVariables (com.evolveum.midpoint.repo.common.expression.ExpressionVariables)1 ObjectDeltaObject (com.evolveum.midpoint.repo.common.expression.ObjectDeltaObject)1 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)1 ExpressionEvaluationException (com.evolveum.midpoint.util.exception.ExpressionEvaluationException)1 SystemException (com.evolveum.midpoint.util.exception.SystemException)1 TunnelException (com.evolveum.midpoint.util.exception.TunnelException)1 ObjectReferenceType (com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectReferenceType)1 ObjectType (com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectType)1 ScriptExpressionEvaluatorType (com.evolveum.midpoint.xml.ns._public.common.common_3.ScriptExpressionEvaluatorType)1