Search in sources :

Example 1 with ReadHook

use of com.evolveum.midpoint.model.api.hooks.ReadHook in project midpoint by Evolveum.

the class ModelObjectResolver method getObject.

public <T extends ObjectType> T getObject(Class<T> clazz, String oid, Collection<SelectorOptions<GetOperationOptions>> options, Task task, OperationResult result) throws ObjectNotFoundException, CommunicationException, SchemaException, ConfigurationException, SecurityViolationException, ExpressionEvaluationException {
    T objectType = null;
    try {
        PrismObject<T> object = null;
        ObjectTypes.ObjectManager manager = ObjectTypes.getObjectManagerForClass(clazz);
        final GetOperationOptions rootOptions = SelectorOptions.findRootOptions(options);
        switch(manager) {
            case PROVISIONING:
                object = provisioning.getObject(clazz, oid, options, task, result);
                if (object == null) {
                    throw new SystemException("Got null result from provisioning.getObject while looking for " + clazz.getSimpleName() + " with OID " + oid + "; using provisioning implementation " + provisioning.getClass().getName());
                }
                break;
            case TASK_MANAGER:
                object = taskManager.getObject(clazz, oid, options, result);
                if (object == null) {
                    throw new SystemException("Got null result from taskManager.getObject while looking for " + clazz.getSimpleName() + " with OID " + oid + "; using task manager implementation " + taskManager.getClass().getName());
                }
                if (workflowManager != null && TaskType.class.isAssignableFrom(clazz) && !GetOperationOptions.isRaw(rootOptions) && !GetOperationOptions.isNoFetch(rootOptions)) {
                    workflowManager.augmentTaskObject(object, options, task, result);
                }
                break;
            default:
                object = cacheRepositoryService.getObject(clazz, oid, options, result);
                if (object == null) {
                    throw new SystemException("Got null result from repository.getObject while looking for " + clazz.getSimpleName() + " with OID " + oid + "; using repository implementation " + cacheRepositoryService.getClass().getName());
                }
        }
        objectType = object.asObjectable();
        if (!clazz.isInstance(objectType)) {
            throw new ObjectNotFoundException("Bad object type returned for referenced oid '" + oid + "'. Expected '" + clazz + "', but was '" + (objectType == null ? "null" : objectType.getClass()) + "'.");
        }
        if (hookRegistry != null) {
            for (ReadHook hook : hookRegistry.getAllReadHooks()) {
                hook.invoke(object, options, task, result);
            }
        }
    } catch (SystemException | ObjectNotFoundException | CommunicationException | ConfigurationException | SecurityViolationException | ExpressionEvaluationException ex) {
        result.recordFatalError(ex);
        throw ex;
    } catch (RuntimeException | Error ex) {
        LoggingUtils.logException(LOGGER, "Error resolving object with oid {}, expected type was {}.", ex, oid, clazz);
        throw new SystemException("Error resolving object with oid '" + oid + "': " + ex.getMessage(), ex);
    } finally {
        result.computeStatus();
    }
    return objectType;
}
Also used : ExpressionEvaluationException(com.evolveum.midpoint.util.exception.ExpressionEvaluationException) CommunicationException(com.evolveum.midpoint.util.exception.CommunicationException) SecurityViolationException(com.evolveum.midpoint.util.exception.SecurityViolationException) ReadHook(com.evolveum.midpoint.model.api.hooks.ReadHook) ObjectTypes(com.evolveum.midpoint.schema.constants.ObjectTypes) GetOperationOptions(com.evolveum.midpoint.schema.GetOperationOptions) SystemException(com.evolveum.midpoint.util.exception.SystemException) ConfigurationException(com.evolveum.midpoint.util.exception.ConfigurationException) ObjectNotFoundException(com.evolveum.midpoint.util.exception.ObjectNotFoundException)

Example 2 with ReadHook

use of com.evolveum.midpoint.model.api.hooks.ReadHook in project midpoint by Evolveum.

the class ModelController method searchObjects.

@Override
public <T extends ObjectType> SearchResultList<PrismObject<T>> searchObjects(Class<T> type, ObjectQuery query, Collection<SelectorOptions<GetOperationOptions>> options, Task task, OperationResult parentResult) throws SchemaException, ObjectNotFoundException, CommunicationException, ConfigurationException, SecurityViolationException, ExpressionEvaluationException {
    Validate.notNull(type, "Object type must not be null.");
    Validate.notNull(parentResult, "Operation result must not be null.");
    if (query != null) {
        ModelUtils.validatePaging(query.getPaging());
    }
    GetOperationOptions rootOptions = SelectorOptions.findRootOptions(options);
    ObjectTypes.ObjectManager searchProvider = ObjectTypes.getObjectManagerForClass(type);
    if (searchProvider == null || searchProvider == ObjectTypes.ObjectManager.MODEL || GetOperationOptions.isRaw(rootOptions)) {
        searchProvider = ObjectTypes.ObjectManager.REPOSITORY;
    }
    OperationResult result = parentResult.createSubresult(SEARCH_OBJECTS);
    result.addParams(new String[] { "query", "paging", "searchProvider" }, query, (query != null ? query.getPaging() : "undefined"), searchProvider);
    query = preProcessQuerySecurity(type, query);
    if (isFilterNone(query, result)) {
        return new SearchResultList(new ArrayList<>());
    }
    SearchResultList<PrismObject<T>> list;
    try {
        RepositoryCache.enter();
        logQuery(query);
        try {
            if (GetOperationOptions.isRaw(rootOptions)) {
                // MID-2218
                QNameUtil.setTemporarilyTolerateUndeclaredPrefixes(true);
            }
            switch(searchProvider) {
                case REPOSITORY:
                    list = cacheRepositoryService.searchObjects(type, query, options, result);
                    break;
                case PROVISIONING:
                    list = provisioning.searchObjects(type, query, options, task, result);
                    break;
                case TASK_MANAGER:
                    list = taskManager.searchObjects(type, query, options, result);
                    if (workflowManager != null && TaskType.class.isAssignableFrom(type) && !GetOperationOptions.isRaw(rootOptions) && !GetOperationOptions.isNoFetch(rootOptions)) {
                        workflowManager.augmentTaskObjectList(list, options, task, result);
                    }
                    break;
                default:
                    throw new AssertionError("Unexpected search provider: " + searchProvider);
            }
            result.computeStatus();
            result.cleanupResult();
        } catch (CommunicationException | ConfigurationException | SchemaException | SecurityViolationException | RuntimeException | ObjectNotFoundException e) {
            processSearchException(e, rootOptions, searchProvider, result);
            throw e;
        } finally {
            QNameUtil.setTemporarilyTolerateUndeclaredPrefixes(false);
            if (LOGGER.isTraceEnabled()) {
                LOGGER.trace(result.dump(false));
            }
        }
        if (list == null) {
            list = new SearchResultList(new ArrayList<PrismObject<T>>());
        }
        for (PrismObject<T> object : list) {
            if (hookRegistry != null) {
                for (ReadHook hook : hookRegistry.getAllReadHooks()) {
                    hook.invoke(object, options, task, result);
                }
            }
            resolve(object, options, task, result);
        }
    } finally {
        RepositoryCache.exit();
    }
    // TODO generalize this approach somehow (something like "postprocess" in task/provisioning interface)
    if (searchProvider == ObjectTypes.ObjectManager.REPOSITORY && !GetOperationOptions.isRaw(rootOptions)) {
        for (PrismObject<T> object : list) {
            if (object.asObjectable() instanceof ResourceType || object.asObjectable() instanceof ShadowType) {
                provisioning.applyDefinition(object, task, result);
            }
        }
    }
    schemaTransformer.applySchemasAndSecurityToObjects(list, rootOptions, null, task, result);
    return list;
}
Also used : ReadHook(com.evolveum.midpoint.model.api.hooks.ReadHook) ObjectTypes(com.evolveum.midpoint.schema.constants.ObjectTypes) OperationResult(com.evolveum.midpoint.schema.result.OperationResult)

Example 3 with ReadHook

use of com.evolveum.midpoint.model.api.hooks.ReadHook in project midpoint by Evolveum.

the class ModelController method searchObjectsIterative.

@Override
public <T extends ObjectType> SearchResultMetadata searchObjectsIterative(Class<T> type, ObjectQuery query, final ResultHandler<T> handler, final Collection<SelectorOptions<GetOperationOptions>> options, final Task task, OperationResult parentResult) throws SchemaException, ObjectNotFoundException, CommunicationException, ConfigurationException, SecurityViolationException, ExpressionEvaluationException {
    Validate.notNull(type, "Object type must not be null.");
    Validate.notNull(parentResult, "Result type must not be null.");
    if (query != null) {
        ModelUtils.validatePaging(query.getPaging());
    }
    final GetOperationOptions rootOptions = SelectorOptions.findRootOptions(options);
    ObjectTypes.ObjectManager searchProvider = ObjectTypes.getObjectManagerForClass(type);
    if (searchProvider == null || searchProvider == ObjectTypes.ObjectManager.MODEL || GetOperationOptions.isRaw(rootOptions)) {
        searchProvider = ObjectTypes.ObjectManager.REPOSITORY;
    }
    final OperationResult result = parentResult.createSubresult(SEARCH_OBJECTS);
    result.addParams(new String[] { "query", "paging", "searchProvider" }, query, (query != null ? query.getPaging() : "undefined"), searchProvider);
    query = preProcessQuerySecurity(type, query);
    if (isFilterNone(query, result)) {
        return null;
    }
    ResultHandler<T> internalHandler = (object, parentResult1) -> {
        try {
            object = object.cloneIfImmutable();
            if (hookRegistry != null) {
                for (ReadHook hook : hookRegistry.getAllReadHooks()) {
                    // TODO result or parentResult??? [med]
                    hook.invoke(object, options, task, result);
                }
            }
            resolve(object, options, task, result);
            schemaTransformer.applySchemasAndSecurity(object, rootOptions, null, task, parentResult1);
        } catch (SchemaException | ObjectNotFoundException | SecurityViolationException | CommunicationException | ConfigurationException ex) {
            parentResult1.recordFatalError(ex);
            throw new SystemException(ex.getMessage(), ex);
        }
        return handler.handle(object, parentResult1);
    };
    SearchResultMetadata metadata;
    try {
        RepositoryCache.enter();
        logQuery(query);
        try {
            switch(searchProvider) {
                // TODO move strictSequential flag to model API in some form
                case REPOSITORY:
                    metadata = cacheRepositoryService.searchObjectsIterative(type, query, internalHandler, options, false, result);
                    break;
                case PROVISIONING:
                    metadata = provisioning.searchObjectsIterative(type, query, options, internalHandler, task, result);
                    break;
                case TASK_MANAGER:
                    metadata = taskManager.searchObjectsIterative(type, query, options, internalHandler, result);
                    break;
                default:
                    throw new AssertionError("Unexpected search provider: " + searchProvider);
            }
            result.computeStatusIfUnknown();
            result.cleanupResult();
        } catch (CommunicationException | ConfigurationException | ObjectNotFoundException | SchemaException | SecurityViolationException | ExpressionEvaluationException | RuntimeException | Error e) {
            processSearchException(e, rootOptions, searchProvider, result);
            throw e;
        } finally {
            if (LOGGER.isTraceEnabled()) {
                LOGGER.trace(result.dump(false));
            }
        }
    } finally {
        RepositoryCache.exit();
    }
    return metadata;
}
Also used : ItemPathSegment(com.evolveum.midpoint.prism.path.ItemPathSegment) com.evolveum.midpoint.prism.query(com.evolveum.midpoint.prism.query) ChangeType(com.evolveum.midpoint.prism.delta.ChangeType) AuthorizationConstants(com.evolveum.midpoint.security.api.AuthorizationConstants) Autowired(org.springframework.beans.factory.annotation.Autowired) DiffUtil(com.evolveum.midpoint.prism.delta.DiffUtil) com.evolveum.midpoint.util.exception(com.evolveum.midpoint.util.exception) InternalsConfig(com.evolveum.midpoint.schema.internals.InternalsConfig) OperationResultStatus(com.evolveum.midpoint.schema.result.OperationResultStatus) QNameUtil(com.evolveum.midpoint.util.QNameUtil) Visitor(com.evolveum.midpoint.prism.Visitor) UserProfileService(com.evolveum.midpoint.security.api.UserProfileService) AuditEventRecord(com.evolveum.midpoint.audit.api.AuditEventRecord) com.evolveum.midpoint.prism(com.evolveum.midpoint.prism) ObjectQueryUtil(com.evolveum.midpoint.schema.util.ObjectQueryUtil) ScriptingExpressionEvaluator(com.evolveum.midpoint.model.impl.scripting.ScriptingExpressionEvaluator) OperationResultRunner(com.evolveum.midpoint.schema.result.OperationResultRunner) Utils(com.evolveum.midpoint.model.impl.util.Utils) ObjectDelta(com.evolveum.midpoint.prism.delta.ObjectDelta) ExecutionContext(com.evolveum.midpoint.model.impl.scripting.ExecutionContext) Task(com.evolveum.midpoint.task.api.Task) Projector(com.evolveum.midpoint.model.impl.lens.projector.Projector) IOUtils(org.apache.commons.io.IOUtils) NameItemPathSegment(com.evolveum.midpoint.prism.path.NameItemPathSegment) SystemObjectCache(com.evolveum.midpoint.model.common.SystemObjectCache) ProvisioningService(com.evolveum.midpoint.provisioning.api.ProvisioningService) ExecuteScriptType(com.evolveum.midpoint.xml.ns._public.model.scripting_3.ExecuteScriptType) QName(javax.xml.namespace.QName) NotNull(org.jetbrains.annotations.NotNull) ObjectImporter(com.evolveum.midpoint.model.impl.importer.ObjectImporter) ImportOptionsType(com.evolveum.midpoint.xml.ns._public.common.api_types_3.ImportOptionsType) Validate(org.apache.commons.lang.Validate) com.evolveum.midpoint.schema(com.evolveum.midpoint.schema) EvaluationTimeType(com.evolveum.prism.xml.ns._public.types_3.EvaluationTimeType) ParentPathSegment(com.evolveum.midpoint.prism.path.ParentPathSegment) java.util(java.util) CompareResultType(com.evolveum.midpoint.xml.ns._public.common.api_types_3.CompareResultType) com.evolveum.midpoint.xml.ns._public.common.common_3(com.evolveum.midpoint.xml.ns._public.common.common_3) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) WorkflowManager(com.evolveum.midpoint.wf.api.WorkflowManager) SecurityEnforcer(com.evolveum.midpoint.security.api.SecurityEnforcer) Trace(com.evolveum.midpoint.util.logging.Trace) TaskManager(com.evolveum.midpoint.task.api.TaskManager) DebugUtil(com.evolveum.midpoint.util.DebugUtil) AuditEventStage(com.evolveum.midpoint.audit.api.AuditEventStage) RepositoryCache(com.evolveum.midpoint.repo.cache.RepositoryCache) ReadHook(com.evolveum.midpoint.model.api.hooks.ReadHook) com.evolveum.midpoint.model.api(com.evolveum.midpoint.model.api) HookRegistry(com.evolveum.midpoint.model.api.hooks.HookRegistry) ObjectTypeUtil(com.evolveum.midpoint.schema.util.ObjectTypeUtil) Qualifier(org.springframework.beans.factory.annotation.Qualifier) RepositoryService(com.evolveum.midpoint.repo.api.RepositoryService) ProvisioningOperationOptions(com.evolveum.midpoint.provisioning.api.ProvisioningOperationOptions) PolyString(com.evolveum.midpoint.prism.polystring.PolyString) AuditService(com.evolveum.midpoint.audit.api.AuditService) com.evolveum.midpoint.model.impl.lens(com.evolveum.midpoint.model.impl.lens) CertificationManager(com.evolveum.midpoint.certification.api.CertificationManager) ItemPath(com.evolveum.midpoint.prism.path.ItemPath) LoggingUtils(com.evolveum.midpoint.util.logging.LoggingUtils) QueryBuilder(com.evolveum.midpoint.prism.query.builder.QueryBuilder) RepoAddOptions(com.evolveum.midpoint.repo.api.RepoAddOptions) Component(org.springframework.stereotype.Component) AuditEventType(com.evolveum.midpoint.audit.api.AuditEventType) ScriptingExpressionType(com.evolveum.midpoint.xml.ns._public.model.scripting_3.ScriptingExpressionType) java.io(java.io) Protector(com.evolveum.midpoint.prism.crypto.Protector) SecurityUtil(com.evolveum.midpoint.security.api.SecurityUtil) ShadowUtil(com.evolveum.midpoint.schema.util.ShadowUtil) ModelObjectResolver(com.evolveum.midpoint.model.impl.ModelObjectResolver) ObjectTypes(com.evolveum.midpoint.schema.constants.ObjectTypes) TraceManager(com.evolveum.midpoint.util.logging.TraceManager) ImportAccountsFromResourceTaskHandler(com.evolveum.midpoint.model.impl.importer.ImportAccountsFromResourceTaskHandler) ReadHook(com.evolveum.midpoint.model.api.hooks.ReadHook) ObjectTypes(com.evolveum.midpoint.schema.constants.ObjectTypes) OperationResult(com.evolveum.midpoint.schema.result.OperationResult)

Aggregations

ReadHook (com.evolveum.midpoint.model.api.hooks.ReadHook)3 ObjectTypes (com.evolveum.midpoint.schema.constants.ObjectTypes)3 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)2 AuditEventRecord (com.evolveum.midpoint.audit.api.AuditEventRecord)1 AuditEventStage (com.evolveum.midpoint.audit.api.AuditEventStage)1 AuditEventType (com.evolveum.midpoint.audit.api.AuditEventType)1 AuditService (com.evolveum.midpoint.audit.api.AuditService)1 CertificationManager (com.evolveum.midpoint.certification.api.CertificationManager)1 com.evolveum.midpoint.model.api (com.evolveum.midpoint.model.api)1 HookRegistry (com.evolveum.midpoint.model.api.hooks.HookRegistry)1 SystemObjectCache (com.evolveum.midpoint.model.common.SystemObjectCache)1 ModelObjectResolver (com.evolveum.midpoint.model.impl.ModelObjectResolver)1 ImportAccountsFromResourceTaskHandler (com.evolveum.midpoint.model.impl.importer.ImportAccountsFromResourceTaskHandler)1 ObjectImporter (com.evolveum.midpoint.model.impl.importer.ObjectImporter)1 com.evolveum.midpoint.model.impl.lens (com.evolveum.midpoint.model.impl.lens)1 Projector (com.evolveum.midpoint.model.impl.lens.projector.Projector)1 ExecutionContext (com.evolveum.midpoint.model.impl.scripting.ExecutionContext)1 ScriptingExpressionEvaluator (com.evolveum.midpoint.model.impl.scripting.ScriptingExpressionEvaluator)1 Utils (com.evolveum.midpoint.model.impl.util.Utils)1 com.evolveum.midpoint.prism (com.evolveum.midpoint.prism)1