use of com.evolveum.midpoint.repo.common.expression.ExpressionSyntaxException in project midpoint by Evolveum.
the class ScriptExecutor method execute.
@Override
public PipelineData execute(ActionExpressionType expression, PipelineData input, ExecutionContext context, OperationResult globalResult) throws ScriptExecutionException {
checkRootAuthorization(globalResult, NAME);
ScriptExpressionEvaluatorType script = expressionHelper.getSingleArgumentValue(expression.getParameter(), PARAM_SCRIPT, true, true, NAME, input, context, ScriptExpressionEvaluatorType.class, globalResult);
String outputItem = expressionHelper.getSingleArgumentValue(expression.getParameter(), PARAM_OUTPUT_ITEM, false, false, NAME, input, context, String.class, globalResult);
ItemDefinition<?> outputDefinition = getItemDefinition(outputItem);
ScriptExpression scriptExpression;
try {
scriptExpression = scriptExpressionFactory.createScriptExpression(script, outputDefinition, "script");
} catch (ExpressionSyntaxException e) {
throw new ScriptExecutionException("Couldn't parse script expression: " + e.getMessage(), e);
}
PipelineData output = PipelineData.createEmpty();
for (PipelineItem item : input.getData()) {
PrismValue value = item.getValue();
OperationResult result = operationsHelper.createActionResult(item, this, context, globalResult);
context.checkTaskStop();
String valueDescription;
long started;
if (value instanceof PrismObjectValue) {
started = operationsHelper.recordStart(context, asObjectType(value));
valueDescription = asObjectType(value).asPrismObject().toString();
} else {
started = 0;
valueDescription = value.toHumanReadableString();
}
Throwable exception = null;
try {
Object outObject = executeScript(scriptExpression, value, context, result);
if (outObject != null) {
addToData(outObject, item.getResult(), output);
} else {
output.add(item);
}
if (value instanceof PrismObjectValue) {
operationsHelper.recordEnd(context, asObjectType(value), started, null);
}
} catch (Throwable ex) {
if (value instanceof PrismObjectValue) {
operationsHelper.recordEnd(context, asObjectType(value), started, ex);
}
exception = processActionException(ex, NAME, value, context);
}
context.println((exception != null ? "Attempted to execute " : "Executed ") + "script on " + valueDescription + exceptionSuffix(exception));
operationsHelper.trimAndCloneResult(result, globalResult, context);
}
return output;
}
use of com.evolveum.midpoint.repo.common.expression.ExpressionSyntaxException in project midpoint by Evolveum.
the class LazyXPathVariableResolver method resolveVariable.
@Override
public Object resolveVariable(QName name) {
if (variables == null) {
return null;
}
if (name != null && (name.getNamespaceURI() == null || name.getNamespaceURI().isEmpty())) {
LOGGER.warn("Using variable without a namespace (" + name + "), possible namespace problem (e.g. missing namespace prefix declaration) in " + contextDescription);
}
// Note: null is a legal variable name here. It corresponds to the root node
Object variableValue = variables.get(name);
if (variableValue == null) {
// TODO: warning ???
return null;
}
QName type = null;
// Attempt to resolve object reference
if (objectResolver != null && variableValue instanceof ObjectReferenceType) {
ObjectReferenceType ref = (ObjectReferenceType) variableValue;
if (ref.getOid() == null) {
SchemaException newEx = new SchemaException("Null OID in reference in variable " + name + " in " + contextDescription, name);
throw new TunnelException(newEx);
} else {
type = ref.getType();
try {
// TODO task
variableValue = objectResolver.resolve(ref, ObjectType.class, null, contextDescription, null, result);
} catch (ObjectNotFoundException e) {
ObjectNotFoundException newEx = new ObjectNotFoundException("Object not found during variable " + name + " resolution in " + contextDescription + ": " + e.getMessage(), e, ref.getOid());
// We have no other practical way how to handle the error
throw new TunnelException(newEx);
} catch (SchemaException e) {
ExpressionSyntaxException newEx = new ExpressionSyntaxException("Schema error during variable " + name + " resolution in " + contextDescription + ": " + e.getMessage(), e, name);
throw new TunnelException(newEx);
}
}
}
try {
return convertToXml(variableValue, name, prismContext, contextDescription);
} catch (SchemaException e) {
throw new TunnelException(e);
}
}
use of com.evolveum.midpoint.repo.common.expression.ExpressionSyntaxException in project midpoint by Evolveum.
the class XPathScriptEvaluator method evaluate.
private Object evaluate(QName returnType, String code, ExpressionVariables variables, ObjectResolver objectResolver, Collection<FunctionLibrary> functions, String contextDescription, OperationResult result) throws ExpressionEvaluationException, ObjectNotFoundException, ExpressionSyntaxException {
XPathExpressionCodeHolder codeHolder = new XPathExpressionCodeHolder(code);
//System.out.println("code " + code);
XPath xpath = factory.newXPath();
XPathVariableResolver variableResolver = new LazyXPathVariableResolver(variables, objectResolver, contextDescription, prismContext, result);
xpath.setXPathVariableResolver(variableResolver);
xpath.setNamespaceContext(new MidPointNamespaceContext(codeHolder.getNamespaceMap()));
xpath.setXPathFunctionResolver(getFunctionResolver(functions));
XPathExpression expr;
try {
expr = xpath.compile(codeHolder.getExpressionAsString());
} catch (Exception e) {
Throwable originalException = ExceptionUtil.lookForTunneledException(e);
if (originalException != null && originalException instanceof ObjectNotFoundException) {
throw (ObjectNotFoundException) originalException;
}
if (originalException != null && originalException instanceof ExpressionSyntaxException) {
throw (ExpressionSyntaxException) originalException;
}
if (e instanceof XPathExpressionException) {
throw createExpressionEvaluationException(e, contextDescription);
}
if (e instanceof RuntimeException) {
throw (RuntimeException) e;
}
throw new SystemException(e.getMessage(), e);
}
Object rootNode;
try {
rootNode = determineRootNode(variableResolver, contextDescription);
} catch (SchemaException e) {
throw new ExpressionSyntaxException(e.getMessage(), e);
}
Object evaluatedExpression;
try {
evaluatedExpression = expr.evaluate(rootNode, returnType);
} catch (Exception e) {
Throwable originalException = ExceptionUtil.lookForTunneledException(e);
if (originalException != null && originalException instanceof ObjectNotFoundException) {
throw (ObjectNotFoundException) originalException;
}
if (originalException != null && originalException instanceof ExpressionSyntaxException) {
throw (ExpressionSyntaxException) originalException;
}
if (e instanceof XPathExpressionException) {
throw createExpressionEvaluationException(e, contextDescription);
}
if (e instanceof RuntimeException) {
throw (RuntimeException) e;
}
throw new SystemException(e.getMessage(), e);
}
if (evaluatedExpression == null) {
return null;
}
return evaluatedExpression;
}
use of com.evolveum.midpoint.repo.common.expression.ExpressionSyntaxException in project midpoint by Evolveum.
the class AbstractValueTransformationExpressionEvaluator method evaluateScriptExpression.
private Collection<V> evaluateScriptExpression(Collection<Source<?, ?>> sources, ExpressionVariables variables, String contextDescription, boolean useNew, ExpressionEvaluationContext params, Task task, OperationResult result) throws ExpressionEvaluationException, ObjectNotFoundException, SchemaException {
ExpressionVariables scriptVariables = new ExpressionVariables();
if (useNew) {
scriptVariables.addVariableDefinitionsNew(variables);
} else {
scriptVariables.addVariableDefinitionsOld(variables);
}
if (sources != null) {
// Add sources to variables
for (Source<?, ?> source : sources) {
LOGGER.trace("source: {}", source);
QName name = source.getName();
if (name == null) {
if (sources.size() == 1) {
name = ExpressionConstants.VAR_INPUT;
} else {
throw new ExpressionSyntaxException("No name definition for source in " + contextDescription);
}
}
Object value = null;
if (useNew) {
value = getRealContent(source.getItemNew(), source.getResidualPath());
} else {
value = getRealContent(source.getItemOld(), source.getResidualPath());
}
scriptVariables.addVariableDefinition(name, value);
}
}
List<V> scriptResults = transformSingleValue(scriptVariables, null, useNew, params, (useNew ? "(new) " : "(old) ") + contextDescription, task, result);
if (scriptResults == null || scriptResults.isEmpty()) {
return null;
}
Collection<V> outputSet = new ArrayList<V>(scriptResults.size());
for (V pval : scriptResults) {
if (pval instanceof PrismPropertyValue<?>) {
if (((PrismPropertyValue<?>) pval).getValue() == null) {
continue;
}
Object realValue = ((PrismPropertyValue<?>) pval).getValue();
if (realValue instanceof String) {
if (((String) realValue).isEmpty()) {
continue;
}
}
if (realValue instanceof PolyString) {
if (((PolyString) realValue).isEmpty()) {
continue;
}
}
}
outputSet.add(pval);
}
return outputSet;
}
Aggregations