Search in sources :

Example 11 with ExpressionProfile

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();
        }
    }
}
Also used : VariablesMap(com.evolveum.midpoint.schema.expression.VariablesMap) ExpressionProfile(com.evolveum.midpoint.schema.expression.ExpressionProfile)

Example 12 with ExpressionProfile

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;
}
Also used : PrismContext(com.evolveum.midpoint.prism.PrismContext) StringPolicyType(com.evolveum.midpoint.xml.ns._public.common.common_3.StringPolicyType) ExpressionEvaluationException(com.evolveum.midpoint.util.exception.ExpressionEvaluationException) ValuePolicyType(com.evolveum.midpoint.xml.ns._public.common.common_3.ValuePolicyType) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) LimitationsType(com.evolveum.midpoint.xml.ns._public.common.common_3.LimitationsType) ExpressionProfile(com.evolveum.midpoint.schema.expression.ExpressionProfile)

Example 13 with ExpressionProfile

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;
}
Also used : ExpressionEvaluatorProfile(com.evolveum.midpoint.schema.expression.ExpressionEvaluatorProfile) ScriptExpressionProfile(com.evolveum.midpoint.schema.expression.ScriptExpressionProfile) ExpressionProfile(com.evolveum.midpoint.schema.expression.ExpressionProfile)

Example 14 with 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;
}
Also used : ExpressionEvaluationException(com.evolveum.midpoint.util.exception.ExpressionEvaluationException) PrismPropertyDefinition(com.evolveum.midpoint.prism.PrismPropertyDefinition) ExpressionEvaluationContext(com.evolveum.midpoint.repo.common.expression.ExpressionEvaluationContext) ExpressionProfile(com.evolveum.midpoint.schema.expression.ExpressionProfile) VariablesMap(com.evolveum.midpoint.schema.expression.VariablesMap) PrismPropertyValue(com.evolveum.midpoint.prism.PrismPropertyValue)

Example 15 with ExpressionProfile

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;
}
Also used : ExpressionEvaluationContext(com.evolveum.midpoint.repo.common.expression.ExpressionEvaluationContext) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) ExpressionProfile(com.evolveum.midpoint.schema.expression.ExpressionProfile) VariablesMap(com.evolveum.midpoint.schema.expression.VariablesMap) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

ExpressionProfile (com.evolveum.midpoint.schema.expression.ExpressionProfile)16 VariablesMap (com.evolveum.midpoint.schema.expression.VariablesMap)7 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)6 ExpressionEvaluationContext (com.evolveum.midpoint.repo.common.expression.ExpressionEvaluationContext)3 ExpressionFactory (com.evolveum.midpoint.repo.common.expression.ExpressionFactory)3 ScriptExpressionProfile (com.evolveum.midpoint.schema.expression.ScriptExpressionProfile)3 ExpressionEvaluationException (com.evolveum.midpoint.util.exception.ExpressionEvaluationException)3 Trace (com.evolveum.midpoint.util.logging.Trace)3 TraceManager (com.evolveum.midpoint.util.logging.TraceManager)3 NotNull (org.jetbrains.annotations.NotNull)3 PrismContext (com.evolveum.midpoint.prism.PrismContext)2 ExpressionSyntaxException (com.evolveum.midpoint.repo.common.expression.ExpressionSyntaxException)2 ExpressionUtil (com.evolveum.midpoint.repo.common.expression.ExpressionUtil)2 ResultHandler (com.evolveum.midpoint.schema.ResultHandler)2 ExpressionEvaluatorProfile (com.evolveum.midpoint.schema.expression.ExpressionEvaluatorProfile)2 MiscSchemaUtil (com.evolveum.midpoint.schema.util.MiscSchemaUtil)2 SchemaException (com.evolveum.midpoint.util.exception.SchemaException)2 SecurityViolationException (com.evolveum.midpoint.util.exception.SecurityViolationException)2 Collection (java.util.Collection)2 List (java.util.List)2