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;
}
}
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;
}
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);
}
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());
}
}
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);
}
}
Aggregations