Search in sources :

Example 96 with PrismObject

use of com.evolveum.midpoint.prism.PrismObject in project midpoint by Evolveum.

the class AbstractBasicDummyTest method test015ListResourcesNoFetch.

/**
	 * List resources with noFetch option. This is what GUI does. This operation
	 * should be harmless and should not change resource state.
	 */
@Test
public void test015ListResourcesNoFetch() throws Exception {
    final String TEST_NAME = "test015ListResourcesNoFetch";
    TestUtil.displayTestTile(TEST_NAME);
    // GIVEN
    Task task = taskManager.createTaskInstance(AbstractBasicDummyTest.class.getName() + "." + TEST_NAME);
    OperationResult result = task.getResult();
    Collection<SelectorOptions<GetOperationOptions>> options = SelectorOptions.createCollection(GetOperationOptions.createNoFetch());
    // WHEN
    SearchResultList<PrismObject<ResourceType>> resources = provisioningService.searchObjects(ResourceType.class, null, options, task, result);
    // THEN
    result.computeStatus();
    display("searchObjects result", result);
    TestUtil.assertSuccess(result);
    assertFalse("No resources found", resources.isEmpty());
    for (PrismObject<ResourceType> resource : resources) {
        ResourceType resourceType = resource.asObjectable();
        display("Found resource " + resourceType, resourceType);
        display("XML " + resourceType, PrismTestUtil.serializeObjectToString(resource, PrismContext.LANG_XML));
        XmlSchemaType xmlSchemaType = resourceType.getSchema();
        if (xmlSchemaType != null) {
            Element xsdSchemaElement = ResourceTypeUtil.getResourceXsdSchema(resourceType);
            assertNull("Found schema in " + resource, xsdSchemaElement);
        }
    }
    assertConnectorSchemaParseIncrement(1);
    assertConnectorCapabilitiesFetchIncrement(0);
    assertConnectorInitializationCountIncrement(0);
    assertResourceSchemaFetchIncrement(0);
    assertResourceSchemaParseCountIncrement(0);
}
Also used : PrismObject(com.evolveum.midpoint.prism.PrismObject) Task(com.evolveum.midpoint.task.api.Task) SelectorOptions(com.evolveum.midpoint.schema.SelectorOptions) Element(org.w3c.dom.Element) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) ResourceType(com.evolveum.midpoint.xml.ns._public.common.common_3.ResourceType) XmlSchemaType(com.evolveum.midpoint.xml.ns._public.common.common_3.XmlSchemaType) Test(org.testng.annotations.Test)

Example 97 with PrismObject

use of com.evolveum.midpoint.prism.PrismObject in project midpoint by Evolveum.

the class ShadowManager method findOrAddShadowFromChangeGlobalContext.

public PrismObject<ShadowType> findOrAddShadowFromChangeGlobalContext(ProvisioningContext globalCtx, Change change, OperationResult parentResult) throws SchemaException, CommunicationException, ConfigurationException, SecurityViolationException, ObjectNotFoundException, ExpressionEvaluationException {
    // Try to locate existing shadow in the repository
    List<PrismObject<ShadowType>> accountList = searchShadowByIdenifiers(globalCtx, change, parentResult);
    if (accountList.size() > 1) {
        String message = "Found more than one shadow with the identifier " + change.getIdentifiers() + ".";
        LOGGER.error(message);
        parentResult.recordFatalError(message);
        throw new IllegalArgumentException(message);
    }
    PrismObject<ShadowType> newShadow = null;
    if (accountList.isEmpty()) {
        if (change.getObjectDelta() == null || change.getObjectDelta().getChangeType() != ChangeType.DELETE) {
            newShadow = createNewShadowFromChange(globalCtx, change, parentResult);
            try {
                ConstraintsChecker.onShadowAddOperation(newShadow.asObjectable());
                String oid = repositoryService.addObject(newShadow, null, parentResult);
                newShadow.setOid(oid);
                if (change.getObjectDelta() != null && change.getObjectDelta().getOid() == null) {
                    change.getObjectDelta().setOid(oid);
                }
            } catch (ObjectAlreadyExistsException e) {
                parentResult.recordFatalError("Can't add " + SchemaDebugUtil.prettyPrint(newShadow) + " to the repository. Reason: " + e.getMessage(), e);
                throw new IllegalStateException(e.getMessage(), e);
            }
            LOGGER.debug("Added new shadow (from global change): {}", newShadow);
            if (LOGGER.isTraceEnabled()) {
                LOGGER.trace("Added new shadow (from global change):\n{}", newShadow.debugDump());
            }
        }
    } else {
        // Account was found in repository
        newShadow = accountList.get(0);
        if (change.getObjectDelta() != null && change.getObjectDelta().getChangeType() == ChangeType.DELETE) {
            Collection<? extends ItemDelta> deadDeltas = PropertyDelta.createModificationReplacePropertyCollection(ShadowType.F_DEAD, newShadow.getDefinition(), true);
            try {
                ConstraintsChecker.onShadowModifyOperation(deadDeltas);
                repositoryService.modifyObject(ShadowType.class, newShadow.getOid(), deadDeltas, parentResult);
            } catch (ObjectAlreadyExistsException e) {
                parentResult.recordFatalError("Can't add " + SchemaDebugUtil.prettyPrint(newShadow) + " to the repository. Reason: " + e.getMessage(), e);
                throw new IllegalStateException(e.getMessage(), e);
            } catch (ObjectNotFoundException e) {
                parentResult.recordWarning("Shadow " + SchemaDebugUtil.prettyPrint(newShadow) + " was probably deleted from the repository in the meantime. Exception: " + e.getMessage(), e);
                return null;
            }
        }
    }
    return newShadow;
}
Also used : PrismObject(com.evolveum.midpoint.prism.PrismObject) ObjectNotFoundException(com.evolveum.midpoint.util.exception.ObjectNotFoundException) PolyString(com.evolveum.midpoint.prism.polystring.PolyString) ObjectAlreadyExistsException(com.evolveum.midpoint.util.exception.ObjectAlreadyExistsException)

Example 98 with PrismObject

use of com.evolveum.midpoint.prism.PrismObject in project midpoint by Evolveum.

the class ShadowManager method updateShadow.

@SuppressWarnings("unchecked")
public Collection<ItemDelta> updateShadow(ProvisioningContext ctx, PrismObject<ShadowType> resourceShadow, Collection<? extends ItemDelta> aprioriDeltas, OperationResult result) throws SchemaException, ConfigurationException, ObjectNotFoundException, CommunicationException, ExpressionEvaluationException {
    PrismObject<ShadowType> repoShadow = repositoryService.getObject(ShadowType.class, resourceShadow.getOid(), null, result);
    RefinedObjectClassDefinition objectClassDefinition = ctx.getObjectClassDefinition();
    Collection<ItemDelta> repoShadowChanges = new ArrayList<ItemDelta>();
    CachingStategyType cachingStrategy = ProvisioningUtil.getCachingStrategy(ctx);
    for (RefinedAttributeDefinition attrDef : objectClassDefinition.getAttributeDefinitions()) {
        if (ProvisioningUtil.shouldStoreAtributeInShadow(objectClassDefinition, attrDef.getName(), cachingStrategy)) {
            ResourceAttribute<Object> resourceAttr = ShadowUtil.getAttribute(resourceShadow, attrDef.getName());
            PrismProperty<Object> repoAttr = repoShadow.findProperty(new ItemPath(ShadowType.F_ATTRIBUTES, attrDef.getName()));
            PropertyDelta attrDelta;
            if (repoAttr == null && repoAttr == null) {
                continue;
            }
            if (repoAttr == null) {
                attrDelta = attrDef.createEmptyDelta(new ItemPath(ShadowType.F_ATTRIBUTES, attrDef.getName()));
                attrDelta.setValuesToReplace(PrismValue.cloneCollection(resourceAttr.getValues()));
            } else {
                attrDelta = repoAttr.diff(resourceAttr);
            //					LOGGER.trace("DIFF:\n{}\n-\n{}\n=:\n{}", repoAttr==null?null:repoAttr.debugDump(1), resourceAttr==null?null:resourceAttr.debugDump(1), attrDelta==null?null:attrDelta.debugDump(1));
            }
            if (attrDelta != null && !attrDelta.isEmpty()) {
                normalizeDelta(attrDelta, attrDef);
                repoShadowChanges.add(attrDelta);
            }
        }
    }
    if (LOGGER.isTraceEnabled()) {
        LOGGER.trace("Updating repo shadow {}:\n{}", resourceShadow.getOid(), DebugUtil.debugDump(repoShadowChanges));
    }
    try {
        repositoryService.modifyObject(ShadowType.class, resourceShadow.getOid(), repoShadowChanges, result);
    } catch (ObjectAlreadyExistsException e) {
        // We are not renaming the object here. This should not happen.
        throw new SystemException(e.getMessage(), e);
    }
    return repoShadowChanges;
}
Also used : ArrayList(java.util.ArrayList) ItemDelta(com.evolveum.midpoint.prism.delta.ItemDelta) RefinedObjectClassDefinition(com.evolveum.midpoint.common.refinery.RefinedObjectClassDefinition) SystemException(com.evolveum.midpoint.util.exception.SystemException) RefinedAttributeDefinition(com.evolveum.midpoint.common.refinery.RefinedAttributeDefinition) PrismObject(com.evolveum.midpoint.prism.PrismObject) PropertyDelta(com.evolveum.midpoint.prism.delta.PropertyDelta) ObjectAlreadyExistsException(com.evolveum.midpoint.util.exception.ObjectAlreadyExistsException) ItemPath(com.evolveum.midpoint.prism.path.ItemPath)

Example 99 with PrismObject

use of com.evolveum.midpoint.prism.PrismObject in project midpoint by Evolveum.

the class ShadowManager method createRepositoryShadow.

/**
	 * Create a copy of a shadow that is suitable for repository storage. 
	 */
private PrismObject<ShadowType> createRepositoryShadow(ProvisioningContext ctx, PrismObject<ShadowType> shadow) throws SchemaException, ConfigurationException, ObjectNotFoundException, CommunicationException, ExpressionEvaluationException {
    ResourceAttributeContainer attributesContainer = ShadowUtil.getAttributesContainer(shadow);
    PrismObject<ShadowType> repoShadow = shadow.clone();
    ShadowType repoShadowType = repoShadow.asObjectable();
    ResourceAttributeContainer repoAttributesContainer = ShadowUtil.getAttributesContainer(repoShadow);
    CachingStategyType cachingStrategy = ProvisioningUtil.getCachingStrategy(ctx);
    if (cachingStrategy == CachingStategyType.NONE) {
        // Clean all repoShadow attributes and add only those that should be
        // there
        repoAttributesContainer.clear();
        Collection<ResourceAttribute<?>> primaryIdentifiers = attributesContainer.getPrimaryIdentifiers();
        for (PrismProperty<?> p : primaryIdentifiers) {
            repoAttributesContainer.add(p.clone());
        }
        Collection<ResourceAttribute<?>> secondaryIdentifiers = attributesContainer.getSecondaryIdentifiers();
        for (PrismProperty<?> p : secondaryIdentifiers) {
            repoAttributesContainer.add(p.clone());
        }
        // Also add all the attributes that act as association identifiers.
        // We will need them when the shadow is deleted (to remove the shadow from entitlements).
        RefinedObjectClassDefinition objectClassDefinition = ctx.getObjectClassDefinition();
        for (RefinedAssociationDefinition associationDef : objectClassDefinition.getAssociationDefinitions()) {
            if (associationDef.getResourceObjectAssociationType().getDirection() == ResourceObjectAssociationDirectionType.OBJECT_TO_SUBJECT) {
                QName valueAttributeName = associationDef.getResourceObjectAssociationType().getValueAttribute();
                if (repoAttributesContainer.findAttribute(valueAttributeName) == null) {
                    ResourceAttribute<Object> valueAttribute = attributesContainer.findAttribute(valueAttributeName);
                    if (valueAttribute != null) {
                        repoAttributesContainer.add(valueAttribute.clone());
                    }
                }
            }
        }
        repoShadowType.setCachingMetadata(null);
        ProvisioningUtil.cleanupShadowActivation(repoShadowType);
    } else if (cachingStrategy == CachingStategyType.PASSIVE) {
        // Do not need to clear anything. Just store all attributes and add metadata.
        CachingMetadataType cachingMetadata = new CachingMetadataType();
        cachingMetadata.setRetrievalTimestamp(clock.currentTimeXMLGregorianCalendar());
        repoShadowType.setCachingMetadata(cachingMetadata);
    } else {
        throw new ConfigurationException("Unknown caching strategy " + cachingStrategy);
    }
    setKindIfNecessary(repoShadowType, ctx.getObjectClassDefinition());
    //        setIntentIfNecessary(repoShadowType, objectClassDefinition);
    // Store only password meta-data in repo
    CredentialsType creds = repoShadowType.getCredentials();
    if (creds != null) {
        PasswordType passwordType = creds.getPassword();
        if (passwordType != null) {
            ProvisioningUtil.cleanupShadowPassword(passwordType);
            PrismObject<UserType> owner = null;
            if (ctx.getTask() != null) {
                owner = ctx.getTask().getOwner();
            }
            ProvisioningUtil.addPasswordMetadata(passwordType, clock.currentTimeXMLGregorianCalendar(), owner);
        }
    // TODO: other credential types - later
    }
    // convert to the resource reference.
    if (repoShadowType.getResource() != null) {
        repoShadowType.setResource(null);
        repoShadowType.setResourceRef(ObjectTypeUtil.createObjectRef(ctx.getResource()));
    }
    // now
    if (repoShadowType.getResourceRef() == null) {
        repoShadowType.setResourceRef(ObjectTypeUtil.createObjectRef(ctx.getResource()));
    }
    if (repoShadowType.getName() == null) {
        repoShadowType.setName(new PolyStringType(ShadowUtil.determineShadowName(shadow)));
    }
    if (repoShadowType.getObjectClass() == null) {
        repoShadowType.setObjectClass(attributesContainer.getDefinition().getTypeName());
    }
    if (repoShadowType.isProtectedObject() != null) {
        repoShadowType.setProtectedObject(null);
    }
    normalizeAttributes(repoShadow, ctx.getObjectClassDefinition());
    return repoShadow;
}
Also used : PolyStringType(com.evolveum.prism.xml.ns._public.types_3.PolyStringType) QName(javax.xml.namespace.QName) ResourceAttributeContainer(com.evolveum.midpoint.schema.processor.ResourceAttributeContainer) RefinedAssociationDefinition(com.evolveum.midpoint.common.refinery.RefinedAssociationDefinition) RefinedObjectClassDefinition(com.evolveum.midpoint.common.refinery.RefinedObjectClassDefinition) ConfigurationException(com.evolveum.midpoint.util.exception.ConfigurationException) PrismObject(com.evolveum.midpoint.prism.PrismObject) ResourceAttribute(com.evolveum.midpoint.schema.processor.ResourceAttribute)

Example 100 with PrismObject

use of com.evolveum.midpoint.prism.PrismObject in project midpoint by Evolveum.

the class ShadowManager method lookupShadowsBySecondaryIdentifiers.

private List<PrismObject<ShadowType>> lookupShadowsBySecondaryIdentifiers(ProvisioningContext ctx, Collection<ResourceAttribute<?>> secondaryIdentifiers, OperationResult parentResult) throws SchemaException, ConfigurationException, ObjectNotFoundException, CommunicationException, ExpressionEvaluationException {
    if (secondaryIdentifiers.size() < 1) {
        LOGGER.trace("Shadow does not contain secondary identifier. Skipping lookup shadows according to name.");
        return null;
    }
    S_FilterEntry q = QueryBuilder.queryFor(ShadowType.class, prismContext).block();
    for (ResourceAttribute<?> secondaryIdentifier : secondaryIdentifiers) {
        // There may be identifiers that come from associations and they will have parent set to association/identifiers
        // For the search to succeed we need all attribute to have "attributes" parent path.
        secondaryIdentifier = ShadowUtil.fixAttributePath(secondaryIdentifier);
        q = q.item(secondaryIdentifier.getPath(), secondaryIdentifier.getDefinition()).eq(getNormalizedValue(secondaryIdentifier, ctx.getObjectClassDefinition())).or();
    }
    ObjectQuery query = q.none().endBlock().and().item(ShadowType.F_RESOURCE_REF).ref(ctx.getResourceOid()).build();
    if (LOGGER.isTraceEnabled()) {
        LOGGER.trace("Searching for shadow using filter on secondary identifier:\n{}", query.debugDump());
    }
    // TODO: check for errors
    List<PrismObject<ShadowType>> results = repositoryService.searchObjects(ShadowType.class, query, null, parentResult);
    MiscSchemaUtil.reduceSearchResult(results);
    LOGGER.trace("lookupShadow found {} objects", results.size());
    if (LOGGER.isTraceEnabled() && results.size() == 1) {
        LOGGER.trace("lookupShadow found\n{}", results.get(0).debugDump(1));
    }
    return results;
}
Also used : PrismObject(com.evolveum.midpoint.prism.PrismObject) S_FilterEntry(com.evolveum.midpoint.prism.query.builder.S_FilterEntry) ObjectQuery(com.evolveum.midpoint.prism.query.ObjectQuery)

Aggregations

PrismObject (com.evolveum.midpoint.prism.PrismObject)488 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)358 Test (org.testng.annotations.Test)227 ObjectQuery (com.evolveum.midpoint.prism.query.ObjectQuery)219 Task (com.evolveum.midpoint.task.api.Task)205 ShadowType (com.evolveum.midpoint.xml.ns._public.common.common_3.ShadowType)97 SchemaException (com.evolveum.midpoint.util.exception.SchemaException)95 ArrayList (java.util.ArrayList)81 UserType (com.evolveum.midpoint.xml.ns._public.common.common_3.UserType)79 ObjectNotFoundException (com.evolveum.midpoint.util.exception.ObjectNotFoundException)64 QName (javax.xml.namespace.QName)59 PolyString (com.evolveum.midpoint.prism.polystring.PolyString)50 ConfigurationException (com.evolveum.midpoint.util.exception.ConfigurationException)40 ItemPath (com.evolveum.midpoint.prism.path.ItemPath)37 ResultHandler (com.evolveum.midpoint.schema.ResultHandler)37 SearchResultMetadata (com.evolveum.midpoint.schema.SearchResultMetadata)37 CommunicationException (com.evolveum.midpoint.util.exception.CommunicationException)37 ExpressionEvaluationException (com.evolveum.midpoint.util.exception.ExpressionEvaluationException)37 ObjectType (com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectType)35 SystemException (com.evolveum.midpoint.util.exception.SystemException)34