http://git-wip-us.apache.org/repos/asf/syncope/blob/9c289134/core/provisioning-java/src/main/java/org/apache/syncope/core/provisioning/java/pushpull/RealmPullResultHandlerImpl.java ---------------------------------------------------------------------- diff --git a/core/provisioning-java/src/main/java/org/apache/syncope/core/provisioning/java/pushpull/RealmPullResultHandlerImpl.java b/core/provisioning-java/src/main/java/org/apache/syncope/core/provisioning/java/pushpull/RealmPullResultHandlerImpl.java deleted file mode 100644 index c4be4e2..0000000 --- a/core/provisioning-java/src/main/java/org/apache/syncope/core/provisioning/java/pushpull/RealmPullResultHandlerImpl.java +++ /dev/null @@ -1,795 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.syncope.core.provisioning.java.pushpull; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.Set; -import org.apache.commons.lang3.exception.ExceptionUtils; -import org.apache.syncope.common.lib.SyncopeClientException; -import org.apache.syncope.common.lib.to.RealmTO; -import org.apache.syncope.common.lib.types.AnyTypeKind; -import org.apache.syncope.common.lib.types.AuditElements; -import org.apache.syncope.common.lib.types.AuditElements.Result; -import org.apache.syncope.common.lib.types.ClientExceptionType; -import org.apache.syncope.common.lib.types.MatchingRule; -import org.apache.syncope.core.provisioning.api.PropagationByResource; -import org.apache.syncope.common.lib.types.PullMode; -import org.apache.syncope.common.lib.types.ResourceOperation; -import org.apache.syncope.common.lib.types.UnmatchingRule; -import org.apache.syncope.core.persistence.api.dao.AnySearchDAO; -import org.apache.syncope.core.persistence.api.dao.search.AnyCond; -import org.apache.syncope.core.persistence.api.dao.search.AttributeCond; -import org.apache.syncope.core.persistence.api.dao.search.SearchCond; -import org.apache.syncope.core.persistence.api.entity.Realm; -import org.apache.syncope.core.persistence.api.entity.resource.OrgUnit; -import org.apache.syncope.core.persistence.api.entity.task.PropagationTask; -import org.apache.syncope.core.persistence.api.entity.task.PullTask; -import org.apache.syncope.core.provisioning.api.propagation.PropagationException; -import org.apache.syncope.core.provisioning.api.pushpull.IgnoreProvisionException; -import org.apache.syncope.core.provisioning.api.pushpull.ProvisioningReport; -import org.apache.syncope.core.provisioning.api.pushpull.PullActions; -import org.apache.syncope.core.provisioning.api.pushpull.SyncopePullExecutor; -import org.apache.syncope.core.provisioning.api.pushpull.SyncopePullResultHandler; -import org.apache.syncope.core.provisioning.java.utils.ConnObjectUtils; -import org.apache.syncope.core.spring.security.DelegatedAdministrationException; -import org.identityconnectors.framework.common.objects.SyncDelta; -import org.identityconnectors.framework.common.objects.SyncDeltaType; -import org.quartz.JobExecutionException; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.transaction.annotation.Transactional; - -@Transactional(rollbackFor = Throwable.class) -public class RealmPullResultHandlerImpl - extends AbstractRealmResultHandler<PullTask, PullActions> - implements SyncopePullResultHandler { - - @Autowired - private PullUtils pullUtils; - - @Autowired - private ConnObjectUtils connObjectUtils; - - @Autowired - private AnySearchDAO searchDAO; - - private SyncopePullExecutor executor; - - private Result latestResult; - - @Override - public void setPullExecutor(final SyncopePullExecutor executor) { - this.executor = executor; - } - - @Override - public boolean handle(final SyncDelta delta) { - try { - OrgUnit orgUnit = profile.getTask().getResource().getOrgUnit(); - if (orgUnit == null) { - throw new JobExecutionException("No orgUnit found on " + profile.getTask().getResource() + " for " - + delta.getObject().getObjectClass()); - } - - doHandle(delta, orgUnit); - - LOG.debug("Successfully handled {}", delta); - - if (profile.getTask().getPullMode() != PullMode.INCREMENTAL) { - return true; - } - - boolean shouldContinue; - synchronized (this) { - shouldContinue = latestResult == Result.SUCCESS; - this.latestResult = null; - } - if (shouldContinue) { - executor.setLatestSyncToken(delta.getObjectClass(), delta.getToken()); - } - return shouldContinue; - } catch (IgnoreProvisionException e) { - ProvisioningReport ignoreResult = new ProvisioningReport(); - ignoreResult.setOperation(ResourceOperation.NONE); - ignoreResult.setStatus(ProvisioningReport.Status.IGNORE); - ignoreResult.setAnyType(REALM_TYPE); - ignoreResult.setKey(null); - ignoreResult.setName(delta.getObject().getName().getNameValue()); - profile.getResults().add(ignoreResult); - - LOG.warn("Ignoring during pull", e); - - executor.setLatestSyncToken(delta.getObjectClass(), delta.getToken()); - - return true; - } catch (JobExecutionException e) { - LOG.error("Pull failed", e); - - return false; - } - } - - private List<ProvisioningReport> assign(final SyncDelta delta, final OrgUnit orgUnit) throws JobExecutionException { - if (!profile.getTask().isPerformCreate()) { - LOG.debug("PullTask not configured for create"); - finalize(UnmatchingRule.toEventName(UnmatchingRule.ASSIGN), Result.SUCCESS, null, null, delta); - return Collections.<ProvisioningReport>emptyList(); - } - - RealmTO realmTO = connObjectUtils.getRealmTO(delta.getObject(), profile.getTask(), orgUnit); - if (realmTO.getFullPath() == null) { - if (realmTO.getParent() == null) { - realmTO.setParent(profile.getTask().getDestinatioRealm().getFullPath()); - } - - realmTO.setFullPath(realmTO.getParent() + "/" + realmTO.getName()); - } - realmTO.getResources().add(profile.getTask().getResource().getKey()); - - ProvisioningReport result = new ProvisioningReport(); - result.setOperation(ResourceOperation.CREATE); - result.setAnyType(REALM_TYPE); - result.setStatus(ProvisioningReport.Status.SUCCESS); - result.setName(realmTO.getFullPath()); - - if (profile.isDryRun()) { - result.setKey(null); - finalize(UnmatchingRule.toEventName(UnmatchingRule.ASSIGN), Result.SUCCESS, null, null, delta); - } else { - SyncDelta actionedDelta = delta; - for (PullActions action : profile.getActions()) { - actionedDelta = action.beforeAssign(profile, actionedDelta, realmTO); - } - - create(realmTO, actionedDelta, UnmatchingRule.toEventName(UnmatchingRule.ASSIGN), result); - } - - return Collections.singletonList(result); - } - - private List<ProvisioningReport> provision(final SyncDelta delta, final OrgUnit orgUnit) - throws JobExecutionException { - - if (!profile.getTask().isPerformCreate()) { - LOG.debug("PullTask not configured for create"); - finalize(UnmatchingRule.toEventName(UnmatchingRule.PROVISION), Result.SUCCESS, null, null, delta); - return Collections.<ProvisioningReport>emptyList(); - } - - RealmTO realmTO = connObjectUtils.getRealmTO(delta.getObject(), profile.getTask(), orgUnit); - if (realmTO.getFullPath() == null) { - if (realmTO.getParent() == null) { - realmTO.setParent(profile.getTask().getDestinatioRealm().getFullPath()); - } - - realmTO.setFullPath(realmTO.getParent() + "/" + realmTO.getName()); - } - - ProvisioningReport result = new ProvisioningReport(); - result.setOperation(ResourceOperation.CREATE); - result.setAnyType(REALM_TYPE); - result.setStatus(ProvisioningReport.Status.SUCCESS); - result.setName(realmTO.getFullPath()); - - if (profile.isDryRun()) { - result.setKey(null); - finalize(UnmatchingRule.toEventName(UnmatchingRule.PROVISION), Result.SUCCESS, null, null, delta); - } else { - SyncDelta actionedDelta = delta; - for (PullActions action : profile.getActions()) { - actionedDelta = action.beforeProvision(profile, actionedDelta, realmTO); - } - - create(realmTO, actionedDelta, UnmatchingRule.toEventName(UnmatchingRule.PROVISION), result); - } - - return Collections.singletonList(result); - } - - private void throwIgnoreProvisionException(final SyncDelta delta, final Exception exception) - throws JobExecutionException { - - if (exception instanceof IgnoreProvisionException) { - throw IgnoreProvisionException.class.cast(exception); - } - - IgnoreProvisionException ipe = null; - for (PullActions action : profile.getActions()) { - if (ipe == null) { - ipe = action.onError(profile, delta, exception); - } - } - if (ipe != null) { - throw ipe; - } - } - - private void create( - final RealmTO realmTO, - final SyncDelta delta, - final String operation, - final ProvisioningReport result) - throws JobExecutionException { - - Object output; - Result resultStatus; - - try { - Realm realm = realmDAO.save(binder.create(profile.getTask().getDestinatioRealm().getFullPath(), realmTO)); - - PropagationByResource propByRes = new PropagationByResource(); - for (String resource : realm.getResourceKeys()) { - propByRes.add(ResourceOperation.CREATE, resource); - } - List<PropagationTask> tasks = propagationManager.createTasks(realm, propByRes, null); - taskExecutor.execute(tasks, false); - - RealmTO actual = binder.getRealmTO(realm, true); - - result.setKey(actual.getKey()); - result.setName(profile.getTask().getDestinatioRealm().getFullPath() + "/" + actual.getName()); - - output = actual; - resultStatus = Result.SUCCESS; - - for (PullActions action : profile.getActions()) { - action.after(profile, delta, actual, result); - } - - LOG.debug("Realm {} successfully created", actual.getKey()); - } catch (PropagationException e) { - // A propagation failure doesn't imply a pull failure. - // The propagation exception status will be reported into the propagation task execution. - LOG.error("Could not propagate Realm {}", delta.getUid().getUidValue(), e); - output = e; - resultStatus = Result.FAILURE; - } catch (Exception e) { - throwIgnoreProvisionException(delta, e); - - result.setStatus(ProvisioningReport.Status.FAILURE); - result.setMessage(ExceptionUtils.getRootCauseMessage(e)); - LOG.error("Could not create Realm {} ", delta.getUid().getUidValue(), e); - output = e; - resultStatus = Result.FAILURE; - } - - finalize(operation, resultStatus, null, output, delta); - } - - private List<ProvisioningReport> update(final SyncDelta delta, final List<String> keys) - throws JobExecutionException { - - if (!profile.getTask().isPerformUpdate()) { - LOG.debug("PullTask not configured for update"); - finalize(MatchingRule.toEventName(MatchingRule.UPDATE), Result.SUCCESS, null, null, delta); - return Collections.<ProvisioningReport>emptyList(); - } - - LOG.debug("About to update {}", keys); - - List<ProvisioningReport> results = new ArrayList<>(); - - SyncDelta workingDelta = delta; - for (String key : keys) { - LOG.debug("About to update {}", key); - - ProvisioningReport result = new ProvisioningReport(); - result.setOperation(ResourceOperation.UPDATE); - result.setAnyType(REALM_TYPE); - result.setStatus(ProvisioningReport.Status.SUCCESS); - result.setKey(key); - - Realm realm = realmDAO.find(key); - RealmTO before = binder.getRealmTO(realm, true); - if (before == null) { - result.setStatus(ProvisioningReport.Status.FAILURE); - result.setMessage(String.format("Realm '%s' not found", key)); - } else { - result.setName(before.getFullPath()); - } - - if (!profile.isDryRun()) { - Result resultStatus; - Object output; - - if (before == null) { - resultStatus = Result.FAILURE; - output = null; - } else { - try { - for (PullActions action : profile.getActions()) { - workingDelta = action.beforeUpdate(profile, workingDelta, before, null); - } - - PropagationByResource propByRes = binder.update(realm, before); - realm = realmDAO.save(realm); - RealmTO updated = binder.getRealmTO(realm, true); - - List<PropagationTask> tasks = propagationManager.createTasks(realm, propByRes, null); - taskExecutor.execute(tasks, false); - - for (PullActions action : profile.getActions()) { - action.after(profile, workingDelta, updated, result); - } - - output = updated; - resultStatus = Result.SUCCESS; - result.setName(updated.getFullPath()); - - LOG.debug("{} successfully updated", updated); - } catch (PropagationException e) { - // A propagation failure doesn't imply a pull failure. - // The propagation exception status will be reported into the propagation task execution. - LOG.error("Could not propagate Realm {}", workingDelta.getUid().getUidValue(), e); - output = e; - resultStatus = Result.FAILURE; - } catch (Exception e) { - throwIgnoreProvisionException(workingDelta, e); - - result.setStatus(ProvisioningReport.Status.FAILURE); - result.setMessage(ExceptionUtils.getRootCauseMessage(e)); - LOG.error("Could not update Realm {}", workingDelta.getUid().getUidValue(), e); - output = e; - resultStatus = Result.FAILURE; - } - } - finalize(MatchingRule.toEventName(MatchingRule.UPDATE), resultStatus, before, output, workingDelta); - } - results.add(result); - } - - return results; - } - - private List<ProvisioningReport> deprovision(final SyncDelta delta, final List<String> keys, final boolean unlink) - throws JobExecutionException { - - if (!profile.getTask().isPerformUpdate()) { - LOG.debug("PullTask not configured for update"); - finalize(unlink - ? MatchingRule.toEventName(MatchingRule.UNASSIGN) - : MatchingRule.toEventName(MatchingRule.DEPROVISION), Result.SUCCESS, null, null, delta); - return Collections.<ProvisioningReport>emptyList(); - } - - LOG.debug("About to deprovision {}", keys); - - final List<ProvisioningReport> results = new ArrayList<>(); - - SyncDelta workingDelta = delta; - for (String key : keys) { - LOG.debug("About to unassign resource {}", key); - - ProvisioningReport result = new ProvisioningReport(); - result.setOperation(ResourceOperation.DELETE); - result.setAnyType(REALM_TYPE); - result.setStatus(ProvisioningReport.Status.SUCCESS); - result.setKey(key); - - Realm realm = realmDAO.find(key); - RealmTO before = binder.getRealmTO(realm, true); - if (before == null) { - result.setStatus(ProvisioningReport.Status.FAILURE); - result.setMessage(String.format("Realm '%s' not found", key)); - } else { - result.setName(before.getFullPath()); - } - - if (!profile.isDryRun()) { - Object output; - Result resultStatus; - - if (before == null) { - resultStatus = Result.FAILURE; - output = null; - } else { - try { - if (unlink) { - for (PullActions action : profile.getActions()) { - workingDelta = action.beforeUnassign(profile, workingDelta, before); - } - } else { - for (PullActions action : profile.getActions()) { - workingDelta = action.beforeDeprovision(profile, workingDelta, before); - } - } - - PropagationByResource propByRes = new PropagationByResource(); - propByRes.add(ResourceOperation.DELETE, profile.getTask().getResource().getKey()); - taskExecutor.execute(propagationManager.createTasks(realm, propByRes, null), false); - - if (unlink) { - realm.getResources().remove(profile.getTask().getResource()); - output = binder.getRealmTO(realmDAO.save(realm), true); - } else { - output = binder.getRealmTO(realm, true); - } - - for (PullActions action : profile.getActions()) { - action.after(profile, workingDelta, RealmTO.class.cast(output), result); - } - - resultStatus = Result.SUCCESS; - - LOG.debug("{} successfully updated", realm); - } catch (PropagationException e) { - // A propagation failure doesn't imply a pull failure. - // The propagation exception status will be reported into the propagation task execution. - LOG.error("Could not propagate Realm {}", workingDelta.getUid().getUidValue(), e); - output = e; - resultStatus = Result.FAILURE; - } catch (Exception e) { - throwIgnoreProvisionException(workingDelta, e); - - result.setStatus(ProvisioningReport.Status.FAILURE); - result.setMessage(ExceptionUtils.getRootCauseMessage(e)); - LOG.error("Could not update Realm {}", delta.getUid().getUidValue(), e); - output = e; - resultStatus = Result.FAILURE; - } - } - finalize(unlink - ? MatchingRule.toEventName(MatchingRule.UNASSIGN) - : MatchingRule.toEventName(MatchingRule.DEPROVISION), resultStatus, before, output, delta); - } - results.add(result); - } - - return results; - } - - private List<ProvisioningReport> link(final SyncDelta delta, final List<String> keys, final boolean unlink) - throws JobExecutionException { - - if (!profile.getTask().isPerformUpdate()) { - LOG.debug("PullTask not configured for update"); - finalize(unlink - ? MatchingRule.toEventName(MatchingRule.UNLINK) - : MatchingRule.toEventName(MatchingRule.LINK), Result.SUCCESS, null, null, delta); - return Collections.<ProvisioningReport>emptyList(); - } - - LOG.debug("About to link {}", keys); - - final List<ProvisioningReport> results = new ArrayList<>(); - - SyncDelta workingDelta = delta; - for (String key : keys) { - LOG.debug("About to unassign resource {}", key); - - ProvisioningReport result = new ProvisioningReport(); - result.setOperation(ResourceOperation.NONE); - result.setAnyType(REALM_TYPE); - result.setStatus(ProvisioningReport.Status.SUCCESS); - result.setKey(key); - - Realm realm = realmDAO.find(key); - RealmTO before = binder.getRealmTO(realm, true); - if (before == null) { - result.setStatus(ProvisioningReport.Status.FAILURE); - result.setMessage(String.format("Realm '%s' not found", key)); - } else { - result.setName(before.getFullPath()); - } - - Object output; - Result resultStatus; - if (!profile.isDryRun()) { - if (before == null) { - resultStatus = Result.FAILURE; - output = null; - } else { - try { - if (unlink) { - for (PullActions action : profile.getActions()) { - workingDelta = action.beforeUnlink(profile, workingDelta, before); - } - } else { - for (PullActions action : profile.getActions()) { - workingDelta = action.beforeLink(profile, workingDelta, before); - } - } - - if (unlink) { - realm.getResources().remove(profile.getTask().getResource()); - } else { - realm.add(profile.getTask().getResource()); - } - output = update(workingDelta, Collections.singletonList(key)); - - for (PullActions action : profile.getActions()) { - action.after(profile, workingDelta, RealmTO.class.cast(output), result); - } - - resultStatus = Result.SUCCESS; - - LOG.debug("{} successfully updated", realm); - } catch (PropagationException e) { - // A propagation failure doesn't imply a pull failure. - // The propagation exception status will be reported into the propagation task execution. - LOG.error("Could not propagate Realm {}", workingDelta.getUid().getUidValue(), e); - output = e; - resultStatus = Result.FAILURE; - } catch (Exception e) { - throwIgnoreProvisionException(workingDelta, e); - - result.setStatus(ProvisioningReport.Status.FAILURE); - result.setMessage(ExceptionUtils.getRootCauseMessage(e)); - LOG.error("Could not update Realm {}", workingDelta.getUid().getUidValue(), e); - output = e; - resultStatus = Result.FAILURE; - } - } - finalize(unlink - ? MatchingRule.toEventName(MatchingRule.UNLINK) - : MatchingRule.toEventName(MatchingRule.LINK), resultStatus, before, output, workingDelta); - } - results.add(result); - } - - return results; - } - - private List<ProvisioningReport> delete(final SyncDelta delta, final List<String> keys) - throws JobExecutionException { - - if (!profile.getTask().isPerformDelete()) { - LOG.debug("PullTask not configured for delete"); - finalize(ResourceOperation.DELETE.name().toLowerCase(), Result.SUCCESS, null, null, delta); - return Collections.<ProvisioningReport>emptyList(); - } - - LOG.debug("About to delete {}", keys); - - List<ProvisioningReport> results = new ArrayList<>(); - - SyncDelta workingDelta = delta; - for (String key : keys) { - Object output; - Result resultStatus = Result.FAILURE; - - ProvisioningReport result = new ProvisioningReport(); - - try { - result.setKey(key); - result.setOperation(ResourceOperation.DELETE); - result.setAnyType(REALM_TYPE); - result.setStatus(ProvisioningReport.Status.SUCCESS); - - Realm realm = realmDAO.find(key); - RealmTO before = binder.getRealmTO(realm, true); - if (before == null) { - result.setStatus(ProvisioningReport.Status.FAILURE); - result.setMessage(String.format("Realm '%s' not found", key)); - } else { - result.setName(before.getFullPath()); - } - - if (!profile.isDryRun()) { - for (PullActions action : profile.getActions()) { - workingDelta = action.beforeDelete(profile, workingDelta, before); - } - - try { - if (!realmDAO.findChildren(realm).isEmpty()) { - throw SyncopeClientException.build(ClientExceptionType.HasChildren); - } - - Set<String> adminRealms = Collections.singleton(realm.getFullPath()); - AnyCond keyCond = new AnyCond(AttributeCond.Type.ISNOTNULL); - keyCond.setSchema("key"); - SearchCond allMatchingCond = SearchCond.getLeafCond(keyCond); - int users = searchDAO.count(adminRealms, allMatchingCond, AnyTypeKind.USER); - int groups = searchDAO.count(adminRealms, allMatchingCond, AnyTypeKind.GROUP); - int anyObjects = searchDAO.count(adminRealms, allMatchingCond, AnyTypeKind.ANY_OBJECT); - - if (users + groups + anyObjects > 0) { - SyncopeClientException containedAnys = SyncopeClientException.build( - ClientExceptionType.AssociatedAnys); - containedAnys.getElements().add(users + " user(s)"); - containedAnys.getElements().add(groups + " group(s)"); - containedAnys.getElements().add(anyObjects + " anyObject(s)"); - throw containedAnys; - } - - PropagationByResource propByRes = new PropagationByResource(); - for (String resource : realm.getResourceKeys()) { - propByRes.add(ResourceOperation.DELETE, resource); - } - List<PropagationTask> tasks = propagationManager.createTasks(realm, propByRes, null); - taskExecutor.execute(tasks, false); - - realmDAO.delete(realm); - - output = null; - resultStatus = Result.SUCCESS; - - for (PullActions action : profile.getActions()) { - action.after(profile, workingDelta, before, result); - } - } catch (Exception e) { - throwIgnoreProvisionException(workingDelta, e); - - result.setStatus(ProvisioningReport.Status.FAILURE); - result.setMessage(ExceptionUtils.getRootCauseMessage(e)); - LOG.error("Could not delete {}", realm, e); - output = e; - } - - finalize(ResourceOperation.DELETE.name().toLowerCase(), resultStatus, before, output, workingDelta); - } - - results.add(result); - } catch (DelegatedAdministrationException e) { - LOG.error("Not allowed to read Realm {}", key, e); - } catch (Exception e) { - LOG.error("Could not delete Realm {}", key, e); - } - } - - return results; - } - - private ProvisioningReport ignore( - final SyncDelta delta, - final boolean matching) - throws JobExecutionException { - - LOG.debug("Any to ignore {}", delta.getObject().getUid().getUidValue()); - - ProvisioningReport result = new ProvisioningReport(); - - result.setKey(null); - result.setName(delta.getObject().getUid().getUidValue()); - result.setOperation(ResourceOperation.NONE); - result.setAnyType(REALM_TYPE); - result.setStatus(ProvisioningReport.Status.SUCCESS); - - if (!profile.isDryRun()) { - finalize(matching - ? MatchingRule.toEventName(MatchingRule.IGNORE) - : UnmatchingRule.toEventName(UnmatchingRule.IGNORE), Result.SUCCESS, null, null, delta); - } - - return result; - } - - private void doHandle(final SyncDelta delta, final OrgUnit orgUnit) throws JobExecutionException { - LOG.debug("Process {} for {} as {}", - delta.getDeltaType(), delta.getUid().getUidValue(), delta.getObject().getObjectClass()); - - String uid = delta.getPreviousUid() == null - ? delta.getUid().getUidValue() - : delta.getPreviousUid().getUidValue(); - - List<String> keys = pullUtils.findExisting(uid, delta.getObject(), orgUnit); - LOG.debug("Match found for {} as {}: {}", - delta.getUid().getUidValue(), delta.getObject().getObjectClass(), keys); - - if (keys.size() > 1) { - switch (profile.getResAct()) { - case IGNORE: - throw new IllegalStateException("More than one match " + keys); - - case FIRSTMATCH: - keys = keys.subList(0, 1); - break; - - case LASTMATCH: - keys = keys.subList(keys.size() - 1, keys.size()); - break; - - default: - // keep keys unmodified - } - } - - try { - if (SyncDeltaType.CREATE_OR_UPDATE == delta.getDeltaType()) { - if (keys.isEmpty()) { - switch (profile.getTask().getUnmatchingRule()) { - case ASSIGN: - profile.getResults().addAll(assign(delta, orgUnit)); - break; - - case PROVISION: - profile.getResults().addAll(provision(delta, orgUnit)); - break; - - case IGNORE: - profile.getResults().add(ignore(delta, false)); - break; - - default: - // do nothing - } - } else { - switch (profile.getTask().getMatchingRule()) { - case UPDATE: - profile.getResults().addAll(update(delta, keys)); - break; - - case DEPROVISION: - profile.getResults().addAll(deprovision(delta, keys, false)); - break; - - case UNASSIGN: - profile.getResults().addAll(deprovision(delta, keys, true)); - break; - - case LINK: - profile.getResults().addAll(link(delta, keys, false)); - break; - - case UNLINK: - profile.getResults().addAll(link(delta, keys, true)); - break; - - case IGNORE: - profile.getResults().add(ignore(delta, true)); - break; - - default: - // do nothing - } - } - } else if (SyncDeltaType.DELETE == delta.getDeltaType()) { - if (keys.isEmpty()) { - finalize(ResourceOperation.DELETE.name().toLowerCase(), Result.SUCCESS, null, null, delta); - LOG.debug("No match found for deletion"); - } else { - profile.getResults().addAll(delete(delta, keys)); - } - } - } catch (IllegalStateException | IllegalArgumentException e) { - LOG.warn(e.getMessage()); - } - } - - private void finalize( - final String event, - final Result result, - final Object before, - final Object output, - final SyncDelta delta) { - - synchronized (this) { - this.latestResult = result; - } - - notificationManager.createTasks(AuditElements.EventCategoryType.PULL, - REALM_TYPE.toLowerCase(), - profile.getTask().getResource().getKey(), - event, - result, - before, - output, - delta); - - auditManager.audit(AuditElements.EventCategoryType.PULL, - REALM_TYPE.toLowerCase(), - profile.getTask().getResource().getKey(), - event, - result, - before, - output, - delta); - } -}
http://git-wip-us.apache.org/repos/asf/syncope/blob/9c289134/core/provisioning-java/src/main/java/org/apache/syncope/core/provisioning/java/pushpull/RealmPushResultHandlerImpl.java ---------------------------------------------------------------------- diff --git a/core/provisioning-java/src/main/java/org/apache/syncope/core/provisioning/java/pushpull/RealmPushResultHandlerImpl.java b/core/provisioning-java/src/main/java/org/apache/syncope/core/provisioning/java/pushpull/RealmPushResultHandlerImpl.java deleted file mode 100644 index 655f0c5..0000000 --- a/core/provisioning-java/src/main/java/org/apache/syncope/core/provisioning/java/pushpull/RealmPushResultHandlerImpl.java +++ /dev/null @@ -1,450 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.syncope.core.provisioning.java.pushpull; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.Iterator; -import java.util.List; -import java.util.Map; -import java.util.Optional; -import org.apache.commons.lang3.exception.ExceptionUtils; -import org.apache.syncope.common.lib.to.RealmTO; -import org.apache.syncope.common.lib.types.AuditElements; -import org.apache.syncope.common.lib.types.AuditElements.Result; -import org.apache.syncope.common.lib.types.MatchingRule; -import org.apache.syncope.common.lib.types.PropagationTaskExecStatus; -import org.apache.syncope.core.provisioning.api.PropagationByResource; -import org.apache.syncope.common.lib.types.ResourceOperation; -import org.apache.syncope.common.lib.types.UnmatchingRule; -import org.apache.syncope.core.persistence.api.entity.Realm; -import org.apache.syncope.core.persistence.api.entity.resource.Item; -import org.apache.syncope.core.persistence.api.entity.resource.OrgUnit; -import org.apache.syncope.core.persistence.api.entity.resource.OrgUnitItem; -import org.apache.syncope.core.persistence.api.entity.task.PushTask; -import org.apache.syncope.core.provisioning.api.MappingManager; -import org.apache.syncope.core.provisioning.api.TimeoutException; -import org.apache.syncope.core.provisioning.api.event.AfterHandlingEvent; -import org.apache.syncope.core.provisioning.api.propagation.PropagationReporter; -import org.apache.syncope.core.provisioning.api.pushpull.IgnoreProvisionException; -import org.apache.syncope.core.provisioning.api.pushpull.ProvisioningReport; -import org.apache.syncope.core.provisioning.api.pushpull.PushActions; -import org.apache.syncope.core.provisioning.api.pushpull.SyncopePushResultHandler; -import org.apache.syncope.core.provisioning.java.job.AfterHandlingJob; -import org.apache.syncope.core.provisioning.java.utils.MappingUtils; -import org.identityconnectors.framework.common.objects.AttributeBuilder; -import org.identityconnectors.framework.common.objects.ConnectorObject; -import org.identityconnectors.framework.common.objects.ObjectClass; -import org.quartz.JobExecutionException; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.scheduling.quartz.SchedulerFactoryBean; -import org.springframework.transaction.annotation.Propagation; -import org.springframework.transaction.annotation.Transactional; - -public class RealmPushResultHandlerImpl - extends AbstractRealmResultHandler<PushTask, PushActions> - implements SyncopePushResultHandler { - - @Autowired - private MappingManager mappingManager; - - @Autowired - private SchedulerFactoryBean scheduler; - - @Transactional(propagation = Propagation.REQUIRES_NEW) - @Override - public boolean handle(final String realmKey) { - Realm realm = null; - try { - realm = realmDAO.find(realmKey); - doHandle(realm); - return true; - } catch (IgnoreProvisionException e) { - ProvisioningReport result = new ProvisioningReport(); - result.setOperation(ResourceOperation.NONE); - result.setAnyType(realm == null ? null : REALM_TYPE); - result.setStatus(ProvisioningReport.Status.IGNORE); - result.setKey(realmKey); - profile.getResults().add(result); - - LOG.warn("Ignoring during push", e); - return true; - } catch (JobExecutionException e) { - LOG.error("Push failed", e); - return false; - } - } - - private void reportPropagation(final ProvisioningReport result, final PropagationReporter reporter) { - if (!reporter.getStatuses().isEmpty()) { - result.setStatus(toProvisioningReportStatus(reporter.getStatuses().get(0).getStatus())); - result.setMessage(reporter.getStatuses().get(0).getFailureReason()); - } - } - - private Realm update(final RealmTO realmTO, final ProvisioningReport result) { - Realm realm = realmDAO.findByFullPath(realmTO.getFullPath()); - PropagationByResource propByRes = binder.update(realm, realmTO); - realm = realmDAO.save(realm); - - PropagationReporter reporter = taskExecutor.execute( - propagationManager.createTasks(realm, propByRes, null), false); - reportPropagation(result, reporter); - - return realm; - } - - private void deprovision(final Realm realm, final ProvisioningReport result) { - List<String> noPropResources = new ArrayList<>(realm.getResourceKeys()); - noPropResources.remove(profile.getTask().getResource().getKey()); - - PropagationByResource propByRes = new PropagationByResource(); - propByRes.addAll(ResourceOperation.DELETE, realm.getResourceKeys()); - - PropagationReporter reporter = taskExecutor.execute( - propagationManager.createTasks(realm, propByRes, noPropResources), false); - reportPropagation(result, reporter); - } - - private void provision(final Realm realm, final ProvisioningReport result) { - List<String> noPropResources = new ArrayList<>(realm.getResourceKeys()); - noPropResources.remove(profile.getTask().getResource().getKey()); - - PropagationByResource propByRes = new PropagationByResource(); - propByRes.add(ResourceOperation.CREATE, profile.getTask().getResource().getKey()); - - PropagationReporter reporter = taskExecutor.execute( - propagationManager.createTasks(realm, propByRes, noPropResources), false); - reportPropagation(result, reporter); - } - - private void link(final Realm realm, final boolean unlink, final ProvisioningReport result) { - RealmTO realmTO = binder.getRealmTO(realm, true); - if (unlink) { - realmTO.getResources().remove(profile.getTask().getResource().getKey()); - } else { - realmTO.getResources().add(profile.getTask().getResource().getKey()); - } - - update(realmTO, result); - } - - private void unassign(final Realm realm, final ProvisioningReport result) { - RealmTO realmTO = binder.getRealmTO(realm, true); - realmTO.getResources().remove(profile.getTask().getResource().getKey()); - - deprovision(update(realmTO, result), result); - } - - private void assign(final Realm realm, final ProvisioningReport result) { - RealmTO realmTO = binder.getRealmTO(realm, true); - realmTO.getResources().add(profile.getTask().getResource().getKey()); - - provision(update(realmTO, result), result); - } - - protected ConnectorObject getRemoteObject( - final ObjectClass objectClass, - final String connObjectKey, - final String connObjectKeyValue, - final Iterator<? extends Item> iterator) { - - ConnectorObject obj = null; - try { - obj = profile.getConnector().getObject( - objectClass, - AttributeBuilder.build(connObjectKey, connObjectKeyValue), - MappingUtils.buildOperationOptions(iterator)); - } catch (TimeoutException toe) { - LOG.debug("Request timeout", toe); - throw toe; - } catch (RuntimeException ignore) { - LOG.debug("While resolving {}", connObjectKeyValue, ignore); - } - - return obj; - } - - private void doHandle(final Realm realm) throws JobExecutionException { - ProvisioningReport result = new ProvisioningReport(); - profile.getResults().add(result); - - result.setKey(realm.getKey()); - result.setAnyType(REALM_TYPE); - result.setName(realm.getFullPath()); - - LOG.debug("Propagating Realm with key {} towards {}", realm.getKey(), profile.getTask().getResource()); - - Object output = null; - Result resultStatus = null; - - // Try to read remote object BEFORE any actual operation - OrgUnit orgUnit = profile.getTask().getResource().getOrgUnit(); - Optional<? extends OrgUnitItem> connObjectKey = orgUnit.getConnObjectKeyItem(); - String connObjecKeyValue = mappingManager.getConnObjectKeyValue(realm, orgUnit); - - ConnectorObject beforeObj = getRemoteObject( - orgUnit.getObjectClass(), - connObjectKey.get().getExtAttrName(), - connObjecKeyValue, - orgUnit.getItems().iterator()); - - if (profile.isDryRun()) { - if (beforeObj == null) { - result.setOperation(toResourceOperation(profile.getTask().getUnmatchingRule())); - } else { - result.setOperation(toResourceOperation(profile.getTask().getMatchingRule())); - } - result.setStatus(ProvisioningReport.Status.SUCCESS); - } else { - String operation = beforeObj == null - ? UnmatchingRule.toEventName(profile.getTask().getUnmatchingRule()) - : MatchingRule.toEventName(profile.getTask().getMatchingRule()); - - boolean notificationsAvailable = notificationManager.notificationsAvailable( - AuditElements.EventCategoryType.PUSH, - REALM_TYPE.toLowerCase(), - profile.getTask().getResource().getKey(), - operation); - boolean auditRequested = auditManager.auditRequested( - AuditElements.EventCategoryType.PUSH, - REALM_TYPE.toLowerCase(), - profile.getTask().getResource().getKey(), - operation); - try { - if (beforeObj == null) { - result.setOperation(toResourceOperation(profile.getTask().getUnmatchingRule())); - - switch (profile.getTask().getUnmatchingRule()) { - case ASSIGN: - for (PushActions action : profile.getActions()) { - action.beforeAssign(profile, realm); - } - - if (!profile.getTask().isPerformCreate()) { - LOG.debug("PushTask not configured for create"); - result.setStatus(ProvisioningReport.Status.IGNORE); - } else { - assign(realm, result); - } - - break; - - case PROVISION: - for (PushActions action : profile.getActions()) { - action.beforeProvision(profile, realm); - } - - if (!profile.getTask().isPerformCreate()) { - LOG.debug("PushTask not configured for create"); - result.setStatus(ProvisioningReport.Status.IGNORE); - } else { - provision(realm, result); - } - - break; - - case UNLINK: - for (PushActions action : profile.getActions()) { - action.beforeUnlink(profile, realm); - } - - if (!profile.getTask().isPerformUpdate()) { - LOG.debug("PushTask not configured for update"); - result.setStatus(ProvisioningReport.Status.IGNORE); - } else { - link(realm, true, result); - } - - break; - - case IGNORE: - LOG.debug("Ignored any: {}", realm); - result.setStatus(ProvisioningReport.Status.IGNORE); - break; - - default: - // do nothing - } - } else { - result.setOperation(toResourceOperation(profile.getTask().getMatchingRule())); - - switch (profile.getTask().getMatchingRule()) { - case UPDATE: - for (PushActions action : profile.getActions()) { - action.beforeUpdate(profile, realm); - } - if (!profile.getTask().isPerformUpdate()) { - LOG.debug("PushTask not configured for update"); - result.setStatus(ProvisioningReport.Status.IGNORE); - } else { - update(binder.getRealmTO(realm, true), result); - } - - break; - - case DEPROVISION: - for (PushActions action : profile.getActions()) { - action.beforeDeprovision(profile, realm); - } - - if (!profile.getTask().isPerformDelete()) { - LOG.debug("PushTask not configured for delete"); - result.setStatus(ProvisioningReport.Status.IGNORE); - } else { - deprovision(realm, result); - } - - break; - - case UNASSIGN: - for (PushActions action : profile.getActions()) { - action.beforeUnassign(profile, realm); - } - - if (!profile.getTask().isPerformDelete()) { - LOG.debug("PushTask not configured for delete"); - result.setStatus(ProvisioningReport.Status.IGNORE); - } else { - unassign(realm, result); - } - - break; - - case LINK: - for (PushActions action : profile.getActions()) { - action.beforeLink(profile, realm); - } - - if (!profile.getTask().isPerformUpdate()) { - LOG.debug("PushTask not configured for update"); - result.setStatus(ProvisioningReport.Status.IGNORE); - } else { - link(realm, false, result); - } - - break; - - case UNLINK: - for (PushActions action : profile.getActions()) { - action.beforeUnlink(profile, realm); - } - - if (!profile.getTask().isPerformUpdate()) { - LOG.debug("PushTask not configured for update"); - result.setStatus(ProvisioningReport.Status.IGNORE); - } else { - link(realm, true, result); - } - - break; - - case IGNORE: - LOG.debug("Ignored any: {}", realm); - result.setStatus(ProvisioningReport.Status.IGNORE); - break; - - default: - // do nothing - } - } - - for (PushActions action : profile.getActions()) { - action.after(profile, realm, result); - } - - if (result.getStatus() == null) { - result.setStatus(ProvisioningReport.Status.SUCCESS); - } - resultStatus = AuditElements.Result.SUCCESS; - output = getRemoteObject( - orgUnit.getObjectClass(), - connObjectKey.get().getExtAttrName(), - connObjecKeyValue, - orgUnit.getItems().iterator()); - } catch (IgnoreProvisionException e) { - throw e; - } catch (Exception e) { - result.setStatus(ProvisioningReport.Status.FAILURE); - result.setMessage(ExceptionUtils.getRootCauseMessage(e)); - resultStatus = AuditElements.Result.FAILURE; - output = e; - - LOG.warn("Error pushing {} towards {}", realm, profile.getTask().getResource(), e); - - for (PushActions action : profile.getActions()) { - action.onError(profile, realm, result, e); - } - - throw new JobExecutionException(e); - } finally { - if (notificationsAvailable || auditRequested) { - Map<String, Object> jobMap = new HashMap<>(); - jobMap.put(AfterHandlingEvent.JOBMAP_KEY, new AfterHandlingEvent( - AuditElements.EventCategoryType.PUSH, - REALM_TYPE.toLowerCase(), - profile.getTask().getResource().getKey(), - operation, - resultStatus, - beforeObj, - output, - realm)); - AfterHandlingJob.schedule(scheduler, jobMap); - } - } - } - } - - private ResourceOperation toResourceOperation(final UnmatchingRule rule) { - switch (rule) { - case ASSIGN: - case PROVISION: - return ResourceOperation.CREATE; - default: - return ResourceOperation.NONE; - } - } - - private ResourceOperation toResourceOperation(final MatchingRule rule) { - switch (rule) { - case UPDATE: - return ResourceOperation.UPDATE; - case DEPROVISION: - case UNASSIGN: - return ResourceOperation.DELETE; - default: - return ResourceOperation.NONE; - } - } - - private ProvisioningReport.Status toProvisioningReportStatus(final PropagationTaskExecStatus status) { - switch (status) { - case FAILURE: - return ProvisioningReport.Status.FAILURE; - - case SUCCESS: - return ProvisioningReport.Status.SUCCESS; - - case CREATED: - case NOT_ATTEMPTED: - default: - return ProvisioningReport.Status.IGNORE; - } - } -} http://git-wip-us.apache.org/repos/asf/syncope/blob/9c289134/core/provisioning-java/src/main/java/org/apache/syncope/core/provisioning/java/pushpull/UserPullResultHandlerImpl.java ---------------------------------------------------------------------- diff --git a/core/provisioning-java/src/main/java/org/apache/syncope/core/provisioning/java/pushpull/UserPullResultHandlerImpl.java b/core/provisioning-java/src/main/java/org/apache/syncope/core/provisioning/java/pushpull/UserPullResultHandlerImpl.java deleted file mode 100644 index fa299a0..0000000 --- a/core/provisioning-java/src/main/java/org/apache/syncope/core/provisioning/java/pushpull/UserPullResultHandlerImpl.java +++ /dev/null @@ -1,134 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.syncope.core.provisioning.java.pushpull; - -import java.util.Collections; -import java.util.List; -import java.util.Map; -import org.apache.commons.lang3.tuple.Pair; -import org.apache.syncope.common.lib.patch.AnyPatch; -import org.apache.syncope.common.lib.patch.UserPatch; -import org.apache.syncope.common.lib.to.AnyTO; -import org.apache.syncope.common.lib.to.PropagationStatus; -import org.apache.syncope.common.lib.to.UserTO; -import org.apache.syncope.common.lib.types.AnyTypeKind; -import org.apache.syncope.core.persistence.api.entity.Any; -import org.apache.syncope.core.persistence.api.entity.AnyUtils; -import org.apache.syncope.core.provisioning.api.ProvisioningManager; -import org.apache.syncope.core.provisioning.api.UserProvisioningManager; -import org.apache.syncope.core.provisioning.api.WorkflowResult; -import org.apache.syncope.core.provisioning.api.pushpull.ProvisioningReport; -import org.identityconnectors.framework.common.objects.SyncDelta; -import org.apache.syncope.core.provisioning.api.pushpull.UserPullResultHandler; -import org.springframework.beans.factory.annotation.Autowired; - -public class UserPullResultHandlerImpl extends AbstractPullResultHandler implements UserPullResultHandler { - - @Autowired - private UserProvisioningManager userProvisioningManager; - - @Override - protected AnyUtils getAnyUtils() { - return anyUtilsFactory.getInstance(AnyTypeKind.USER); - } - - @Override - protected String getName(final AnyTO anyTO) { - return UserTO.class.cast(anyTO).getUsername(); - } - - @Override - protected ProvisioningManager<?, ?> getProvisioningManager() { - return userProvisioningManager; - } - - @Override - protected Any<?> getAny(final String key) { - try { - return userDAO.authFind(key); - } catch (Exception e) { - LOG.warn("Error retrieving user {}", key, e); - return null; - } - } - - @Override - protected AnyTO getAnyTO(final String key) { - return userDataBinder.getUserTO(key); - } - - @Override - protected AnyPatch newPatch(final String key) { - UserPatch patch = new UserPatch(); - patch.setKey(key); - return patch; - } - - @Override - protected WorkflowResult<? extends AnyPatch> update(final AnyPatch patch) { - WorkflowResult<Pair<UserPatch, Boolean>> update = uwfAdapter.update((UserPatch) patch); - return new WorkflowResult<>(update.getResult().getLeft(), update.getPropByRes(), update.getPerformedTasks()); - } - - @Override - protected AnyTO doCreate(final AnyTO anyTO, final SyncDelta delta) { - UserTO userTO = UserTO.class.cast(anyTO); - - Boolean enabled = pullUtils.readEnabled(delta.getObject(), profile.getTask()); - Map.Entry<String, List<PropagationStatus>> created = - userProvisioningManager.create(userTO, true, true, enabled, - Collections.singleton(profile.getTask().getResource().getKey()), true); - - return getAnyTO(created.getKey()); - } - - @Override - protected AnyPatch doUpdate( - final AnyTO before, - final AnyPatch anyPatch, - final SyncDelta delta, - final ProvisioningReport result) { - - UserPatch userPatch = UserPatch.class.cast(anyPatch); - Boolean enabled = pullUtils.readEnabled(delta.getObject(), profile.getTask()); - - Pair<UserPatch, List<PropagationStatus>> updated = userProvisioningManager.update( - userPatch, - result, - enabled, - Collections.singleton(profile.getTask().getResource().getKey()), - true); - - return updated.getLeft(); - } - - @Override - protected void doDelete(final AnyTypeKind kind, final String key) { - try { - userProvisioningManager.delete( - key, Collections.<String>singleton(profile.getTask().getResource().getKey()), true); - } catch (Exception e) { - // A propagation failure doesn't imply a pull failure. - // The propagation exception status will be reported into the propagation task execution. - LOG.error("Could not propagate user " + key, e); - } - - uwfAdapter.delete(key); - } -} http://git-wip-us.apache.org/repos/asf/syncope/blob/9c289134/core/provisioning-java/src/main/java/org/apache/syncope/core/provisioning/java/pushpull/UserPushResultHandlerImpl.java ---------------------------------------------------------------------- diff --git a/core/provisioning-java/src/main/java/org/apache/syncope/core/provisioning/java/pushpull/UserPushResultHandlerImpl.java b/core/provisioning-java/src/main/java/org/apache/syncope/core/provisioning/java/pushpull/UserPushResultHandlerImpl.java deleted file mode 100644 index b89850f..0000000 --- a/core/provisioning-java/src/main/java/org/apache/syncope/core/provisioning/java/pushpull/UserPushResultHandlerImpl.java +++ /dev/null @@ -1,99 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.syncope.core.provisioning.java.pushpull; - -import java.util.ArrayList; -import java.util.List; -import org.apache.commons.lang3.tuple.Pair; -import org.apache.syncope.common.lib.patch.AnyPatch; -import org.apache.syncope.common.lib.patch.UserPatch; -import org.apache.syncope.common.lib.to.AnyTO; -import org.apache.syncope.common.lib.types.AnyTypeKind; -import org.apache.syncope.core.provisioning.api.PropagationByResource; -import org.apache.syncope.common.lib.types.ResourceOperation; -import org.apache.syncope.core.persistence.api.entity.Any; -import org.apache.syncope.core.persistence.api.entity.AnyUtils; -import org.apache.syncope.core.persistence.api.entity.user.User; -import org.apache.syncope.core.provisioning.api.WorkflowResult; -import org.apache.syncope.core.provisioning.api.propagation.PropagationReporter; -import org.apache.syncope.core.provisioning.api.pushpull.ProvisioningReport; -import org.apache.syncope.core.provisioning.api.pushpull.UserPushResultHandler; - -public class UserPushResultHandlerImpl extends AbstractPushResultHandler implements UserPushResultHandler { - - @Override - protected AnyUtils getAnyUtils() { - return anyUtilsFactory.getInstance(AnyTypeKind.USER); - } - - @Override - protected void provision(final Any<?> any, final Boolean enabled, final ProvisioningReport result) { - AnyTO before = getAnyTO(any.getKey()); - - List<String> noPropResources = new ArrayList<>(before.getResources()); - noPropResources.remove(profile.getTask().getResource().getKey()); - - PropagationByResource propByRes = new PropagationByResource(); - propByRes.add(ResourceOperation.CREATE, profile.getTask().getResource().getKey()); - - PropagationReporter reporter = taskExecutor.execute(propagationManager.getUserCreateTasks( - before.getKey(), - null, - enabled, - propByRes, - before.getVirAttrs(), - noPropResources), - false); - reportPropagation(result, reporter); - } - - @Override - protected String getName(final Any<?> any) { - return User.class.cast(any).getUsername(); - } - - @Override - protected Any<?> getAny(final String key) { - try { - return userDAO.authFind(key); - } catch (Exception e) { - LOG.warn("Error retrieving user {}", key, e); - return null; - } - } - - @Override - protected AnyTO getAnyTO(final String key) { - return userDataBinder.getUserTO(key); - } - - @Override - protected AnyPatch newPatch(final String key) { - UserPatch patch = new UserPatch(); - patch.setKey(key); - return patch; - } - - @Override - protected WorkflowResult<? extends AnyPatch> update(final AnyPatch patch) { - WorkflowResult<Pair<UserPatch, Boolean>> update = uwfAdapter.update((UserPatch) patch); - return new WorkflowResult<>(update.getResult().getLeft(), update.getPropByRes(), update.getPerformedTasks()); - } - -}
