Search in sources :

Example 1 with ScriptExpressionEvaluatorType

use of com.evolveum.midpoint.xml.ns._public.common.common_3.ScriptExpressionEvaluatorType 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 ScriptExpressionEvaluatorType

use of com.evolveum.midpoint.xml.ns._public.common.common_3.ScriptExpressionEvaluatorType in project midpoint by Evolveum.

the class ScriptExpressionEvaluatorFactory method createEvaluator.

/* (non-Javadoc)
	 * @see com.evolveum.midpoint.common.expression.ExpressionEvaluatorFactory#createEvaluator(javax.xml.bind.JAXBElement, com.evolveum.midpoint.prism.ItemDefinition)
	 */
@Override
public <V extends PrismValue, D extends ItemDefinition> ExpressionEvaluator<V, D> createEvaluator(Collection<JAXBElement<?>> evaluatorElements, D outputDefinition, String contextDescription, Task task, OperationResult result) throws SchemaException {
    if (evaluatorElements.size() > 1) {
        throw new SchemaException("More than one evaluator specified in " + contextDescription);
    }
    JAXBElement<?> evaluatorElement = evaluatorElements.iterator().next();
    Object evaluatorElementObject = evaluatorElement.getValue();
    if (!(evaluatorElementObject instanceof ScriptExpressionEvaluatorType)) {
        throw new IllegalArgumentException("Script expression cannot handle elements of type " + evaluatorElementObject.getClass().getName());
    }
    ScriptExpressionEvaluatorType scriptType = (ScriptExpressionEvaluatorType) evaluatorElementObject;
    ScriptExpression scriptExpression = scriptExpressionFactory.createScriptExpression(scriptType, outputDefinition, contextDescription);
    return new ScriptExpressionEvaluator<>(scriptType, scriptExpression, securityEnforcer);
}
Also used : SchemaException(com.evolveum.midpoint.util.exception.SchemaException) ScriptExpressionEvaluatorType(com.evolveum.midpoint.xml.ns._public.common.common_3.ScriptExpressionEvaluatorType)

Example 3 with ScriptExpressionEvaluatorType

use of com.evolveum.midpoint.xml.ns._public.common.common_3.ScriptExpressionEvaluatorType in project midpoint by Evolveum.

the class AbstractScriptTest method evaluateStringExpression.

private List<PrismPropertyValue<String>> evaluateStringExpression(String fileName, String testName, VariablesMap variables) throws ObjectNotFoundException, CommunicationException, SecurityViolationException, SchemaException, IOException, ExpressionEvaluationException, ConfigurationException {
    ScriptExpressionEvaluatorType scriptType = parseScriptType(fileName);
    OperationResult opResult = createOperationResult();
    return evaluateExpression(scriptType, DOMUtil.XSD_STRING, true, variables, testName, opResult);
}
Also used : OperationResult(com.evolveum.midpoint.schema.result.OperationResult) ScriptExpressionEvaluatorType(com.evolveum.midpoint.xml.ns._public.common.common_3.ScriptExpressionEvaluatorType)

Example 4 with ScriptExpressionEvaluatorType

use of com.evolveum.midpoint.xml.ns._public.common.common_3.ScriptExpressionEvaluatorType in project midpoint by Evolveum.

the class ExecuteScriptExecutor method getParameters.

private Parameters getParameters(ActionExpressionType action, PipelineData input, ExecutionContext context, OperationResult globalResult) throws CommunicationException, ObjectNotFoundException, SchemaException, ScriptExecutionException, SecurityViolationException, ConfigurationException, ExpressionEvaluationException {
    ScriptExpressionEvaluatorType script = expressionHelper.getActionArgument(ScriptExpressionEvaluatorType.class, action, ExecuteScriptActionExpressionType.F_SCRIPT, PARAM_SCRIPT, input, context, null, PARAM_SCRIPT, globalResult);
    ItemDefinition<?> outputDefinition;
    String outputItemUri = expressionHelper.getSingleArgumentValue(action.getParameter(), PARAM_OUTPUT_ITEM, false, false, NAME, input, context, String.class, globalResult);
    if (StringUtils.isNotBlank(outputItemUri)) {
        outputDefinition = getItemDefinition(outputItemUri);
    } else if (action instanceof ExecuteScriptActionExpressionType) {
        ExecuteScriptActionExpressionType execute = (ExecuteScriptActionExpressionType) action;
        if (execute.getOutputItemName() != null) {
            outputDefinition = getItemDefinitionFromItemName(execute.getOutputItemName());
        } else if (execute.getOutputTypeName() != null) {
            outputDefinition = getItemDefinitionFromTypeName(execute.getOutputTypeName());
        } else {
            outputDefinition = null;
        }
    } else {
        outputDefinition = null;
    }
    boolean forWholeInput = expressionHelper.getActionArgument(Boolean.class, action, ExecuteScriptActionExpressionType.F_FOR_WHOLE_INPUT, PARAM_FOR_WHOLE_INPUT, input, context, false, PARAM_FOR_WHOLE_INPUT, globalResult);
    boolean quiet = expressionHelper.getActionArgument(Boolean.class, action, ExecuteScriptActionExpressionType.F_QUIET, PARAM_QUIET, input, context, false, PARAM_QUIET, globalResult);
    if (script == null) {
        throw new IllegalArgumentException("No script provided");
    }
    // TODO
    ExpressionProfile expressionProfile = null;
    ScriptExpression scriptExpression;
    try {
        scriptExpression = scriptExpressionFactory.createScriptExpression(script, outputDefinition, expressionProfile, expressionFactory, "script", globalResult);
    } catch (ExpressionSyntaxException | SecurityViolationException e) {
        throw new ScriptExecutionException("Couldn't parse script expression: " + e.getMessage(), e);
    }
    return new Parameters(scriptExpression, outputDefinition, forWholeInput, quiet);
}
Also used : ExecuteScriptActionExpressionType(com.evolveum.midpoint.xml.ns._public.model.scripting_3.ExecuteScriptActionExpressionType) ExpressionProfile(com.evolveum.midpoint.schema.expression.ExpressionProfile) 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)

Example 5 with ScriptExpressionEvaluatorType

use of com.evolveum.midpoint.xml.ns._public.common.common_3.ScriptExpressionEvaluatorType in project midpoint by Evolveum.

the class TestModelExpressions method testGetManagersOids.

@Test
public void testGetManagersOids() throws Exception {
    // GIVEN
    OperationResult result = createOperationResult();
    String shortTestName = getTestNameShort();
    PrismObject<UserType> chef = repositoryService.getObject(UserType.class, CHEF_OID, null, result);
    ScriptExpressionEvaluatorType scriptType = parseScriptType("expression-" + shortTestName + ".xml");
    PrismPropertyDefinition<String> outputDefinition = getPrismContext().definitionFactory().createPropertyDefinition(PROPERTY_NAME, DOMUtil.XSD_STRING);
    ScriptExpression scriptExpression = scriptExpressionFactory.createScriptExpression(scriptType, outputDefinition, MiscSchemaUtil.getExpressionProfile(), expressionFactory, shortTestName, result);
    VariablesMap variables = createVariables(ExpressionConstants.VAR_USER, chef, chef.getDefinition());
    // WHEN
    List<PrismPropertyValue<String>> scriptOutputs = evaluate(scriptExpression, variables, false, shortTestName, null, result);
    // THEN
    display("Script output", scriptOutputs);
    assertEquals("Unexpected number of script outputs", 3, scriptOutputs.size());
    Set<String> oids = new HashSet<>();
    oids.add(scriptOutputs.get(0).getValue());
    oids.add(scriptOutputs.get(1).getValue());
    oids.add(scriptOutputs.get(2).getValue());
    Set<String> expectedOids = new HashSet<>(Arrays.asList(CHEESE_OID, CHEESE_JR_OID, LECHUCK_OID));
    assertEquals("Unexpected script output", expectedOids, oids);
}
Also used : OperationResult(com.evolveum.midpoint.schema.result.OperationResult) VariablesMap(com.evolveum.midpoint.schema.expression.VariablesMap) UserType(com.evolveum.midpoint.xml.ns._public.common.common_3.UserType) ScriptExpressionEvaluatorType(com.evolveum.midpoint.xml.ns._public.common.common_3.ScriptExpressionEvaluatorType) ScriptExpression(com.evolveum.midpoint.model.common.expression.script.ScriptExpression) HashSet(java.util.HashSet) Test(org.testng.annotations.Test) AbstractInternalModelIntegrationTest(com.evolveum.midpoint.model.impl.AbstractInternalModelIntegrationTest)

Aggregations

ScriptExpressionEvaluatorType (com.evolveum.midpoint.xml.ns._public.common.common_3.ScriptExpressionEvaluatorType)14 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)10 ScriptExpression (com.evolveum.midpoint.model.common.expression.script.ScriptExpression)6 VariablesMap (com.evolveum.midpoint.schema.expression.VariablesMap)4 AbstractInternalModelIntegrationTest (com.evolveum.midpoint.model.impl.AbstractInternalModelIntegrationTest)2 ItemDefinition (com.evolveum.midpoint.prism.ItemDefinition)2 PrismPropertyValue (com.evolveum.midpoint.prism.PrismPropertyValue)2 ExpressionSyntaxException (com.evolveum.midpoint.repo.common.expression.ExpressionSyntaxException)2 Task (com.evolveum.midpoint.task.api.Task)2 UserType (com.evolveum.midpoint.xml.ns._public.common.common_3.UserType)2 Test (org.testng.annotations.Test)2 PipelineItem (com.evolveum.midpoint.model.api.PipelineItem)1 ScriptExecutionException (com.evolveum.midpoint.model.api.ScriptExecutionException)1 PipelineData (com.evolveum.midpoint.model.impl.scripting.PipelineData)1 ExpressionWrapper (com.evolveum.midpoint.prism.ExpressionWrapper)1 PrismPropertyDefinitionImpl (com.evolveum.midpoint.prism.PrismPropertyDefinitionImpl)1 ExpressionVariables (com.evolveum.midpoint.repo.common.expression.ExpressionVariables)1 ExpressionProfile (com.evolveum.midpoint.schema.expression.ExpressionProfile)1 SchemaException (com.evolveum.midpoint.util.exception.SchemaException)1 ExpressionType (com.evolveum.midpoint.xml.ns._public.common.common_3.ExpressionType)1