Search in sources :

Example 1 with ScriptExecutionException

use of com.evolveum.midpoint.model.api.ScriptExecutionException 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 ScriptExecutionException

use of com.evolveum.midpoint.model.api.ScriptExecutionException in project midpoint by Evolveum.

the class ScriptExecutor method getItemDefinition.

private ItemDefinition<?> getItemDefinition(String itemUri) throws ScriptExecutionException {
    if (StringUtils.isBlank(itemUri)) {
        return null;
    }
    QName itemName = QNameUtil.uriToQName(itemUri, true);
    ItemDefinition def = prismContext.getSchemaRegistry().findItemDefinitionByElementName(itemName);
    if (def != null) {
        return def;
    }
    def = prismContext.getSchemaRegistry().findItemDefinitionByType(itemName);
    if (def != null) {
        return def;
    }
    throw new ScriptExecutionException("Supplied item identification " + itemUri + " corresponds neither to item name nor type name");
}
Also used : ScriptExecutionException(com.evolveum.midpoint.model.api.ScriptExecutionException) QName(javax.xml.namespace.QName)

Example 3 with ScriptExecutionException

use of com.evolveum.midpoint.model.api.ScriptExecutionException in project midpoint by Evolveum.

the class ScriptExecutionTaskHandler method run.

@Override
public TaskRunResult run(Task task) {
    OperationResult result = task.getResult().createSubresult(DOT_CLASS + "run");
    TaskRunResult runResult = new TaskRunResult();
    PrismProperty<ExecuteScriptType> executeScriptProperty = task.getExtensionProperty(SchemaConstants.SE_EXECUTE_SCRIPT);
    if (executeScriptProperty == null || executeScriptProperty.getValue().getValue() == null || executeScriptProperty.getValue().getValue().getScriptingExpression() == null) {
        throw new IllegalStateException("There's no script to be run in task " + task + " (property " + SchemaConstants.SE_EXECUTE_SCRIPT + ")");
    }
    try {
        task.startCollectingOperationStatsFromZero(true, false, true);
        task.setProgress(0);
        ScriptExecutionResult executionResult = scriptingService.evaluateExpression(executeScriptProperty.getRealValue(), task, result);
        LOGGER.debug("Execution output: {} item(s)", executionResult.getDataOutput().size());
        LOGGER.debug("Execution result:\n", executionResult.getConsoleOutput());
        result.computeStatus();
        runResult.setRunResultStatus(TaskRunResult.TaskRunResultStatus.FINISHED);
    } catch (ScriptExecutionException | SecurityViolationException | SchemaException e) {
        result.recordFatalError("Couldn't execute script: " + e.getMessage(), e);
        LoggingUtils.logUnexpectedException(LOGGER, "Couldn't execute script", e);
        runResult.setRunResultStatus(TaskRunResult.TaskRunResultStatus.PERMANENT_ERROR);
    } finally {
        task.storeOperationStats();
    }
    task.getResult().computeStatus();
    runResult.setOperationResult(task.getResult());
    // incremented directly in actions implementations
    runResult.setProgress(task.getProgress());
    return runResult;
}
Also used : SchemaException(com.evolveum.midpoint.util.exception.SchemaException) TaskRunResult(com.evolveum.midpoint.task.api.TaskRunResult) ExecuteScriptType(com.evolveum.midpoint.xml.ns._public.model.scripting_3.ExecuteScriptType) SecurityViolationException(com.evolveum.midpoint.util.exception.SecurityViolationException) ScriptExecutionException(com.evolveum.midpoint.model.api.ScriptExecutionException) ScriptExecutionResult(com.evolveum.midpoint.model.api.ScriptExecutionResult) OperationResult(com.evolveum.midpoint.schema.result.OperationResult)

Example 4 with ScriptExecutionException

use of com.evolveum.midpoint.model.api.ScriptExecutionException in project midpoint by Evolveum.

the class AssignExecutor method execute.

@Override
public PipelineData execute(ActionExpressionType expression, PipelineData input, ExecutionContext context, OperationResult globalResult) throws ScriptExecutionException {
    boolean raw = getParamRaw(expression, input, context, globalResult);
    boolean dryRun = getParamDryRun(expression, input, context, globalResult);
    ActionParameterValueType resourceParameterValue = expressionHelper.getArgument(expression.getParameter(), PARAM_RESOURCE, false, false, NAME);
    ActionParameterValueType roleParameterValue = expressionHelper.getArgument(expression.getParameter(), PARAM_ROLE, false, false, NAME);
    Collection<ObjectReferenceType> resources;
    if (resourceParameterValue != null) {
        PipelineData data = expressionHelper.evaluateParameter(resourceParameterValue, null, input, context, globalResult);
        resources = data.getDataAsReferences(ResourceType.COMPLEX_TYPE);
    } else {
        resources = null;
    }
    Collection<ObjectReferenceType> roles;
    if (roleParameterValue != null) {
        PipelineData data = expressionHelper.evaluateParameter(roleParameterValue, null, input, context, globalResult);
        roles = data.getDataAsReferences(RoleType.COMPLEX_TYPE);
    } else {
        roles = null;
    }
    if (resources == null && roles == null) {
        throw new ScriptExecutionException("Nothing to assign: neither resource nor role specified");
    }
    for (PipelineItem item : input.getData()) {
        PrismValue value = item.getValue();
        OperationResult result = operationsHelper.createActionResult(item, this, context, globalResult);
        context.checkTaskStop();
        if (value instanceof PrismObjectValue && ((PrismObjectValue) value).asObjectable() instanceof FocusType) {
            PrismObject<? extends ObjectType> prismObject = ((PrismObjectValue) value).asPrismObject();
            ObjectType objectType = prismObject.asObjectable();
            long started = operationsHelper.recordStart(context, objectType);
            Throwable exception = null;
            try {
                operationsHelper.applyDelta(createDelta(objectType, resources, roles), operationsHelper.createExecutionOptions(raw), dryRun, context, result);
                operationsHelper.recordEnd(context, objectType, started, null);
            } catch (Throwable ex) {
                operationsHelper.recordEnd(context, objectType, started, ex);
                exception = processActionException(ex, NAME, value, context);
            }
            context.println((exception != null ? "Attempted to modify " : "Modified ") + prismObject.toString() + rawDrySuffix(raw, dryRun) + exceptionSuffix(exception));
        } else {
            //noinspection ThrowableNotThrown
            processActionException(new ScriptExecutionException("Item is not a PrismObject of FocusType"), NAME, value, context);
        }
        operationsHelper.trimAndCloneResult(result, globalResult, context);
    }
    // TODO updated objects?
    return input;
}
Also used : ScriptExecutionException(com.evolveum.midpoint.model.api.ScriptExecutionException) PipelineData(com.evolveum.midpoint.model.impl.scripting.PipelineData) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) ActionParameterValueType(com.evolveum.midpoint.xml.ns._public.model.scripting_3.ActionParameterValueType) ObjectType(com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectType) ObjectReferenceType(com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectReferenceType) PipelineItem(com.evolveum.midpoint.model.api.PipelineItem) FocusType(com.evolveum.midpoint.xml.ns._public.common.common_3.FocusType)

Example 5 with ScriptExecutionException

use of com.evolveum.midpoint.model.api.ScriptExecutionException in project midpoint by Evolveum.

the class AssignExecutor method createDelta.

private ObjectDelta createDelta(ObjectType objectType, Collection<ObjectReferenceType> resources, Collection<ObjectReferenceType> roles) throws ScriptExecutionException {
    List<AssignmentType> assignments = new ArrayList<>();
    if (roles != null) {
        for (ObjectReferenceType roleRef : roles) {
            AssignmentType assignmentType = new AssignmentType();
            assignmentType.setTargetRef(roleRef);
            assignments.add(assignmentType);
        }
    }
    if (resources != null) {
        for (ObjectReferenceType resourceRef : resources) {
            AssignmentType assignmentType = new AssignmentType();
            ConstructionType constructionType = new ConstructionType();
            constructionType.setResourceRef(resourceRef);
            assignmentType.setConstruction(constructionType);
            assignments.add(assignmentType);
        }
    }
    ObjectDelta delta = ObjectDelta.createEmptyModifyDelta(objectType.getClass(), objectType.getOid(), prismContext);
    try {
        delta.addModificationAddContainer(FocusType.F_ASSIGNMENT, assignments.toArray(new AssignmentType[0]));
    } catch (SchemaException e) {
        throw new ScriptExecutionException("Couldn't prepare modification to add resource/role assignments", e);
    }
    return delta;
}
Also used : SchemaException(com.evolveum.midpoint.util.exception.SchemaException) ObjectReferenceType(com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectReferenceType) ScriptExecutionException(com.evolveum.midpoint.model.api.ScriptExecutionException) ConstructionType(com.evolveum.midpoint.xml.ns._public.common.common_3.ConstructionType) AssignmentType(com.evolveum.midpoint.xml.ns._public.common.common_3.AssignmentType) ArrayList(java.util.ArrayList) ObjectDelta(com.evolveum.midpoint.prism.delta.ObjectDelta)

Aggregations

ScriptExecutionException (com.evolveum.midpoint.model.api.ScriptExecutionException)7 PipelineItem (com.evolveum.midpoint.model.api.PipelineItem)3 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)3 ObjectReferenceType (com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectReferenceType)3 PipelineData (com.evolveum.midpoint.model.impl.scripting.PipelineData)2 SchemaException (com.evolveum.midpoint.util.exception.SchemaException)2 ScriptExecutionResult (com.evolveum.midpoint.model.api.ScriptExecutionResult)1 ScriptExpression (com.evolveum.midpoint.model.common.expression.script.ScriptExpression)1 ObjectDelta (com.evolveum.midpoint.prism.delta.ObjectDelta)1 ExpressionSyntaxException (com.evolveum.midpoint.repo.common.expression.ExpressionSyntaxException)1 TaskRunResult (com.evolveum.midpoint.task.api.TaskRunResult)1 SecurityViolationException (com.evolveum.midpoint.util.exception.SecurityViolationException)1 AssignmentType (com.evolveum.midpoint.xml.ns._public.common.common_3.AssignmentType)1 ConstructionType (com.evolveum.midpoint.xml.ns._public.common.common_3.ConstructionType)1 FocusType (com.evolveum.midpoint.xml.ns._public.common.common_3.FocusType)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 ActionParameterValueType (com.evolveum.midpoint.xml.ns._public.model.scripting_3.ActionParameterValueType)1 ExecuteScriptType (com.evolveum.midpoint.xml.ns._public.model.scripting_3.ExecuteScriptType)1 ArrayList (java.util.ArrayList)1