use of com.evolveum.midpoint.repo.common.expression.ExpressionVariables in project midpoint by Evolveum.
the class GeneralNotifier method processEvent.
@Override
public boolean processEvent(Event event, EventHandlerType eventHandlerType, NotificationManager notificationManager, Task task, OperationResult parentResult) throws SchemaException {
OperationResult result = parentResult.createSubresult(GeneralNotifier.class.getName() + ".processEvent");
logStart(getLogger(), event, eventHandlerType);
boolean applies = aggregatedEventHandler.processEvent(event, eventHandlerType, notificationManager, task, result);
if (applies) {
GeneralNotifierType generalNotifierType = (GeneralNotifierType) eventHandlerType;
if (!quickCheckApplicability(event, generalNotifierType, result)) {
// nothing to do -- an appropriate message has to be logged in quickCheckApplicability method
} else {
if (!checkApplicability(event, generalNotifierType, result)) {
// nothing to do -- an appropriate message has to be logged in checkApplicability method
} else if (generalNotifierType.getTransport().isEmpty()) {
getLogger().warn("No transports for this notifier, exiting without sending any notifications.");
} else {
ExpressionVariables variables = getDefaultVariables(event, result);
if (event instanceof ModelEvent) {
((ModelEvent) event).getModelContext().reportProgress(new ProgressInformation(NOTIFICATIONS, ENTERING));
}
try {
for (String transportName : generalNotifierType.getTransport()) {
variables.addVariableDefinition(SchemaConstants.C_TRANSPORT_NAME, transportName);
Transport transport = notificationManager.getTransport(transportName);
List<String> recipientsAddresses = getRecipientsAddresses(event, generalNotifierType, variables, getDefaultRecipient(event, generalNotifierType, result), transportName, transport, task, result);
if (!recipientsAddresses.isEmpty()) {
String body = getBodyFromExpression(event, generalNotifierType, variables, task, result);
String subject = getSubjectFromExpression(event, generalNotifierType, variables, task, result);
String from = getFromFromExpression(event, generalNotifierType, variables, task, result);
String contentType = getContentTypeFromExpression(event, generalNotifierType, variables, task, result);
if (body == null) {
body = getBody(event, generalNotifierType, transportName, task, result);
}
if (subject == null) {
subject = generalNotifierType.getSubjectPrefix() != null ? generalNotifierType.getSubjectPrefix() : "";
subject += getSubject(event, generalNotifierType, transportName, task, result);
}
Message message = new Message();
message.setBody(body != null ? body : "");
if (contentType != null) {
message.setContentType(contentType);
} else if (generalNotifierType.getContentType() != null) {
message.setContentType(generalNotifierType.getContentType());
}
message.setSubject(subject);
if (from != null) {
message.setFrom(from);
}
message.setTo(recipientsAddresses);
message.setCc(getCcBccAddresses(generalNotifierType.getCcExpression(), variables, "notification cc-expression", task, result));
message.setBcc(getCcBccAddresses(generalNotifierType.getBccExpression(), variables, "notification bcc-expression", task, result));
getLogger().trace("Sending notification via transport {}:\n{}", transportName, message);
transport.send(message, transportName, event, task, result);
} else {
getLogger().info("No recipients addresses for transport " + transportName + ", message corresponding to event " + event.getId() + " will not be send.");
}
}
} finally {
if (event instanceof ModelEvent) {
((ModelEvent) event).getModelContext().reportProgress(new ProgressInformation(NOTIFICATIONS, result));
}
}
}
}
}
logEnd(getLogger(), event, eventHandlerType, applies);
result.computeStatusIfUnknown();
// not-applicable notifiers do not stop processing of other notifiers
return true;
}
use of com.evolveum.midpoint.repo.common.expression.ExpressionVariables in project midpoint by Evolveum.
the class ReportServiceImpl method evaluateAuditScript.
public Collection<AuditEventRecord> evaluateAuditScript(String script, Map<QName, Object> parameters) throws SchemaException, ExpressionEvaluationException, ObjectNotFoundException {
Collection<AuditEventRecord> results = new ArrayList<AuditEventRecord>();
ExpressionVariables variables = new ExpressionVariables();
variables.addVariableDefinition(new QName("auditParams"), getConvertedParams(parameters));
Task task = taskManager.createTaskInstance(ReportService.class.getName() + ".searchObjects()");
OperationResult parentResult = task.getResult();
Collection<FunctionLibrary> functions = createFunctionLibraries();
Jsr223ScriptEvaluator scripts = new Jsr223ScriptEvaluator("Groovy", prismContext, prismContext.getDefaultProtector());
ModelExpressionThreadLocalHolder.pushExpressionEnvironment(new ExpressionEnvironment<>(task, task.getResult()));
Object o = null;
try {
o = scripts.evaluateReportScript(script, variables, objectResolver, functions, "desc", parentResult);
} finally {
ModelExpressionThreadLocalHolder.popExpressionEnvironment();
}
if (o != null) {
if (Collection.class.isAssignableFrom(o.getClass())) {
Collection resultSet = (Collection) o;
if (resultSet != null && !resultSet.isEmpty()) {
for (Object obj : resultSet) {
if (!(obj instanceof AuditEventRecord)) {
LOGGER.warn("Skipping result, not an audit event record " + obj);
continue;
}
results.add((AuditEventRecord) obj);
}
}
} else {
results.add((AuditEventRecord) o);
}
}
return results;
}
use of com.evolveum.midpoint.repo.common.expression.ExpressionVariables in project midpoint by Evolveum.
the class ReportServiceImpl method parseQuery.
@Override
public ObjectQuery parseQuery(String query, Map<QName, Object> parameters) throws SchemaException, ObjectNotFoundException, ExpressionEvaluationException {
if (StringUtils.isBlank(query)) {
return null;
}
ObjectQuery parsedQuery;
try {
Task task = taskManager.createTaskInstance();
ModelExpressionThreadLocalHolder.pushExpressionEnvironment(new ExpressionEnvironment<>(task, task.getResult()));
SearchFilterType filter = prismContext.parserFor(query).parseRealValue(SearchFilterType.class);
LOGGER.trace("filter {}", filter);
ObjectFilter f = QueryConvertor.parseFilter(filter, UserType.class, prismContext);
LOGGER.trace("f {}", f.debugDump());
if (!(f instanceof TypeFilter)) {
throw new IllegalArgumentException("Defined query must contain type. Use 'type filter' in your report query.");
}
ObjectFilter subFilter = ((TypeFilter) f).getFilter();
ObjectQuery q = ObjectQuery.createObjectQuery(subFilter);
ExpressionVariables variables = new ExpressionVariables();
variables.addVariableDefinitions(parameters);
q = ExpressionUtil.evaluateQueryExpressions(q, variables, expressionFactory, prismContext, "parsing expression values for report", task, task.getResult());
((TypeFilter) f).setFilter(q.getFilter());
parsedQuery = ObjectQuery.createObjectQuery(f);
LOGGER.trace("query dump {}", parsedQuery.debugDump());
} catch (SchemaException | ObjectNotFoundException | ExpressionEvaluationException e) {
// TODO Auto-generated catch block
throw e;
} finally {
ModelExpressionThreadLocalHolder.popExpressionEnvironment();
}
return parsedQuery;
}
use of com.evolveum.midpoint.repo.common.expression.ExpressionVariables in project midpoint by Evolveum.
the class TestScriptCaching method executeScript.
private long executeScript(String filname, String expectedResult, String desc) throws SchemaException, IOException, JAXBException, ExpressionEvaluationException, ObjectNotFoundException {
// GIVEN
OperationResult result = new OperationResult(desc);
ScriptExpressionEvaluatorType scriptType = parseScriptType(filname);
ItemDefinition outputDefinition = new PrismPropertyDefinitionImpl(PROPERTY_NAME, DOMUtil.XSD_STRING, PrismTestUtil.getPrismContext());
ScriptExpression scriptExpression = scriptExpressionfactory.createScriptExpression(scriptType, outputDefinition, desc);
ExpressionVariables variables = ExpressionVariables.create(new QName(NS_WHATEVER, "foo"), "FOO", new QName(NS_WHATEVER, "bar"), "BAR");
// WHEN
long startTime = System.currentTimeMillis();
List<PrismPropertyValue<String>> scripResults = scriptExpression.evaluate(variables, null, false, desc, null, result);
long endTime = System.currentTimeMillis();
// THEN
System.out.println("Script results " + desc + ", etime: " + (endTime - startTime) + " ms");
System.out.println(scripResults);
String scriptResult = asScalarString(scripResults);
assertEquals("Wrong script " + desc + " result", expectedResult, scriptResult);
return (endTime - startTime);
}
use of com.evolveum.midpoint.repo.common.expression.ExpressionVariables in project midpoint by Evolveum.
the class Construction method resolveTarget.
private ResourceType resolveTarget(String sourceDescription, Task task, OperationResult result) throws SchemaException, ObjectNotFoundException, ExpressionEvaluationException, CommunicationException, ConfigurationException, SecurityViolationException {
// SearchFilterType filter = targetRef.getFilter();
ExpressionVariables variables = Utils.getDefaultExpressionVariables(getFocusOdo().getNewObject().asObjectable(), null, null, null);
if (assignmentPathVariables == null) {
assignmentPathVariables = LensUtil.computeAssignmentPathVariables(getAssignmentPath());
}
Utils.addAssignmentPathVariables(assignmentPathVariables, variables);
LOGGER.info("Expression variables for filter evaluation: {}", variables);
ObjectFilter origFilter = QueryConvertor.parseFilter(getConstructionType().getResourceRef().getFilter(), ResourceType.class, getPrismContext());
LOGGER.info("Orig filter {}", origFilter);
ObjectFilter evaluatedFilter = ExpressionUtil.evaluateFilterExpressions(origFilter, variables, getMappingFactory().getExpressionFactory(), getPrismContext(), " evaluating resource filter expression ", task, result);
LOGGER.info("evaluatedFilter filter {}", evaluatedFilter);
if (evaluatedFilter == null) {
throw new SchemaException("The OID is null and filter could not be evaluated in assignment targetRef in " + getSource());
}
final Collection<PrismObject<ResourceType>> results = new ArrayList<>();
ResultHandler<ResourceType> handler = (object, parentResult) -> {
LOGGER.info("Found object {}", object);
return results.add(object);
};
getObjectResolver().searchIterative(ResourceType.class, ObjectQuery.createObjectQuery(evaluatedFilter), null, handler, task, result);
if (org.apache.commons.collections.CollectionUtils.isEmpty(results)) {
throw new IllegalArgumentException("Got no target from repository, filter:" + evaluatedFilter + ", class:" + ResourceType.class + " in " + sourceDescription);
}
if (results.size() > 1) {
throw new IllegalArgumentException("Got more than one target from repository, filter:" + evaluatedFilter + ", class:" + ResourceType.class + " in " + sourceDescription);
}
PrismObject<ResourceType> target = results.iterator().next();
// assignmentType.getTargetRef().setOid(target.getOid());
return target.asObjectable();
}
Aggregations