Skip to content

Commit

Permalink
Merge branch 'master' of github.com:Evolveum/midpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
mederly committed Sep 3, 2015
2 parents 3c2e441 + b39467d commit 2acabd7
Show file tree
Hide file tree
Showing 19 changed files with 248 additions and 83 deletions.
Expand Up @@ -413,7 +413,14 @@ public boolean equalsComplex(PrismPropertyValue<?> other, boolean ignoreMetadata
}

if (matchingRule != null) {
return matchingRule.match(thisRealValue, otherRealValue);
try {
return matchingRule.match(thisRealValue, otherRealValue);
} catch (SchemaException e) {
// At least one of the values is invalid. But we do not want to throw exception from
// a comparison operation. That will make the system very fragile. Let's fall back to
// ordinary equality mechanism instead.
return thisRealValue.equals(otherRealValue);
}
} else {

if (thisRealValue instanceof Element &&
Expand Down
Expand Up @@ -26,6 +26,7 @@

import com.evolveum.midpoint.prism.PrismConstants;
import com.evolveum.midpoint.util.DOMUtil;
import com.evolveum.midpoint.util.exception.SchemaException;

/**
* Matching rule for LDAP distinguished name (DN).
Expand All @@ -51,7 +52,7 @@ public boolean isSupported(QName xsdType) {
* @see com.evolveum.midpoint.model.match.MatchingRule#match(java.lang.Object, java.lang.Object)
*/
@Override
public boolean match(String a, String b) {
public boolean match(String a, String b) throws SchemaException {
if (StringUtils.isBlank(a) && StringUtils.isBlank(b)) {
return true;
}
Expand All @@ -62,13 +63,13 @@ public boolean match(String a, String b) {
try {
dnA = new LdapName(a);
} catch (InvalidNameException e) {
throw new IllegalArgumentException("String '"+a+"' is not a DN: "+e.getMessage(), e);
throw new SchemaException("String '"+a+"' is not a DN: "+e.getMessage(), e);
}
LdapName dnB;
try {
dnB = new LdapName(b);
} catch (InvalidNameException e) {
throw new IllegalArgumentException("String '"+b+"' is not a DN: "+e.getMessage(), e);
throw new SchemaException("String '"+b+"' is not a DN: "+e.getMessage(), e);
}
return dnA.equals(dnB);
}
Expand All @@ -77,21 +78,21 @@ public boolean match(String a, String b) {
* @see com.evolveum.midpoint.prism.match.MatchingRule#normalize(java.lang.Object)
*/
@Override
public String normalize(String original) {
public String normalize(String original) throws SchemaException {
if (StringUtils.isBlank(original)) {
return null;
}
LdapName dn;
try {
dn = new LdapName(original);
} catch (InvalidNameException e) {
throw new IllegalArgumentException("String '"+original+"' is not a DN: "+e.getMessage(), e);
throw new SchemaException("String '"+original+"' is not a DN: "+e.getMessage(), e);
}
return StringUtils.lowerCase(dn.toString());
}

@Override
public boolean matchRegex(String a, String regex) {
public boolean matchRegex(String a, String regex) throws SchemaException {

a = normalize(a);

Expand Down
Expand Up @@ -19,6 +19,8 @@

import javax.xml.namespace.QName;

import com.evolveum.midpoint.util.exception.SchemaException;

/**
* Interface for generic matching rules. The responsibility of a matching rule is to decide if
* two objects of the same type match. This may seem a simple thing to do but the details may get
Expand All @@ -43,17 +45,17 @@ public interface MatchingRule<T> {
/**
* Matches two objects.
*/
boolean match(T a, T b);
boolean match(T a, T b) throws SchemaException;

/**
* Matches value against given regex.
*/
boolean matchRegex(T a, String regex);
boolean matchRegex(T a, String regex) throws SchemaException;

/**
* Returns a normalized version of the value.
* For normalized version the following holds:
* if A matches B then normalize(A) == normalize(B)
*/
T normalize(T original);
T normalize(T original) throws SchemaException;
}
Expand Up @@ -227,8 +227,17 @@ private boolean isInFilterItem(PrismPropertyValue v, Item filterItem, MatchingRu
}

PrismPropertyValue filterV = (PrismPropertyValue) filterValue;
if (matchingRule.match(filterV.getValue(), v.getValue())){
return true;
try {
if (matchingRule.match(filterV.getValue(), v.getValue())){
return true;
}
} catch (SchemaException e) {
// At least one of the values is invalid. But we do not want to throw exception from
// a comparison operation. That will make the system very fragile. Let's fall back to
// ordinary equality mechanism instead.
if (filterV.getValue().equals(v.getValue())) {
return true;
}
}
}

Expand Down
Expand Up @@ -735,10 +735,15 @@ public static <T> void assertValues(String message, Collection<PrismPropertyValu
}

public static <T> void assertSets(String message, Collection<T> actualValues, T... expectedValues) {
assertSets(message, null, actualValues, expectedValues);
try {
assertSets(message, null, actualValues, expectedValues);
} catch (SchemaException e) {
// no matching rule. should not happen
throw new IllegalStateException(e.getMessage(), e);
}
}

public static <T> void assertSets(String message, MatchingRule<T> matchingRule, Collection<T> actualValues, T... expectedValues) {
public static <T> void assertSets(String message, MatchingRule<T> matchingRule, Collection<T> actualValues, T... expectedValues) throws SchemaException {
assertNotNull("Null set in " + message, actualValues);
assertEquals("Wrong number of values in " + message+ "; expected (real values) "
+PrettyPrinter.prettyPrint(expectedValues)+"; has (pvalues) "+actualValues,
Expand Down Expand Up @@ -901,7 +906,7 @@ public static void assertEquals(String message, Object expected, Object actual)
+ ", was " + MiscUtil.getValueWithClass(actual);
}

public static <T> void assertEquals(String message, MatchingRule<T> matchingRule, T expected, T actual) {
public static <T> void assertEquals(String message, MatchingRule<T> matchingRule, T expected, T actual) throws SchemaException {
assert equals(matchingRule, expected, actual) : message
+ ": expected " + MiscUtil.getValueWithClass(expected)
+ ", was " + MiscUtil.getValueWithClass(actual);
Expand All @@ -917,7 +922,7 @@ static void fail(String message) {
assert false: message;
}

private static <T> boolean equals(MatchingRule<T> matchingRule, T a, T b) {
private static <T> boolean equals(MatchingRule<T> matchingRule, T a, T b) throws SchemaException {
if (a == null && b == null) {
return true;
}
Expand Down
Expand Up @@ -118,15 +118,15 @@ public void testPolyStringNorm() throws Exception {
assertNoMatch(rule, new PolyString("Bar", "bar"), new PolyString("Bar", "barbar"));
}

private <T> void assertMatch(MatchingRule<T> rule, T a, T b) {
private <T> void assertMatch(MatchingRule<T> rule, T a, T b) throws SchemaException {
assertTrue("Values '"+a+"' and '"+b+"' does not match; rule: "+rule, rule.match(a, b));
}

private <T> void assertNoMatch(MatchingRule<T> rule, T a, T b) {
private <T> void assertNoMatch(MatchingRule<T> rule, T a, T b) throws SchemaException {
assertFalse("Values '"+a+"' and '"+b+"' DOES match but they should not; rule: "+rule, rule.match(a, b));
}

private void assertNormalized(MatchingRule<String> rule, String expected, String original) {
private void assertNormalized(MatchingRule<String> rule, String expected, String original) throws SchemaException {
assertEquals("Normalized value does not match", expected, rule.normalize(original));
}
}
Expand Up @@ -287,7 +287,7 @@ protected boolean handleObject(PrismObject<ShadowType> shadow, Task workerTask,
return true;
}

private void checkShadow(ShadowCheckResult checkResult, PrismObject<ShadowType> shadow, Task workerTask, OperationResult result) {
private void checkShadow(ShadowCheckResult checkResult, PrismObject<ShadowType> shadow, Task workerTask, OperationResult result) throws SchemaException {
ShadowType shadowType = shadow.asObjectable();
ObjectReferenceType resourceRef = shadowType.getResourceRef();
if (LOGGER.isTraceEnabled()) {
Expand Down Expand Up @@ -521,7 +521,7 @@ private String skippedForDryRun() {
}
}

private void doCheckNormalization(ShadowCheckResult checkResult, RefinedAttributeDefinition<?> identifier, String value, ObjectTypeContext context) {
private void doCheckNormalization(ShadowCheckResult checkResult, RefinedAttributeDefinition<?> identifier, String value, ObjectTypeContext context) throws SchemaException {
QName matchingRuleQName = identifier.getMatchingRuleQName();
if (matchingRuleQName == null) {
return;
Expand Down
Expand Up @@ -582,21 +582,6 @@ private void decideIfTolerateAssociation(LensProjectionContext accCtx,
}
}

private boolean matchPattern(List<String> patterns,
PrismPropertyValue<Object> isPValue, ValueMatcher valueMatcher) {
if (patterns == null || patterns.isEmpty()) {
return false;
}
for (String toleratePattern : patterns) {
if (valueMatcher.matches(isPValue.getValue(), toleratePattern)) {
return true;
}

}
return false;
}


private <T> void recordDelta(ValueMatcher valueMatcher, LensProjectionContext accCtx,
ResourceAttributeDefinition attrDef, ModificationType changeType, T value, ObjectType originObject)
throws SchemaException {
Expand Down Expand Up @@ -677,7 +662,7 @@ private <T> boolean isToBeDeleted(ItemDelta existingDelta, ValueMatcher valueMat
for (Object isInDeltaValue : existingDelta.getValuesToDelete()) {
if (isInDeltaValue instanceof PrismPropertyValue){
PrismPropertyValue isInRealValue = (PrismPropertyValue) isInDeltaValue;
if (valueMatcher.match(isInRealValue.getValue(), value)) {
if (matchValue(isInRealValue.getValue(), value, valueMatcher)) {
return true;
}
}
Expand All @@ -693,7 +678,7 @@ private boolean isInValues(ValueMatcher valueMatcher, Object shouldBeValue,
return false;
}
for (PrismPropertyValue<Object> isPValue : arePValues) {
if (valueMatcher.match(isPValue.getValue(), shouldBeValue)) {
if (matchValue(isPValue.getValue(), shouldBeValue, valueMatcher)) {
return true;
}
}
Expand All @@ -707,7 +692,7 @@ private boolean isInAssociationValues(ValueMatcher valueMatcher, ShadowAssociati
return false;
}
for (PrismContainerValue<ShadowAssociationType> isPValue : arePValues) {
if (valueMatcher.match(isPValue.getValue(), shouldBeValue)) {
if (matchValue(isPValue.getValue(), shouldBeValue, valueMatcher)) {
return true;
}
}
Expand Down Expand Up @@ -739,7 +724,7 @@ private boolean isInPvwoValues(ValueMatcher valueMatcher, Object value,
}
PrismPropertyValue<?> shouldBePPValue = shouldBePvwo.getPropertyValue();
Object shouldBeValue = shouldBePPValue.getValue();
if (valueMatcher.match(value, shouldBeValue)) {
if (matchValue(value, shouldBeValue, valueMatcher)) {
return true;
}
}
Expand All @@ -759,11 +744,40 @@ private boolean isInCvwoAssociationValues(ValueMatcher valueMatcher, ShadowAssoc
}
PrismContainerValue<ShadowAssociationType> shouldBePCValue = shouldBeCvwo.getItemValue();
ShadowAssociationType shouldBeValue = shouldBePCValue.getValue();
if (valueMatcher.match(value, shouldBeValue)) {
if (matchValue(value, shouldBeValue, valueMatcher)) {
return true;
}
}
return false;
}


private <T> boolean matchValue(T realA, T realB, ValueMatcher valueMatcher) {
try {
return valueMatcher.match(realA, realB);
} catch (SchemaException e) {
LOGGER.warn("Value '{}' or '{}' is invalid: {}", realA, realB, e.getMessage(), e);
return false;
}
}

private boolean matchPattern(List<String> patterns,
PrismPropertyValue<Object> isPValue, ValueMatcher valueMatcher) {
if (patterns == null || patterns.isEmpty()) {
return false;
}
for (String toleratePattern : patterns) {
try {
if (valueMatcher.matches(isPValue.getValue(), toleratePattern)) {
return true;
}
} catch (SchemaException e) {
LOGGER.warn("Value '{}' is invalid: {}", isPValue.getValue(), e.getMessage(), e);
return false;
}

}
return false;
}

}
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010-2013 Evolveum
* Copyright (c) 2010-2015 Evolveum
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -49,18 +49,27 @@ public static <T> ValueMatcher<T> createMatcher(RefinedAttributeDefinition rAttr
return new ValueMatcher<T>((MatchingRule<T>) matchingRule);
}

public boolean match(T realA, T realB) {
public boolean match(T realA, T realB) throws SchemaException {
return matchingRule.match(realA, realB);
}

public boolean matches(T realValue, String regex){
public boolean matches(T realValue, String regex) throws SchemaException{
return matchingRule.matchRegex(realValue, regex);
}

public boolean hasRealValue(PrismProperty<T> property, PrismPropertyValue<T> pValue) {
for (T existingRealValue: property.getRealValues()) {
if (matchingRule.match(existingRealValue, pValue.getValue())) {
return true;
try {
if (matchingRule.match(existingRealValue, pValue.getValue())) {
return true;
}
} catch (SchemaException e) {
// At least one of the values is invalid. But we do not want to throw exception from
// a comparison operation. That will make the system very fragile. Let's fall back to
// ordinary equality mechanism instead.
if (existingRealValue.equals(pValue.getValue())) {
return true;
}
}
}
return false;
Expand All @@ -71,8 +80,17 @@ public boolean isRealValueToAdd(PropertyDelta<T> delta, PrismPropertyValue<T> pV
return false;
}
for (PrismPropertyValue<T> existingPValue: delta.getValuesToAdd()) {
if (matchingRule.match(existingPValue.getValue(), pValue.getValue())) {
return true;
try {
if (matchingRule.match(existingPValue.getValue(), pValue.getValue())) {
return true;
}
} catch (SchemaException e) {
// At least one of the values is invalid. But we do not want to throw exception from
// a comparison operation. That will make the system very fragile. Let's fall back to
// ordinary equality mechanism instead.
if (existingPValue.getValue().equals(pValue.getValue())) {
return true;
}
}
}
return false;
Expand Down
Expand Up @@ -1658,10 +1658,26 @@ private PrismObject<ShadowType> completeShadow(ProvisioningContext ctx, PrismObj
PrismObject<ShadowType> entitlementRepoShadow;
PrismObject<ShadowType> entitlementShadow = (PrismObject<ShadowType>) identifierContainer.getUserData(ResourceObjectConverter.FULL_SHADOW_KEY);
if (entitlementShadow == null) {
entitlementRepoShadow = shadowManager.lookupShadowInRepository(ctxEntitlement, identifierContainer, parentResult);
if (entitlementRepoShadow == null) {
entitlementShadow = resouceObjectConverter.locateResourceObject(ctxEntitlement, entitlementIdentifiers, parentResult);
entitlementRepoShadow = createShadowInRepository(ctxEntitlement, entitlementShadow, parentResult);
try {
entitlementRepoShadow = shadowManager.lookupShadowInRepository(ctxEntitlement, identifierContainer, parentResult);
if (entitlementRepoShadow == null) {
entitlementShadow = resouceObjectConverter.locateResourceObject(ctxEntitlement, entitlementIdentifiers, parentResult);
entitlementRepoShadow = createShadowInRepository(ctxEntitlement, entitlementShadow, parentResult);
}
} catch (ObjectNotFoundException e) {
// The entitlement to which we point is not there.
// Simply ignore this association value.
parentResult.muteLastSubresultError();
LOGGER.warn("The entitlement identified by {} referenced from {} does not exist. Skipping.",
new Object[]{associationCVal, resourceShadow});
continue;
} catch (SchemaException e) {
// The entitlement to which we point is not bad.
// Simply ignore this association value.
parentResult.muteLastSubresultError();
LOGGER.warn("The entitlement identified by {} referenced from {} violates the schema. Skipping. Original error: {}",
new Object[]{associationCVal, resourceShadow, e.getMessage(), e});
continue;
}
} else {
entitlementRepoShadow = lookupOrCreateShadowInRepository(ctxEntitlement, entitlementShadow, parentResult);
Expand Down
Expand Up @@ -803,7 +803,7 @@ private <T> void normalizeDelta(ItemDelta<PrismPropertyValue<T>,PrismPropertyDef
}
}

private <T> void normalizeValues(Collection<PrismPropertyValue<T>> values, MatchingRule<T> matchingRule){
private <T> void normalizeValues(Collection<PrismPropertyValue<T>> values, MatchingRule<T> matchingRule) throws SchemaException {
for (PrismPropertyValue<T> pval: values) {
T normalizedRealValue = matchingRule.normalize(pval.getValue());
pval.setValue(normalizedRealValue);
Expand Down

0 comments on commit 2acabd7

Please sign in to comment.