LDAP-282: Minor tweaks to naming in LdapNameBuilder. Added methods for appending distinguished name strings.

This commit is contained in:
Mattias Hellborg Arthursson
2013-11-18 13:36:16 +01:00
parent 751bdef3b9
commit f89daf692e
8 changed files with 107 additions and 14 deletions

View File

@@ -419,7 +419,7 @@ public class DefaultObjectDirectoryMapper implements ObjectDirectoryMapper {
EntityData entityData = getEntityData(entry.getClass());
if(entityData.metaData.canCalculateDn()) {
Set<AttributeMetaData> dnAttributes = entityData.metaData.getDnAttributes();
LdapNameBuilder ldapNameBuilder = LdapNameBuilder.newLdapName(entityData.metaData.getBase());
LdapNameBuilder ldapNameBuilder = LdapNameBuilder.newInstance(entityData.metaData.getBase());
for (AttributeMetaData dnAttribute : dnAttributes) {
Object dnFieldValue = ReflectionUtils.getField(dnAttribute.getField(), entry);

View File

@@ -26,6 +26,13 @@ import javax.naming.ldap.Rdn;
/**
* Helper class for building {@link javax.naming.ldap.LdapName} instances.
*
* Note that the first part of a Distinguished Name is the least significant, which means that when adding components,
* they will be added to the <b>beginning</b> of the resulting string, e.g.
* <pre>
* LdapNameBuilder.newInstance("dc=261consulting,dc=com").add("ou=people").build().toString();
* </pre>
* will result in <code>ou=people,dc=261consulting,dc=com</code>.
*
* @author Mattias Hellborg Arthursson
* @since 2.0
*/
@@ -52,7 +59,7 @@ public final class LdapNameBuilder {
*
* @return a new instance.
*/
public static LdapNameBuilder newLdapName(Name name) {
public static LdapNameBuilder newInstance(Name name) {
return new LdapNameBuilder(LdapUtils.newLdapName(name));
}
@@ -62,7 +69,7 @@ public final class LdapNameBuilder {
*
* @return a new instance.
*/
public static LdapNameBuilder newLdapName(String name) {
public static LdapNameBuilder newInstance(String name) {
return new LdapNameBuilder(LdapUtils.newLdapName(name));
}
@@ -85,6 +92,35 @@ public final class LdapNameBuilder {
}
}
/**
* Append the specified name to the currently built LdapName.
*
* @param name the name to add.
* @return this builder.
*/
public LdapNameBuilder add(Name name) {
Assert.notNull(name, "name must not be null");
try {
ldapName.addAll(ldapName.size(), name);
return this;
} catch (InvalidNameException e) {
throw new org.springframework.ldap.InvalidNameException(e);
}
}
/**
* Append the LdapName represented by the specified string to the currently built LdapName.
*
* @param name the name to add.
* @return this builder.
*/
public LdapNameBuilder add(String name) {
Assert.notNull(name, "name must not be null");
return add(LdapUtils.newLdapName(name));
}
/**
* Build the LdapName instance.
*

View File

@@ -0,0 +1,54 @@
package org.springframework.ldap.support;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
/**
* @author Mattias Hellborg Arthursson
*/
public class LdapNameBuilderTest {
@Test
public void testAddComponentToEmpty() {
LdapNameBuilder tested = LdapNameBuilder.newInstance().add("dc", "com").add("dc", "261consulting");
assertEquals("dc=261consulting,dc=com", tested.build().toString());
}
@Test
public void testAddComponentToBaseString() {
LdapNameBuilder tested = LdapNameBuilder.newInstance("dc=com").add("dc", "261consulting");
assertEquals("dc=261consulting,dc=com", tested.build().toString());
}
@Test
public void testAddComponentToBaseName() {
LdapNameBuilder tested = LdapNameBuilder.newInstance(LdapUtils.newLdapName("dc=com")).add("dc", "261consulting");
assertEquals("dc=261consulting,dc=com", tested.build().toString());
}
@Test
public void testAddStringNameToBaseString() {
LdapNameBuilder tested = LdapNameBuilder.newInstance("dc=261consulting,dc=com").add("ou=people");
assertEquals("ou=people,dc=261consulting,dc=com", tested.build().toString());
}
@Test
public void testAddNameToBaseString() {
LdapNameBuilder tested = LdapNameBuilder.newInstance("dc=261consulting,dc=com").add(LdapUtils.newLdapName("ou=people"));
assertEquals("ou=people,dc=261consulting,dc=com", tested.build().toString());
}
@Test
public void testAddNameToEmpty() {
LdapNameBuilder tested = LdapNameBuilder.newInstance().add(LdapUtils.newLdapName("ou=people"));
assertEquals("ou=people", tested.build().toString());
}
@Test
public void testAddEmptyToEmpty() {
LdapNameBuilder tested = LdapNameBuilder.newInstance().add("");
assertEquals("", tested.build().toString());
}
}

View File

@@ -59,7 +59,7 @@ public class DepartmentRepoImpl implements DepartmentRepo {
private List<String> getAllUnitsForDepartment(String department) {
return ldapTemplate.list(LdapNameBuilder
.newLdapName(DEPARTMENTS_OU).add("ou", department).build(), new OuValueNameClassPairMapper());
.newInstance(DEPARTMENTS_OU).add("ou", department).build(), new OuValueNameClassPairMapper());
}
private static class OuValueNameClassPairMapper implements NameClassPairMapper<String> {

View File

@@ -26,6 +26,7 @@ import org.springframework.ldap.samples.useradmin.domain.Group;
import org.springframework.ldap.samples.useradmin.domain.GroupRepo;
import org.springframework.ldap.samples.useradmin.domain.User;
import org.springframework.ldap.samples.useradmin.domain.UserRepo;
import org.springframework.ldap.support.LdapNameBuilder;
import org.springframework.ldap.support.LdapUtils;
import javax.naming.Name;
@@ -83,7 +84,9 @@ public class UserService implements BaseLdapNameAware {
}
public LdapName toAbsoluteDn(Name relativeName) {
return LdapUtils.prepend(relativeName, baseLdapPath);
return LdapNameBuilder.newInstance(baseLdapPath)
.add(relativeName)
.build();
}
/**

View File

@@ -374,7 +374,7 @@ public class PersonDaoImpl implements PersonDao {
public static final String BASE_DN = "dc=example,dc=com";
protected Name buildDn(Person p) {
**return LdapNameBuilder.newLdapName(BASE_DN)
**return LdapNameBuilder.newInstance(BASE_DN)
.add("c", p.getCountry())
.add("ou", p.getCompany())
.add("cn", p.getFullname())
@@ -728,7 +728,7 @@ distinguished name.
As of version 2.0, if you supply `javax.naming.Name` instances to the attribute modification methods in `DirContextAdapter`,
modification calculation will use distinguished name equality, meaning that if we modify the example above to:
`ctx.addAttributeValue("member", LdapUtils.newLdapName("CN=John Doe, OU=People"))`, this will no longer be considered
`ctx.addAttributeValue("member", LdapUtils.newInstance("CN=John Doe, OU=People"))`, this will no longer be considered
a modification.
.Group membership modification example
@@ -774,12 +774,12 @@ public class GroupDao implements BaseLdapNameAware {
}
private Name buildGroupDn(String groupName) {
return LdapNameBuilder.newLdapName("ou=Groups")
return LdapNameBuilder.newInstance("ou=Groups")
.add("cn", groupName).build();
}
private Name buildPersonDn(String fullname, String company, String country) {
return LdapNameBuilder.newLdapName(baseLdapPath)
return LdapNameBuilder.newInstance(baseLdapPath)
.add("c", country)
.add("ou", company)
.add("cn", fullname)
@@ -863,7 +863,7 @@ public class PersonDaoImpl implements PersonDao {
}
protected Name buildDn(String fullname, String company, String country) {
return LdapNameBuilder.newLdapName()
return LdapNameBuilder.newInstance()
.add("c", country)
.add("ou", company)
.add("cn", fullname)
@@ -1421,8 +1421,8 @@ public class PersonService implements PersonService**, BaseLdapNameAware** {
}**
...
private LdapName getFullPersonDn(Person person) {
return LdapNameBuilder.newLdapName(**basePath**)
.append(person.getDn())
return LdapNameBuilder.newInstance(**basePath**)
.add(person.getDn())
.build();
}
...

View File

@@ -114,7 +114,7 @@ public class LdapTemplateOdmWithNoDnAnnotationsITest extends AbstractLdapTemplat
@Test
public void testCreate() {
Person person = new Person();
person.setDn(LdapNameBuilder.newLdapName("ou=company1,ou=Sweden")
person.setDn(LdapNameBuilder.newInstance("ou=company1,ou=Sweden")
.add("cn", "New Person").build());
person.setCommonName("New Person");
person.setSurname("Person");

View File

@@ -173,7 +173,7 @@ public class RepositoryScanITest extends AbstractLdapTemplateIntegrationTest {
@Test
public void testCreate() {
Person person = new Person();
LdapName dn = LdapNameBuilder.newLdapName("ou=company1,ou=Sweden")
LdapName dn = LdapNameBuilder.newInstance("ou=company1,ou=Sweden")
.add("cn", "New Person").build();
person.setDn(dn);
person.setCommonName("New Person");