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);
}
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;
}
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;
}
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;
}
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;
}
Aggregations