DanElbert / vt-middleware

Automatically exported from code.google.com/p/vt-middleware
0 stars 0 forks source link

vt-ldap: optionally sort ldif by attribute name and value #58

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
For visual comparison of ldif entries, it would be helpful if the ldif produced 
by LdifResult.toLdif() 
was (optionally) sortable by attribute name and attribute value.

Here's a hack to Ldif.java :

protected String createLdifEntry(final SearchResult result, boolean sortNames, 
boolean 
sortValues)
    throws NamingException
  {
    final StringBuffer entry = new StringBuffer();
    if (result != null) {

      final LdapEntry ldapEntry = new LdapEntry(result);
      final String dn = ldapEntry.getDn();
      if (dn != null) {
        if (encodeData(dn)) {
          final String encodedDn = LdapUtil.base64Encode(dn);
          if (encodedDn != null) {
            entry.append("dn:: ").append(dn).append(LINE_SEPARATOR);
          }
        } else {
          entry.append("dn: ").append(dn).append(LINE_SEPARATOR);
        }
      }

      Collection<LdapAttribute> attributes;
      if (sortNames) {
          Map<String, LdapAttribute> map = new TreeMap<String, LdapAttribute>();
          for(LdapAttribute attribute : ldapEntry.getLdapAttributes().getAttributes()) {
              map.put(attribute.getName(), attribute);
          }
          attributes = map.values();
      } else {
          attributes = ldapEntry.getLdapAttributes().getAttributes();
      }

      for (LdapAttribute attr : attributes) {
        final String attrName = attr.getName();

        List<String> values = new ArrayList<String>();

        for (Object attrValue : attr.getValues()) {
          if (encodeData(attrValue)) {
            String encodedAttrValue = null;
            if (attrValue instanceof String) {
              encodedAttrValue = LdapUtil.base64Encode((String) attrValue);
            } else if (attrValue instanceof byte[]) {
              encodedAttrValue = LdapUtil.base64Encode((byte[]) attrValue);
            } else {
              if (this.logger.isWarnEnabled()) {
                this.logger.warn(
                  "Could not cast attribute value as a byte[]" +
                  " or a String");
              }
            }
            if (encodedAttrValue != null) {
              entry.append(attrName).append(":: ").append(encodedAttrValue)
                .append(LINE_SEPARATOR);
            }
          } else {
            values.add(String.valueOf(attrValue));
          }
        }

        if (sortValues) {
            Collections.sort(values);
        }
        for(String value : values) {
            entry.append(attrName).append(": ").append(value).append(
                    LINE_SEPARATOR);
        }
      }
    }

    if (entry.length() > 0) {
      entry.append(LINE_SEPARATOR);
    }
    return entry.toString();
  }

Original issue reported on code.google.com by zellerta@gmail.com on 20 Jan 2010 at 3:22

GoogleCodeExporter commented 8 years ago
Thanks, I'll take a look at your patch.

Original comment by dfis...@gmail.com on 20 Jan 2010 at 3:42

GoogleCodeExporter commented 8 years ago
We have an internal requirement for sorting DSML as well.
This caused me to push the sorting implementation into the bean objects so all
classes could potentially take advantage.
Unfortunately I had to tear down the existing bean objects to make way for the
interfaces, but the end result is cleaner and more extensible.

I'm providing three factory implementations: Unordered, Ordered, and Sorted.
In order to the change the default behavior (unordered) you can set the factory 
like so:

Ldif ldif = new Ldif();
ldif.setLdapBeanFactory(new SortedLdapBeanFactory());
....

The default factory can also be changed JVM wide by using:
LdapBeanProvider.setLdapBeanFactory(...) or by setting the following system 
property:
-Dedu.vt.middleware.ldap.beanFactory=....

Please take a look at the latest 3.3 snapshot and let me know if that works for 
you.

Original comment by dfis...@gmail.com on 30 Jan 2010 at 4:43

GoogleCodeExporter commented 8 years ago
I reviewed these and they look good.  Initially I had a negative reaction to 
making
the concrete implementations of AbstractLdap* inner classes on the factories, 
but I
warmed up to the idea after further consideration.  They really are so simple 
that
they are arguably an implementation detail; I can't imagine anyone wanting to 
extend
them without simply creating another factory class.  With that in mind you 
might want
to make the inner classes default (package) access instead of public. 

Original comment by marvin.addison@gmail.com on 9 Feb 2010 at 2:40

GoogleCodeExporter commented 8 years ago
Attached patch that fixes an apparent bug in LdifResultConverter where it does 
not
respect a given LdapBeanFactory when generating an LDIF.

Original comment by marvin.addison@gmail.com on 17 Feb 2010 at 9:34

Attachments:

GoogleCodeExporter commented 8 years ago
Committed patch in r1157.
Bug should have affected toXXX methods, not fromXXX methods.

Original comment by dfis...@gmail.com on 18 Feb 2010 at 4:55

GoogleCodeExporter commented 8 years ago
DSML sorting confirmed to work.
Marking fixed.

Original comment by dfis...@gmail.com on 12 Apr 2010 at 7:36