diff --git a/core-tiger/src/main/java/org/springframework/ldap/core/simple/SimpleLdapOperations.java b/core-tiger/src/main/java/org/springframework/ldap/core/simple/SimpleLdapOperations.java index 174246d6..8cb66780 100644 --- a/core-tiger/src/main/java/org/springframework/ldap/core/simple/SimpleLdapOperations.java +++ b/core-tiger/src/main/java/org/springframework/ldap/core/simple/SimpleLdapOperations.java @@ -1,5 +1,5 @@ /* - * Copyright 2005-2010 the original author or authors. + * Copyright 2005-2013 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,12 +15,6 @@ */ package org.springframework.ldap.core.simple; -import java.util.List; - -import javax.naming.Name; -import javax.naming.directory.Attributes; -import javax.naming.directory.SearchControls; - import org.springframework.dao.IncorrectResultSizeDataAccessException; import org.springframework.ldap.NamingException; import org.springframework.ldap.core.ContextSource; @@ -28,6 +22,11 @@ import org.springframework.ldap.core.DirContextOperations; import org.springframework.ldap.core.DirContextProcessor; import org.springframework.ldap.core.LdapOperations; +import javax.naming.Name; +import javax.naming.directory.Attributes; +import javax.naming.directory.SearchControls; +import java.util.List; + /** * LDAP operations interface usable on Java 5 and above, exposing a set of * common LDAP operations. @@ -284,7 +283,7 @@ public interface SimpleLdapOperations { *
 	 * AndFilter filter = new AndFilter();
 	 * filter.and("objectclass", "person").and("uid", userId);
-	 * boolean authenticated = ldapTemplate.authenticate(DistinguishedName.EMPTY_PATH,
+	 * boolean authenticated = ldapTemplate.authenticate(LdapUtils.emptyLdapName(),
 	 * 		filter.toString(), password);
 	 * 
* @@ -309,7 +308,7 @@ public interface SimpleLdapOperations { *
 	 * AndFilter filter = new AndFilter();
 	 * filter.and("objectclass", "person").and("uid", userId);
-	 * boolean authenticated = ldapTemplate.authenticate(DistinguishedName.EMPTY_PATH,
+	 * boolean authenticated = ldapTemplate.authenticate(LdapUtils.emptyLdapName(),
 	 * 		filter.toString(), password);
 	 * 
* diff --git a/core/src/main/java/org/springframework/ldap/core/DirContextAdapter.java b/core/src/main/java/org/springframework/ldap/core/DirContextAdapter.java index d1eb6fd3..4fe8a9a0 100644 --- a/core/src/main/java/org/springframework/ldap/core/DirContextAdapter.java +++ b/core/src/main/java/org/springframework/ldap/core/DirContextAdapter.java @@ -37,9 +37,7 @@ import javax.naming.directory.DirContext; import javax.naming.directory.ModificationItem; import javax.naming.directory.SearchControls; import javax.naming.ldap.LdapName; -import javax.naming.ldap.Rdn; import java.util.ArrayList; -import java.util.Collections; import java.util.Hashtable; import java.util.LinkedList; import java.util.List; @@ -1302,12 +1300,7 @@ public class DirContextAdapter implements DirContextOperations { */ public final void setDn(Name dn) { if (!updateMode) { - this.dn = new LdapName(Collections.emptyList()); - try { - this.dn.addAll(0, dn); - } catch (InvalidNameException e) { - throw new org.springframework.ldap.InvalidNameException(e); - } + this.dn = LdapUtils.newLdapName(dn); } else { throw new IllegalStateException( diff --git a/core/src/main/java/org/springframework/ldap/core/DirContextOperations.java b/core/src/main/java/org/springframework/ldap/core/DirContextOperations.java index de9c458a..52aab5ab 100644 --- a/core/src/main/java/org/springframework/ldap/core/DirContextOperations.java +++ b/core/src/main/java/org/springframework/ldap/core/DirContextOperations.java @@ -16,11 +16,10 @@ package org.springframework.ldap.core; -import java.util.SortedSet; - import javax.naming.Name; import javax.naming.directory.Attributes; import javax.naming.directory.DirContext; +import java.util.SortedSet; /** * Interface for DirContextAdapter. @@ -209,6 +208,7 @@ public interface DirContextOperations extends DirContext, /** * Returns the DN relative to the base path. + * NB: as of version 2.0 the returned name will be an LdapName instance. * * @return The distinguished name of the current context. * diff --git a/core/src/main/java/org/springframework/ldap/support/LdapNameBuilder.java b/core/src/main/java/org/springframework/ldap/support/LdapNameBuilder.java new file mode 100644 index 00000000..abec24c1 --- /dev/null +++ b/core/src/main/java/org/springframework/ldap/support/LdapNameBuilder.java @@ -0,0 +1,70 @@ +/* + * Copyright 2005-2013 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.ldap.support; + +import org.springframework.util.Assert; + +import javax.naming.InvalidNameException; +import javax.naming.ldap.LdapName; +import javax.naming.ldap.Rdn; + +/** + * Helper class for building {@javax.naming.ldap.LdapName} instances. + * + * @author Mattias Hellborg Arthursson + * @since 2.0 + */ +public class LdapNameBuilder { + + private final LdapName ldapName = LdapUtils.emptyLdapName(); + + private LdapNameBuilder() { + + } + + public static LdapNameBuilder newInstance() { + return new LdapNameBuilder(); + } + + /** + * Add a Rdn to the built LdapName. + * @param key the rdn attribute key. + * @param value the rdn value. + * + * @return this builder. + */ + public LdapNameBuilder add(String key, Object value) { + Assert.hasText(key, "key must not be blank"); + Assert.notNull(key, "value must not be null"); + + try { + ldapName.add(new Rdn(key, value)); + return this; + } catch (InvalidNameException e) { + throw new org.springframework.ldap.InvalidNameException(e); + } + } + + /** + * Build the LdapName instance. + * + * @return the LdapName instance that has been built. + */ + public LdapName build() { + return LdapUtils.newLdapName(ldapName); + } +} diff --git a/core/src/main/java/org/springframework/ldap/support/LdapUtils.java b/core/src/main/java/org/springframework/ldap/support/LdapUtils.java index 7e3aeca2..44c50f2a 100644 --- a/core/src/main/java/org/springframework/ldap/support/LdapUtils.java +++ b/core/src/main/java/org/springframework/ldap/support/LdapUtils.java @@ -25,6 +25,7 @@ import org.springframework.util.Assert; import javax.naming.CompositeName; import javax.naming.InvalidNameException; import javax.naming.Name; +import javax.naming.NamingEnumeration; import javax.naming.directory.Attribute; import javax.naming.directory.Attributes; import javax.naming.directory.DirContext; @@ -275,7 +276,7 @@ public final class LdapUtils { callbackHandler.handleAttributeValue(attribute.getID(), attribute.get(i), i); } catch (javax.naming.NamingException e) { - throw LdapUtils.convertLdapException(e); + throw convertLdapException(e); } } } @@ -339,14 +340,14 @@ public final class LdapUtils { try { return new LdapName(convertCompositeNameToString(compositeName)); } catch (InvalidNameException e) { - throw new org.springframework.ldap.InvalidNameException(e); + throw convertLdapException(e); } } else { LdapName result = emptyLdapName(); try { result.addAll(0, name); } catch (InvalidNameException e) { - throw new org.springframework.ldap.InvalidNameException(e); + throw convertLdapException(e); } return result; @@ -367,27 +368,37 @@ public final class LdapUtils { try { return new LdapName(distinguishedName); } catch (InvalidNameException e) { - throw new org.springframework.ldap.InvalidNameException(e); + throw convertLdapException(e); } } + private static LdapName returnOrConstructLdapNameFromName(Name name) { + if (name instanceof LdapName) { + return (LdapName) name; + } else { + return newLdapName(name); + } + } + /** - * Remove the supplied path from the beginning of this - * LdapName if this instance starts with + * Remove the supplied path from the beginning the specified + * Name if the name instance starts with * path. Useful for stripping base path suffix from a - * LdapName. The original LdapName will not be affected. + * Name. The original Name will not be affected. * * @param dn the dn to strip from. - * @param path the path to remove from the beginning of this instance. - * @return a copy of the original LdapName with the specified path stripped from its beginning. + * @param pathToRemove the path to remove from the beginning the dn instance. + * @return an LdapName instance that is a copy of the original name with the + * specified path stripped from its beginning. * @since 2.0 */ - public static LdapName removeFirst(LdapName dn, LdapName path) { + public static LdapName removeFirst(Name dn, Name pathToRemove) { Assert.notNull(dn, "dn must not be null"); - Assert.notNull(path, "path must not be null"); + Assert.notNull(pathToRemove, "pathToRemove must not be null"); LdapName result = newLdapName(dn); + LdapName path = returnOrConstructLdapNameFromName(pathToRemove); if(path.size() == 0 || !dn.startsWith(path)) { return result; @@ -397,13 +408,38 @@ public final class LdapUtils { try { result.remove(0); } catch (InvalidNameException e) { - throw new org.springframework.ldap.InvalidNameException(e); + throw convertLdapException(e); } } return result; } + /** + * Prepend the supplied path in the beginning the specified + * Name if the name instance starts with + * path. The original Name will not be affected. + * + * @param dn the dn to strip from. + * @param pathToPrepend the path to prepend in the beginning of the dn. + * @return an LdapName instance that is a copy of the original name with the + * specified path inserted at its beginning. + * @since 2.0 + */ + public static LdapName prepend(Name dn, Name pathToPrepend) { + Assert.notNull(dn, "dn must not be null"); + Assert.notNull(pathToPrepend, "pathToRemove must not be null"); + + LdapName result = newLdapName(dn); + try { + result.addAll(0, pathToPrepend); + } catch (InvalidNameException e) { + throw convertLdapException(e); + } + + return result; + } + /** * Construct a new, empty LdapName instance. * @return a new LdapName instance representing the empty path (""). @@ -414,28 +450,108 @@ public final class LdapUtils { } /** - * Find the Rdn with the requested key in the supplied LdapName. + * Find the Rdn with the requested key in the supplied Name. * - * @param name the LdapName in which to search for the key. + * @param name the Name in which to search for the key. * @param key the attribute key to search for. * @return the rdn corresponding to the first occurrence of the requested key. * @throws NoSuchElementException if no corresponding entry is found. * @since 2.0 */ - public static Rdn getRdn(LdapName name, String key) { + public static Rdn getRdn(Name name, String key) { Assert.notNull(name, "name must not be null"); Assert.hasText(key, "key must not be blank"); - List rdns = name.getRdns(); + LdapName ldapName = returnOrConstructLdapNameFromName(name); + + List rdns = ldapName.getRdns(); for (Rdn rdn : rdns) { - if(rdn.getType().equalsIgnoreCase(key)) { - return rdn; + NamingEnumeration ids = rdn.toAttributes().getIDs(); + while (ids.hasMoreElements()) { + String id = ids.nextElement(); + if(key.equalsIgnoreCase(id)) { + return rdn; + } } } throw new NoSuchElementException("No Rdn with the requested key: '" + key + "'"); } + /** + * Get the value of the Rdn with the requested key in the supplied Name. + * + * @param name the Name in which to search for the key. + * @param key the attribute key to search for. + * @return the value of the rdn corresponding to the first occurrence of the requested key. + * @throws NoSuchElementException if no corresponding entry is found. + * @since 2.0 + */ + public static Object getValue(Name name, String key) { + NamingEnumeration allAttributes = getRdn(name, key).toAttributes().getAll(); + while (allAttributes.hasMoreElements()) { + Attribute oneAttribute = allAttributes.nextElement(); + if(key.equalsIgnoreCase(oneAttribute.getID())) { + try { + return oneAttribute.get(); + } catch (javax.naming.NamingException e) { + throw convertLdapException(e); + } + } + } + + // This really shouldn't happen + throw new NoSuchElementException("No Rdn with the requested key: '" + key + "'"); + } + + /** + * Get the value of the Rdn at the requested index in the supplied Name. + * + * @param name the Name to work on. + * @param index The 0-based index of the rdn value to retrieve. Must be in the range [0,size()). + * @return the value of the rdn at the requested index. + * @throws IndexOutOfBoundsException if index is outside the specified range. + */ + public static Object getValue(Name name, int index) { + Assert.notNull(name, "name must not be null"); + + LdapName ldapName = returnOrConstructLdapNameFromName(name); + Rdn rdn = ldapName.getRdn(index); + if(rdn.size() > 0) { + logger.warn("Rdn at position " + index + " of dn '" + name + + "' is multi-value - returned value is not to be trusted. " + + "Consider using name-based getValue method instead"); + } + return rdn.getValue(); + } + + /** + * Get the value of the Rdn at the requested index in the supplied Name as a String. + * + * @param name the Name to work on. + * @param index The 0-based index of the rdn value to retrieve. Must be in the range [0,size()). + * @return the value of the rdn at the requested index as a String. + * @throws IndexOutOfBoundsException if index is outside the specified range. + * @throws ClassCastException if the value of the requested component is not a String. + */ + public static String getStringValue(Name name, int index) { + return (String) getValue(name, index); + } + + /** + * Get the value of the Rdn with the requested key in the supplied Name as a String. + * + * @param name the Name in which to search for the key. + * @param key the attribute key to search for. + * @return the String value of the rdn corresponding to the first occurrence of the requested key. + * @throws NoSuchElementException if no corresponding entry is found. + * @throws ClassCastException if the value of the requested component is not a String. + * @since 2.0 + */ + public static String getStringValue(Name name, String key) { + return (String) getValue(name, key); + } + /** * Converts a binary SID to its String representation, according to the * algorithm described @@ -33,25 +32,25 @@ public class BasicSchemaSpecification implements Specification { if (record != null) { //DN is required. - DistinguishedName dn = record.getDN(); + LdapName dn = record.getName(); if (dn != null) { //objectClass definition is required. if (record.get("objectClass") != null) { //Naming attribute is required. - LdapRdn rdn = dn.getLdapRdn(dn.size() - 1); - if (record.get(rdn.getKey()) != null) { - Object object = record.get(rdn.getKey()).get(); + Rdn rdn = dn.getRdn(dn.size() - 1); + if (record.get(rdn.getType()) != null) { + Object object = record.get(rdn.getType()).get(); if (object instanceof String) { String value = (String) object; - if (rdn.getValue().equalsIgnoreCase(value)) { + if (((String)rdn.getValue()).equalsIgnoreCase(value)) { return true; } } else if(object instanceof byte[]) { BASE64Encoder encoder = new BASE64Encoder(); - String rdnValue = encoder.encode(rdn.getValue().getBytes()); + String rdnValue = encoder.encode(((String)rdn.getValue()).getBytes()); String attributeValue = encoder.encode((byte[]) object); if (rdnValue.equals(attributeValue)) return true; } diff --git a/odm/src/main/java/org/springframework/ldap/odm/core/impl/OdmManagerImpl.java b/odm/src/main/java/org/springframework/ldap/odm/core/impl/OdmManagerImpl.java index 68a76e5d..d4993c19 100755 --- a/odm/src/main/java/org/springframework/ldap/odm/core/impl/OdmManagerImpl.java +++ b/odm/src/main/java/org/springframework/ldap/odm/core/impl/OdmManagerImpl.java @@ -21,7 +21,6 @@ import org.apache.commons.logging.LogFactory; import org.springframework.ldap.core.ContextSource; import org.springframework.ldap.core.DirContextAdapter; import org.springframework.ldap.core.DirContextOperations; -import org.springframework.ldap.core.DistinguishedName; import org.springframework.ldap.core.LdapOperations; import org.springframework.ldap.core.LdapTemplate; import org.springframework.ldap.core.simple.ParameterizedContextMapper; @@ -30,6 +29,7 @@ import org.springframework.ldap.filter.EqualsFilter; import org.springframework.ldap.odm.core.OdmException; import org.springframework.ldap.odm.core.OdmManager; import org.springframework.ldap.odm.typeconversion.ConverterManager; +import org.springframework.ldap.support.LdapUtils; import javax.naming.Name; import javax.naming.NamingEnumeration; @@ -254,7 +254,7 @@ public final class OdmManagerImpl implements OdmManager { // Search from the root if we are not told where to search from Name localBase = base; if (base == null || base.size() == 0) { - localBase = DistinguishedName.EMPTY_PATH; + localBase = LdapUtils.emptyLdapName(); } if (LOG.isDebugEnabled()) { diff --git a/odm/src/test/java/org/springframework/ldap/odm/test/TestLdap.java b/odm/src/test/java/org/springframework/ldap/odm/test/TestLdap.java index 4dd4802d..346cc345 100755 --- a/odm/src/test/java/org/springframework/ldap/odm/test/TestLdap.java +++ b/odm/src/test/java/org/springframework/ldap/odm/test/TestLdap.java @@ -33,7 +33,6 @@ import org.junit.Test; import org.springframework.core.io.ClassPathResource; import org.springframework.ldap.NameNotFoundException; import org.springframework.ldap.core.ContextSource; -import org.springframework.ldap.core.DistinguishedName; import org.springframework.ldap.core.support.LdapContextSource; import org.springframework.ldap.odm.annotations.Attribute; import org.springframework.ldap.odm.annotations.Entry; @@ -57,6 +56,7 @@ import org.springframework.util.CollectionUtils; import javax.naming.Name; import javax.naming.directory.SearchControls; +import javax.naming.ldap.LdapName; import java.awt.image.BufferedImage; import java.io.IOException; import java.lang.reflect.Method; @@ -73,7 +73,7 @@ public final class TestLdap { private static final Log LOG = LogFactory.getLog(TestLdap.class); // Base DN for test data - private static final DistinguishedName baseName = new DistinguishedName("o=Whoniverse"); + private static final LdapName baseName = LdapUtils.newLdapName("o=Whoniverse"); // This port MUST be free on local host for these unit tests to function. private static int port; @@ -437,13 +437,13 @@ public final class TestLdap { // Trying to read a non-existant entry should be flagged as an error @Test(expected = NameNotFoundException.class) public void readNonExistant() throws Exception { - odmManager.read(Person.class, new DistinguishedName("cn=Hili Harvey,ou=Doctors,o=Whoniverse")); + odmManager.read(Person.class, LdapUtils.newLdapName("cn=Hili Harvey,ou=Doctors,o=Whoniverse")); } // Read an entry with classes in addition to those supported by the Entry @Test(expected = OdmException.class) public void readNonMatchingObjectclasses() throws Exception { - odmManager.read(Person.class, new DistinguishedName("ou=Doctors,o=Whoniverse")); + odmManager.read(Person.class, LdapUtils.newLdapName("ou=Doctors,o=Whoniverse")); } private final static class NoEntry { diff --git a/odm/src/test/java/org/springframework/ldap/odm/test/TestSchemaToJava.java b/odm/src/test/java/org/springframework/ldap/odm/test/TestSchemaToJava.java index c4d513b8..40179b97 100755 --- a/odm/src/test/java/org/springframework/ldap/odm/test/TestSchemaToJava.java +++ b/odm/src/test/java/org/springframework/ldap/odm/test/TestSchemaToJava.java @@ -25,7 +25,6 @@ import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; import org.springframework.core.io.ClassPathResource; -import org.springframework.ldap.core.DistinguishedName; import org.springframework.ldap.core.support.LdapContextSource; import org.springframework.ldap.odm.core.impl.OdmManagerImpl; import org.springframework.ldap.odm.test.utils.CompilerInterface; @@ -53,7 +52,7 @@ import static org.junit.Assert.assertEquals; public final class TestSchemaToJava { private static final Log LOG = LogFactory.getLog(TestLdap.class); - private static final DistinguishedName baseName = new DistinguishedName("o=Whoniverse"); + private static final LdapName baseName = LdapUtils.newLdapName("o=Whoniverse"); private static final String tempDir=System.getProperty("java.io.tmpdir"); diff --git a/odm/src/test/java/org/springframework/ldap/odm/test/TestSchemaViewer.java b/odm/src/test/java/org/springframework/ldap/odm/test/TestSchemaViewer.java index 58f75bec..0021ad75 100755 --- a/odm/src/test/java/org/springframework/ldap/odm/test/TestSchemaViewer.java +++ b/odm/src/test/java/org/springframework/ldap/odm/test/TestSchemaViewer.java @@ -21,13 +21,14 @@ import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; -import org.springframework.ldap.core.DistinguishedName; import org.springframework.ldap.odm.test.utils.ExecuteRunnable; import org.springframework.ldap.odm.test.utils.GetFreePort; import org.springframework.ldap.odm.test.utils.RunnableTest; import org.springframework.ldap.odm.tools.SchemaViewer; +import org.springframework.ldap.support.LdapUtils; import org.springframework.ldap.test.LdapTestUtils; +import javax.naming.ldap.LdapName; import java.io.ByteArrayOutputStream; import java.io.PrintStream; import java.util.ArrayList; @@ -38,7 +39,7 @@ import static org.junit.Assert.assertEquals; public final class TestSchemaViewer { // Base DN for test data - private static final DistinguishedName baseName = new DistinguishedName("o=Whoniverse"); + private static final LdapName baseName = LdapUtils.newLdapName("o=Whoniverse"); private static final String lineSeparator = System.getProperty ("line.separator"); diff --git a/samples/article-spring20/src/main/java/org/springframework/ldap/samples/article/dao/PersonDaoImpl.java b/samples/article-spring20/src/main/java/org/springframework/ldap/samples/article/dao/PersonDaoImpl.java index 950b8851..46b6429b 100644 --- a/samples/article-spring20/src/main/java/org/springframework/ldap/samples/article/dao/PersonDaoImpl.java +++ b/samples/article-spring20/src/main/java/org/springframework/ldap/samples/article/dao/PersonDaoImpl.java @@ -15,19 +15,19 @@ */ package org.springframework.ldap.samples.article.dao; -import java.util.List; +import org.springframework.ldap.core.AttributesMapper; +import org.springframework.ldap.core.ContextMapper; +import org.springframework.ldap.core.DirContextAdapter; +import org.springframework.ldap.core.LdapTemplate; +import org.springframework.ldap.filter.EqualsFilter; +import org.springframework.ldap.samples.article.domain.Person; +import org.springframework.ldap.support.LdapNameBuilder; +import org.springframework.ldap.support.LdapUtils; import javax.naming.Name; import javax.naming.NamingException; import javax.naming.directory.Attributes; - -import org.springframework.ldap.core.AttributesMapper; -import org.springframework.ldap.core.ContextMapper; -import org.springframework.ldap.core.DirContextAdapter; -import org.springframework.ldap.core.DistinguishedName; -import org.springframework.ldap.core.LdapTemplate; -import org.springframework.ldap.filter.EqualsFilter; -import org.springframework.ldap.samples.article.domain.Person; +import java.util.List; /** * Default implementation of PersonDao. This implementation uses @@ -83,7 +83,7 @@ public class PersonDaoImpl implements PersonDao { */ public List getAllPersonNames() { EqualsFilter filter = new EqualsFilter("objectclass", "person"); - return ldapTemplate.search(DistinguishedName.EMPTY_PATH, filter.encode(), new AttributesMapper() { + return ldapTemplate.search(LdapUtils.emptyLdapName(), filter.encode(), new AttributesMapper() { public Object mapFromAttributes(Attributes attrs) throws NamingException { return attrs.get("cn").get(); } @@ -95,7 +95,7 @@ public class PersonDaoImpl implements PersonDao { */ public List findAll() { EqualsFilter filter = new EqualsFilter("objectclass", "person"); - return ldapTemplate.search(DistinguishedName.EMPTY_PATH, filter.encode(), getContextMapper()); + return ldapTemplate.search(LdapUtils.emptyLdapName(), filter.encode(), getContextMapper()); } /* @@ -103,7 +103,7 @@ public class PersonDaoImpl implements PersonDao { * java.lang.String) */ public Person findByPrimaryKey(String country, String company, String fullname) { - DistinguishedName dn = buildDn(country, company, fullname); + Name dn = buildDn(country, company, fullname); return (Person) ldapTemplate.lookup(dn, getContextMapper()); } @@ -111,16 +111,16 @@ public class PersonDaoImpl implements PersonDao { return new PersonContextMapper(); } - private DistinguishedName buildDn(Person person) { + private Name buildDn(Person person) { return buildDn(person.getCountry(), person.getCompany(), person.getFullName()); } - private DistinguishedName buildDn(String country, String company, String fullname) { - DistinguishedName dn = new DistinguishedName(); - dn.add("c", country); - dn.add("ou", company); - dn.add("cn", fullname); - return dn; + private Name buildDn(String country, String company, String fullname) { + return LdapNameBuilder.newInstance() + .add("c", country) + .add("ou", company) + .add("cn", fullname) + .build(); } private void mapToContext(Person person, DirContextAdapter context) { @@ -135,7 +135,7 @@ public class PersonDaoImpl implements PersonDao { * Maps from DirContextAdapter to Person objects. A DN for a person will be * of the form cn=[fullname],ou=[company],c=[country], so * the values of these attributes must be extracted from the DN. For this, - * we use the DistinguishedName. + * we use LdapName and helper methods in LdapUtils. * *Mattias Hellborg Arthurssonellborg Arthursson * @author Ulrik Sandberg @@ -144,10 +144,10 @@ public class PersonDaoImpl implements PersonDao { public Object mapFromContext(Object ctx) { DirContextAdapter context = (DirContextAdapter) ctx; - DistinguishedName dn = new DistinguishedName(context.getDn()); + Name dn = context.getDn(); Person person = new Person(); - person.setCountry(dn.getLdapRdn(0).getComponent().getValue()); - person.setCompany(dn.getLdapRdn(1).getComponent().getValue()); + person.setCountry(LdapUtils.getStringValue(dn, 0)); + person.setCompany(LdapUtils.getStringValue(dn, 1)); person.setFullName(context.getStringAttribute("cn")); person.setLastName(context.getStringAttribute("sn")); person.setDescription(context.getStringAttribute("description")); diff --git a/samples/article-spring20/src/main/java/org/springframework/ldap/samples/article/web/DefaultController.java b/samples/article-spring20/src/main/java/org/springframework/ldap/samples/article/web/DefaultController.java index 71accf2c..c276f721 100644 --- a/samples/article-spring20/src/main/java/org/springframework/ldap/samples/article/web/DefaultController.java +++ b/samples/article-spring20/src/main/java/org/springframework/ldap/samples/article/web/DefaultController.java @@ -1,22 +1,22 @@ package org.springframework.ldap.samples.article.web; -import java.io.UnsupportedEncodingException; -import java.net.URLEncoder; - -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - import org.apache.commons.lang.StringUtils; import org.springframework.ldap.core.DirContextOperations; -import org.springframework.ldap.core.DistinguishedName; import org.springframework.ldap.samples.article.dao.PersonDao; import org.springframework.ldap.samples.article.domain.Person; import org.springframework.ldap.samples.utils.HtmlRowLdapTreeVisitor; import org.springframework.ldap.samples.utils.LdapTree; import org.springframework.ldap.samples.utils.LdapTreeBuilder; +import org.springframework.ldap.support.LdapUtils; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.multiaction.MultiActionController; +import javax.naming.Name; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.UnsupportedEncodingException; +import java.net.URLEncoder; + /** * Default controller. * @@ -42,7 +42,7 @@ public class DefaultController extends MultiActionController { } public ModelAndView showTree(HttpServletRequest request, HttpServletResponse response) { - LdapTree ldapTree = ldapTreeBuilder.getLdapTree(DistinguishedName.EMPTY_PATH); + LdapTree ldapTree = ldapTreeBuilder.getLdapTree(LdapUtils.emptyLdapName()); HtmlRowLdapTreeVisitor visitor = new PersonLinkHtmlRowLdapTreeVisitor(); ldapTree.traverse(visitor); return new ModelAndView("showTree", "rows", visitor.getRows()); @@ -98,10 +98,10 @@ public class DefaultController extends MultiActionController { protected String getLinkForNode(DirContextOperations node) { String[] objectClassValues = node.getStringAttributes("objectClass"); if (containsValue(objectClassValues, "person")) { - DistinguishedName distinguishedName = (DistinguishedName) node.getDn(); - String country = encodeValue(distinguishedName.getValue("c")); - String company = encodeValue(distinguishedName.getValue("ou")); - String fullName = encodeValue(distinguishedName.getValue("cn")); + Name dn = node.getDn(); + String country = encodeValue(LdapUtils.getStringValue(dn, "c")); + String company = encodeValue(LdapUtils.getStringValue(dn, "ou")); + String fullName = encodeValue(LdapUtils.getStringValue(dn, "cn")); return "showPerson.do?country=" + country + "&company=" + company + "&fullName=" + fullName; } diff --git a/samples/article-spring30/src/main/java/org/springframework/ldap/samples/article/dao/PersonDaoImpl.java b/samples/article-spring30/src/main/java/org/springframework/ldap/samples/article/dao/PersonDaoImpl.java index 950b8851..771c64b8 100644 --- a/samples/article-spring30/src/main/java/org/springframework/ldap/samples/article/dao/PersonDaoImpl.java +++ b/samples/article-spring30/src/main/java/org/springframework/ldap/samples/article/dao/PersonDaoImpl.java @@ -15,19 +15,19 @@ */ package org.springframework.ldap.samples.article.dao; -import java.util.List; +import org.springframework.ldap.core.AttributesMapper; +import org.springframework.ldap.core.ContextMapper; +import org.springframework.ldap.core.DirContextAdapter; +import org.springframework.ldap.core.LdapTemplate; +import org.springframework.ldap.filter.EqualsFilter; +import org.springframework.ldap.samples.article.domain.Person; +import org.springframework.ldap.support.LdapNameBuilder; +import org.springframework.ldap.support.LdapUtils; import javax.naming.Name; import javax.naming.NamingException; import javax.naming.directory.Attributes; - -import org.springframework.ldap.core.AttributesMapper; -import org.springframework.ldap.core.ContextMapper; -import org.springframework.ldap.core.DirContextAdapter; -import org.springframework.ldap.core.DistinguishedName; -import org.springframework.ldap.core.LdapTemplate; -import org.springframework.ldap.filter.EqualsFilter; -import org.springframework.ldap.samples.article.domain.Person; +import java.util.List; /** * Default implementation of PersonDao. This implementation uses @@ -83,7 +83,7 @@ public class PersonDaoImpl implements PersonDao { */ public List getAllPersonNames() { EqualsFilter filter = new EqualsFilter("objectclass", "person"); - return ldapTemplate.search(DistinguishedName.EMPTY_PATH, filter.encode(), new AttributesMapper() { + return ldapTemplate.search(LdapUtils.emptyLdapName(), filter.encode(), new AttributesMapper() { public Object mapFromAttributes(Attributes attrs) throws NamingException { return attrs.get("cn").get(); } @@ -95,7 +95,7 @@ public class PersonDaoImpl implements PersonDao { */ public List findAll() { EqualsFilter filter = new EqualsFilter("objectclass", "person"); - return ldapTemplate.search(DistinguishedName.EMPTY_PATH, filter.encode(), getContextMapper()); + return ldapTemplate.search(LdapUtils.emptyLdapName(), filter.encode(), getContextMapper()); } /* @@ -103,7 +103,7 @@ public class PersonDaoImpl implements PersonDao { * java.lang.String) */ public Person findByPrimaryKey(String country, String company, String fullname) { - DistinguishedName dn = buildDn(country, company, fullname); + Name dn = buildDn(country, company, fullname); return (Person) ldapTemplate.lookup(dn, getContextMapper()); } @@ -111,16 +111,16 @@ public class PersonDaoImpl implements PersonDao { return new PersonContextMapper(); } - private DistinguishedName buildDn(Person person) { + private Name buildDn(Person person) { return buildDn(person.getCountry(), person.getCompany(), person.getFullName()); } - private DistinguishedName buildDn(String country, String company, String fullname) { - DistinguishedName dn = new DistinguishedName(); - dn.add("c", country); - dn.add("ou", company); - dn.add("cn", fullname); - return dn; + private Name buildDn(String country, String company, String fullname) { + return LdapNameBuilder.newInstance() + .add("c", country) + .add("ou", company) + .add("cn", fullname) + .build(); } private void mapToContext(Person person, DirContextAdapter context) { @@ -135,7 +135,7 @@ public class PersonDaoImpl implements PersonDao { * Maps from DirContextAdapter to Person objects. A DN for a person will be * of the form cn=[fullname],ou=[company],c=[country], so * the values of these attributes must be extracted from the DN. For this, - * we use the DistinguishedName. + * we use LdapName and utility methods in LdapUtils. * *Mattias Hellborg Arthurssonellborg Arthursson * @author Ulrik Sandberg @@ -144,10 +144,10 @@ public class PersonDaoImpl implements PersonDao { public Object mapFromContext(Object ctx) { DirContextAdapter context = (DirContextAdapter) ctx; - DistinguishedName dn = new DistinguishedName(context.getDn()); + Name dn = context.getDn(); Person person = new Person(); - person.setCountry(dn.getLdapRdn(0).getComponent().getValue()); - person.setCompany(dn.getLdapRdn(1).getComponent().getValue()); + person.setCountry(LdapUtils.getStringValue(dn, 0)); + person.setCompany(LdapUtils.getStringValue(dn, 1)); person.setFullName(context.getStringAttribute("cn")); person.setLastName(context.getStringAttribute("sn")); person.setDescription(context.getStringAttribute("description")); diff --git a/samples/article-spring30/src/main/java/org/springframework/ldap/samples/article/web/DefaultController.java b/samples/article-spring30/src/main/java/org/springframework/ldap/samples/article/web/DefaultController.java index 6c8ac431..f45a3d75 100644 --- a/samples/article-spring30/src/main/java/org/springframework/ldap/samples/article/web/DefaultController.java +++ b/samples/article-spring30/src/main/java/org/springframework/ldap/samples/article/web/DefaultController.java @@ -1,22 +1,23 @@ package org.springframework.ldap.samples.article.web; -import java.io.UnsupportedEncodingException; -import java.net.URLEncoder; - import org.apache.commons.lang.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.ldap.core.DirContextOperations; -import org.springframework.ldap.core.DistinguishedName; import org.springframework.ldap.samples.article.dao.PersonDao; import org.springframework.ldap.samples.article.domain.Person; import org.springframework.ldap.samples.utils.HtmlRowLdapTreeVisitor; import org.springframework.ldap.samples.utils.LdapTree; import org.springframework.ldap.samples.utils.LdapTreeBuilder; +import org.springframework.ldap.support.LdapUtils; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; +import javax.naming.Name; +import java.io.UnsupportedEncodingException; +import java.net.URLEncoder; + /** * Default controller. * @@ -37,7 +38,7 @@ public class DefaultController { @RequestMapping("/showTree.do") public ModelAndView showTree() { - LdapTree ldapTree = ldapTreeBuilder.getLdapTree(DistinguishedName.EMPTY_PATH); + LdapTree ldapTree = ldapTreeBuilder.getLdapTree(LdapUtils.emptyLdapName()); HtmlRowLdapTreeVisitor visitor = new PersonLinkHtmlRowLdapTreeVisitor(); ldapTree.traverse(visitor); return new ModelAndView("showTree", "rows", visitor.getRows()); @@ -94,10 +95,10 @@ public class DefaultController { protected String getLinkForNode(DirContextOperations node) { String[] objectClassValues = node.getStringAttributes("objectClass"); if (containsValue(objectClassValues, "person")) { - DistinguishedName distinguishedName = (DistinguishedName) node.getDn(); - String country = encodeValue(distinguishedName.getValue("c")); - String company = encodeValue(distinguishedName.getValue("ou")); - String fullName = encodeValue(distinguishedName.getValue("cn")); + Name dn = node.getDn(); + String country = encodeValue(LdapUtils.getStringValue(dn, "c")); + String company = encodeValue(LdapUtils.getStringValue(dn, "ou")); + String fullName = encodeValue(LdapUtils.getStringValue(dn, "cn")); return "showPerson.do?country=" + country + "&company=" + company + "&fullName=" + fullName; } diff --git a/samples/article/src/main/java/org/springframework/ldap/samples/article/dao/PersonDaoImpl.java b/samples/article/src/main/java/org/springframework/ldap/samples/article/dao/PersonDaoImpl.java index 950b8851..07937525 100644 --- a/samples/article/src/main/java/org/springframework/ldap/samples/article/dao/PersonDaoImpl.java +++ b/samples/article/src/main/java/org/springframework/ldap/samples/article/dao/PersonDaoImpl.java @@ -15,19 +15,20 @@ */ package org.springframework.ldap.samples.article.dao; -import java.util.List; +import org.springframework.ldap.core.AttributesMapper; +import org.springframework.ldap.core.ContextMapper; +import org.springframework.ldap.core.DirContextAdapter; +import org.springframework.ldap.core.LdapTemplate; +import org.springframework.ldap.filter.EqualsFilter; +import org.springframework.ldap.samples.article.domain.Person; +import org.springframework.ldap.support.LdapNameBuilder; +import org.springframework.ldap.support.LdapUtils; import javax.naming.Name; import javax.naming.NamingException; import javax.naming.directory.Attributes; - -import org.springframework.ldap.core.AttributesMapper; -import org.springframework.ldap.core.ContextMapper; -import org.springframework.ldap.core.DirContextAdapter; -import org.springframework.ldap.core.DistinguishedName; -import org.springframework.ldap.core.LdapTemplate; -import org.springframework.ldap.filter.EqualsFilter; -import org.springframework.ldap.samples.article.domain.Person; +import javax.naming.ldap.LdapName; +import java.util.List; /** * Default implementation of PersonDao. This implementation uses @@ -83,7 +84,7 @@ public class PersonDaoImpl implements PersonDao { */ public List getAllPersonNames() { EqualsFilter filter = new EqualsFilter("objectclass", "person"); - return ldapTemplate.search(DistinguishedName.EMPTY_PATH, filter.encode(), new AttributesMapper() { + return ldapTemplate.search(LdapUtils.emptyLdapName(), filter.encode(), new AttributesMapper() { public Object mapFromAttributes(Attributes attrs) throws NamingException { return attrs.get("cn").get(); } @@ -95,7 +96,7 @@ public class PersonDaoImpl implements PersonDao { */ public List findAll() { EqualsFilter filter = new EqualsFilter("objectclass", "person"); - return ldapTemplate.search(DistinguishedName.EMPTY_PATH, filter.encode(), getContextMapper()); + return ldapTemplate.search(LdapUtils.emptyLdapName(), filter.encode(), getContextMapper()); } /* @@ -103,7 +104,7 @@ public class PersonDaoImpl implements PersonDao { * java.lang.String) */ public Person findByPrimaryKey(String country, String company, String fullname) { - DistinguishedName dn = buildDn(country, company, fullname); + LdapName dn = buildDn(country, company, fullname); return (Person) ldapTemplate.lookup(dn, getContextMapper()); } @@ -111,16 +112,16 @@ public class PersonDaoImpl implements PersonDao { return new PersonContextMapper(); } - private DistinguishedName buildDn(Person person) { + private LdapName buildDn(Person person) { return buildDn(person.getCountry(), person.getCompany(), person.getFullName()); } - private DistinguishedName buildDn(String country, String company, String fullname) { - DistinguishedName dn = new DistinguishedName(); - dn.add("c", country); - dn.add("ou", company); - dn.add("cn", fullname); - return dn; + private LdapName buildDn(String country, String company, String fullname) { + return LdapNameBuilder.newInstance() + .add("c", country) + .add("ou", company) + .add("cn", fullname) + .build(); } private void mapToContext(Person person, DirContextAdapter context) { @@ -135,19 +136,19 @@ public class PersonDaoImpl implements PersonDao { * Maps from DirContextAdapter to Person objects. A DN for a person will be * of the form cn=[fullname],ou=[company],c=[country], so * the values of these attributes must be extracted from the DN. For this, - * we use the DistinguishedName. + * we use the LdapName along with utility methods in LdapUtils. * - *Mattias Hellborg Arthurssonellborg Arthursson + * @author Mattias Hellborg Arthursson * @author Ulrik Sandberg */ private static class PersonContextMapper implements ContextMapper { public Object mapFromContext(Object ctx) { DirContextAdapter context = (DirContextAdapter) ctx; - DistinguishedName dn = new DistinguishedName(context.getDn()); + LdapName dn = LdapUtils.newLdapName(context.getDn()); Person person = new Person(); - person.setCountry(dn.getLdapRdn(0).getComponent().getValue()); - person.setCompany(dn.getLdapRdn(1).getComponent().getValue()); + person.setCountry(LdapUtils.getStringValue(dn, 0)); + person.setCompany(LdapUtils.getStringValue(dn, 1)); person.setFullName(context.getStringAttribute("cn")); person.setLastName(context.getStringAttribute("sn")); person.setDescription(context.getStringAttribute("description")); diff --git a/samples/article/src/main/java/org/springframework/ldap/samples/article/web/DefaultController.java b/samples/article/src/main/java/org/springframework/ldap/samples/article/web/DefaultController.java index 6c8ac431..afe4b4e8 100644 --- a/samples/article/src/main/java/org/springframework/ldap/samples/article/web/DefaultController.java +++ b/samples/article/src/main/java/org/springframework/ldap/samples/article/web/DefaultController.java @@ -1,22 +1,23 @@ package org.springframework.ldap.samples.article.web; -import java.io.UnsupportedEncodingException; -import java.net.URLEncoder; - import org.apache.commons.lang.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.ldap.core.DirContextOperations; -import org.springframework.ldap.core.DistinguishedName; import org.springframework.ldap.samples.article.dao.PersonDao; import org.springframework.ldap.samples.article.domain.Person; import org.springframework.ldap.samples.utils.HtmlRowLdapTreeVisitor; import org.springframework.ldap.samples.utils.LdapTree; import org.springframework.ldap.samples.utils.LdapTreeBuilder; +import org.springframework.ldap.support.LdapUtils; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; +import javax.naming.Name; +import java.io.UnsupportedEncodingException; +import java.net.URLEncoder; + /** * Default controller. * @@ -37,7 +38,7 @@ public class DefaultController { @RequestMapping("/showTree.do") public ModelAndView showTree() { - LdapTree ldapTree = ldapTreeBuilder.getLdapTree(DistinguishedName.EMPTY_PATH); + LdapTree ldapTree = ldapTreeBuilder.getLdapTree(LdapUtils.emptyLdapName()); HtmlRowLdapTreeVisitor visitor = new PersonLinkHtmlRowLdapTreeVisitor(); ldapTree.traverse(visitor); return new ModelAndView("showTree", "rows", visitor.getRows()); @@ -94,10 +95,10 @@ public class DefaultController { protected String getLinkForNode(DirContextOperations node) { String[] objectClassValues = node.getStringAttributes("objectClass"); if (containsValue(objectClassValues, "person")) { - DistinguishedName distinguishedName = (DistinguishedName) node.getDn(); - String country = encodeValue(distinguishedName.getValue("c")); - String company = encodeValue(distinguishedName.getValue("ou")); - String fullName = encodeValue(distinguishedName.getValue("cn")); + Name dn = node.getDn(); + String country = encodeValue(LdapUtils.getStringValue(dn, "c")); + String company = encodeValue(LdapUtils.getStringValue(dn, "ou")); + String fullName = encodeValue(LdapUtils.getStringValue(dn, "cn")); return "showPerson.do?country=" + country + "&company=" + company + "&fullName=" + fullName; } diff --git a/samples/demos/demo-tiger/src/main/java/org/springframework/ldap/demo/solution/PersonDaoImpl.java b/samples/demos/demo-tiger/src/main/java/org/springframework/ldap/demo/solution/PersonDaoImpl.java index e041be36..b64ed163 100644 --- a/samples/demos/demo-tiger/src/main/java/org/springframework/ldap/demo/solution/PersonDaoImpl.java +++ b/samples/demos/demo-tiger/src/main/java/org/springframework/ldap/demo/solution/PersonDaoImpl.java @@ -15,17 +15,17 @@ */ package org.springframework.ldap.demo.solution; -import java.util.List; - -import javax.naming.Name; - import org.springframework.ldap.core.DirContextAdapter; import org.springframework.ldap.core.DirContextOperations; -import org.springframework.ldap.core.DistinguishedName; import org.springframework.ldap.core.simple.AbstractParameterizedContextMapper; import org.springframework.ldap.core.simple.SimpleLdapTemplate; import org.springframework.ldap.demo.dao.PersonDao; import org.springframework.ldap.demo.domain.Person; +import org.springframework.ldap.support.LdapNameBuilder; +import org.springframework.ldap.support.LdapUtils; + +import javax.naming.Name; +import java.util.List; /** * Spring LDAP implementation of PersonDao. This implementation uses many Spring @@ -48,9 +48,9 @@ public class PersonDaoImpl implements PersonDao { person.setDescription(ctx.getStringAttribute("description")); person.setPhone(ctx.getStringAttribute("telephoneNumber")); - DistinguishedName dn = (DistinguishedName) ctx.getDn(); - person.setCountry(dn.getValue("c")); - person.setCompany(dn.getValue("ou")); + Name dn = ctx.getDn(); + person.setCountry(LdapUtils.getStringValue(dn, "c")); + person.setCompany(LdapUtils.getStringValue(dn, "ou")); return person; } } @@ -123,11 +123,11 @@ public class PersonDaoImpl implements PersonDao { } private Name buildDn(String country, String company, String fullname) { - DistinguishedName dn = new DistinguishedName(); - dn.append("c", country); - dn.append("ou", company); - dn.append("cn", fullname); - return dn; + return LdapNameBuilder.newInstance() + .add("c", country) + .add("ou", company) + .add("cn", fullname) + .build(); } private void mapToContext(Person person, DirContextOperations ctx) { diff --git a/samples/demos/demo/src/main/java/org/springframework/ldap/demo/solution/PersonDaoImpl.java b/samples/demos/demo/src/main/java/org/springframework/ldap/demo/solution/PersonDaoImpl.java index d29629e1..c46e229a 100644 --- a/samples/demos/demo/src/main/java/org/springframework/ldap/demo/solution/PersonDaoImpl.java +++ b/samples/demos/demo/src/main/java/org/springframework/ldap/demo/solution/PersonDaoImpl.java @@ -15,17 +15,17 @@ */ package org.springframework.ldap.demo.solution; -import java.util.List; - -import javax.naming.Name; - import org.springframework.ldap.core.DirContextAdapter; import org.springframework.ldap.core.DirContextOperations; -import org.springframework.ldap.core.DistinguishedName; import org.springframework.ldap.core.LdapTemplate; import org.springframework.ldap.core.support.AbstractContextMapper; import org.springframework.ldap.demo.dao.PersonDao; import org.springframework.ldap.demo.domain.Person; +import org.springframework.ldap.support.LdapNameBuilder; +import org.springframework.ldap.support.LdapUtils; + +import javax.naming.Name; +import java.util.List; /** * Spring LDAP implementation of PersonDao. This implementation uses many Spring @@ -47,9 +47,9 @@ public class PersonDaoImpl implements PersonDao { person.setDescription(ctx.getStringAttribute("description")); person.setPhone(ctx.getStringAttribute("telephoneNumber")); - DistinguishedName dn = (DistinguishedName) ctx.getDn(); - person.setCountry(dn.getValue("c")); - person.setCompany(dn.getValue("ou")); + Name dn = ctx.getDn(); + person.setCountry(LdapUtils.getStringValue(dn, "c")); + person.setCompany(LdapUtils.getStringValue(dn, "ou")); return person; } } @@ -121,12 +121,12 @@ public class PersonDaoImpl implements PersonDao { } private Name buildDn(String country, String company, String fullname) { - DistinguishedName dn = new DistinguishedName(); - dn.append("c", country); - dn.append("ou", company); - dn.append("cn", fullname); - return dn; - } + return LdapNameBuilder.newInstance() + .add("c", country) + .add("ou", company) + .add("cn", fullname) + .build(); + } private void mapToContext(Person person, DirContextOperations ctx) { ctx.setAttributeValues("objectclass", new String[] { "top", "person" }); diff --git a/samples/samples-utils/src/main/java/org/springframework/ldap/samples/utils/LdapTreeBuilder.java b/samples/samples-utils/src/main/java/org/springframework/ldap/samples/utils/LdapTreeBuilder.java index d06033cc..2ef112ce 100644 --- a/samples/samples-utils/src/main/java/org/springframework/ldap/samples/utils/LdapTreeBuilder.java +++ b/samples/samples-utils/src/main/java/org/springframework/ldap/samples/utils/LdapTreeBuilder.java @@ -1,9 +1,11 @@ package org.springframework.ldap.samples.utils; import org.springframework.ldap.core.DirContextOperations; -import org.springframework.ldap.core.DistinguishedName; import org.springframework.ldap.core.LdapTemplate; import org.springframework.ldap.core.support.AbstractContextMapper; +import org.springframework.ldap.support.LdapUtils; + +import javax.naming.Name; public class LdapTreeBuilder { @@ -13,9 +15,8 @@ public class LdapTreeBuilder { this.ldapTemplate = ldapTemplate; } - public LdapTree getLdapTree(DistinguishedName root) { - DirContextOperations context = ldapTemplate.lookupContext(root - .toString()); + public LdapTree getLdapTree(Name root) { + DirContextOperations context = ldapTemplate.lookupContext(root); return getLdapTree(context); } @@ -25,8 +26,8 @@ public class LdapTreeBuilder { new AbstractContextMapper() { @Override protected Object doMapFromContext(DirContextOperations ctx) { - DistinguishedName dn = (DistinguishedName) ctx.getDn(); - dn.prepend((DistinguishedName) rootContext.getDn()); + Name dn = ctx.getDn(); + dn = LdapUtils.prepend(dn, rootContext.getDn()); ldapTree.addSubTree(getLdapTree(ldapTemplate .lookupContext(dn))); return null; diff --git a/samples/samples-utils/src/test/java/org/springframework/ldap/samples/utils/LdapTreeBuilderIntegrationTest.java b/samples/samples-utils/src/test/java/org/springframework/ldap/samples/utils/LdapTreeBuilderIntegrationTest.java index ab4e4c48..7d276d5c 100644 --- a/samples/samples-utils/src/test/java/org/springframework/ldap/samples/utils/LdapTreeBuilderIntegrationTest.java +++ b/samples/samples-utils/src/test/java/org/springframework/ldap/samples/utils/LdapTreeBuilderIntegrationTest.java @@ -1,18 +1,19 @@ package org.springframework.ldap.samples.utils; -import static junit.framework.Assert.assertEquals; - -import java.util.Iterator; -import java.util.LinkedHashMap; -import java.util.Map; - import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.ldap.core.DirContextOperations; -import org.springframework.ldap.core.DistinguishedName; +import org.springframework.ldap.support.LdapUtils; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests; +import javax.naming.ldap.LdapName; +import java.util.Iterator; +import java.util.LinkedHashMap; +import java.util.Map; + +import static junit.framework.Assert.assertEquals; + @ContextConfiguration(locations = { "/conf/testContext.xml" }) public class LdapTreeBuilderIntegrationTest extends AbstractJUnit4SpringContextTests { @@ -21,22 +22,19 @@ public class LdapTreeBuilderIntegrationTest extends AbstractJUnit4SpringContextT @Test public void testGetLdapTree() { - LdapTree ldapTree = tested.getLdapTree(new DistinguishedName("c=Sweden")); + LdapTree ldapTree = tested.getLdapTree(LdapUtils.newLdapName("c=Sweden")); ldapTree.traverse(new TestVisitor()); } private static final class TestVisitor implements LdapTreeVisitor { - private static final DistinguishedName DN_1 = new DistinguishedName("c=Sweden"); + private static final LdapName DN_1 = LdapUtils.newLdapName("c=Sweden"); + private static final LdapName DN_2 = LdapUtils.newLdapName("ou=company1,c=Sweden"); + private static final LdapName DN_3 = LdapUtils.newLdapName("cn=Some Person,ou=company1,c=Sweden"); + private static final LdapName DN_4 = LdapUtils.newLdapName("cn=Some Person2,ou=company1,c=Sweden"); - private static final DistinguishedName DN_2 = new DistinguishedName("ou=company1,c=Sweden"); + private Map names = new LinkedHashMap(); - private static final DistinguishedName DN_3 = new DistinguishedName("cn=Some Person,ou=company1,c=Sweden"); - - private static final DistinguishedName DN_4 = new DistinguishedName("cn=Some Person2,ou=company1,c=Sweden"); - - private Map names = new LinkedHashMap(); - - private Iterator keyIterator; + private Iterator keyIterator; public TestVisitor() { names.put(DN_1, 0); @@ -48,7 +46,7 @@ public class LdapTreeBuilderIntegrationTest extends AbstractJUnit4SpringContextT } public void visit(DirContextOperations node, int currentDepth) { - DistinguishedName next = keyIterator.next(); + LdapName next = keyIterator.next(); assertEquals(next, node.getDn()); assertEquals(names.get(next).intValue(), currentDepth); } diff --git a/samples/simple-odm/src/main/java/org/springframework/ldap/odm/sample/SearchForPeople.java b/samples/simple-odm/src/main/java/org/springframework/ldap/odm/sample/SearchForPeople.java index fb88825d..f23a211a 100755 --- a/samples/simple-odm/src/main/java/org/springframework/ldap/odm/sample/SearchForPeople.java +++ b/samples/simple-odm/src/main/java/org/springframework/ldap/odm/sample/SearchForPeople.java @@ -1,13 +1,13 @@ package org.springframework.ldap.odm.sample; -import java.util.List; - -import javax.naming.directory.SearchControls; - import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; -import org.springframework.ldap.core.DistinguishedName; import org.springframework.ldap.odm.core.OdmManager; +import org.springframework.ldap.support.LdapUtils; + +import javax.naming.directory.SearchControls; +import javax.naming.ldap.LdapName; +import java.util.List; // A very simple example - just showing how little code you actually need to write // when using Spring LDAP ODM @@ -16,7 +16,7 @@ public class SearchForPeople { new SearchControls(SearchControls.SUBTREE_SCOPE, 100, 10000, null, true, false); - private static final DistinguishedName baseDn = new DistinguishedName("o=Whoniverse"); + private static final LdapName baseDn = LdapUtils.newLdapName("o=Whoniverse"); private static void print(List personList) { for (SimplePerson person : personList) { diff --git a/samples/simple-odm/src/test/java/org/springframework/ldap/odm/sample/test/TestSearchForPeople.java b/samples/simple-odm/src/test/java/org/springframework/ldap/odm/sample/test/TestSearchForPeople.java index 4aa7f1cb..751d2e1c 100755 --- a/samples/simple-odm/src/test/java/org/springframework/ldap/odm/sample/test/TestSearchForPeople.java +++ b/samples/simple-odm/src/test/java/org/springframework/ldap/odm/sample/test/TestSearchForPeople.java @@ -22,12 +22,13 @@ import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; import org.springframework.core.io.ClassPathResource; -import org.springframework.ldap.core.DistinguishedName; import org.springframework.ldap.core.LdapTemplate; import org.springframework.ldap.core.support.LdapContextSource; import org.springframework.ldap.odm.sample.SearchForPeople; +import org.springframework.ldap.support.LdapUtils; import org.springframework.ldap.test.LdapTestUtils; +import javax.naming.ldap.LdapName; import java.io.ByteArrayOutputStream; import java.io.PrintStream; @@ -35,7 +36,7 @@ import static org.junit.Assert.assertEquals; public class TestSearchForPeople { // Base DN for test data - private static final DistinguishedName baseName = new DistinguishedName("o=Whoniverse"); + private static final LdapName baseName = LdapUtils.newLdapName("o=Whoniverse"); private static final String PRINCIPAL="uid=admin,ou=system"; private static final String CREDENTIALS="secret"; diff --git a/test-support/src/main/java/org/springframework/ldap/test/LdapTestUtils.java b/test-support/src/main/java/org/springframework/ldap/test/LdapTestUtils.java index 5f192ad0..7fc3b952 100644 --- a/test-support/src/main/java/org/springframework/ldap/test/LdapTestUtils.java +++ b/test-support/src/main/java/org/springframework/ldap/test/LdapTestUtils.java @@ -21,7 +21,6 @@ import org.apache.directory.server.core.DefaultDirectoryService; import org.apache.directory.server.protocol.shared.store.LdifFileLoader; import org.springframework.core.io.Resource; import org.springframework.ldap.core.ContextSource; -import org.springframework.ldap.core.DistinguishedName; import org.springframework.ldap.core.LdapAttributes; import org.springframework.ldap.core.support.DefaultDirObjectFactory; import org.springframework.ldap.ldif.parser.LdifParser; @@ -186,8 +185,8 @@ public class LdapTestUtils { enumeration = ctx.listBindings(name); while (enumeration.hasMore()) { Binding element = (Binding) enumeration.next(); - DistinguishedName childName = new DistinguishedName(element.getName()); - childName.prepend((DistinguishedName) name); + Name childName = LdapUtils.newLdapName(element.getName()); + childName = LdapUtils.prepend(childName, name); try { ctx.destroySubcontext(childName); @@ -228,7 +227,7 @@ public class LdapTestUtils { } } - public static void cleanAndSetup(ContextSource contextSource, DistinguishedName rootNode, Resource ldifFile) + public static void cleanAndSetup(ContextSource contextSource, Name rootNode, Resource ldifFile) throws NamingException, IOException { clearSubContexts(contextSource, rootNode); diff --git a/test/integration-tests/src/main/java/org/springframework/ldap/itest/core/support/DummyBaseLdapNameAware.java b/test/integration-tests/src/main/java/org/springframework/ldap/itest/core/support/DummyBaseLdapNameAware.java new file mode 100644 index 00000000..26a896eb --- /dev/null +++ b/test/integration-tests/src/main/java/org/springframework/ldap/itest/core/support/DummyBaseLdapNameAware.java @@ -0,0 +1,37 @@ +/* + * Copyright 2005-2013 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.ldap.itest.core.support; + +import org.springframework.ldap.core.support.BaseLdapNameAware; + +import javax.naming.ldap.LdapName; + +/** + * @author Mattias Hellborg Arthursson + */ +public class DummyBaseLdapNameAware implements BaseLdapNameAware { + private LdapName baseLdapPath; + + @Override + public void setBaseLdapPath(LdapName baseLdapPath) { + this.baseLdapPath = baseLdapPath; + } + + public LdapName getBaseLdapPath() { + return baseLdapPath; + } +} diff --git a/test/integration-tests/src/test/java/org/springframework/ldap/AbstractLdapTemplateIntegrationTest.java b/test/integration-tests/src/test/java/org/springframework/ldap/AbstractLdapTemplateIntegrationTest.java index 0226db41..a2130851 100644 --- a/test/integration-tests/src/test/java/org/springframework/ldap/AbstractLdapTemplateIntegrationTest.java +++ b/test/integration-tests/src/test/java/org/springframework/ldap/AbstractLdapTemplateIntegrationTest.java @@ -22,11 +22,12 @@ import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import org.springframework.ldap.core.ContextSource; -import org.springframework.ldap.core.DistinguishedName; +import org.springframework.ldap.support.LdapUtils; import org.springframework.ldap.test.LdapTestUtils; import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests; +import javax.naming.Name; import javax.naming.NamingException; import java.io.IOException; @@ -46,7 +47,7 @@ public abstract class AbstractLdapTemplateIntegrationTest extends AbstractJUnit4 return new ClassPathResource("/setup_data.ldif"); } - protected DistinguishedName getRoot() { - return DistinguishedName.EMPTY_PATH; + protected Name getRoot() { + return LdapUtils.emptyLdapName(); } } diff --git a/test/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateBindUnbindITest.java b/test/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateBindUnbindITest.java index 5342001b..450102e1 100644 --- a/test/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateBindUnbindITest.java +++ b/test/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateBindUnbindITest.java @@ -1,5 +1,5 @@ /* - * Copyright 2005-2010 the original author or authors. + * Copyright 2005-2013 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,20 +16,20 @@ package org.springframework.ldap; -import static junit.framework.Assert.assertEquals; -import static junit.framework.Assert.assertTrue; -import static junit.framework.Assert.fail; +import org.junit.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.ldap.core.DirContextAdapter; +import org.springframework.ldap.core.LdapTemplate; +import org.springframework.ldap.support.LdapUtils; +import org.springframework.test.context.ContextConfiguration; import javax.naming.directory.Attributes; import javax.naming.directory.BasicAttribute; import javax.naming.directory.BasicAttributes; -import org.junit.Test; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.ldap.core.DirContextAdapter; -import org.springframework.ldap.core.DistinguishedName; -import org.springframework.ldap.core.LdapTemplate; -import org.springframework.test.context.ContextConfiguration; +import static junit.framework.Assert.assertEquals; +import static junit.framework.Assert.assertTrue; +import static junit.framework.Assert.fail; /** * Tests the bind and unbind methods of LdapTemplate. The test methods in this @@ -58,11 +58,11 @@ public class LdapTemplateBindUnbindITest extends } @Test - public void testBindAndUnbindWithAttributesUsingDistinguishedName() { + public void testBindAndUnbindWithAttributesUsingLdapName() { Attributes attributes = setupAttributes(); - tested.bind(new DistinguishedName(DN), null, attributes); + tested.bind(LdapUtils.newLdapName(DN), null, attributes); verifyBoundCorrectData(); - tested.unbind(new DistinguishedName(DN)); + tested.unbind(LdapUtils.newLdapName(DN)); verifyCleanup(); } @@ -81,23 +81,22 @@ public class LdapTemplateBindUnbindITest extends } @Test - public void testBindAndUnbindWithDirContextAdapterUsingDistinguishedName() { + public void testBindAndUnbindWithDirContextAdapterUsingLdapName() { DirContextAdapter adapter = new DirContextAdapter(); adapter.setAttributeValues("objectclass", new String[] { "top", "person" }); adapter.setAttributeValue("cn", "Some Person4"); adapter.setAttributeValue("sn", "Person4"); - tested.bind(new DistinguishedName(DN), adapter, null); + tested.bind(LdapUtils.newLdapName(DN), adapter, null); verifyBoundCorrectData(); - tested.unbind(new DistinguishedName(DN)); + tested.unbind(LdapUtils.newLdapName(DN)); verifyCleanup(); } @Test public void testBindAndUnbindWithDirContextAdapterOnly() { - DirContextAdapter adapter = new DirContextAdapter( - new DistinguishedName(DN)); + DirContextAdapter adapter = new DirContextAdapter(LdapUtils.newLdapName(DN)); adapter.setAttributeValues("objectclass", new String[] { "top", "person" }); adapter.setAttributeValue("cn", "Some Person4"); @@ -111,8 +110,7 @@ public class LdapTemplateBindUnbindITest extends @Test public void testBindAndRebindWithDirContextAdapterOnly() { - DirContextAdapter adapter = new DirContextAdapter( - new DistinguishedName(DN)); + DirContextAdapter adapter = new DirContextAdapter(LdapUtils.newLdapName(DN)); adapter.setAttributeValues("objectclass", new String[] { "top", "person" }); adapter.setAttributeValue("cn", "Some Person4"); diff --git a/test/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateListITest.java b/test/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateListITest.java index 88984c8d..9361771a 100644 --- a/test/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateListITest.java +++ b/test/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateListITest.java @@ -1,5 +1,5 @@ /* - * Copyright 2005-2010 the original author or authors. + * Copyright 2005-2013 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,22 +15,23 @@ */ package org.springframework.ldap; -import static junit.framework.Assert.assertEquals; -import static junit.framework.Assert.assertTrue; - -import java.util.List; - import org.junit.After; import org.junit.Before; import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.ldap.core.DistinguishedName; import org.springframework.ldap.core.LdapTemplate; import org.springframework.ldap.core.support.CountNameClassPairCallbackHandler; import org.springframework.ldap.itest.PersonContextMapper; +import org.springframework.ldap.support.LdapUtils; import org.springframework.ldap.test.AttributeCheckContextMapper; import org.springframework.test.context.ContextConfiguration; +import javax.naming.ldap.LdapName; +import java.util.List; + +import static junit.framework.Assert.assertEquals; +import static junit.framework.Assert.assertTrue; + /** * Tests for LdapTemplate's list methods. * @@ -46,7 +47,7 @@ public class LdapTemplateListITest extends AbstractLdapTemplateIntegrationTest { private static final String BASE_STRING = ""; - private static final DistinguishedName BASE_NAME = new DistinguishedName(BASE_STRING); + private static final LdapName BASE_NAME = LdapUtils.newLdapName(BASE_STRING); private static final String[] ALL_ATTRIBUTES = { "cn", "sn", "description", "telephoneNumber" }; @@ -75,16 +76,14 @@ public class LdapTemplateListITest extends AbstractLdapTemplateIntegrationTest { public void testListBindings_ContextMapper_Name() { contextMapper.setExpectedAttributes(ALL_ATTRIBUTES); contextMapper.setExpectedValues(ALL_VALUES); - DistinguishedName dn = new DistinguishedName("ou=company2,c=Sweden"); - dn.append(BASE_NAME); + LdapName dn = LdapUtils.newLdapName("ou=company2,c=Sweden"); List list = tested.listBindings(dn, contextMapper); assertEquals(1, list.size()); } @Test public void testListBindings_ContextMapper_MapToPersons() { - DistinguishedName dn = new DistinguishedName("ou=company1,c=Sweden"); - dn.append(BASE_NAME); + LdapName dn = LdapUtils.newLdapName("ou=company1,c=Sweden"); List list = tested.listBindings(dn, new PersonContextMapper()); assertEquals(3, list.size()); String personClass = "org.springframework.ldap.itest.Person"; diff --git a/test/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateLookupITest.java b/test/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateLookupITest.java index f8bc3c04..66fa413f 100644 --- a/test/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateLookupITest.java +++ b/test/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateLookupITest.java @@ -21,7 +21,6 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.ldap.core.AttributesMapper; import org.springframework.ldap.core.ContextMapper; import org.springframework.ldap.core.DirContextAdapter; -import org.springframework.ldap.core.DistinguishedName; import org.springframework.ldap.core.LdapTemplate; import org.springframework.ldap.core.support.AbstractContextSource; import org.springframework.ldap.itest.Person; @@ -87,9 +86,9 @@ public class LdapTemplateLookupITest extends AbstractLdapTemplateIntegrationTest } @Test - public void testLookup_AttributesMapper_DistinguishedName() { + public void testLookup_AttributesMapper_LdapName() { AttributesMapper mapper = new PersonAttributesMapper(); - Person person = (Person) tested.lookup(new DistinguishedName("cn=Some Person2, ou=company1,c=Sweden"), mapper); + Person person = (Person) tested.lookup(LdapUtils.newLdapName("cn=Some Person2, ou=company1,c=Sweden"), mapper); assertEquals("Some Person2", person.getFullname()); assertEquals("Person2", person.getLastname()); @@ -136,13 +135,13 @@ public class LdapTemplateLookupITest extends AbstractLdapTemplateIntegrationTest /** * Verifies that only the subset is used when specifying a subset of the - * available attributes as return attributes. Uses DistinguishedName instead + * available attributes as return attributes. Uses LdapName instead * of plain string as name. */ @Test - public void testLookup_ReturnAttributes_AttributesMapper_DistinguishedName() { + public void testLookup_ReturnAttributes_AttributesMapper_LdapName() { AttributesMapper mapper = new SubsetPersonAttributesMapper(); - Person person = (Person) tested.lookup(new DistinguishedName("cn=Some Person2, ou=company1,c=Sweden"), + Person person = (Person) tested.lookup(LdapUtils.newLdapName("cn=Some Person2, ou=company1,c=Sweden"), new String[] { "cn" }, mapper); assertEquals("Some Person2", person.getFullname()); diff --git a/test/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateModifyITest.java b/test/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateModifyITest.java index 298d2b1f..2b22f43b 100644 --- a/test/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateModifyITest.java +++ b/test/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateModifyITest.java @@ -24,6 +24,7 @@ import org.springframework.ldap.core.DirContextAdapter; import org.springframework.ldap.core.DirContextOperations; import org.springframework.ldap.core.DistinguishedName; import org.springframework.ldap.core.LdapTemplate; +import org.springframework.ldap.support.LdapUtils; import org.springframework.test.context.ContextConfiguration; import javax.naming.directory.Attributes; @@ -95,10 +96,10 @@ public class LdapTemplateModifyITest extends AbstractLdapTemplateIntegrationTest } @Test - public void testRebind_Attributes_DistinguishedName() { + public void testRebind_Attributes_LdapName() { Attributes attributes = setupAttributes(); - tested.rebind(new DistinguishedName(PERSON4_DN), null, attributes); + tested.rebind(LdapUtils.newLdapName(PERSON4_DN), null, attributes); verifyBoundCorrectData(); } @@ -231,11 +232,11 @@ public class LdapTemplateModifyITest extends AbstractLdapTemplateIntegrationTest } @Test - public void testModifyAttributes_DistinguishedName() { + public void testModifyAttributes_LdapName() { ModificationItem item = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, new BasicAttribute("description", "Some other description")); - tested.modifyAttributes(new DistinguishedName(PERSON4_DN), new ModificationItem[] { item }); + tested.modifyAttributes(LdapUtils.newLdapName(PERSON4_DN), new ModificationItem[] { item }); verifyBoundCorrectData(); } diff --git a/test/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateNoBaseSuffixITest.java b/test/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateNoBaseSuffixITest.java index cebfbdc7..10f7f1c6 100644 --- a/test/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateNoBaseSuffixITest.java +++ b/test/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateNoBaseSuffixITest.java @@ -19,12 +19,12 @@ package org.springframework.ldap; import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.ldap.core.DirContextAdapter; -import org.springframework.ldap.core.DistinguishedName; import org.springframework.ldap.core.LdapTemplate; import org.springframework.ldap.core.support.CountNameClassPairCallbackHandler; import org.springframework.ldap.support.LdapUtils; import org.springframework.test.context.ContextConfiguration; +import javax.naming.Name; import javax.naming.ldap.LdapName; import static junit.framework.Assert.assertEquals; @@ -46,8 +46,8 @@ public class LdapTemplateNoBaseSuffixITest extends AbstractLdapTemplateIntegrati private LdapTemplate tested; @Override - protected DistinguishedName getRoot() { - return new DistinguishedName("dc=jayway,dc=se"); + protected Name getRoot() { + return LdapUtils.newLdapName("dc=jayway,dc=se"); } /** diff --git a/test/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateRecursiveDeleteITest.java b/test/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateRecursiveDeleteITest.java index d5599f2c..6e19827d 100644 --- a/test/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateRecursiveDeleteITest.java +++ b/test/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateRecursiveDeleteITest.java @@ -1,5 +1,5 @@ /* - * Copyright 2005-2010 the original author or authors. + * Copyright 2005-2013 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,19 +16,20 @@ package org.springframework.ldap; -import static junit.framework.Assert.fail; - -import javax.naming.Name; - import org.junit.After; import org.junit.Before; import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.ldap.core.DirContextAdapter; -import org.springframework.ldap.core.DistinguishedName; import org.springframework.ldap.core.LdapTemplate; +import org.springframework.ldap.support.LdapUtils; import org.springframework.test.context.ContextConfiguration; +import javax.naming.Name; +import javax.naming.ldap.LdapName; + +import static junit.framework.Assert.fail; + /** * Tests the recursive modification methods (unbind and the protected delete * methods) of LdapTemplate. @@ -42,13 +43,13 @@ public class LdapTemplateRecursiveDeleteITest extends AbstractLdapTemplateIntegr @Autowired private LdapTemplate tested; - private static DistinguishedName DN = new DistinguishedName("cn=Some Person5,ou=company1,c=Sweden"); + private static LdapName DN = LdapUtils.newLdapName("cn=Some Person5,ou=company1,c=Sweden"); - private DistinguishedName firstSubDn; + private LdapName firstSubDn; - private DistinguishedName secondSubDn; + private LdapName secondSubDn; - private DistinguishedName leafDn; + private LdapName leafDn; @Before public void prepareTestedInstance() throws Exception { @@ -59,16 +60,18 @@ public class LdapTemplateRecursiveDeleteITest extends AbstractLdapTemplateIntegr adapter.setAttributeValue("description", "Some description"); tested.bind(DN, adapter, null); - firstSubDn = new DistinguishedName("cn=subPerson"); - firstSubDn.prepend(DN); + firstSubDn = LdapUtils.newLdapName("cn=subPerson"); + firstSubDn = LdapUtils.prepend(firstSubDn, DN); + adapter = new DirContextAdapter(); adapter.setAttributeValues("objectclass", new String[] { "top", "person" }); adapter.setAttributeValue("cn", "subPerson"); adapter.setAttributeValue("sn", "subPerson"); adapter.setAttributeValue("description", "Should be recursively deleted"); tested.bind(firstSubDn, adapter, null); - secondSubDn = new DistinguishedName("cn=subPerson2"); - secondSubDn.prepend(DN); + secondSubDn = LdapUtils.newLdapName("cn=subPerson2"); + secondSubDn = LdapUtils.prepend(secondSubDn, DN); + adapter = new DirContextAdapter(); adapter.setAttributeValues("objectclass", new String[] { "top", "person" }); adapter.setAttributeValue("cn", "subPerson2"); @@ -76,8 +79,9 @@ public class LdapTemplateRecursiveDeleteITest extends AbstractLdapTemplateIntegr adapter.setAttributeValue("description", "Should be recursively deleted"); tested.bind(secondSubDn, adapter, null); - leafDn = new DistinguishedName("cn=subSubPerson"); - leafDn.prepend(firstSubDn); + leafDn = LdapUtils.newLdapName("cn=subSubPerson"); + leafDn = LdapUtils.prepend(leafDn, DN); + adapter = new DirContextAdapter(); adapter.setAttributeValues("objectclass", new String[] { "top", "person" }); adapter.setAttributeValue("cn", "subSubPerson"); diff --git a/test/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateRenameITest.java b/test/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateRenameITest.java index 2f169db7..5d0015c6 100644 --- a/test/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateRenameITest.java +++ b/test/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateRenameITest.java @@ -1,5 +1,5 @@ /* - * Copyright 2005-2010 the original author or authors. + * Copyright 2005-2013 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,20 +16,20 @@ package org.springframework.ldap; -import static junit.framework.Assert.assertEquals; -import static junit.framework.Assert.fail; - -import javax.naming.Name; - import org.junit.After; import org.junit.Before; import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.ldap.core.DirContextAdapter; -import org.springframework.ldap.core.DistinguishedName; import org.springframework.ldap.core.LdapTemplate; +import org.springframework.ldap.support.LdapUtils; import org.springframework.test.context.ContextConfiguration; +import javax.naming.Name; + +import static junit.framework.Assert.assertEquals; +import static junit.framework.Assert.fail; + /** * Tests the rename methods of LdapTemplate. * @@ -69,14 +69,14 @@ public class LdapTemplateRenameITest extends AbstractLdapTemplateIntegrationTest public void testRename() { tested.rename(DN, NEWDN); - verifyDeleted(new DistinguishedName(DN)); + verifyDeleted(LdapUtils.newLdapName(DN)); verifyBoundCorrectData(); } @Test - public void testRename_DistinguishedName() throws Exception { - Name oldDn = new DistinguishedName(DN); - Name newDn = new DistinguishedName(NEWDN); + public void testRename_LdapName() throws Exception { + Name oldDn = LdapUtils.newLdapName(DN); + Name newDn = LdapUtils.newLdapName(NEWDN); tested.rename(oldDn, newDn); verifyDeleted(oldDn); diff --git a/test/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateSearchResultITest.java b/test/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateSearchResultITest.java index 053949ed..2239fdf0 100644 --- a/test/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateSearchResultITest.java +++ b/test/integration-tests/src/test/java/org/springframework/ldap/LdapTemplateSearchResultITest.java @@ -1,5 +1,5 @@ /* - * Copyright 2005-2010 the original author or authors. + * Copyright 2005-2013 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,16 +15,6 @@ */ package org.springframework.ldap; -import static junit.framework.Assert.assertEquals; -import static junit.framework.Assert.assertNotNull; -import static junit.framework.Assert.assertTrue; -import static junit.framework.Assert.fail; - -import java.util.List; - -import javax.naming.Name; -import javax.naming.directory.SearchControls; - import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -33,13 +23,22 @@ import org.springframework.dao.EmptyResultDataAccessException; import org.springframework.dao.IncorrectResultSizeDataAccessException; import org.springframework.ldap.core.DirContextAdapter; import org.springframework.ldap.core.DirContextOperations; -import org.springframework.ldap.core.DistinguishedName; import org.springframework.ldap.core.LdapTemplate; import org.springframework.ldap.core.support.AbstractContextMapper; +import org.springframework.ldap.support.LdapUtils; import org.springframework.ldap.test.AttributeCheckAttributesMapper; import org.springframework.ldap.test.AttributeCheckContextMapper; import org.springframework.test.context.ContextConfiguration; +import javax.naming.Name; +import javax.naming.directory.SearchControls; +import java.util.List; + +import static junit.framework.Assert.assertEquals; +import static junit.framework.Assert.assertNotNull; +import static junit.framework.Assert.assertTrue; +import static junit.framework.Assert.fail; + /** * Tests for LdapTemplate's search methods. This test class tests all the * different versions of the search methods except the generic ones covered in @@ -72,7 +71,7 @@ public class LdapTemplateSearchResultITest extends AbstractLdapTemplateIntegrati private static final String FILTER_STRING = "(&(objectclass=person)(sn=Person2))"; - private static final Name BASE_NAME = new DistinguishedName(BASE_STRING); + private static final Name BASE_NAME = LdapUtils.newLdapName(BASE_STRING); @Before public void prepareTestedInstance() throws Exception { diff --git a/test/integration-tests/src/test/java/org/springframework/ldap/control/SupportedControlsITest.java b/test/integration-tests/src/test/java/org/springframework/ldap/control/SupportedControlsITest.java index 469f43f3..b5f84cd2 100644 --- a/test/integration-tests/src/test/java/org/springframework/ldap/control/SupportedControlsITest.java +++ b/test/integration-tests/src/test/java/org/springframework/ldap/control/SupportedControlsITest.java @@ -1,5 +1,5 @@ /* - * Copyright 2005-2010 the original author or authors. + * Copyright 2005-2013 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,19 +16,20 @@ package org.springframework.ldap.control; -import static junit.framework.Assert.assertEquals; - -import java.util.Arrays; - import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.ldap.AbstractLdapTemplateIntegrationTest; import org.springframework.ldap.core.ContextMapper; import org.springframework.ldap.core.DirContextAdapter; -import org.springframework.ldap.core.DistinguishedName; import org.springframework.ldap.core.LdapTemplate; +import org.springframework.ldap.support.LdapUtils; import org.springframework.test.context.ContextConfiguration; +import javax.naming.Name; +import java.util.Arrays; + +import static junit.framework.Assert.assertEquals; + /** * Provides tests that verify that the server supports certain controls. * @@ -43,8 +44,8 @@ public class SupportedControlsITest extends AbstractLdapTemplateIntegrationTest private static final String SUPPORTED_CONTROL = "supportedcontrol"; @Override - protected DistinguishedName getRoot() { - return new DistinguishedName("dc=jayway,dc=se"); + protected Name getRoot() { + return LdapUtils.newLdapName("dc=jayway,dc=se"); } @Test diff --git a/test/integration-tests/src/test/java/org/springframework/ldap/core/simple/SimpleLdapTemplateITest.java b/test/integration-tests/src/test/java/org/springframework/ldap/core/simple/SimpleLdapTemplateITest.java index 6bff01e2..c37f0633 100644 --- a/test/integration-tests/src/test/java/org/springframework/ldap/core/simple/SimpleLdapTemplateITest.java +++ b/test/integration-tests/src/test/java/org/springframework/ldap/core/simple/SimpleLdapTemplateITest.java @@ -1,5 +1,5 @@ /* - * Copyright 2005-2010 the original author or authors. + * Copyright 2005-2013 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,16 +15,6 @@ */ package org.springframework.ldap.core.simple; -import static junit.framework.Assert.assertEquals; -import static junit.framework.Assert.assertTrue; -import static junit.framework.Assert.fail; - -import java.util.List; - -import javax.naming.NamingException; -import javax.naming.directory.DirContext; -import javax.naming.directory.SearchControls; - import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.ldap.AbstractLdapTemplateIntegrationTest; @@ -32,16 +22,26 @@ import org.springframework.ldap.NameNotFoundException; import org.springframework.ldap.core.DirContextAdapter; import org.springframework.ldap.core.DirContextOperations; import org.springframework.ldap.core.DirContextProcessor; -import org.springframework.ldap.core.DistinguishedName; import org.springframework.ldap.filter.AndFilter; import org.springframework.ldap.filter.EqualsFilter; +import org.springframework.ldap.support.LdapUtils; import org.springframework.test.context.ContextConfiguration; +import javax.naming.NamingException; +import javax.naming.directory.DirContext; +import javax.naming.directory.SearchControls; +import javax.naming.ldap.LdapName; +import java.util.List; + +import static junit.framework.Assert.assertEquals; +import static junit.framework.Assert.assertTrue; +import static junit.framework.Assert.fail; + @ContextConfiguration(locations = { "/conf/simpleLdapTemplateTestContext.xml" }) public class SimpleLdapTemplateITest extends AbstractLdapTemplateIntegrationTest { private static String DN_STRING = "cn=Some Person4,ou=company1,c=Sweden"; - private static DistinguishedName DN = new DistinguishedName("cn=Some Person4,ou=company1,c=Sweden"); + private static LdapName DN = LdapUtils.newLdapName("cn=Some Person4,ou=company1,c=Sweden"); @Autowired private SimpleLdapTemplate ldapTemplate; @@ -54,7 +54,7 @@ public class SimpleLdapTemplateITest extends AbstractLdapTemplateIntegrationTest @Test public void testLookupName() { - String result = ldapTemplate.lookup(new DistinguishedName("cn=Some Person,ou=company1,c=Sweden"), + String result = ldapTemplate.lookup(LdapUtils.newLdapName("cn=Some Person,ou=company1,c=Sweden"), new CnContextMapper()); assertEquals("Some Person", result); } @@ -94,7 +94,7 @@ public class SimpleLdapTemplateITest extends AbstractLdapTemplateIntegrationTest searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE); DummyDirContextProcessor processor = new DummyDirContextProcessor(); - List cns = ldapTemplate.search(DistinguishedName.EMPTY_PATH, "(&(objectclass=person)(sn=Person3))", + List cns = ldapTemplate.search(LdapUtils.emptyLdapName(), "(&(objectclass=person)(sn=Person3))", searchControls, new CnContextMapper(), processor); assertEquals(1, cns.size()); @@ -105,7 +105,7 @@ public class SimpleLdapTemplateITest extends AbstractLdapTemplateIntegrationTest @Test public void testSearchName() { - List cns = ldapTemplate.search(DistinguishedName.EMPTY_PATH, "(&(objectclass=person)(sn=Person3))", + List cns = ldapTemplate.search(LdapUtils.emptyLdapName(), "(&(objectclass=person)(sn=Person3))", new CnContextMapper()); assertEquals(1, cns.size()); @@ -134,7 +134,7 @@ public class SimpleLdapTemplateITest extends AbstractLdapTemplateIntegrationTest @Test public void testModifyAttributesName() { - DirContextOperations ctx = ldapTemplate.lookupContext(new DistinguishedName( + DirContextOperations ctx = ldapTemplate.lookupContext(LdapUtils.newLdapName( "cn=Some Person,ou=company1,c=Sweden")); ctx.setAttributeValue("description", "updated description"); diff --git a/test/integration-tests/src/test/java/org/springframework/ldap/core/support/BaseLdapPathBeanPostprocessorITest.java b/test/integration-tests/src/test/java/org/springframework/ldap/core/support/BaseLdapPathBeanPostprocessorITest.java index bb68bebb..b322f7f3 100644 --- a/test/integration-tests/src/test/java/org/springframework/ldap/core/support/BaseLdapPathBeanPostprocessorITest.java +++ b/test/integration-tests/src/test/java/org/springframework/ldap/core/support/BaseLdapPathBeanPostprocessorITest.java @@ -1,5 +1,5 @@ /* - * Copyright 2005-2010 the original author or authors. + * Copyright 2005-2013 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,17 +15,19 @@ */ package org.springframework.ldap.core.support; -import static junit.framework.Assert.assertEquals; -import static junit.framework.Assert.assertNotNull; -import static junit.framework.Assert.assertTrue; -import static junit.framework.Assert.fail; - import org.junit.Test; import org.springframework.beans.factory.BeanCreationException; import org.springframework.beans.factory.NoSuchBeanDefinitionException; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.ldap.core.DistinguishedName; +import org.springframework.ldap.itest.core.support.DummyBaseLdapNameAware; import org.springframework.ldap.itest.core.support.DummyBaseLdapPathAware; +import org.springframework.ldap.support.LdapUtils; + +import static junit.framework.Assert.assertEquals; +import static junit.framework.Assert.assertNotNull; +import static junit.framework.Assert.assertTrue; +import static junit.framework.Assert.fail; /** * Integration tests for {@link BaseLdapPathBeanPostProcessor}. @@ -38,12 +40,15 @@ public class BaseLdapPathBeanPostprocessorITest { public void testPostProcessBeforeInitialization() throws Exception { ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext( "/conf/baseLdapPathPostProcessorTestContext.xml"); - DummyBaseLdapPathAware tested = (DummyBaseLdapPathAware) ctx.getBean("dummyBaseContextAware"); + DummyBaseLdapPathAware tested = ctx.getBean(DummyBaseLdapPathAware.class); DistinguishedName base = tested.getBase(); assertNotNull(base); assertEquals(new DistinguishedName("dc=jayway,dc=se"), base); - } + + DummyBaseLdapNameAware otherTested = ctx.getBean(DummyBaseLdapNameAware.class); + assertEquals(LdapUtils.newLdapName("dc=jayway,dc=se"), otherTested.getBaseLdapPath()); + } @Test public void testPostProcessBeforeInitializationMultipleContextSources() throws Exception { diff --git a/test/integration-tests/src/test/java/org/springframework/ldap/filter/HardcodedFilterIntegrationTest.java b/test/integration-tests/src/test/java/org/springframework/ldap/filter/HardcodedFilterIntegrationTest.java index d880081e..a1d2b406 100644 --- a/test/integration-tests/src/test/java/org/springframework/ldap/filter/HardcodedFilterIntegrationTest.java +++ b/test/integration-tests/src/test/java/org/springframework/ldap/filter/HardcodedFilterIntegrationTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2005-2010 the original author or authors. + * Copyright 2005-2013 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,18 +15,18 @@ */ package org.springframework.ldap.filter; -import static junit.framework.Assert.assertEquals; -import static junit.framework.Assert.assertTrue; - import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.ldap.AbstractLdapTemplateIntegrationTest; -import org.springframework.ldap.core.DistinguishedName; import org.springframework.ldap.core.LdapTemplate; import org.springframework.ldap.core.support.CountNameClassPairCallbackHandler; import org.springframework.ldap.itest.filter.DummyFilterConsumer; +import org.springframework.ldap.support.LdapUtils; import org.springframework.test.context.ContextConfiguration; +import static junit.framework.Assert.assertEquals; +import static junit.framework.Assert.assertTrue; + /** * @author Mattias Hellborg Arthursson */ @@ -50,7 +50,7 @@ public class HardcodedFilterIntegrationTest extends AbstractLdapTemplateIntegrat public void verifyThatWildcardsAreUnescaped() { HardcodedFilter filter = new HardcodedFilter("cn=Some*"); CountNameClassPairCallbackHandler handler = new CountNameClassPairCallbackHandler(); - ldapTemplate.search(DistinguishedName.EMPTY_PATH, filter.encode(), handler); + ldapTemplate.search(LdapUtils.emptyLdapName(), filter.encode(), handler); int hits = handler.getNoOfRows(); assertTrue("expected more than one hit, got " + hits, hits > 1); } diff --git a/test/integration-tests/src/test/java/org/springframework/ldap/transaction/compensating/manager/ContextSourceAndDataSourceTransactionManagerIntegrationTest.java b/test/integration-tests/src/test/java/org/springframework/ldap/transaction/compensating/manager/ContextSourceAndDataSourceTransactionManagerIntegrationTest.java index af2b2205..843f6215 100644 --- a/test/integration-tests/src/test/java/org/springframework/ldap/transaction/compensating/manager/ContextSourceAndDataSourceTransactionManagerIntegrationTest.java +++ b/test/integration-tests/src/test/java/org/springframework/ldap/transaction/compensating/manager/ContextSourceAndDataSourceTransactionManagerIntegrationTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2005-2010 the original author or authors. + * Copyright 2005-2013 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,17 +15,6 @@ */ package org.springframework.ldap.transaction.compensating.manager; -import static junit.framework.Assert.assertEquals; -import static junit.framework.Assert.assertNotNull; -import static junit.framework.Assert.assertTrue; -import static junit.framework.Assert.fail; - -import java.sql.ResultSet; -import java.sql.SQLException; - -import javax.naming.NamingException; -import javax.naming.directory.Attributes; - import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.junit.After; @@ -39,13 +28,22 @@ import org.springframework.jdbc.core.RowMapper; import org.springframework.ldap.AbstractLdapTemplateIntegrationTest; import org.springframework.ldap.NameNotFoundException; import org.springframework.ldap.core.AttributesMapper; -import org.springframework.ldap.core.DistinguishedName; import org.springframework.ldap.core.LdapTemplate; import org.springframework.ldap.itest.transaction.compensating.manager.DummyDao; import org.springframework.ldap.itest.transaction.compensating.manager.DummyException; import org.springframework.test.context.ContextConfiguration; import org.springframework.transaction.support.TransactionSynchronizationManager; +import javax.naming.NamingException; +import javax.naming.directory.Attributes; +import java.sql.ResultSet; +import java.sql.SQLException; + +import static junit.framework.Assert.assertEquals; +import static junit.framework.Assert.assertNotNull; +import static junit.framework.Assert.assertTrue; +import static junit.framework.Assert.fail; + /** * Integration tests for {@link ContextSourceAndDataSourceTransactionManager}. * diff --git a/test/integration-tests/src/test/resources/conf/baseLdapPathPostProcessorTestContext.xml b/test/integration-tests/src/test/resources/conf/baseLdapPathPostProcessorTestContext.xml index 7213fb41..545594e7 100644 --- a/test/integration-tests/src/test/resources/conf/baseLdapPathPostProcessorTestContext.xml +++ b/test/integration-tests/src/test/resources/conf/baseLdapPathPostProcessorTestContext.xml @@ -1,7 +1,7 @@ @@ -20,9 +20,11 @@ - - + + + +