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