Search in sources :

Example 56 with VariablesMap

use of com.evolveum.midpoint.schema.expression.VariablesMap in project midpoint by Evolveum.

the class Expression method evaluate.

public PrismValueDeltaSetTriple<V> evaluate(ExpressionEvaluationContext context, OperationResult result) throws SchemaException, ExpressionEvaluationException, ObjectNotFoundException, CommunicationException, ConfigurationException, SecurityViolationException {
    VariablesMap processedVariables = null;
    if (context.getExpressionProfile() == null) {
        context.setExpressionProfile(expressionProfile);
    }
    try {
        processedVariables = processInnerVariables(context.getVariables(), context.getContextDescription(), context.getTask(), result);
        ExpressionEvaluationContext contextWithProcessedVariables = context.shallowClone();
        contextWithProcessedVariables.setVariables(processedVariables);
        PrismValueDeltaSetTriple<V> outputTriple;
        ObjectReferenceType runAsRef = null;
        if (expressionType != null) {
            runAsRef = expressionType.getRunAsRef();
        }
        if (runAsRef == null) {
            outputTriple = evaluateExpressionEvaluators(contextWithProcessedVariables, result);
        } else {
            UserType userType = objectResolver.resolve(runAsRef, UserType.class, null, "runAs in " + context.getContextDescription(), context.getTask(), result);
            LOGGER.trace("Running {} as {} ({})", context.getContextDescription(), userType, runAsRef);
            try {
                outputTriple = securityContextManager.runAs(() -> {
                    try {
                        return evaluateExpressionEvaluators(contextWithProcessedVariables, result);
                    } catch (SchemaException | ExpressionEvaluationException | ObjectNotFoundException | CommunicationException | ConfigurationException | SecurityViolationException e) {
                        throw new TunnelException(e);
                    }
                }, userType.asPrismObject());
            } catch (TunnelException te) {
                return unwrapTunnelException(te);
            }
        }
        traceSuccess(context, processedVariables, outputTriple);
        return outputTriple;
    } catch (Throwable e) {
        traceFailure(context, processedVariables, e);
        throw e;
    }
}
Also used : VariablesMap(com.evolveum.midpoint.schema.expression.VariablesMap)

Example 57 with VariablesMap

use of com.evolveum.midpoint.schema.expression.VariablesMap in project midpoint by Evolveum.

the class Expression method processInnerVariables.

private VariablesMap processInnerVariables(VariablesMap variables, String contextDescription, Task task, OperationResult result) throws SchemaException, ObjectNotFoundException, CommunicationException, ConfigurationException, SecurityViolationException, ExpressionEvaluationException {
    if (expressionType == null) {
        // shortcut
        return variables;
    }
    VariablesMap newVariables = new VariablesMap();
    // We need to add actor variable before we switch user identity (runAs)
    ExpressionUtil.addActorVariable(newVariables, securityContextManager, prismContext);
    boolean actorDefined = newVariables.get(ExpressionConstants.VAR_ACTOR) != null;
    for (Entry<String, TypedValue> entry : variables.entrySet()) {
        String key = entry.getKey();
        if (ExpressionConstants.VAR_ACTOR.equals(key) && actorDefined) {
            // avoid pointless warning about redefined value of actor
            continue;
        }
        newVariables.put(key, entry.getValue());
        if (variables.isAlias(key)) {
            newVariables.registerAlias(key, variables.getAliasResolution(key));
        }
    }
    for (ExpressionVariableDefinitionType variableDefType : expressionType.getVariable()) {
        String varName = variableDefType.getName().getLocalPart();
        if (varName == null) {
            throw new SchemaException("No variable name in expression in " + contextDescription);
        }
        if (variableDefType.getObjectRef() != null) {
            ObjectReferenceType ref = variableDefType.getObjectRef();
            ref.setType(prismContext.getSchemaRegistry().qualifyTypeName(ref.getType()));
            ObjectType varObject = objectResolver.resolve(ref, ObjectType.class, null, "variable " + varName + " in " + contextDescription, task, result);
            newVariables.addVariableDefinition(varName, varObject, varObject.asPrismObject().getDefinition());
        } else if (variableDefType.getValue() != null) {
            // Only string is supported now
            Object valueObject = variableDefType.getValue();
            if (valueObject instanceof String) {
                MutablePrismPropertyDefinition<Object> def = prismContext.definitionFactory().createPropertyDefinition(new ItemName(SchemaConstants.NS_C, varName), PrimitiveType.STRING.getQname());
                newVariables.addVariableDefinition(varName, valueObject, def);
            } else if (valueObject instanceof Element) {
                MutablePrismPropertyDefinition<Object> def = prismContext.definitionFactory().createPropertyDefinition(new ItemName(SchemaConstants.NS_C, varName), PrimitiveType.STRING.getQname());
                newVariables.addVariableDefinition(varName, ((Element) valueObject).getTextContent(), def);
            } else if (valueObject instanceof RawType) {
                ItemName varQName = new ItemName(SchemaConstants.NS_C, varName);
                MutablePrismPropertyDefinition<Object> def = prismContext.definitionFactory().createPropertyDefinition(varQName, PrimitiveType.STRING.getQname());
                newVariables.addVariableDefinition(varName, ((RawType) valueObject).getParsedValue(null, varQName), def);
            } else {
                throw new SchemaException("Unexpected type " + valueObject.getClass() + " in variable definition " + varName + " in " + contextDescription);
            }
        } else if (variableDefType.getPath() != null) {
            ItemPath itemPath = variableDefType.getPath().getItemPath();
            TypedValue resolvedValueAndDefinition = ExpressionUtil.resolvePathGetTypedValue(itemPath, variables, false, null, objectResolver, prismContext, contextDescription, task, result);
            newVariables.put(varName, resolvedValueAndDefinition);
        } else {
            throw new SchemaException("No value for variable " + varName + " in " + contextDescription);
        }
    }
    return newVariables;
}
Also used : JAXBElement(javax.xml.bind.JAXBElement) Element(org.w3c.dom.Element) ItemName(com.evolveum.midpoint.prism.path.ItemName) VariablesMap(com.evolveum.midpoint.schema.expression.VariablesMap) RawType(com.evolveum.prism.xml.ns._public.types_3.RawType) TypedValue(com.evolveum.midpoint.schema.expression.TypedValue) ItemPath(com.evolveum.midpoint.prism.path.ItemPath)

Example 58 with VariablesMap

use of com.evolveum.midpoint.schema.expression.VariablesMap in project midpoint by Evolveum.

the class TestCommandLine method test110RedirExecuteEcho.

@Test
public void test110RedirExecuteEcho() throws Exception {
    if (!isOsUnix()) {
        displaySkip();
        return;
    }
    Task task = getTestTask();
    OperationResult result = task.getResult();
    CommandLineScriptType scriptType = getScript(REPORT_REDIR_ECHO_FILE);
    VariablesMap variables = VariablesMap.create(prismContext, VAR_HELLOTEXT, "Hello World", PrimitiveType.STRING);
    // WHEN
    when();
    commandLineScriptExecutor.executeScript(scriptType, variables, "test", task, result);
    // THEN
    then();
    assertSuccess(result);
    File targetFile = new File(MidPointTestConstants.TARGET_DIR_PATH, "echo-out");
    assertTrue("Target file is not there", targetFile.exists());
    String targetFileContent = FileUtils.readFileToString(targetFile, StandardCharsets.UTF_8);
    assertEquals("Wrong target file content", "Hello World", targetFileContent);
}
Also used : Task(com.evolveum.midpoint.task.api.Task) CommandLineScriptType(com.evolveum.midpoint.xml.ns._public.common.common_3.CommandLineScriptType) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) VariablesMap(com.evolveum.midpoint.schema.expression.VariablesMap) File(java.io.File) AbstractIntegrationTest(com.evolveum.midpoint.test.AbstractIntegrationTest) Test(org.testng.annotations.Test)

Example 59 with VariablesMap

use of com.evolveum.midpoint.schema.expression.VariablesMap in project midpoint by Evolveum.

the class ValueSetDefinition method evalCondition.

private boolean evalCondition(IV pval) throws SchemaException, ExpressionEvaluationException, ObjectNotFoundException, CommunicationException, ConfigurationException, SecurityViolationException {
    VariablesMap variables = new VariablesMap();
    Object value = getInputValue(pval);
    variables.addVariableDefinition(ExpressionConstants.VAR_INPUT, value, itemDefinition);
    if (additionalVariableName != null) {
        variables.addVariableDefinition(additionalVariableName, value, itemDefinition);
    }
    if (additionalVariables != null) {
        variables.addVariableDefinitions(additionalVariables, variables.keySet());
    }
    ExpressionEvaluationContext context = new ExpressionEvaluationContext(null, variables, shortDesc, task);
    context.setLocalContextDescription(localContextDescription);
    context.setSkipEvaluationMinus(true);
    PrismValueDeltaSetTriple<PrismPropertyValue<Boolean>> outputTriple = condition.evaluate(context, result);
    // noinspection SimplifiableIfStatement
    if (outputTriple == null) {
        return false;
    } else {
        return ExpressionUtil.computeConditionResult(outputTriple.getNonNegativeValues());
    }
}
Also used : VariablesMap(com.evolveum.midpoint.schema.expression.VariablesMap)

Example 60 with VariablesMap

use of com.evolveum.midpoint.schema.expression.VariablesMap in project midpoint by Evolveum.

the class ResourceManager method evaluateExpression.

private <T> void evaluateExpression(PrismProperty<T> configurationProperty, PrismObject<ResourceType> resource, Task task, OperationResult result) throws SchemaException, ObjectNotFoundException, ExpressionEvaluationException, CommunicationException, ConfigurationException, SecurityViolationException {
    PrismPropertyDefinition<T> propDef = configurationProperty.getDefinition();
    String shortDesc = "connector configuration property " + configurationProperty + " in " + resource;
    List<PrismPropertyValue<T>> extraValues = new ArrayList<>();
    for (PrismPropertyValue<T> configurationPropertyValue : configurationProperty.getValues()) {
        ExpressionWrapper expressionWrapper = configurationPropertyValue.getExpression();
        if (expressionWrapper == null) {
            return;
        }
        Object expressionObject = expressionWrapper.getExpression();
        if (!(expressionObject instanceof ExpressionType)) {
            throw new IllegalStateException("Expected that expression in " + configurationPropertyValue + " will be ExpressionType, but it was " + expressionObject);
        }
        ExpressionType expressionType = (ExpressionType) expressionWrapper.getExpression();
        Expression<PrismPropertyValue<T>, PrismPropertyDefinition<T>> expression = expressionFactory.makeExpression(expressionType, propDef, MiscSchemaUtil.getExpressionProfile(), shortDesc, task, result);
        VariablesMap variables = new VariablesMap();
        SystemConfigurationType systemConfiguration = getSystemConfiguration();
        variables.put(ExpressionConstants.VAR_CONFIGURATION, PrismObject.asPrismObject(systemConfiguration), SystemConfigurationType.class);
        variables.put(ExpressionConstants.VAR_RESOURCE, resource, ResourceType.class);
        // TODO: are there other variables that should be populated?
        ExpressionEvaluationContext expressionContext = new ExpressionEvaluationContext(null, variables, shortDesc, task);
        PrismValueDeltaSetTriple<PrismPropertyValue<T>> expressionOutputTriple = expression.evaluate(expressionContext, result);
        Collection<PrismPropertyValue<T>> expressionOutputValues = expressionOutputTriple.getNonNegativeValues();
        if (!expressionOutputValues.isEmpty()) {
            Iterator<PrismPropertyValue<T>> iterator = expressionOutputValues.iterator();
            PrismPropertyValue<T> firstValue = iterator.next();
            configurationPropertyValue.setValue(firstValue.getValue());
            while (iterator.hasNext()) {
                extraValues.add(iterator.next());
            }
        }
    }
    for (PrismPropertyValue<T> extraValue : extraValues) {
        configurationProperty.add(extraValue);
    }
}
Also used : ExpressionEvaluationContext(com.evolveum.midpoint.repo.common.expression.ExpressionEvaluationContext) VariablesMap(com.evolveum.midpoint.schema.expression.VariablesMap)

Aggregations

VariablesMap (com.evolveum.midpoint.schema.expression.VariablesMap)166 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)48 ExpressionEvaluationContext (com.evolveum.midpoint.repo.common.expression.ExpressionEvaluationContext)30 Test (org.testng.annotations.Test)28 Task (com.evolveum.midpoint.task.api.Task)23 NotNull (org.jetbrains.annotations.NotNull)23 QName (javax.xml.namespace.QName)15 AbstractInternalModelIntegrationTest (com.evolveum.midpoint.model.impl.AbstractInternalModelIntegrationTest)12 Source (com.evolveum.midpoint.repo.common.expression.Source)12 AbstractModelCommonTest (com.evolveum.midpoint.model.common.AbstractModelCommonTest)11 UserType (com.evolveum.midpoint.xml.ns._public.common.common_3.UserType)11 ExpressionFactory (com.evolveum.midpoint.repo.common.expression.ExpressionFactory)10 Trace (com.evolveum.midpoint.util.logging.Trace)10 TraceManager (com.evolveum.midpoint.util.logging.TraceManager)10 ObjectFilter (com.evolveum.midpoint.prism.query.ObjectFilter)9 ObjectQuery (com.evolveum.midpoint.prism.query.ObjectQuery)9 com.evolveum.midpoint.xml.ns._public.common.common_3 (com.evolveum.midpoint.xml.ns._public.common.common_3)9 PrismPropertyValue (com.evolveum.midpoint.prism.PrismPropertyValue)8 ExpressionType (com.evolveum.midpoint.xml.ns._public.common.common_3.ExpressionType)8 ExpressionEvaluationException (com.evolveum.midpoint.util.exception.ExpressionEvaluationException)7