Search in sources :

Example 1 with ConnectorObjectBuilder

use of org.identityconnectors.framework.common.objects.ConnectorObjectBuilder in project syncope by apache.

the class AbstractPropagationTaskExecutor method execute.

protected TaskExec execute(final PropagationTaskTO taskTO, final PropagationReporter reporter) {
    PropagationTask task = entityFactory.newEntity(PropagationTask.class);
    task.setResource(resourceDAO.find(taskTO.getResource()));
    task.setObjectClassName(taskTO.getObjectClassName());
    task.setAnyTypeKind(taskTO.getAnyTypeKind());
    task.setAnyType(taskTO.getAnyType());
    task.setEntityKey(taskTO.getEntityKey());
    task.setOperation(taskTO.getOperation());
    task.setConnObjectKey(taskTO.getConnObjectKey());
    task.setOldConnObjectKey(taskTO.getOldConnObjectKey());
    Set<Attribute> attributes = new HashSet<>();
    if (StringUtils.isNotBlank(taskTO.getAttributes())) {
        attributes.addAll(Arrays.asList(POJOHelper.deserialize(taskTO.getAttributes(), Attribute[].class)));
    }
    task.setAttributes(attributes);
    List<PropagationActions> actions = getPropagationActions(task.getResource());
    String resource = task.getResource().getKey();
    Date start = new Date();
    TaskExec execution = entityFactory.newEntity(TaskExec.class);
    execution.setStatus(PropagationTaskExecStatus.CREATED.name());
    String taskExecutionMessage = null;
    String failureReason = null;
    // Flag to state whether any propagation has been attempted
    AtomicReference<Boolean> propagationAttempted = new AtomicReference<>(false);
    ConnectorObject beforeObj = null;
    ConnectorObject afterObj = null;
    Provision provision = null;
    OrgUnit orgUnit = null;
    Uid uid = null;
    Connector connector = null;
    Result result;
    try {
        provision = task.getResource().getProvision(new ObjectClass(task.getObjectClassName())).orElse(null);
        orgUnit = task.getResource().getOrgUnit();
        connector = connFactory.getConnector(task.getResource());
        // Try to read remote object BEFORE any actual operation
        beforeObj = provision == null && orgUnit == null ? null : orgUnit == null ? getRemoteObject(task, connector, provision, false) : getRemoteObject(task, connector, orgUnit, false);
        for (PropagationActions action : actions) {
            action.before(task, beforeObj);
        }
        switch(task.getOperation()) {
            case CREATE:
            case UPDATE:
                uid = createOrUpdate(task, beforeObj, connector, propagationAttempted);
                break;
            case DELETE:
                uid = delete(task, beforeObj, connector, propagationAttempted);
                break;
            default:
        }
        execution.setStatus(propagationAttempted.get() ? PropagationTaskExecStatus.SUCCESS.name() : PropagationTaskExecStatus.NOT_ATTEMPTED.name());
        LOG.debug("Successfully propagated to {}", task.getResource());
        result = Result.SUCCESS;
    } catch (Exception e) {
        result = Result.FAILURE;
        LOG.error("Exception during provision on resource " + resource, e);
        if (e instanceof ConnectorException && e.getCause() != null) {
            taskExecutionMessage = e.getCause().getMessage();
            if (e.getCause().getMessage() == null) {
                failureReason = e.getMessage();
            } else {
                failureReason = e.getMessage() + "\n\n Cause: " + e.getCause().getMessage().split("\n")[0];
            }
        } else {
            taskExecutionMessage = ExceptionUtils2.getFullStackTrace(e);
            if (e.getCause() == null) {
                failureReason = e.getMessage();
            } else {
                failureReason = e.getMessage() + "\n\n Cause: " + e.getCause().getMessage().split("\n")[0];
            }
        }
        try {
            execution.setStatus(PropagationTaskExecStatus.FAILURE.name());
        } catch (Exception wft) {
            LOG.error("While executing KO action on {}", execution, wft);
        }
        propagationAttempted.set(true);
        actions.forEach(action -> {
            action.onError(task, execution, e);
        });
    } finally {
        // Try to read remote object AFTER any actual operation
        if (connector != null) {
            if (uid != null) {
                task.setConnObjectKey(uid.getUidValue());
            }
            try {
                afterObj = provision == null && orgUnit == null ? null : orgUnit == null ? getRemoteObject(task, connector, provision, true) : getRemoteObject(task, connector, orgUnit, true);
            } catch (Exception ignore) {
                // ignore exception
                LOG.error("Error retrieving after object", ignore);
            }
        }
        if (task.getOperation() != ResourceOperation.DELETE && afterObj == null && uid != null) {
            afterObj = new ConnectorObjectBuilder().setObjectClass(new ObjectClass(task.getObjectClassName())).setUid(uid).setName(AttributeUtil.getNameFromAttributes(task.getAttributes())).build();
        }
        execution.setStart(start);
        execution.setMessage(taskExecutionMessage);
        execution.setEnd(new Date());
        LOG.debug("Execution finished: {}", execution);
        if (hasToBeregistered(task, execution)) {
            LOG.debug("Execution to be stored: {}", execution);
            execution.setTask(task);
            task.add(execution);
            taskDAO.save(task);
            // needed to generate a value for the execution key
            taskDAO.flush();
        }
        if (reporter != null) {
            reporter.onSuccessOrNonPriorityResourceFailures(taskTO, PropagationTaskExecStatus.valueOf(execution.getStatus()), failureReason, beforeObj, afterObj);
        }
    }
    for (PropagationActions action : actions) {
        action.after(task, execution, afterObj);
    }
    // SYNCOPE-1136
    String anyTypeKind = task.getAnyTypeKind() == null ? "realm" : task.getAnyTypeKind().name().toLowerCase();
    String operation = task.getOperation().name().toLowerCase();
    boolean notificationsAvailable = notificationManager.notificationsAvailable(AuditElements.EventCategoryType.PROPAGATION, anyTypeKind, resource, operation);
    boolean auditRequested = auditManager.auditRequested(AuditElements.EventCategoryType.PROPAGATION, anyTypeKind, resource, operation);
    if (notificationsAvailable || auditRequested) {
        ExecTO execTO = taskDataBinder.getExecTO(execution);
        notificationManager.createTasks(AuditElements.EventCategoryType.PROPAGATION, anyTypeKind, resource, operation, result, beforeObj, new Object[] { execTO, afterObj }, taskTO);
        auditManager.audit(AuditElements.EventCategoryType.PROPAGATION, anyTypeKind, resource, operation, result, beforeObj, new Object[] { execTO, afterObj }, taskTO);
    }
    return execution;
}
Also used : OrgUnit(org.apache.syncope.core.persistence.api.entity.resource.OrgUnit) Arrays(java.util.Arrays) Date(java.util.Date) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) AuditElements(org.apache.syncope.common.lib.types.AuditElements) ResourceOperation(org.apache.syncope.common.lib.types.ResourceOperation) NotificationManager(org.apache.syncope.core.provisioning.api.notification.NotificationManager) StringUtils(org.apache.commons.lang3.StringUtils) PropagationTask(org.apache.syncope.core.persistence.api.entity.task.PropagationTask) VirAttrCacheValue(org.apache.syncope.core.provisioning.api.cache.VirAttrCacheValue) Attribute(org.identityconnectors.framework.common.objects.Attribute) PropagationTaskTO(org.apache.syncope.common.lib.to.PropagationTaskTO) GroupDAO(org.apache.syncope.core.persistence.api.dao.GroupDAO) AnyObjectDAO(org.apache.syncope.core.persistence.api.dao.AnyObjectDAO) ConnObjectUtils(org.apache.syncope.core.provisioning.java.utils.ConnObjectUtils) Map(java.util.Map) OrgUnit(org.apache.syncope.core.persistence.api.entity.resource.OrgUnit) ExceptionUtils2(org.apache.syncope.core.provisioning.api.utils.ExceptionUtils2) ExecTO(org.apache.syncope.common.lib.to.ExecTO) UserDAO(org.apache.syncope.core.persistence.api.dao.UserDAO) OrgUnitItem(org.apache.syncope.core.persistence.api.entity.resource.OrgUnitItem) Collection(java.util.Collection) Set(java.util.Set) PropagationActions(org.apache.syncope.core.provisioning.api.propagation.PropagationActions) Collectors(java.util.stream.Collectors) MappingItem(org.apache.syncope.core.persistence.api.entity.resource.MappingItem) ImplementationManager(org.apache.syncope.core.spring.ImplementationManager) EntityFactory(org.apache.syncope.core.persistence.api.entity.EntityFactory) Connector(org.apache.syncope.core.provisioning.api.Connector) ConnectorObject(org.identityconnectors.framework.common.objects.ConnectorObject) List(java.util.List) Provision(org.apache.syncope.core.persistence.api.entity.resource.Provision) AttributeUtil(org.identityconnectors.framework.common.objects.AttributeUtil) TaskUtilsFactory(org.apache.syncope.core.persistence.api.entity.task.TaskUtilsFactory) AuditManager(org.apache.syncope.core.provisioning.api.AuditManager) AttributeBuilder(org.identityconnectors.framework.common.objects.AttributeBuilder) ObjectClass(org.identityconnectors.framework.common.objects.ObjectClass) ConnectorFactory(org.apache.syncope.core.provisioning.api.ConnectorFactory) PropagationTaskExecutor(org.apache.syncope.core.provisioning.api.propagation.PropagationTaskExecutor) Optional(java.util.Optional) ExternalResourceDAO(org.apache.syncope.core.persistence.api.dao.ExternalResourceDAO) VirAttrCache(org.apache.syncope.core.provisioning.api.cache.VirAttrCache) POJOHelper(org.apache.syncope.core.provisioning.api.serialization.POJOHelper) PropagationTaskExecStatus(org.apache.syncope.common.lib.types.PropagationTaskExecStatus) TaskDataBinder(org.apache.syncope.core.provisioning.api.data.TaskDataBinder) ConnectorObjectBuilder(org.identityconnectors.framework.common.objects.ConnectorObjectBuilder) AtomicReference(java.util.concurrent.atomic.AtomicReference) ConnectorException(org.identityconnectors.framework.common.exceptions.ConnectorException) ArrayList(java.util.ArrayList) TaskDAO(org.apache.syncope.core.persistence.api.dao.TaskDAO) HashSet(java.util.HashSet) Result(org.apache.syncope.common.lib.types.AuditElements.Result) TaskExec(org.apache.syncope.core.persistence.api.entity.task.TaskExec) TimeoutException(org.apache.syncope.core.provisioning.api.TimeoutException) Logger(org.slf4j.Logger) Uid(org.identityconnectors.framework.common.objects.Uid) PropagationException(org.apache.syncope.core.provisioning.api.propagation.PropagationException) IteratorChain(org.apache.syncope.common.lib.collections.IteratorChain) PropagationReporter(org.apache.syncope.core.provisioning.api.propagation.PropagationReporter) Name(org.identityconnectors.framework.common.objects.Name) MappingUtils(org.apache.syncope.core.provisioning.java.utils.MappingUtils) ExternalResource(org.apache.syncope.core.persistence.api.entity.resource.ExternalResource) VirSchemaDAO(org.apache.syncope.core.persistence.api.dao.VirSchemaDAO) Collections(java.util.Collections) TraceLevel(org.apache.syncope.common.lib.types.TraceLevel) Transactional(org.springframework.transaction.annotation.Transactional) PropagationActions(org.apache.syncope.core.provisioning.api.propagation.PropagationActions) Connector(org.apache.syncope.core.provisioning.api.Connector) PropagationTask(org.apache.syncope.core.persistence.api.entity.task.PropagationTask) Attribute(org.identityconnectors.framework.common.objects.Attribute) ConnectorObjectBuilder(org.identityconnectors.framework.common.objects.ConnectorObjectBuilder) Result(org.apache.syncope.common.lib.types.AuditElements.Result) HashSet(java.util.HashSet) Provision(org.apache.syncope.core.persistence.api.entity.resource.Provision) ObjectClass(org.identityconnectors.framework.common.objects.ObjectClass) ExecTO(org.apache.syncope.common.lib.to.ExecTO) ConnectorObject(org.identityconnectors.framework.common.objects.ConnectorObject) AtomicReference(java.util.concurrent.atomic.AtomicReference) Date(java.util.Date) ConnectorException(org.identityconnectors.framework.common.exceptions.ConnectorException) TimeoutException(org.apache.syncope.core.provisioning.api.TimeoutException) PropagationException(org.apache.syncope.core.provisioning.api.propagation.PropagationException) Uid(org.identityconnectors.framework.common.objects.Uid) TaskExec(org.apache.syncope.core.persistence.api.entity.task.TaskExec) ConnectorException(org.identityconnectors.framework.common.exceptions.ConnectorException)

Aggregations

ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 Date (java.util.Date)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Set (java.util.Set)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Collectors (java.util.stream.Collectors)1 StringUtils (org.apache.commons.lang3.StringUtils)1 IteratorChain (org.apache.syncope.common.lib.collections.IteratorChain)1 ExecTO (org.apache.syncope.common.lib.to.ExecTO)1 PropagationTaskTO (org.apache.syncope.common.lib.to.PropagationTaskTO)1 AuditElements (org.apache.syncope.common.lib.types.AuditElements)1 Result (org.apache.syncope.common.lib.types.AuditElements.Result)1 PropagationTaskExecStatus (org.apache.syncope.common.lib.types.PropagationTaskExecStatus)1 ResourceOperation (org.apache.syncope.common.lib.types.ResourceOperation)1