use of com.evolveum.midpoint.schema.expression.ExpressionProfile in project midpoint by Evolveum.
the class ScriptingTaskCreator method customizeTask.
TaskType customizeTask(TaskType preparedTask, OperationResult result) throws SchemaException, ObjectNotFoundException, SecurityViolationException, CommunicationException, ConfigurationException, ExpressionEvaluationException {
ExpressionType customizer = asynchronousScriptExecution.getTaskCustomizer();
if (customizer == null) {
return preparedTask;
} else {
ModelExpressionThreadLocalHolder.pushExpressionEnvironment(new ExpressionEnvironment<>(actx.context, null, actx.task, result));
try {
PrismObjectDefinition<TaskType> taskDefinition = preparedTask.asPrismObject().getDefinition();
VariablesMap variables = createVariables();
variables.addVariableDefinition(VAR_PREPARED_TASK, preparedTask, taskDefinition);
ExpressionProfile expressionProfile = MiscSchemaUtil.getExpressionProfile();
PrismValue customizedTaskValue = ExpressionUtil.evaluateExpression(variables, taskDefinition, customizer, expressionProfile, beans.expressionFactory, "task customizer", actx.task, result);
if (customizedTaskValue == null) {
throw new IllegalStateException("Task customizer returned no value");
}
if (!(customizedTaskValue instanceof PrismObjectValue)) {
throw new IllegalStateException("Task customizer returned a value that is not a PrismObjectValue: " + customizedTaskValue);
}
Objectable customizedTaskBean = ((PrismObjectValue<?>) customizedTaskValue).asObjectable();
if (!(customizedTaskBean instanceof TaskType)) {
throw new IllegalStateException("Task customizer returned a value that is not a TaskType: " + customizedTaskBean);
}
return (TaskType) customizedTaskBean;
} finally {
ModelExpressionThreadLocalHolder.popExpressionEnvironment();
}
}
}
use of com.evolveum.midpoint.schema.expression.ExpressionProfile in project midpoint by Evolveum.
the class ValuePolicyProcessor method generate.
public String generate(ItemPath path, ValuePolicyType policy, int defaultLength, boolean generateMinimalSize, ObjectBasedValuePolicyOriginResolver<?> originResolver, String shortDesc, Task task, OperationResult parentResult) throws ExpressionEvaluationException, SchemaException, ObjectNotFoundException, CommunicationException, ConfigurationException, SecurityViolationException {
Context ctx = new Context(path != null ? path : SchemaConstants.PATH_PASSWORD_VALUE);
OperationResult result = parentResult.createSubresult(OP_GENERATE);
if (policy == null) {
// lets create some default policy
policy = new ValuePolicyType().stringPolicy(new StringPolicyType().limitations(new LimitationsType().maxLength(defaultLength).minLength(defaultLength)));
}
StringPolicyType stringPolicy = policy.getStringPolicy();
int maxAttempts = DEFAULT_MAX_ATTEMPTS;
if (stringPolicy.getLimitations() != null && stringPolicy.getLimitations().getMaxAttempts() != null) {
maxAttempts = stringPolicy.getLimitations().getMaxAttempts();
}
if (maxAttempts < 1) {
ExpressionEvaluationException e = new ExpressionEvaluationException("Illegal number of maximum value generation attempts: " + maxAttempts);
result.recordFatalError(e);
throw e;
}
String generatedValue;
int attempt = 1;
for (; ; ) {
generatedValue = generateAttempt(policy, defaultLength, generateMinimalSize, ctx, result);
if (result.isError()) {
throw new ExpressionEvaluationException(result.getMessage());
}
// TODO: this needs to be determined from ValuePolicyType archetype
ExpressionProfile expressionProfile = MiscSchemaUtil.getExpressionProfile();
if (checkAttempt(generatedValue, policy, expressionProfile, originResolver, shortDesc, task, result)) {
break;
}
LOGGER.trace("Generator attempt {}: check failed", attempt);
if (attempt == maxAttempts) {
ExpressionEvaluationException e = new ExpressionEvaluationException("Unable to generate value, maximum number of attempts (" + maxAttempts + ") exceeded");
result.recordFatalError(e);
throw e;
}
attempt++;
}
return generatedValue;
}
use of com.evolveum.midpoint.schema.expression.ExpressionProfile in project midpoint by Evolveum.
the class AbstractScriptTest method getExpressionProfile.
protected ExpressionProfile getExpressionProfile(ScriptExpressionProfile scriptExpressionProfile) {
if (scriptExpressionProfile == null) {
return null;
}
ExpressionProfile expressionProfile = new ExpressionProfile(this.getClass().getSimpleName());
expressionProfile.setDecision(AccessDecision.DENY);
ExpressionEvaluatorProfile evaluatorProfile = new ExpressionEvaluatorProfile(ScriptExpressionEvaluatorFactory.ELEMENT_NAME);
expressionProfile.add(evaluatorProfile);
evaluatorProfile.setDecision(AccessDecision.DENY);
evaluatorProfile.add(scriptExpressionProfile);
return expressionProfile;
}
use of com.evolveum.midpoint.schema.expression.ExpressionProfile in project midpoint by Evolveum.
the class ExpressionHandler method evaluateConfirmationExpression.
public boolean evaluateConfirmationExpression(UserType user, ShadowType shadow, ExpressionType expressionType, Task task, OperationResult result) throws ExpressionEvaluationException, ObjectNotFoundException, SchemaException, CommunicationException, ConfigurationException, SecurityViolationException {
Validate.notNull(user, "User must not be null.");
Validate.notNull(shadow, "Resource object shadow must not be null.");
Validate.notNull(expressionType, "Expression must not be null.");
Validate.notNull(result, "Operation result must not be null.");
ResourceType resource = resolveResource(shadow, result);
VariablesMap variables = getDefaultXPathVariables(user, shadow, resource);
String shortDesc = "confirmation expression for " + resource.asPrismObject();
PrismPropertyDefinition<Boolean> outputDefinition = prismContext.definitionFactory().createPropertyDefinition(ExpressionConstants.OUTPUT_ELEMENT_NAME, DOMUtil.XSD_BOOLEAN);
ExpressionProfile expressionProfile = MiscSchemaUtil.getExpressionProfile();
Expression<PrismPropertyValue<Boolean>, PrismPropertyDefinition<Boolean>> expression = expressionFactory.makeExpression(expressionType, outputDefinition, expressionProfile, shortDesc, task, result);
ExpressionEvaluationContext params = new ExpressionEvaluationContext(null, variables, shortDesc, task);
PrismValueDeltaSetTriple<PrismPropertyValue<Boolean>> outputTriple = ModelExpressionThreadLocalHolder.evaluateExpressionInContext(expression, params, task, result);
Collection<PrismPropertyValue<Boolean>> nonNegativeValues = outputTriple.getNonNegativeValues();
if (nonNegativeValues == null || nonNegativeValues.isEmpty()) {
throw new ExpressionEvaluationException("Expression returned no value (" + nonNegativeValues.size() + ") in " + shortDesc);
}
if (nonNegativeValues.size() > 1) {
throw new ExpressionEvaluationException("Expression returned more than one value (" + nonNegativeValues.size() + ") in " + shortDesc);
}
PrismPropertyValue<Boolean> resultpval = nonNegativeValues.iterator().next();
if (resultpval == null) {
throw new ExpressionEvaluationException("Expression returned no value (" + nonNegativeValues.size() + ") in " + shortDesc);
}
Boolean resultVal = resultpval.getValue();
if (resultVal == null) {
throw new ExpressionEvaluationException("Expression returned no value (" + nonNegativeValues.size() + ") in " + shortDesc);
}
return resultVal;
}
use of com.evolveum.midpoint.schema.expression.ExpressionProfile in project midpoint by Evolveum.
the class FunctionExpressionEvaluator method createFunctionEvaluationContext.
@NotNull
private ExpressionEvaluationContext createFunctionEvaluationContext(ExpressionType functionExpressionBean, ExpressionEvaluationContext context, OperationResult parentResult) throws SchemaException, ObjectNotFoundException, SecurityViolationException, ExpressionEvaluationException, CommunicationException, ConfigurationException {
ExpressionEvaluationContext functionContext = context.shallowClone();
VariablesMap functionVariables = new VariablesMap();
for (ExpressionParameterType param : expressionEvaluatorBean.getParameter()) {
ExpressionType parameterExpressionBean = param.getExpression();
OperationResult variableResult = parentResult.createMinorSubresult(OP_RESOLVE_PARAMETER_VALUE);
Expression<V, D> parameterExpression = null;
try {
variableResult.addArbitraryObjectAsParam("parameterExpression", parameterExpressionBean);
D variableOutputDefinition = determineVariableOutputDefinition(functionExpressionBean, param.getName());
// TODO: expression profile should be determined somehow
ExpressionProfile expressionProfile = MiscSchemaUtil.getExpressionProfile();
parameterExpression = context.getExpressionFactory().makeExpression(parameterExpressionBean, variableOutputDefinition, expressionProfile, "parameter " + param.getName() + " evaluation in " + context.getContextDescription(), context.getTask(), variableResult);
PrismValueDeltaSetTriple<V> evaluatedValue = parameterExpression.evaluate(context, parentResult);
V value = ExpressionUtil.getExpressionOutputValue(evaluatedValue, "evaluated value for parameter " + param.getName());
functionVariables.addVariableDefinition(param.getName(), value, variableOutputDefinition);
} catch (SchemaException | ExpressionEvaluationException | ObjectNotFoundException | CommunicationException | ConfigurationException | SecurityViolationException e) {
variableResult.recordFatalError("Failed to resolve variable: " + parameterExpression + ". Reason: " + e.getMessage());
throw e;
} finally {
variableResult.computeStatusIfUnknown();
}
}
functionContext.setVariables(functionVariables);
return functionContext;
}
Aggregations