Search in sources :

Example 6 with ScriptExecutionException

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

the class ResolveExecutor method resolveReference.

private void resolveReference(ExecutionContext context, Collection<SelectorOptions<GetOperationOptions>> options, PipelineData output, PipelineItem item, PrismValue value, OperationResult result) throws ScriptExecutionException {
    if (value instanceof PrismReferenceValue) {
        PrismReferenceValue prismReferenceValue = (PrismReferenceValue) value;
        String oid = prismReferenceValue.getOid();
        QName targetTypeQName = prismReferenceValue.getTargetType();
        if (targetTypeQName == null) {
            throw new ScriptExecutionException("Couldn't resolve reference, because target type is unknown: " + prismReferenceValue);
        }
        Class<? extends ObjectType> type = prismContext.getSchemaRegistry().determineCompileTimeClass(targetTypeQName);
        if (type == null) {
            throw new ScriptExecutionException("Couldn't resolve reference, because target type class is unknown for target type " + targetTypeQName);
        }
        try {
            PrismObjectValue<? extends ObjectType> resolved = modelService.getObject(type, oid, options, context.getTask(), result).getValue();
            output.add(new PipelineItem(resolved, item.getResult()));
        } catch (Throwable e) {
            // noinspection ThrowableNotThrown
            processActionException(e, NAME, value, context);
            // to keep track of failed item (may trigger exceptions downstream)
            output.add(item);
        }
    } else {
        // noinspection ThrowableNotThrown
        processActionException(new ScriptExecutionException("Value is not a reference"), NAME, value, context);
        // to keep track of failed item (may trigger exceptions downstream)
        output.add(item);
    }
}
Also used : PrismReferenceValue(com.evolveum.midpoint.prism.PrismReferenceValue) PipelineItem(com.evolveum.midpoint.model.api.PipelineItem) ScriptExecutionException(com.evolveum.midpoint.util.exception.ScriptExecutionException) QName(javax.xml.namespace.QName)

Example 7 with ScriptExecutionException

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

the class ValidateExecutor method execute.

@Override
public PipelineData execute(ActionExpressionType expression, PipelineData input, ExecutionContext context, OperationResult globalResult) throws ScriptExecutionException {
    PipelineData output = PipelineData.createEmpty();
    for (PipelineItem item : input.getData()) {
        PrismValue value = item.getValue();
        OperationResult result = operationsHelper.createActionResult(item, this, globalResult);
        context.checkTaskStop();
        if (value instanceof PrismObjectValue && ((PrismObjectValue<?>) value).asObjectable() instanceof ResourceType) {
            // noinspection unchecked
            PrismObject<ResourceType> resourceTypePrismObject = ((PrismObjectValue<ResourceType>) value).asPrismObject();
            ResourceType resourceType = resourceTypePrismObject.asObjectable();
            Operation op = operationsHelper.recordStart(context, resourceType);
            try {
                ValidationResult validationResult = resourceValidator.validate(resourceTypePrismObject, Scope.THOROUGH, null, context.getTask(), result);
                PrismContainerDefinition<ValidationResultType> pcd = prismContext.getSchemaRegistry().findContainerDefinitionByElementName(SchemaConstantsGenerated.C_VALIDATION_RESULT);
                PrismContainer<ValidationResultType> pc = pcd.instantiate();
                // noinspection unchecked
                pc.add(validationResult.toValidationResultType().asPrismContainerValue());
                context.println("Validated " + resourceTypePrismObject + ": " + validationResult.getIssues().size() + " issue(s)");
                operationsHelper.recordEnd(context, op, null, result);
                output.add(new PipelineItem(pc.getValue(), item.getResult()));
            } catch (SchemaException | RuntimeException e) {
                operationsHelper.recordEnd(context, op, e, result);
                context.println("Error validation " + resourceTypePrismObject + ": " + e.getMessage());
                // noinspection ThrowableNotThrown
                processActionException(e, NAME, value, context);
                output.add(item);
            }
        } else {
            // noinspection ThrowableNotThrown
            processActionException(new ScriptExecutionException("Item is not a PrismObject<ResourceType>"), NAME, value, context);
        }
        operationsHelper.trimAndCloneResult(result, item.getResult());
    }
    return output;
}
Also used : SchemaException(com.evolveum.midpoint.util.exception.SchemaException) ScriptExecutionException(com.evolveum.midpoint.util.exception.ScriptExecutionException) PipelineData(com.evolveum.midpoint.model.impl.scripting.PipelineData) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) ResourceType(com.evolveum.midpoint.xml.ns._public.common.common_3.ResourceType) Operation(com.evolveum.midpoint.schema.statistics.Operation) ValidationResult(com.evolveum.midpoint.model.api.validator.ValidationResult) PipelineItem(com.evolveum.midpoint.model.api.PipelineItem) ValidationResultType(com.evolveum.midpoint.xml.ns._public.common.common_3.ValidationResultType)

Example 8 with ScriptExecutionException

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

the class SelectEvaluator method evaluate.

public PipelineData evaluate(SelectExpressionType selectExpression, PipelineData input, ExecutionContext context, OperationResult result) throws ScriptExecutionException {
    if (selectExpression.getPath() == null) {
        return input;
    }
    ItemPath path = selectExpression.getPath().getItemPath();
    PipelineData output = PipelineData.createEmpty();
    for (PipelineItem item : input.getData()) {
        Object o = item.getValue().find(path);
        if (o != null) {
            if (o instanceof Item) {
                List<? extends PrismValue> values = ((Item<? extends PrismValue, ?>) o).getValues();
                values.forEach((v) -> output.addValue(v, item.getResult().clone(), item.getVariables()));
            } else {
                throw new ScriptExecutionException("In 'select' commands, only property/container/reference selection is supported for now. Select on '" + path + "' returned this instead: " + o);
            }
        }
    }
    return output;
}
Also used : Item(com.evolveum.midpoint.prism.Item) PipelineItem(com.evolveum.midpoint.model.api.PipelineItem) PipelineItem(com.evolveum.midpoint.model.api.PipelineItem) ScriptExecutionException(com.evolveum.midpoint.util.exception.ScriptExecutionException) PipelineData(com.evolveum.midpoint.model.impl.scripting.PipelineData) PrismValue(com.evolveum.midpoint.prism.PrismValue) ItemPath(com.evolveum.midpoint.prism.path.ItemPath)

Example 9 with ScriptExecutionException

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

the class PageBulkAction method startPerformed.

private void startPerformed(AjaxRequestTarget target) {
    Task task = createSimpleTask(OPERATION_PERFORM_BULK);
    OperationResult result = new OperationResult(OPERATION_PERFORM_BULK);
    BulkActionDto bulkActionDto = model.getObject();
    if (StringUtils.isEmpty(bulkActionDto.getScript())) {
        warn(getString("PageBulkAction.message.emptyString"));
        target.add(getFeedbackPanel());
        return;
    }
    Object parsed = null;
    try {
        parsed = getPrismContext().parserFor(bulkActionDto.getScript()).parseRealValue();
        if (parsed == null) {
            result.recordFatalError(createStringResource("PageBulkAction.message.startPerformed.fatalError.provided").getString());
        } else if (!(parsed instanceof ExecuteScriptType) && !(parsed instanceof ScriptingExpressionType)) {
            result.recordFatalError(createStringResource("PageBulkAction.message.startPerformed.fatalError.notBulkAction", "{scripting-3}ScriptingExpressionType", parsed.getClass()).getString());
        }
    } catch (SchemaException | RuntimeException e) {
        result.recordFatalError(createStringResource("PageBulkAction.message.startPerformed.fatalError.parse").getString(), e);
        LoggingUtils.logUnexpectedException(LOGGER, "Couldn't parse bulk action object", e);
    }
    if (parsed != null) {
        if (bulkActionDto.isAsync()) {
            try {
                if (parsed instanceof ExecuteScriptType) {
                    getScriptingService().evaluateExpressionInBackground((ExecuteScriptType) parsed, task, result);
                } else {
                    // noinspection ConstantConditions
                    getScriptingService().evaluateExpressionInBackground((ScriptingExpressionType) parsed, task, result);
                }
                result.recordStatus(OperationResultStatus.IN_PROGRESS, createStringResource("PageBulkAction.message.startPerformed.inProgress", task.getName()).getString());
            } catch (SchemaException | SecurityViolationException | ExpressionEvaluationException | ObjectNotFoundException | CommunicationException | ConfigurationException | ClassCastException e) {
                result.recordFatalError(createStringResource("PageBulkAction.message.startPerformed.fatalError.submit").getString(), e);
                LoggingUtils.logUnexpectedException(LOGGER, "Couldn't submit bulk action to execution", e);
            }
        } else {
            try {
                // noinspection ConstantConditions
                ScriptExecutionResult executionResult = parsed instanceof ExecuteScriptType ? getScriptingService().evaluateExpression((ExecuteScriptType) parsed, VariablesMap.emptyMap(), false, task, result) : getScriptingService().evaluateExpression((ScriptingExpressionType) parsed, task, result);
                result.recordStatus(OperationResultStatus.SUCCESS, createStringResource("PageBulkAction.message.startPerformed.success", executionResult.getDataOutput().size()).getString());
                result.addReturn("console", executionResult.getConsoleOutput());
                result.addArbitraryObjectCollectionAsReturn("data", executionResult.getDataOutput());
            } catch (ScriptExecutionException | SchemaException | SecurityViolationException | ExpressionEvaluationException | ObjectNotFoundException | CommunicationException | ConfigurationException | ClassCastException e) {
                result.recordFatalError(createStringResource("PageBulkAction.message.startPerformed.fatalError.execute").getString(), e);
                LoggingUtils.logUnexpectedException(LOGGER, "Couldn't execute bulk action", e);
            }
        }
    }
    showResult(result);
    target.add(getFeedbackPanel());
}
Also used : Task(com.evolveum.midpoint.task.api.Task) ExecuteScriptType(com.evolveum.midpoint.xml.ns._public.model.scripting_3.ExecuteScriptType) ScriptExecutionException(com.evolveum.midpoint.util.exception.ScriptExecutionException) ScriptExecutionResult(com.evolveum.midpoint.model.api.ScriptExecutionResult) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) ScriptingExpressionType(com.evolveum.midpoint.xml.ns._public.model.scripting_3.ScriptingExpressionType) BulkActionDto(com.evolveum.midpoint.web.page.admin.configuration.dto.BulkActionDto)

Example 10 with ScriptExecutionException

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

the class DiscoverConnectorsExecutor method determineConnectorMappings.

private void determineConnectorMappings(Map<String, String> rebindMap, ConnectorType connectorType, ExecutionContext context, OperationResult result) throws ScriptExecutionException {
    if (LOGGER.isTraceEnabled()) {
        LOGGER.trace("Finding obsolete versions for connector: {}", connectorType.asPrismObject().debugDump());
    }
    ObjectQuery query = prismContext.queryFor(ConnectorType.class).item(SchemaConstants.C_CONNECTOR_FRAMEWORK).eq(connectorType.getFramework()).and().item(SchemaConstants.C_CONNECTOR_CONNECTOR_TYPE).eq(connectorType.getConnectorType()).build();
    List<PrismObject<ConnectorType>> foundConnectors;
    try {
        foundConnectors = modelService.searchObjects(ConnectorType.class, query, null, null, result);
    } catch (SchemaException | ConfigurationException | ObjectNotFoundException | CommunicationException | SecurityViolationException | ExpressionEvaluationException e) {
        throw new ScriptExecutionException("Couldn't get connectors of type: " + connectorType.getConnectorType() + ": " + e.getMessage(), e);
    }
    for (PrismObject<ConnectorType> foundConnector : foundConnectors) {
        ConnectorType foundConnectorType = foundConnector.asObjectable();
        // TODO temporary hack. fix it after MID-3355 is implemented.
        if (connectorType.getConnectorHostRef() != null && connectorType.getConnectorHostRef().asReferenceValue().getObject() != null) {
            String connectorHostOid = connectorType.getConnectorHostRef().getOid();
            connectorType.getConnectorHostRef().asReferenceValue().setObject(null);
            connectorType.getConnectorHostRef().setOid(connectorHostOid);
        }
        if (connectorType.getConnectorHostRef().equals(foundConnectorType.getConnectorHostRef()) && foundConnectorType.getConnectorVersion() != null && !foundConnectorType.getConnectorVersion().equals(connectorType.getConnectorVersion())) {
            if (LOGGER.isTraceEnabled()) {
                LOGGER.trace("Found obsolete connector:\n{}", foundConnectorType.asPrismObject().debugDump(1));
            }
            rebindMap.put(foundConnectorType.getOid(), connectorType.getOid());
        }
    }
}
Also used : SchemaException(com.evolveum.midpoint.util.exception.SchemaException) ExpressionEvaluationException(com.evolveum.midpoint.util.exception.ExpressionEvaluationException) ConnectorType(com.evolveum.midpoint.xml.ns._public.common.common_3.ConnectorType) CommunicationException(com.evolveum.midpoint.util.exception.CommunicationException) SecurityViolationException(com.evolveum.midpoint.util.exception.SecurityViolationException) ScriptExecutionException(com.evolveum.midpoint.util.exception.ScriptExecutionException) ObjectQuery(com.evolveum.midpoint.prism.query.ObjectQuery) ConfigurationException(com.evolveum.midpoint.util.exception.ConfigurationException) ObjectNotFoundException(com.evolveum.midpoint.util.exception.ObjectNotFoundException)

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