Partial OPENDJ-3106 Migrate Entry
Entry.java:
Add getAllAttributes() (like SDK's method) + make use of it in client code
| | |
| | | |
| | | import java.io.IOException; |
| | | import java.util.ArrayList; |
| | | import java.util.Collection; |
| | | import java.util.Collections; |
| | | import java.util.HashMap; |
| | | import java.util.LinkedHashMap; |
| | |
| | | try |
| | | { |
| | | final Entry configEntry = DirectoryServer.getConfigEntry(configEntryDN); |
| | | addAllNonMonitorConfigAttributes(userAttrs, configEntry.getUserAttributes().values()); |
| | | addAllNonMonitorConfigAttributes(userAttrs, configEntry.getOperationalAttributes().values()); |
| | | addAllNonMonitorConfigAttributes(userAttrs, configEntry.getAllAttributes()); |
| | | } |
| | | catch (final Exception e) |
| | | { |
| | |
| | | return ccr; |
| | | } |
| | | |
| | | private void addAllNonMonitorConfigAttributes(final List<Attribute> userAttrs, Collection<List<Attribute>> attrbutes) |
| | | private void addAllNonMonitorConfigAttributes(List<Attribute> userAttrs, Iterable<Attribute> attributes) |
| | | { |
| | | for (final List<Attribute> attrs : attrbutes) |
| | | for (final Attribute a : attributes) |
| | | { |
| | | for (final Attribute a : attrs) |
| | | if (!isMonitorConfigAttribute(a)) |
| | | { |
| | | if (!isMonitorConfigAttribute(a)) |
| | | { |
| | | userAttrs.add(a); |
| | | } |
| | | userAttrs.add(a); |
| | | } |
| | | } |
| | | } |
| | |
| | | // attributes that we don't recognize will be included directly in the base |
| | | // monitor entry. |
| | | userDefinedAttributes = new ArrayList<>(); |
| | | addAll(userDefinedAttributes, configEntry.getUserAttributes().values()); |
| | | addAll(userDefinedAttributes, configEntry.getOperationalAttributes().values()); |
| | | addAllNonMonitorConfigAttributes(userDefinedAttributes, configEntry.getAllAttributes()); |
| | | |
| | | // Construct the set of objectclasses to include in the base monitor entry. |
| | | monitorObjectClasses.put(CoreSchema.getTopObjectClass(), OC_TOP); |
| | |
| | | currentConfig = cfg; |
| | | } |
| | | |
| | | private void addAll(ArrayList<Attribute> attributes, Collection<List<Attribute>> attributesToAdd) |
| | | { |
| | | addAllNonMonitorConfigAttributes(attributes, attributesToAdd); |
| | | } |
| | | |
| | | @Override |
| | | public void createBackup(final BackupConfig backupConfig) |
| | | throws DirectoryException |
| | |
| | | */ |
| | | private void addAllUserDefinedAttrs(List<Attribute> userDefinedAttrs, Entry configEntry) |
| | | { |
| | | for (List<Attribute> attrs : configEntry.getUserAttributes().values()) |
| | | for (Attribute a : configEntry.getAllAttributes()) |
| | | { |
| | | for (Attribute a : attrs) |
| | | if (!isDSEConfigAttribute(a)) |
| | | { |
| | | if (!isDSEConfigAttribute(a)) |
| | | { |
| | | userDefinedAttrs.add(a); |
| | | } |
| | | } |
| | | } |
| | | for (List<Attribute> attrs : configEntry.getOperationalAttributes().values()) |
| | | { |
| | | for (Attribute a : attrs) |
| | | { |
| | | if (!isDSEConfigAttribute(a)) |
| | | { |
| | | userDefinedAttrs.add(a); |
| | | } |
| | | userDefinedAttrs.add(a); |
| | | } |
| | | } |
| | | } |
| | |
| | | // attributes that we don't recognize will be included directly in the |
| | | // schema entry. |
| | | userDefinedAttributes = new ArrayList<>(); |
| | | addAllNonSchemaConfigAttributes(userDefinedAttributes, configEntry.getUserAttributes().values()); |
| | | addAllNonSchemaConfigAttributes(userDefinedAttributes, configEntry.getOperationalAttributes().values()); |
| | | addAllNonSchemaConfigAttributes(userDefinedAttributes, configEntry.getAllAttributes()); |
| | | |
| | | currentConfig = cfg; |
| | | } |
| | |
| | | try |
| | | { |
| | | Entry configEntry = DirectoryServer.getConfigEntry(configEntryDN); |
| | | addAllNonSchemaConfigAttributes(newUserAttrs, configEntry.getUserAttributes().values()); |
| | | addAllNonSchemaConfigAttributes(newUserAttrs, configEntry.getOperationalAttributes().values()); |
| | | addAllNonSchemaConfigAttributes(newUserAttrs, configEntry.getAllAttributes()); |
| | | } |
| | | catch (ConfigException e) |
| | | { |
| | |
| | | return ccr; |
| | | } |
| | | |
| | | private void addAllNonSchemaConfigAttributes(List<Attribute> newUserAttrs, Collection<List<Attribute>> attributes) |
| | | private void addAllNonSchemaConfigAttributes(List<Attribute> newUserAttrs, Iterable<Attribute> attributes) |
| | | { |
| | | for (List<Attribute> attrs : attributes) |
| | | for (Attribute a : attributes) |
| | | { |
| | | for (Attribute a : attrs) |
| | | if (!isSchemaConfigAttribute(a)) |
| | | { |
| | | if (!isSchemaConfigAttribute(a)) |
| | | { |
| | | newUserAttrs.add(a); |
| | | } |
| | | newUserAttrs.add(a); |
| | | } |
| | | } |
| | | } |
| | |
| | | import org.forgerock.opendj.ldap.ByteString; |
| | | import org.forgerock.opendj.ldap.DN; |
| | | import org.forgerock.opendj.ldap.schema.AttributeType; |
| | | import org.forgerock.opendj.ldap.schema.ObjectClass; |
| | | import org.opends.server.core.DirectoryServer; |
| | | import org.opends.server.types.Attribute; |
| | | import org.opends.server.types.AttributeBuilder; |
| | | import org.opends.server.types.Entry; |
| | | import org.opends.server.types.LDAPException; |
| | | import org.forgerock.opendj.ldap.schema.ObjectClass; |
| | | import org.opends.server.types.SearchResultEntry; |
| | | import org.opends.server.util.Base64; |
| | | |
| | |
| | | { |
| | | if (ldapVersion == 2) |
| | | { |
| | | // Merge attributes having the same type into a single |
| | | // attribute. |
| | | boolean needsMerge; |
| | | Map<AttributeType, List<Attribute>> attrs = |
| | | entry.getUserAttributes(); |
| | | for (Map.Entry<AttributeType, List<Attribute>> attrList : attrs |
| | | .entrySet()) |
| | | { |
| | | needsMerge = true; |
| | | |
| | | if (attrList != null && attrList.getValue().size() == 1) |
| | | { |
| | | Attribute a = attrList.getValue().get(0); |
| | | if (!a.getAttributeDescription().hasOptions()) |
| | | { |
| | | needsMerge = false; |
| | | tmp.add(new LDAPAttribute(a)); |
| | | } |
| | | } |
| | | |
| | | if (needsMerge) |
| | | { |
| | | AttributeBuilder builder = |
| | | new AttributeBuilder(attrList.getKey()); |
| | | for (Attribute a : attrList.getValue()) |
| | | { |
| | | builder.addAll(a); |
| | | } |
| | | tmp.add(new LDAPAttribute(builder.toAttribute())); |
| | | } |
| | | } |
| | | |
| | | attrs = entry.getOperationalAttributes(); |
| | | for (Map.Entry<AttributeType, List<Attribute>> attrList : attrs |
| | | .entrySet()) |
| | | { |
| | | needsMerge = true; |
| | | |
| | | if (attrList != null && attrList.getValue().size() == 1) |
| | | { |
| | | Attribute a = attrList.getValue().get(0); |
| | | if (!a.getAttributeDescription().hasOptions()) |
| | | { |
| | | needsMerge = false; |
| | | tmp.add(new LDAPAttribute(a)); |
| | | } |
| | | } |
| | | |
| | | if (needsMerge) |
| | | { |
| | | AttributeBuilder builder = new AttributeBuilder(attrList.getKey()); |
| | | for (Attribute a : attrList.getValue()) |
| | | { |
| | | builder.addAll(a); |
| | | } |
| | | tmp.add(new LDAPAttribute(builder.toAttribute())); |
| | | } |
| | | } |
| | | // Merge attributes having the same type into a single attribute. |
| | | merge(tmp, entry.getUserAttributes()); |
| | | merge(tmp, entry.getOperationalAttributes()); |
| | | } |
| | | else |
| | | { |
| | | // LDAPv3 |
| | | for (List<Attribute> attrList : entry.getUserAttributes().values()) |
| | | for (Attribute a : entry.getAllAttributes()) |
| | | { |
| | | for (Attribute a : attrList) |
| | | { |
| | | tmp.add(new LDAPAttribute(a)); |
| | | } |
| | | } |
| | | |
| | | for (List<Attribute> attrList : entry.getOperationalAttributes().values()) |
| | | { |
| | | for (Attribute a : attrList) |
| | | { |
| | | tmp.add(new LDAPAttribute(a)); |
| | | } |
| | | tmp.add(new LDAPAttribute(a)); |
| | | } |
| | | } |
| | | } |
| | |
| | | return attributes; |
| | | } |
| | | |
| | | private void merge(LinkedList<LDAPAttribute> tmp, Map<AttributeType, List<Attribute>> attrs) |
| | | { |
| | | boolean needsMerge; |
| | | for (Map.Entry<AttributeType, List<Attribute>> attrList : attrs.entrySet()) |
| | | { |
| | | needsMerge = true; |
| | | |
| | | if (attrList != null && attrList.getValue().size() == 1) |
| | | { |
| | | Attribute a = attrList.getValue().get(0); |
| | | if (!a.getAttributeDescription().hasOptions()) |
| | | { |
| | | needsMerge = false; |
| | | tmp.add(new LDAPAttribute(a)); |
| | | } |
| | | } |
| | | |
| | | if (needsMerge) |
| | | { |
| | | AttributeBuilder builder = |
| | | new AttributeBuilder(attrList.getKey()); |
| | | for (Attribute a : attrList.getValue()) |
| | | { |
| | | builder.addAll(a); |
| | | } |
| | | tmp.add(new LDAPAttribute(builder.toAttribute())); |
| | | } |
| | | } |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | |
| | | } |
| | | } |
| | | |
| | | for (List<Attribute> attrList : entry.getUserAttributes().values()) |
| | | { |
| | | for (Attribute a : attrList) |
| | | { |
| | | for (ByteString v : a) |
| | | { |
| | | try |
| | | { |
| | | String[] valueStrings = new String[] { v.toString() }; |
| | | Tag[] tags = new Tag[] { new StaticTextTag() }; |
| | | tags[0].initializeForBranch(templateFile, this, valueStrings, 0, warnings); |
| | | lineList.add(new TemplateLine(a.getAttributeDescription().getAttributeType(), 0, tags)); |
| | | } |
| | | catch (Exception e) |
| | | { |
| | | // This should never happen. |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | for (List<Attribute> attrList : entry.getOperationalAttributes().values()) |
| | | { |
| | | for (Attribute a : attrList) |
| | | { |
| | | for (ByteString v : a) |
| | | { |
| | | try |
| | | { |
| | | String[] valueStrings = new String[] { v.toString() }; |
| | | Tag[] tags = new Tag[] { new StaticTextTag() }; |
| | | tags[0].initializeForBranch(templateFile, this, valueStrings, 0, warnings); |
| | | lineList.add(new TemplateLine(a.getAttributeDescription().getAttributeType(), 0, tags)); |
| | | } |
| | | catch (Exception e) |
| | | { |
| | | // This should never happen. |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | addLines(lineList, entry.getAllAttributes(), templateFile, warnings); |
| | | |
| | | rdnLines = new TemplateLine[lineList.size()]; |
| | | lineList.toArray(rdnLines); |
| | | } |
| | | |
| | | private void addLines(List<TemplateLine> lineList, Iterable<Attribute> attrs, TemplateFile templateFile, |
| | | List<LocalizableMessage> warnings) |
| | | { |
| | | for (Attribute a : attrs) |
| | | { |
| | | for (ByteString v : a) |
| | | { |
| | | try |
| | | { |
| | | String[] valueStrings = new String[] { v.toString() }; |
| | | Tag[] tags = new Tag[] { new StaticTextTag() }; |
| | | tags[0].initializeForBranch(templateFile, this, valueStrings, 0, warnings); |
| | | lineList.add(new TemplateLine(a.getAttributeDescription().getAttributeType(), 0, tags)); |
| | | } |
| | | catch (Exception e) |
| | | { |
| | | // This should never happen. |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | |
| | | import org.forgerock.opendj.ldap.schema.NameForm; |
| | | import org.forgerock.opendj.ldap.schema.ObjectClass; |
| | | import org.forgerock.opendj.ldap.schema.ObjectClassType; |
| | | import org.forgerock.util.Reject; |
| | | import org.forgerock.util.Utils; |
| | | import org.opends.server.api.CompressedSchema; |
| | | import org.opends.server.api.ProtocolElement; |
| | |
| | | /** The DN for this entry. */ |
| | | private DN dn; |
| | | |
| | | /** |
| | | * A generic attachment that may be used to associate this entry with some |
| | | * other object. |
| | | */ |
| | | /** A generic attachment that may be used to associate this entry with some other object. */ |
| | | private transient Object attachment; |
| | | |
| | | /** |
| | |
| | | return attributes; |
| | | } |
| | | |
| | | /** Iterator over a {@code Collection<List<Attribute>>}. */ |
| | | private static final class CollectionListIterator implements Iterator<Attribute> |
| | | { |
| | | private final Iterator<List<Attribute>> parentIt; |
| | | private List<Attribute> subList = Collections.emptyList(); |
| | | private Iterator<Attribute> subIt = subList.iterator(); |
| | | |
| | | private CollectionListIterator(Collection<List<Attribute>> list) |
| | | { |
| | | this.parentIt = Reject.checkNotNull(list).iterator(); |
| | | advance(); |
| | | } |
| | | |
| | | private void advance() |
| | | { |
| | | while (!subIt.hasNext()) |
| | | { |
| | | if (!parentIt.hasNext()) |
| | | { |
| | | return; |
| | | } |
| | | subList = parentIt.next(); |
| | | subIt = subList.iterator(); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public boolean hasNext() |
| | | { |
| | | return subIt.hasNext(); |
| | | } |
| | | |
| | | @Override |
| | | public Attribute next() |
| | | { |
| | | final Attribute result = subIt.next(); |
| | | if (!subIt.hasNext()) |
| | | { |
| | | advance(); |
| | | } |
| | | return result; |
| | | } |
| | | |
| | | @Override |
| | | public void remove() |
| | | { |
| | | throw new UnsupportedOperationException(); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * Returns an {@code Iterable} containing all of the attributes in this entry, |
| | | * excluding the objectClass attribute. |
| | | * <p> |
| | | * The returned {@code Iterable} may NOT be used to remove attributes. |
| | | * |
| | | * @return An {@code Iterable} containing all of the attributes. |
| | | */ |
| | | public Iterable<Attribute> getAllAttributes() |
| | | { |
| | | /** Iterator over all the attributes of this entry. */ |
| | | final class AllAttributesIterator implements Iterator<Attribute> |
| | | { |
| | | private boolean iteratesOnOperationalAttributes; |
| | | private Iterator<Attribute> currentIterator = new CollectionListIterator(getUserAttributes().values()); |
| | | |
| | | @Override |
| | | public boolean hasNext() |
| | | { |
| | | if (currentIterator.hasNext()) |
| | | { |
| | | return true; |
| | | } |
| | | if (iteratesOnOperationalAttributes) |
| | | { |
| | | return false; |
| | | } |
| | | iteratesOnOperationalAttributes = true; |
| | | currentIterator = new CollectionListIterator(getOperationalAttributes().values()); |
| | | return currentIterator.hasNext(); |
| | | } |
| | | |
| | | @Override |
| | | public Attribute next() |
| | | { |
| | | return currentIterator.next(); |
| | | } |
| | | |
| | | @Override |
| | | public void remove() |
| | | { |
| | | currentIterator.remove(); |
| | | } |
| | | } |
| | | |
| | | /** Can return an iterator over all the attributes of this entry. */ |
| | | final class AllAttributesIterable implements Iterable<Attribute> |
| | | { |
| | | @Override |
| | | public Iterator<Attribute> iterator() |
| | | { |
| | | return new AllAttributesIterator(); |
| | | } |
| | | } |
| | | return new AllAttributesIterable(); |
| | | } |
| | | |
| | | /** |
| | | * Retrieves the entire set of user (i.e., non-operational) |
| | | * attributes for this entry. The caller should be allowed to |
| | |
| | | Collection<NameForm> forms = DirectoryServer.getSchema().getNameForm(structuralClass); |
| | | if (forms != null) |
| | | { |
| | | List<NameForm> listForms = new ArrayList<NameForm>(forms); |
| | | List<NameForm> listForms = new ArrayList<>(forms); |
| | | boolean matchFound = false; |
| | | boolean obsolete = true; |
| | | for(int index=0; index <listForms.size(); index++) |
| | |
| | | Entry entry) |
| | | throws DirectoryException |
| | | { |
| | | // We must have an assertion value for which to make the |
| | | // determination. |
| | | // We must have an assertion value for which to make the determination. |
| | | if (assertionValue == null) |
| | | { |
| | | LocalizableMessage message = |
| | |
| | | { |
| | | logger.traceException(e); |
| | | |
| | | // We can't normalize the assertion value, so the result must be |
| | | // undefined. |
| | | // We can't normalize the assertion value, so the result must be undefined. |
| | | return ConditionResult.UNDEFINED; |
| | | } |
| | | |
| | |
| | | ConditionResult result = ConditionResult.FALSE; |
| | | if (getAttributeType() == null) |
| | | { |
| | | for (List<Attribute> attrList : |
| | | entry.getUserAttributes().values()) |
| | | final Iterable<Attribute> attrs = entry.getAllAttributes(); |
| | | result = assertionMatchesAnyAttribute(matchingRule, assertion, attrs, result, entry, completeFilter); |
| | | if (ConditionResult.TRUE.equals(result)) |
| | | { |
| | | for (Attribute a : attrList) |
| | | { |
| | | for (ByteString v : a) |
| | | { |
| | | try |
| | | { |
| | | ByteString nv = matchingRule.normalizeAttributeValue(v); |
| | | ConditionResult r = assertion.matches(nv); |
| | | switch (r) |
| | | { |
| | | case TRUE: |
| | | return ConditionResult.TRUE; |
| | | case FALSE: |
| | | break; |
| | | case UNDEFINED: |
| | | result = ConditionResult.UNDEFINED; |
| | | break; |
| | | default: |
| | | LocalizableMessage message = |
| | | ERR_SEARCH_FILTER_INVALID_RESULT_TYPE. |
| | | get(entry.getName(), completeFilter, r); |
| | | throw new DirectoryException( |
| | | ResultCode.PROTOCOL_ERROR, message); |
| | | } |
| | | } |
| | | catch (Exception e) |
| | | { |
| | | logger.traceException(e); |
| | | |
| | | // We couldn't normalize one of the values. If we don't |
| | | // find a definite match, then we should return |
| | | // undefined. |
| | | result = ConditionResult.UNDEFINED; |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | for (List<Attribute> attrList : |
| | | entry.getOperationalAttributes().values()) |
| | | { |
| | | for (Attribute a : attrList) |
| | | { |
| | | for (ByteString v : a) |
| | | { |
| | | try |
| | | { |
| | | ByteString nv = matchingRule.normalizeAttributeValue(v); |
| | | ConditionResult r = assertion.matches(nv); |
| | | switch (r) |
| | | { |
| | | case TRUE: |
| | | return ConditionResult.TRUE; |
| | | case FALSE: |
| | | break; |
| | | case UNDEFINED: |
| | | result = ConditionResult.UNDEFINED; |
| | | break; |
| | | default: |
| | | LocalizableMessage message = |
| | | ERR_SEARCH_FILTER_INVALID_RESULT_TYPE. |
| | | get(entry.getName(), completeFilter, r); |
| | | throw new DirectoryException( |
| | | ResultCode.PROTOCOL_ERROR, message); |
| | | } |
| | | } |
| | | catch (Exception e) |
| | | { |
| | | logger.traceException(e); |
| | | |
| | | // We couldn't normalize one of the values. If we don't |
| | | // find a definite match, then we should return |
| | | // undefined. |
| | | result = ConditionResult.UNDEFINED; |
| | | } |
| | | } |
| | | } |
| | | return ConditionResult.TRUE; |
| | | } |
| | | |
| | | Attribute a = entry.getObjectClassAttribute(); |
| | |
| | | } |
| | | else |
| | | { |
| | | for (Attribute a : entry.getAttribute(attributeDescription)) |
| | | final Iterable<Attribute> attrs = entry.getAttribute(attributeDescription); |
| | | result = assertionMatchesAnyAttribute(matchingRule, assertion, attrs, result, entry, completeFilter); |
| | | if (ConditionResult.TRUE.equals(result)) |
| | | { |
| | | for (ByteString v : a) |
| | | { |
| | | try |
| | | { |
| | | ByteString nv = matchingRule.normalizeAttributeValue(v); |
| | | ConditionResult r = assertion.matches(nv); |
| | | switch (r) |
| | | { |
| | | case TRUE: |
| | | return ConditionResult.TRUE; |
| | | case FALSE: |
| | | break; |
| | | case UNDEFINED: |
| | | result = ConditionResult.UNDEFINED; |
| | | break; |
| | | default: |
| | | LocalizableMessage message = |
| | | ERR_SEARCH_FILTER_INVALID_RESULT_TYPE.get(entry.getName(), completeFilter, r); |
| | | throw new DirectoryException(ResultCode.PROTOCOL_ERROR, message); |
| | | } |
| | | } |
| | | catch (Exception e) |
| | | { |
| | | logger.traceException(e); |
| | | |
| | | // We couldn't normalize one of the values. |
| | | // If we don't find a definite match, then we should return undefined. |
| | | result = ConditionResult.UNDEFINED; |
| | | } |
| | | } |
| | | return ConditionResult.TRUE; |
| | | } |
| | | } |
| | | |
| | | |
| | | // If we've gotten here, then we know that there is no definite |
| | | // match in the set of attributes. If we should check DN |
| | | // attributes, then do so. |
| | | // match in the set of attributes. If we should check DN attributes, then do so. |
| | | if (dnAttributes) |
| | | { |
| | | for (RDN rdn : entry.getName()) |
| | |
| | | return result; |
| | | } |
| | | |
| | | private ConditionResult assertionMatchesAnyAttribute(MatchingRule matchingRule, Assertion assertion, |
| | | Iterable<Attribute> attributes, ConditionResult result, Entry entry, SearchFilter filter) |
| | | { |
| | | for (Attribute a : attributes) |
| | | { |
| | | for (ByteString v : a) |
| | | { |
| | | try |
| | | { |
| | | ConditionResult r = assertion.matches(matchingRule.normalizeAttributeValue(v)); |
| | | switch (r) |
| | | { |
| | | case TRUE: |
| | | return ConditionResult.TRUE; |
| | | case FALSE: |
| | | break; |
| | | case UNDEFINED: |
| | | result = ConditionResult.UNDEFINED; |
| | | break; |
| | | default: |
| | | LocalizableMessage message = ERR_SEARCH_FILTER_INVALID_RESULT_TYPE.get(entry.getName(), filter, r); |
| | | throw new DirectoryException(ResultCode.PROTOCOL_ERROR, message); |
| | | } |
| | | } |
| | | catch (Exception e) |
| | | { |
| | | logger.traceException(e); |
| | | |
| | | // We couldn't normalize one of the values. |
| | | // If we don't find a definite match, then we should return undefined. |
| | | result = ConditionResult.UNDEFINED; |
| | | } |
| | | } |
| | | } |
| | | return result; |
| | | } |
| | | |
| | | /** |
| | | * Indicates whether this search filter is equal to the provided |
| | |
| | | |
| | | import java.io.ByteArrayInputStream; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | |
| | | import org.forgerock.opendj.ldap.ByteString; |
| | | import org.forgerock.opendj.ldap.ByteStringBuilder; |
| | |
| | | buffer.appendBytes(bsb); |
| | | |
| | | |
| | | // Encode the user attributes in the appropriate manner. |
| | | encodeV1Attributes(buffer, entry.getUserAttributes()); |
| | | |
| | | |
| | | // The operational attributes will be encoded in the same way as |
| | | // the user attributes. |
| | | encodeV1Attributes(buffer, entry.getOperationalAttributes()); |
| | | encodeV1Attributes(buffer, entry.getAllAttributes(), false); |
| | | encodeV1Attributes(buffer, entry.getAllAttributes(), true); |
| | | } |
| | | |
| | | private void encodeV1Attributes(ByteStringBuilder buffer, |
| | | Map<AttributeType,List<Attribute>> attributes) |
| | | private void encodeV1Attributes(ByteStringBuilder buffer, Iterable<Attribute> attributes, boolean isOperational) |
| | | { |
| | | int numAttributes = 0; |
| | | |
| | | // First count how many attributes are there to encode. |
| | | for (List<Attribute> attrList : attributes.values()) |
| | | { |
| | | for (Attribute a : attrList) |
| | | { |
| | | if (a.isVirtual() || a.isEmpty()) |
| | | { |
| | | continue; |
| | | } |
| | | |
| | | numAttributes++; |
| | | } |
| | | } |
| | | |
| | | // Encoded one-to-five byte number of attributes |
| | | buffer.appendBERLength(numAttributes); |
| | | buffer.appendBERLength(countNbAttrsToEncode(attributes, isOperational)); |
| | | |
| | | append(buffer, attributes); |
| | | append(buffer, attributes, isOperational); |
| | | } |
| | | |
| | | /** |
| | |
| | | buffer.appendBytes(bsb); |
| | | } |
| | | |
| | | |
| | | // Encode the user attributes in the appropriate manner. |
| | | encodeV2Attributes(buffer, entry.getUserAttributes(), config); |
| | | |
| | | |
| | | // The operational attributes will be encoded in the same way as |
| | | // the user attributes. |
| | | encodeV2Attributes(buffer, entry.getOperationalAttributes(), config); |
| | | encodeV2Attributes(buffer, entry.getAllAttributes(), config, false); |
| | | encodeV2Attributes(buffer, entry.getAllAttributes(), config, true); |
| | | } |
| | | |
| | | private void encodeV2Attributes(ByteStringBuilder buffer, |
| | | Map<AttributeType,List<Attribute>> attributes, |
| | | EntryEncodeConfig config) |
| | | private void encodeV2Attributes( |
| | | ByteStringBuilder buffer, Iterable<Attribute> attributes, EntryEncodeConfig config, boolean isOperational) |
| | | throws DirectoryException |
| | | { |
| | | int numAttributes = 0; |
| | | // Encoded one-to-five byte number of attributes |
| | | buffer.appendBERLength(countNbAttrsToEncode(attributes, isOperational)); |
| | | |
| | | // First count how many attributes are there to encode. |
| | | for (List<Attribute> attrList : attributes.values()) |
| | | if (config.compressAttributeDescriptions()) |
| | | { |
| | | for (Attribute a : attrList) |
| | | for (Attribute a : attributes) |
| | | { |
| | | if (a.isVirtual() || a.isEmpty()) |
| | | { |
| | | continue; |
| | | } |
| | | |
| | | numAttributes++; |
| | | } |
| | | } |
| | | |
| | | // Encoded one-to-five byte number of attributes |
| | | buffer.appendBERLength(numAttributes); |
| | | |
| | | if (config.compressAttributeDescriptions()) |
| | | { |
| | | for (List<Attribute> attrList : attributes.values()) |
| | | { |
| | | for (Attribute a : attrList) |
| | | { |
| | | if (a.isVirtual() || a.isEmpty()) |
| | | { |
| | | continue; |
| | | } |
| | | |
| | | ByteStringBuilder bsb = new ByteStringBuilder(); |
| | | config.getCompressedSchema().encodeAttribute(bsb, a); |
| | | buffer.appendBERLength(bsb.length()); |
| | | buffer.appendBytes(bsb); |
| | | } |
| | | ByteStringBuilder bsb = new ByteStringBuilder(); |
| | | config.getCompressedSchema().encodeAttribute(bsb, a); |
| | | buffer.appendBERLength(bsb.length()); |
| | | buffer.appendBytes(bsb); |
| | | } |
| | | } |
| | | else |
| | | { |
| | | append(buffer, attributes); |
| | | append(buffer, attributes, isOperational); |
| | | } |
| | | } |
| | | |
| | | private int countNbAttrsToEncode(Iterable<Attribute> attributes, boolean isOperational) |
| | | { |
| | | int result = 0; |
| | | for (Attribute a : attributes) |
| | | { |
| | | if (!a.isVirtual() |
| | | && !a.isEmpty() |
| | | && a.getAttributeDescription().getAttributeType().isOperational() == isOperational) |
| | | { |
| | | result++; |
| | | } |
| | | } |
| | | return result; |
| | | } |
| | | |
| | | /** |
| | | * The attributes will be encoded as a sequence of: |
| | | * - A UTF-8 byte representation of the attribute name. |
| | |
| | | * - A one-to-five byte length for the value |
| | | * - A UTF-8 byte representation for the value |
| | | */ |
| | | private void append(ByteStringBuilder buffer, |
| | | Map<AttributeType, List<Attribute>> attributes) |
| | | private void append(ByteStringBuilder buffer, Iterable<Attribute> attributes, boolean isOperational) |
| | | { |
| | | for (List<Attribute> attrList : attributes.values()) |
| | | for (Attribute a : attributes) |
| | | { |
| | | for (Attribute a : attrList) |
| | | if (a.getAttributeDescription().getAttributeType().isOperational() != isOperational) |
| | | { |
| | | buffer.appendBytes(getBytes(a.getAttributeDescription().toString())); |
| | | buffer.appendByte(0x00); |
| | | break; |
| | | } |
| | | |
| | | buffer.appendBERLength(a.size()); |
| | | for (ByteString v : a) |
| | | { |
| | | buffer.appendBERLength(v.length()); |
| | | buffer.appendBytes(v); |
| | | } |
| | | buffer.appendBytes(getBytes(a.getAttributeDescription().toString())); |
| | | buffer.appendByte(0x00); |
| | | |
| | | buffer.appendBERLength(a.size()); |
| | | for (ByteString v : a) |
| | | { |
| | | buffer.appendBERLength(v.length()); |
| | | buffer.appendBytes(v); |
| | | } |
| | | } |
| | | } |
| | |
| | | import static org.opends.server.util.CollectionUtils.*; |
| | | import static org.testng.Assert.*; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.HashMap; |
| | | import java.util.HashSet; |
| | | import java.util.Iterator; |
| | | import java.util.LinkedHashSet; |
| | | import java.util.List; |
| | | import java.util.Set; |
| | |
| | | assertThat(e.getOperationalAttribute(AttributeDescription.create(uidType, options))).isEmpty(); |
| | | assertThat(e.getOperationalAttribute(AttributeDescription.create(mnType, options))).isEmpty(); |
| | | } |
| | | |
| | | @Test |
| | | public void testGetAllAttributes() throws Exception |
| | | { |
| | | Entry e = TestCaseUtils.makeEntry( |
| | | "dn: cn=Test User,ou=People,dc=example,dc=com", |
| | | "objectClass: top", |
| | | "objectClass: person", |
| | | "objectClass: organizationalPerson", |
| | | "objectClass: inetOrgPerson", |
| | | "cn: Test User", |
| | | "cn;lang-en-US: Test User", |
| | | "givenName: Test", |
| | | "givenName;lang-en-US: Test", |
| | | "sn: User", |
| | | "sn;lang-en-US: User", |
| | | "creatorsName: cn=Directory Manager", |
| | | "createTimestamp: 20070101000000Z", |
| | | "modifiersName: cn=Directory Manager", |
| | | "modifyTimestamp: 20070101000001Z"); |
| | | |
| | | List<String> expectedAttrNames = newArrayList( |
| | | "cn", "cn;lang-en-US", "sn", "sn;lang-en-US", "givenName", "givenName;lang-en-US", "creatorsName", |
| | | "createTimestamp", "modifyTimestamp", "modifiersName"); |
| | | |
| | | Iterator<Attribute> allAttrsIt = e.getAllAttributes().iterator(); |
| | | Iterator<String> expectedAttrNameIt = expectedAttrNames.iterator(); |
| | | do |
| | | { |
| | | assertThat(getNames(e.getAllAttributes())).containsOnly(expectedAttrNames.toArray(new String[0])); |
| | | assertThat(getName(allAttrsIt.next())).isEqualTo(expectedAttrNameIt.next()); |
| | | } |
| | | while (allAttrsIt.hasNext()); |
| | | System.out.println(); |
| | | } |
| | | |
| | | private List<String> getNames(Iterable<Attribute> allAttributes) |
| | | { |
| | | List<String> results = new ArrayList<>(); |
| | | for (Attribute attr : allAttributes) |
| | | { |
| | | results.add(getName(attr)); |
| | | } |
| | | return results; |
| | | } |
| | | |
| | | private String getName(Attribute attr) |
| | | { |
| | | return attr.getAttributeDescription().toString(); |
| | | } |
| | | } |