Search in sources :

Example 1 with ScriptExecutionException

use of com.evolveum.midpoint.util.exception.ScriptExecutionException in project midpoint by Evolveum.

the class DiscoverConnectorsExecutor method rebindResources.

private void rebindResources(Map<String, String> rebindMap, ExecutionContext context, OperationResult result) throws ScriptExecutionException {
    List<PrismObject<ResourceType>> resources;
    try {
        resources = modelService.searchObjects(ResourceType.class, null, null, null, result);
    } catch (SchemaException | ConfigurationException | ObjectNotFoundException | CommunicationException | SecurityViolationException | ExpressionEvaluationException e) {
        throw new ScriptExecutionException("Couldn't list resources: " + e.getMessage(), e);
    }
    for (PrismObject<ResourceType> resource : resources) {
        if (resource.asObjectable().getConnectorRef() != null) {
            String connectorOid = resource.asObjectable().getConnectorRef().getOid();
            String newOid = rebindMap.get(connectorOid);
            if (newOid != null) {
                String msg = "resource " + resource + " from connector " + connectorOid + " to new one: " + newOid;
                LOGGER.info("Rebinding {}", msg);
                ReferenceDelta refDelta = prismContext.deltaFactory().reference().createModificationReplace(ResourceType.F_CONNECTOR_REF, resource.getDefinition(), newOid);
                ObjectDelta<ResourceType> objDelta = prismContext.deltaFactory().object().createModifyDelta(resource.getOid(), refDelta, ResourceType.class);
                operationsHelper.applyDelta(objDelta, context, result);
                context.println("Rebound " + msg);
            }
        }
    }
}
Also used : SchemaException(com.evolveum.midpoint.util.exception.SchemaException) ExpressionEvaluationException(com.evolveum.midpoint.util.exception.ExpressionEvaluationException) CommunicationException(com.evolveum.midpoint.util.exception.CommunicationException) SecurityViolationException(com.evolveum.midpoint.util.exception.SecurityViolationException) ScriptExecutionException(com.evolveum.midpoint.util.exception.ScriptExecutionException) ReferenceDelta(com.evolveum.midpoint.prism.delta.ReferenceDelta) ResourceType(com.evolveum.midpoint.xml.ns._public.common.common_3.ResourceType) ConfigurationException(com.evolveum.midpoint.util.exception.ConfigurationException) ObjectNotFoundException(com.evolveum.midpoint.util.exception.ObjectNotFoundException)

Example 2 with ScriptExecutionException

use of com.evolveum.midpoint.util.exception.ScriptExecutionException in project midpoint by Evolveum.

the class StateConstraintEvaluator method evaluateForObject.

private <AH extends AssignmentHolderType> EvaluatedStateTrigger evaluateForObject(JAXBElement<StatePolicyConstraintType> constraintElement, PolicyRuleEvaluationContext<AH> ctx, OperationResult result) throws SchemaException, ExpressionEvaluationException, ObjectNotFoundException, CommunicationException, ConfigurationException, SecurityViolationException {
    StatePolicyConstraintType constraint = constraintElement.getValue();
    int count = (constraint.getFilter() != null ? 1 : 0) + (constraint.getExpression() != null ? 1 : 0) + (constraint.getMessageExpression() != null ? 1 : 0) + (constraint.getExecuteScript() != null ? 1 : 0);
    if (count != 1) {
        throw new SchemaException("Exactly one of filter, expression, messageExpression, executeScript element must be present.");
    }
    PrismObject<AH> object = ctx.getObject();
    if (object == null) {
        return null;
    }
    if (constraint.getFilter() != null) {
        ObjectFilter filter = prismContext.getQueryConverter().parseFilter(constraint.getFilter(), object.asObjectable().getClass());
        if (!filter.match(object.getValue(), matchingRuleRegistry)) {
            return null;
        }
    }
    if (constraint.getExecuteScript() != null) {
        VariablesMap variables = new VariablesMap();
        variables.put(VAR_OBJECT, object, object.getDefinition());
        variables.put(VAR_RULE_EVALUATION_CONTEXT, ctx, PolicyRuleEvaluationContext.class);
        ExecutionContext resultingContext;
        try {
            resultingContext = scriptingExpressionEvaluator.evaluateExpressionPrivileged(constraint.getExecuteScript(), variables, ctx.task, result);
        } catch (ScriptExecutionException e) {
            // TODO
            throw new SystemException(e);
        }
        PipelineData output = resultingContext.getFinalOutput();
        LOGGER.trace("Scripting expression returned {} item(s); console output is:\n{}", output != null ? output.getData().size() : null, resultingContext.getConsoleOutput());
        List<PipelineItem> items = output != null ? output.getData() : emptyList();
        if (items.isEmpty()) {
            return null;
        }
    // TODO retrieve localization messages from output
    }
    if (constraint.getMessageExpression() != null) {
        LocalizableMessageType messageBean = evaluatorHelper.evaluateLocalizableMessageType(constraint.getMessageExpression(), evaluatorHelper.createVariablesMap(ctx, constraintElement), "message expression in object state constraint " + constraint.getName() + " (" + ctx.state + ")", ctx.task, result);
        if (messageBean == null) {
            return null;
        } else {
            LocalizableMessage message = LocalizationUtil.toLocalizableMessage(messageBean);
            return new EvaluatedStateTrigger(OBJECT_STATE, constraint, message, message);
        }
    }
    if (constraint.getExpression() != null) {
        if (!evaluatorHelper.evaluateBoolean(constraint.getExpression(), evaluatorHelper.createVariablesMap(ctx, constraintElement), "expression in object state constraint " + constraint.getName() + " (" + ctx.state + ")", ctx.task, result)) {
            return null;
        }
    }
    return new EvaluatedStateTrigger(OBJECT_STATE, constraint, createMessage(OBJECT_CONSTRAINT_KEY_PREFIX, constraintElement, ctx, false, result), createShortMessage(OBJECT_CONSTRAINT_KEY_PREFIX, constraintElement, ctx, false, result));
}
Also used : SchemaException(com.evolveum.midpoint.util.exception.SchemaException) ScriptExecutionException(com.evolveum.midpoint.util.exception.ScriptExecutionException) LocalizableMessageType(com.evolveum.midpoint.xml.ns._public.common.common_3.LocalizableMessageType) PipelineData(com.evolveum.midpoint.model.impl.scripting.PipelineData) LocalizableMessage(com.evolveum.midpoint.util.LocalizableMessage) StatePolicyConstraintType(com.evolveum.midpoint.xml.ns._public.common.common_3.StatePolicyConstraintType) ObjectFilter(com.evolveum.midpoint.prism.query.ObjectFilter) EvaluatedStateTrigger(com.evolveum.midpoint.model.api.context.EvaluatedStateTrigger) ExecutionContext(com.evolveum.midpoint.model.impl.scripting.ExecutionContext) PipelineItem(com.evolveum.midpoint.model.api.PipelineItem) SystemException(com.evolveum.midpoint.util.exception.SystemException) VariablesMap(com.evolveum.midpoint.schema.expression.VariablesMap)

Example 3 with ScriptExecutionException

use of com.evolveum.midpoint.util.exception.ScriptExecutionException in project midpoint by Evolveum.

the class PipelineData method getDataAsReferences.

/**
 * Returns the pipeline content as a list of references. Objects, PRVs, OIDs are converted directly
 * to references. Search filters and queries are evaluated first.
 *
 * This is a legacy method and its use should be avoided.
 */
@NotNull
public List<ObjectReferenceType> getDataAsReferences(QName defaultTargetType, Class<? extends ObjectType> typeForQuery, ExecutionContext context, OperationResult result) throws ScriptExecutionException, CommunicationException, ObjectNotFoundException, SchemaException, SecurityViolationException, ConfigurationException, ExpressionEvaluationException {
    List<ObjectReferenceType> retval = new ArrayList<>(data.size());
    for (PipelineItem item : data) {
        PrismValue value = item.getValue();
        if (value instanceof PrismObjectValue) {
            PrismObjectValue<?> objectValue = (PrismObjectValue<?>) value;
            ObjectReferenceType ref = new ObjectReferenceType();
            ref.setType(objectValue.asPrismObject().getDefinition().getTypeName());
            ref.setOid(objectValue.getOid());
            retval.add(ref);
        } else if (value instanceof PrismPropertyValue) {
            Object realValue = value.getRealValue();
            if (realValue instanceof SearchFilterType) {
                retval.addAll(resolveQuery(typeForQuery, new QueryType().filter((SearchFilterType) realValue), context, result));
            } else if (realValue instanceof QueryType) {
                retval.addAll(resolveQuery(typeForQuery, (QueryType) realValue, context, result));
            } else if (realValue instanceof String) {
                ObjectReferenceType ref = new ObjectReferenceType();
                ref.setType(defaultTargetType);
                ref.setOid((String) realValue);
                retval.add(ref);
            } else if (realValue instanceof ObjectReferenceType) {
                retval.add((ObjectReferenceType) realValue);
            } else {
                throw new ScriptExecutionException("Unsupported reference type: " + value.getClass());
            }
        } else if (value instanceof PrismReferenceValue) {
            PrismReferenceValue referenceValue = (PrismReferenceValue) value;
            ObjectReferenceType ref = new ObjectReferenceType();
            ref.setupReferenceValue(referenceValue);
            retval.add(ref);
        }
    }
    return retval;
}
Also used : SearchFilterType(com.evolveum.prism.xml.ns._public.query_3.SearchFilterType) ScriptExecutionException(com.evolveum.midpoint.util.exception.ScriptExecutionException) ObjectReferenceType(com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectReferenceType) PipelineItem(com.evolveum.midpoint.model.api.PipelineItem) QueryType(com.evolveum.prism.xml.ns._public.query_3.QueryType) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with ScriptExecutionException

use of com.evolveum.midpoint.util.exception.ScriptExecutionException in project midpoint by Evolveum.

the class AbstractObjectBasedActionExecutor method castToObject.

@SuppressWarnings("ThrowableNotThrown")
private PrismObject<T> castToObject(PrismValue value, Class<T> expectedType, ExecutionContext context) throws ScriptExecutionException {
    if (value instanceof PrismObjectValue) {
        PrismObjectValue objectValue = (PrismObjectValue) value;
        Class<? extends Objectable> realType = objectValue.asObjectable().getClass();
        if (expectedType.isAssignableFrom(realType)) {
            // noinspection unchecked
            return objectValue.asPrismObject();
        } else {
            processActionException(new ScriptExecutionException("Item is not a PrismObject of " + expectedType.getName() + "; it is " + realType.getName() + " instead"), getActionName(), value, context);
            return null;
        }
    } else {
        processActionException(new ScriptExecutionException("Item is not a PrismObject"), getActionName(), value, context);
        return null;
    }
}
Also used : PrismObjectValue(com.evolveum.midpoint.prism.PrismObjectValue) ScriptExecutionException(com.evolveum.midpoint.util.exception.ScriptExecutionException)

Example 5 with ScriptExecutionException

use of com.evolveum.midpoint.util.exception.ScriptExecutionException in project midpoint by Evolveum.

the class ExpressionHelper method getSingleArgumentValue.

public <T> T getSingleArgumentValue(List<ActionParameterValueType> arguments, String parameterName, boolean required, boolean requiredNonNull, String context, PipelineData input, ExecutionContext executionContext, Class<T> clazz, OperationResult result) throws ScriptExecutionException, SchemaException, ObjectNotFoundException, SecurityViolationException, CommunicationException, ConfigurationException, ExpressionEvaluationException {
    ActionParameterValueType paramValue = getArgument(arguments, parameterName, required, requiredNonNull, context);
    if (paramValue == null) {
        return null;
    }
    PipelineData paramData = evaluateParameter(paramValue, clazz, input, executionContext, result);
    if (paramData.getData().size() != 1) {
        throw new ScriptExecutionException("Exactly one item was expected in '" + parameterName + "' parameter. Got " + paramData.getData().size());
    }
    PrismValue prismValue = paramData.getData().get(0).getValue();
    if (!(prismValue instanceof PrismPropertyValue)) {
        throw new ScriptExecutionException("A prism property value was expected in '" + parameterName + "' parameter. Got " + prismValue.getClass().getName() + " instead.");
    }
    Object value = ((PrismPropertyValue) prismValue).getValue();
    try {
        return JavaTypeConverter.convert(clazz, value);
    } catch (Throwable t) {
        throw new ScriptExecutionException("Couldn't retrieve value of parameter '" + parameterName + "': " + t.getMessage(), t);
    }
}
Also used : ScriptExecutionException(com.evolveum.midpoint.util.exception.ScriptExecutionException) PipelineData(com.evolveum.midpoint.model.impl.scripting.PipelineData) ActionParameterValueType(com.evolveum.midpoint.xml.ns._public.model.scripting_3.ActionParameterValueType) PrismValue(com.evolveum.midpoint.prism.PrismValue) PrismPropertyValue(com.evolveum.midpoint.prism.PrismPropertyValue)

Aggregations

ScriptExecutionException (com.evolveum.midpoint.util.exception.ScriptExecutionException)15 PipelineItem (com.evolveum.midpoint.model.api.PipelineItem)8 SchemaException (com.evolveum.midpoint.util.exception.SchemaException)7 PipelineData (com.evolveum.midpoint.model.impl.scripting.PipelineData)6 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)5 CommunicationException (com.evolveum.midpoint.util.exception.CommunicationException)4 ConfigurationException (com.evolveum.midpoint.util.exception.ConfigurationException)4 ExpressionEvaluationException (com.evolveum.midpoint.util.exception.ExpressionEvaluationException)4 ObjectNotFoundException (com.evolveum.midpoint.util.exception.ObjectNotFoundException)4 SecurityViolationException (com.evolveum.midpoint.util.exception.SecurityViolationException)4 PrismValue (com.evolveum.midpoint.prism.PrismValue)3 Operation (com.evolveum.midpoint.schema.statistics.Operation)3 ItemPath (com.evolveum.midpoint.prism.path.ItemPath)2 VariablesMap (com.evolveum.midpoint.schema.expression.VariablesMap)2 Task (com.evolveum.midpoint.task.api.Task)2 ConnectorType (com.evolveum.midpoint.xml.ns._public.common.common_3.ConnectorType)2 ResourceType (com.evolveum.midpoint.xml.ns._public.common.common_3.ResourceType)2 ScriptExecutionResult (com.evolveum.midpoint.model.api.ScriptExecutionResult)1 EvaluatedStateTrigger (com.evolveum.midpoint.model.api.context.EvaluatedStateTrigger)1 ValidationResult (com.evolveum.midpoint.model.api.validator.ValidationResult)1