Search in sources :

Example 36 with ExpressionVariables

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;
}
Also used : ExpressionVariables(com.evolveum.midpoint.repo.common.expression.ExpressionVariables) ProgressInformation(com.evolveum.midpoint.model.api.ProgressInformation) Message(com.evolveum.midpoint.notifications.api.transports.Message) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) ArrayList(java.util.ArrayList) List(java.util.List) PolyString(com.evolveum.midpoint.prism.polystring.PolyString) Transport(com.evolveum.midpoint.notifications.api.transports.Transport) ModelEvent(com.evolveum.midpoint.notifications.api.events.ModelEvent)

Example 37 with ExpressionVariables

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;
}
Also used : ExpressionVariables(com.evolveum.midpoint.repo.common.expression.ExpressionVariables) Jsr223ScriptEvaluator(com.evolveum.midpoint.model.common.expression.script.jsr223.Jsr223ScriptEvaluator) Task(com.evolveum.midpoint.task.api.Task) QName(javax.xml.namespace.QName) ArrayList(java.util.ArrayList) FunctionLibrary(com.evolveum.midpoint.model.common.expression.functions.FunctionLibrary) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) Collection(java.util.Collection) PrismObject(com.evolveum.midpoint.prism.PrismObject) AuditEventRecord(com.evolveum.midpoint.audit.api.AuditEventRecord)

Example 38 with ExpressionVariables

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;
}
Also used : ExpressionVariables(com.evolveum.midpoint.repo.common.expression.ExpressionVariables) SchemaException(com.evolveum.midpoint.util.exception.SchemaException) Task(com.evolveum.midpoint.task.api.Task) ExpressionEvaluationException(com.evolveum.midpoint.util.exception.ExpressionEvaluationException) SearchFilterType(com.evolveum.prism.xml.ns._public.query_3.SearchFilterType) ObjectNotFoundException(com.evolveum.midpoint.util.exception.ObjectNotFoundException) TypeFilter(com.evolveum.midpoint.prism.query.TypeFilter) ObjectFilter(com.evolveum.midpoint.prism.query.ObjectFilter) ObjectQuery(com.evolveum.midpoint.prism.query.ObjectQuery)

Example 39 with ExpressionVariables

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);
}
Also used : ExpressionVariables(com.evolveum.midpoint.repo.common.expression.ExpressionVariables) QName(javax.xml.namespace.QName) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) ScriptExpressionEvaluatorType(com.evolveum.midpoint.xml.ns._public.common.common_3.ScriptExpressionEvaluatorType)

Example 40 with ExpressionVariables

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();
}
Also used : ExpressionVariables(com.evolveum.midpoint.repo.common.expression.ExpressionVariables) PrismValue(com.evolveum.midpoint.prism.PrismValue) ResourceAttributeDefinition(com.evolveum.midpoint.schema.processor.ResourceAttributeDefinition) ResourceObjectAssociationType(com.evolveum.midpoint.xml.ns._public.common.common_3.ResourceObjectAssociationType) ObjectType(com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectType) ConfigurationException(com.evolveum.midpoint.util.exception.ConfigurationException) SchemaException(com.evolveum.midpoint.util.exception.SchemaException) PrismPropertyValue(com.evolveum.midpoint.prism.PrismPropertyValue) ConstructionType(com.evolveum.midpoint.xml.ns._public.common.common_3.ConstructionType) ExpressionConstants(com.evolveum.midpoint.schema.constants.ExpressionConstants) SystemConfigurationType(com.evolveum.midpoint.xml.ns._public.common.common_3.SystemConfigurationType) MappingFactory(com.evolveum.midpoint.model.common.mapping.MappingFactory) PrismValueDeltaSetTriple(com.evolveum.midpoint.prism.delta.PrismValueDeltaSetTriple) OriginType(com.evolveum.midpoint.prism.OriginType) ResultHandler(com.evolveum.midpoint.schema.ResultHandler) Utils(com.evolveum.midpoint.model.impl.util.Utils) ItemPathUtil(com.evolveum.midpoint.prism.util.ItemPathUtil) Mapping(com.evolveum.midpoint.model.common.mapping.Mapping) ObjectNotFoundException(com.evolveum.midpoint.util.exception.ObjectNotFoundException) Collection(java.util.Collection) MappingEvaluator(com.evolveum.midpoint.model.impl.lens.projector.MappingEvaluator) Task(com.evolveum.midpoint.task.api.Task) ShadowKindType(com.evolveum.midpoint.xml.ns._public.common.common_3.ShadowKindType) List(java.util.List) ExpressionVariables(com.evolveum.midpoint.repo.common.expression.ExpressionVariables) SystemException(com.evolveum.midpoint.util.exception.SystemException) FocusType(com.evolveum.midpoint.xml.ns._public.common.common_3.FocusType) ExpressionUtil(com.evolveum.midpoint.repo.common.expression.ExpressionUtil) RefinedAssociationDefinition(com.evolveum.midpoint.common.refinery.RefinedAssociationDefinition) CommunicationException(com.evolveum.midpoint.util.exception.CommunicationException) QName(javax.xml.namespace.QName) ShadowType(com.evolveum.midpoint.xml.ns._public.common.common_3.ShadowType) MappingType(com.evolveum.midpoint.xml.ns._public.common.common_3.MappingType) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) ItemDefinition(com.evolveum.midpoint.prism.ItemDefinition) Trace(com.evolveum.midpoint.util.logging.Trace) DebugUtil(com.evolveum.midpoint.util.DebugUtil) PrettyPrinter(com.evolveum.midpoint.util.PrettyPrinter) ObjectFilter(com.evolveum.midpoint.prism.query.ObjectFilter) ExpressionEvaluationException(com.evolveum.midpoint.util.exception.ExpressionEvaluationException) ArrayList(java.util.ArrayList) PrismObjectDefinition(com.evolveum.midpoint.prism.PrismObjectDefinition) ResourceAttributeDefinitionType(com.evolveum.midpoint.xml.ns._public.common.common_3.ResourceAttributeDefinitionType) RefinedResourceSchema(com.evolveum.midpoint.common.refinery.RefinedResourceSchema) SecurityViolationException(com.evolveum.midpoint.util.exception.SecurityViolationException) RefinedResourceSchemaImpl(com.evolveum.midpoint.common.refinery.RefinedResourceSchemaImpl) ObjectTypeUtil(com.evolveum.midpoint.schema.util.ObjectTypeUtil) PrismContainerDefinition(com.evolveum.midpoint.prism.PrismContainerDefinition) RefinedAttributeDefinition(com.evolveum.midpoint.common.refinery.RefinedAttributeDefinition) ConstructionStrengthType(com.evolveum.midpoint.xml.ns._public.common.common_3.ConstructionStrengthType) PrismPropertyDefinition(com.evolveum.midpoint.prism.PrismPropertyDefinition) ShadowAssociationType(com.evolveum.midpoint.xml.ns._public.common.common_3.ShadowAssociationType) PrismObject(com.evolveum.midpoint.prism.PrismObject) RefinedObjectClassDefinition(com.evolveum.midpoint.common.refinery.RefinedObjectClassDefinition) QueryConvertor(com.evolveum.midpoint.prism.marshaller.QueryConvertor) PrismContainerValue(com.evolveum.midpoint.prism.PrismContainerValue) LayerType(com.evolveum.midpoint.xml.ns._public.common.common_3.LayerType) ResourceType(com.evolveum.midpoint.xml.ns._public.common.common_3.ResourceType) ObjectQuery(com.evolveum.midpoint.prism.query.ObjectQuery) TraceManager(com.evolveum.midpoint.util.logging.TraceManager) SchemaException(com.evolveum.midpoint.util.exception.SchemaException) PrismObject(com.evolveum.midpoint.prism.PrismObject) ArrayList(java.util.ArrayList) ResourceType(com.evolveum.midpoint.xml.ns._public.common.common_3.ResourceType) ObjectFilter(com.evolveum.midpoint.prism.query.ObjectFilter)

Aggregations

ExpressionVariables (com.evolveum.midpoint.repo.common.expression.ExpressionVariables)65 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)17 QName (javax.xml.namespace.QName)15 ExpressionEvaluationException (com.evolveum.midpoint.util.exception.ExpressionEvaluationException)10 SchemaException (com.evolveum.midpoint.util.exception.SchemaException)10 Test (org.testng.annotations.Test)10 AbstractInternalModelIntegrationTest (com.evolveum.midpoint.model.impl.AbstractInternalModelIntegrationTest)9 PrismPropertyValue (com.evolveum.midpoint.prism.PrismPropertyValue)9 ExpressionEvaluationContext (com.evolveum.midpoint.repo.common.expression.ExpressionEvaluationContext)9 Task (com.evolveum.midpoint.task.api.Task)8 ArrayList (java.util.ArrayList)8 ObjectNotFoundException (com.evolveum.midpoint.util.exception.ObjectNotFoundException)7 PrismObject (com.evolveum.midpoint.prism.PrismObject)6 PrismPropertyDefinitionImpl (com.evolveum.midpoint.prism.PrismPropertyDefinitionImpl)6 ExpressionType (com.evolveum.midpoint.xml.ns._public.common.common_3.ExpressionType)6 PrismContext (com.evolveum.midpoint.prism.PrismContext)5 ObjectFilter (com.evolveum.midpoint.prism.query.ObjectFilter)5 SystemException (com.evolveum.midpoint.util.exception.SystemException)5 UserType (com.evolveum.midpoint.xml.ns._public.common.common_3.UserType)5 ScriptExpression (com.evolveum.midpoint.model.common.expression.script.ScriptExpression)4