Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -191,45 +191,44 @@ public Uid create(final ObjectClass objClass, final Set<Attribute> attributes) {

ObjectClassInfo objInfo = connSchema.findObjectClassInfo(objClass.getObjectClassValue());
Map<String, AttributeInfo> objAttributes = AttributeInfoUtil.toMap(objInfo.getAttributeInfo());
Map<String, Attribute> providedAttributesMap = null;
String uidValue = null;

if (attributes != null) {
providedAttributesMap = new HashMap<String, Attribute>(AttributeUtil.toMap(attributes));
}

// Check if __NAME__ is defined
if (providedAttributesMap == null || !providedAttributesMap.containsKey(Name.NAME) || providedAttributesMap.get(Name.NAME).getValue().isEmpty()) {
// Check if __NAME__ is defined. The framework names attributes in any case, so __name__ defines it too (#212).
// Only the first spelling found is looked at. So __NAME__ given along with a __name__ without a value is refused
// for the missing value, here or in checkAttribute as the order of the set falls, not as a name given twice.
Attribute givenName = AttributeUtil.find(Name.NAME, attributes);
if (givenName == null || givenName.getValue() == null || givenName.getValue().isEmpty()) {
throw new IllegalArgumentException(Name.NAME + " must be defined.");
}

// Every given attribute is checked as update checks it, so one the schema does not have is refused rather than
// dropped (#192). AttributeUtil.toMap is case-insensitive: of two names that differ only in case it keeps the
// first spelling as the key and the last attribute as the value (#212).
// dropped (#192). It is kept under the schema's name, which the checks below and the file use, whatever case it
// was given in (#212).
Map<String, List<String>> checked = new HashMap<String, List<String>>();
for (Map.Entry<String, Attribute> provided : providedAttributesMap.entrySet()) {
checked.put(provided.getKey(), checkAttribute(provided.getValue(), objAttributes, Change.CREATE));
for (CheckedAttribute c : checkAttributes(attributes, objInfo, Change.CREATE)) {
checked.put(c.name, c.values);
}
for (AttributeInfo attributeInfo : objInfo.getAttributeInfo()) {
if (attributeInfo.isRequired() && !checked.containsKey(attributeInfo.getName())) {
throw new IllegalArgumentException("No values provided for required attribute: " + attributeInfo.getName());
}
}

Name name = AttributeUtil.getNameFromAttributes(attributes);
// Not AttributeUtil.getNameFromAttributes: it casts the attribute to Name, which only an exact __NAME__ is.
String name = checked.get(Name.NAME).get(0);

// Check if entry already exists
if (entryExists(objClass, new Uid(name.getNameValue()), ElementIdentifierFieldType.BY_NAME)) {
if (entryExists(objClass, new Uid(name), ElementIdentifierFieldType.BY_NAME)) {
throw new AlreadyExistsException("Could not create entry. An entry with the " + Uid.NAME + " of "
+ name.getNameValue() + " already exists.");
+ name + " already exists.");
}

// Create or get UID. The lookup is case-insensitive, but the loop below writes the uid only into __UID__.
AttributeInfo uidInfo = objAttributes.get(Uid.NAME);
if (uidInfo != null && uidInfo.getName().equals(Uid.NAME)) {
uidValue = UUID.randomUUID().toString();
} else {
uidValue = name.getNameValue();
uidValue = name;
}

// Create object type element
Expand Down Expand Up @@ -279,23 +278,19 @@ public Uid update(ObjectClass objClass, Uid uid, Set<Attribute> replaceAttribute
XmlHandlerUtil.checkObjectType(objClass, riSchema);

ObjectClassInfo objInfo = connSchema.findObjectClassInfo(objClass.getObjectClassValue());
Map<String, AttributeInfo> objAttributes = AttributeInfoUtil.toMap(objInfo.getAttributeInfo());

Element entry = getEntry(objClass, uid, ElementIdentifierFieldType.AUTO);
if (entry == null) {
throw new UnknownUidException("Could not update entry. No entry of type " + objClass.getObjectClassValue() + " with the id " + uid.getUidValue() + " found.");
}

// Every attribute is checked before the entry changes, so a rejected update leaves the document as it was (#158).
List<CheckedAttribute> checkedAttributes = new ArrayList<CheckedAttribute>();
// The name is checked for a clash once every attribute has passed, as create() checks it.
List<CheckedAttribute> checkedAttributes = checkAttributes(replaceAttributes, objInfo, Change.UPDATE);
Uid newUid = null;
for (Attribute attribute : replaceAttributes) {

String attributeName = attribute.getName();
List<String> values = checkAttribute(attribute, objAttributes, Change.UPDATE);

if (Name.NAME.equals(attributeName)) {
for (String value : values) {
for (CheckedAttribute checked : checkedAttributes) {
if (Name.NAME.equals(checked.name)) {
for (String value : checked.values) {
if (nameTakenByAnother(objClass, entry, value)) {
throw new AlreadyExistsException("Could not update entry. An entry with the " + Name.NAME + " of "
+ value + " already exists.");
Expand All @@ -306,8 +301,6 @@ public Uid update(ObjectClass objClass, Uid uid, Set<Attribute> replaceAttribute
}
}
}

checkedAttributes.add(new CheckedAttribute(attributeName, values));
}

try {
Expand Down Expand Up @@ -832,17 +825,25 @@ String refusal(String attributeName) {
}

/**
* Checks one attribute given to create or update and returns its values as text. Both operations check an attribute
* with this method; only the operation's own schema rule differs (#192). The values are read once, so a guarded
* value is decrypted once.
* Checks one attribute given to create or update and returns its values as text, under the schema's name. Both
* operations check an attribute with this method; only the operation's own schema rule differs (#192). The values
* are read once, so a guarded value is decrypted once.
* <p>
* A name the schema spells exactly is that attribute. Any other name is looked up without regard to case, as the
* framework names attributes, while the file and the XSD name them exactly: so from here on the attribute is named,
* written and reported as the schema spells it (#212). An XSD may declare two names that differ only in case, and
* the case-insensitive map holds one of the two for both, so the exact lookup comes first.
*/
private static List<String> checkAttribute(Attribute attribute, Map<String, AttributeInfo> objAttributes,
Change change) {
String attributeName = attribute.getName();
AttributeInfo attributeInfo = objAttributes.get(attributeName);
private static CheckedAttribute checkAttribute(Attribute attribute, Map<String, AttributeInfo> exactAttributes,
Map<String, AttributeInfo> anyCaseAttributes, Change change) {
AttributeInfo attributeInfo = exactAttributes.get(attribute.getName());
if (attributeInfo == null) {
attributeInfo = anyCaseAttributes.get(attribute.getName());
}
if (attributeInfo == null) {
throw new IllegalArgumentException("Data field: " + attributeName + " is not supported.");
throw new IllegalArgumentException("Data field: " + attribute.getName() + " is not supported.");
}
String attributeName = attributeInfo.getName();
if (!change.allows(attributeInfo)) {
throw new IllegalArgumentException(change.refusal(attributeName));
}
Expand All @@ -864,13 +865,39 @@ private static List<String> checkAttribute(Attribute attribute, Map<String, Attr
if (!attributeInfo.isMultiValued() && values.size() > 1) {
throw new IllegalArgumentException("Data field: " + attributeName + " is not multivalued can not have more than one value");
}
return values;
return new CheckedAttribute(attributeName, values);
}

/**
* An attribute of an update that passed every check, with the values to write. Kept in a list, not a map, so a
* name given twice is applied twice, in order.
* Checks the attributes given to create or update, in the order given, and refuses a schema attribute given more than
* once. The framework names attributes in any case, so lastname and LastName are one attribute unless the schema
* has both (#212), and an attribute set can hold exactly one name twice with other values: either way neither value
* is surely the one meant. The framework's CreateImpl refuses only exactly one name given twice, and UpdateImpl
* neither.
*/
private static List<CheckedAttribute> checkAttributes(Set<Attribute> attributes, ObjectClassInfo objInfo,
Change change) {
Map<String, AttributeInfo> exactAttributes = new HashMap<String, AttributeInfo>();
for (AttributeInfo attributeInfo : objInfo.getAttributeInfo()) {
exactAttributes.put(attributeInfo.getName(), attributeInfo);
}
Map<String, AttributeInfo> anyCaseAttributes = AttributeInfoUtil.toMap(objInfo.getAttributeInfo());

List<CheckedAttribute> checkedAttributes = new ArrayList<CheckedAttribute>();
Map<String, String> givenNames = new HashMap<String, String>();
for (Attribute attribute : attributes) {
CheckedAttribute checked = checkAttribute(attribute, exactAttributes, anyCaseAttributes, change);
String firstGiven = givenNames.put(checked.name, attribute.getName());
if (firstGiven != null) {
throw new IllegalArgumentException("Data field: " + checked.name + " is given more than once, as "
+ firstGiven + " and as " + attribute.getName() + ".");
}
checkedAttributes.add(checked);
}
return checkedAttributes;
}

/** An attribute that passed every check, under the schema's name, with the values to write. */
private static final class CheckedAttribute {
final String name;
final List<String> values;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ public static void setUpClass() throws Exception {
attributesFirst.add(AttributeBuilder.build(ATTR_ACCOUNT_MS_EMPLOYED, ATTR_ACCOUNT_VALUE_MS_EMPLOYED));
attributesFirst.add(AttributeBuilder.build(ATTR_ACCOUNT_SIXTH_SENSE, ATTR_ACCOUNT_VALUE_SIXTH_SENSE));
attributesFirst.add(AttributeBuilder.build(ATTR_ACCOUNT_FIRST_NAME, ATTR_ACCOUNT_VALUE_FIRST_NAME));
attributesFirst.add(AttributeBuilder.build(ATTR_ACCOUNT_EMAIL, ATTR_ACCOUNT_VALUE_EMAIL_1));
attributesFirst.add(AttributeBuilder.build(ATTR_ACCOUNT_EMAIL, ATTR_ACCOUNT_VALUE_EMAIL_2));
// One attribute with both values: create refuses an attribute given twice (#212).
attributesFirst.add(AttributeBuilder.build(ATTR_ACCOUNT_EMAIL, ATTR_ACCOUNT_VALUE_EMAIL_1, ATTR_ACCOUNT_VALUE_EMAIL_2));
attributesFirst.add(AttributeBuilder.build(ATTR_ACCOUNT_SECRET_PIN, new GuardedString(ATTR_ACCOUNT_VALUE_SECRET_PIN.toCharArray())));
attributesFirst.add(AttributeBuilder.build(ATTR_ACCOUNT_PERMANTENT_EMPLOYEE, ATTR_ACCOUNT_VALUE_PERMANENT_EPLOYEE));
attributesFirst.add(AttributeBuilder.build(ATTR_ACCOUNT_YEARS_EMPLOYED, ATTR_ACCOUNT_VALUE_YEARS_EPLOYED));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,11 +182,14 @@ public void clearingAnOptionalNameIsRefused() throws Exception {
optional.init();

// create() requires a name whatever the XSD says, and an object without one cannot be built:
// a cleared entry would fail every search of the class.
for (String blank : Arrays.asList("", " ")) {
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> optional.update(
ObjectClass.ACCOUNT, new Uid("uid-alice"), Collections.singleton(AttributeBuilder.build(Name.NAME, blank))));
assertEquals(e.getMessage(), "Parameter '__NAME__' must not be blank.");
// a cleared entry would fail every search of the class. __name__ is the name in another case (#212).
for (String nameAttribute : Arrays.asList(Name.NAME, "__name__")) {
for (String blank : Arrays.asList("", " ")) {
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> optional.update(
ObjectClass.ACCOUNT, new Uid("uid-alice"),
Collections.singleton(AttributeBuilder.build(nameAttribute, blank))));
assertEquals(e.getMessage(), "Parameter '__NAME__' must not be blank.");
}
}
assertEquals(describe(optional.search((Query) null, ObjectClass.ACCOUNT)),
Arrays.asList("uid-alice|alice", "uid-bob|bob"));
Expand Down Expand Up @@ -234,6 +237,45 @@ public void clearingTheOptionalNameOfAnEntryWithoutUidIsRefused() throws Excepti
}
}

/**
* An XSD may declare two attributes whose names differ only in case, and the framework's case-insensitive map holds
* one of them for both names. A name given exactly as the schema spells it is that attribute, so create and update
* write each value into its own element, as before #212.
*/
@Test
public void attributesWhoseNamesDifferOnlyInCaseAreWrittenApart() throws Exception {
File casePairXsd = getTestFile("case-pair.xsd");
String xsd = new String(Files.readAllBytes(XSD_SCHEMA_FILEPATH.toPath()), StandardCharsets.UTF_8);
String lastName = "<xsd:element name=\"lastname\" type=\"xsd:string\" >";
// __ACCOUNT__'s required lastname gets an optional sibling LastName.
Files.write(casePairXsd.toPath(), xsd.replace(lastName,
"<xsd:element name=\"LastName\" type=\"xsd:string\" minOccurs=\"0\"/>" + lastName)
.getBytes(StandardCharsets.UTF_8));
try {
File file = getRandomXMLFile();
writeAccounts(file, "alice");
XMLConfiguration config = new XMLConfiguration();
config.setXmlFilePath(file);
config.setXsdFilePath(casePairXsd);
SchemaParser parser = new SchemaParser(XMLConnector.class, casePairXsd);
XMLHandlerImpl pair = new XMLHandlerImpl(config, parser.parseSchema(), parser.getXsdSchema());
pair.init();

Set<Attribute> carol = account("carol");
carol.add(AttributeBuilder.build("LastName", "Other-carol"));
pair.create(ObjectClass.ACCOUNT, carol);
pair.update(ObjectClass.ACCOUNT, new Uid("uid-alice"), new LinkedHashSet<Attribute>(Arrays.asList(
AttributeBuilder.build("LastName", "Other-alice"),
AttributeBuilder.build(ATTR_ACCOUNT_LAST_NAME, "New-alice"))));
pair.dispose();

assertEquals(valuesInFile(file, ATTR_ACCOUNT_LAST_NAME), Arrays.asList("New-alice", "Last-carol"));
assertEquals(valuesInFile(file, "LastName"), Arrays.asList("Other-alice", "Other-carol"));
} finally {
Files.delete(casePairXsd.toPath());
}
}

/**
* Renames to {@code newName} along with an unknown attribute. update() rejects the unknown attribute
* before it changes the entry, so the rename is not applied. A two-valued __NAME__ cannot be used:
Expand Down
Loading
Loading