diff --git a/spring-ldap/src/main/java/org/springframework/ldap/core/DistinguishedName.java b/spring-ldap/src/main/java/org/springframework/ldap/core/DistinguishedName.java index f2d8b1eb..4506ef5e 100644 --- a/spring-ldap/src/main/java/org/springframework/ldap/core/DistinguishedName.java +++ b/spring-ldap/src/main/java/org/springframework/ldap/core/DistinguishedName.java @@ -43,7 +43,7 @@ import org.springframework.ldap.support.ListComparator; * the most significant part be in position 0. *
* Example: - * + * *
* DistinguishedName path = new DistinguishedName();
* path.addLast("uid", person.getUid());
@@ -176,6 +177,44 @@ public class DistinguishedName implements Name {
return (LdapRdn) names.get(index);
}
+ /**
+ * Get the LdapRdn with the specified key. If there are several Rdns with
+ * the same key, the first one found (in order of significance) will be
+ * returned.
+ *
+ * @param key
+ * Attribute name of the LdapRdn to retrieve.
+ * @return the LdapRdn with the requested key.
+ * @throws IllegalArgumentException
+ * if no Rdn matches the given key.
+ */
+ public LdapRdn getLdapRdn(String key) {
+ for (Iterator iter = names.iterator(); iter.hasNext();) {
+ LdapRdn rdn = (LdapRdn) iter.next();
+ if (StringUtils.equals(rdn.getKey(), key)) {
+ return rdn;
+ }
+ }
+
+ throw new IllegalArgumentException("No Rdn with the requested key: '"
+ + key + "'");
+ }
+
+ /**
+ * Get the value of the RdnComponent with the specified key (Attribute
+ * value). If there are several Rdns with the same key, the value of the
+ * first one found (in order of significance) will be returned.
+ *
+ * @param key
+ * Attribute name of the LdapRdn to retrieve.
+ * @return the value.
+ * @throws IllegalArgumentException
+ * if no Rdn matches the given key.
+ */
+ public String getValue(String key) {
+ return getLdapRdn(key).getValue();
+ }
+
/**
* Get the name list.
*
diff --git a/spring-ldap/src/main/java/org/springframework/ldap/core/LdapRdn.java b/spring-ldap/src/main/java/org/springframework/ldap/core/LdapRdn.java
index 12fe63e9..1f1c6639 100644
--- a/spring-ldap/src/main/java/org/springframework/ldap/core/LdapRdn.java
+++ b/spring-ldap/src/main/java/org/springframework/ldap/core/LdapRdn.java
@@ -22,6 +22,7 @@ import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
+import org.apache.commons.lang.StringUtils;
import org.springframework.ldap.BadLdapGrammarException;
import org.springframework.ldap.support.ListComparator;
@@ -195,4 +196,54 @@ public class LdapRdn implements Serializable, Comparable {
public String toString() {
return getLdapEncoded();
}
+
+ /**
+ * Get the value of this LdapRdn. Note that if this Rdn is multi-value the
+ * first value will be returned. E.g. for the Rdn
+ * cn=john doe+sn=doe, the return value would be
+ * john doe.
+ *
+ * @return the (first) value of this LdapRdn.
+ * @throws IndexOutOfBoundsException
+ * if there is no components in this Rdn.
+ */
+ public String getValue() {
+ return getComponent().getValue();
+ }
+
+ /**
+ * Get the key of this LdapRdn. Note that if this Rdn is multi-value the
+ * first key will be returned. E.g. for the Rdn
+ * cn=john doe+sn=doe, the return value would be
+ * cn.
+ *
+ * @return the (first) key of this LdapRdn.
+ * @throws IndexOutOfBoundsException
+ * if there is no components in this Rdn.
+ */
+ public String getKey() {
+ return getComponent().getKey();
+ }
+
+ /**
+ * Get the value of the LdapComponent with the specified key (Attribute
+ * name).
+ *
+ * @param key
+ * the key
+ * @return the value.
+ * @throws IllegalArgumentException
+ * if there is no component with the specified key.
+ */
+ public String getValue(String key) {
+ for (Iterator iter = components.iterator(); iter.hasNext();) {
+ LdapRdnComponent component = (LdapRdnComponent) iter.next();
+ if (StringUtils.equals(component.getKey(), key)) {
+ return component.getValue();
+ }
+ }
+
+ throw new IllegalArgumentException("No RdnComponent with the key "
+ + key);
+ }
}
\ No newline at end of file
diff --git a/spring-ldap/src/main/java/org/springframework/ldap/core/LdapRdnComponent.java b/spring-ldap/src/main/java/org/springframework/ldap/core/LdapRdnComponent.java
index 6993096d..bdae1d62 100644
--- a/spring-ldap/src/main/java/org/springframework/ldap/core/LdapRdnComponent.java
+++ b/spring-ldap/src/main/java/org/springframework/ldap/core/LdapRdnComponent.java
@@ -75,18 +75,40 @@ public class LdapRdnComponent implements Comparable, Serializable {
}
}
+ /**
+ * Get the key (Attribute name) of this component.
+ *
+ * @return the key.
+ */
public String getKey() {
return key;
}
+ /**
+ * Set the key (Attribute name) of this component.
+ *
+ * @param key
+ * the key.
+ */
public void setKey(String key) {
this.key = key;
}
+ /**
+ * Get the (Attribute) value of this component.
+ *
+ * @return the value.
+ */
public String getValue() {
return value;
}
+ /**
+ * Set the (Attribute) value of this component.
+ *
+ * @param value
+ * the value.
+ */
public void setValue(String value) {
this.value = value;
}
diff --git a/spring-ldap/src/test/java/org/springframework/ldap/core/DistinguishedNameTest.java b/spring-ldap/src/test/java/org/springframework/ldap/core/DistinguishedNameTest.java
index 39422022..13f5fa2c 100644
--- a/spring-ldap/src/test/java/org/springframework/ldap/core/DistinguishedNameTest.java
+++ b/spring-ldap/src/test/java/org/springframework/ldap/core/DistinguishedNameTest.java
@@ -448,6 +448,43 @@ public class DistinguishedNameTest extends TestCase {
assertTrue(result < 0);
}
+ public void testGetLdapRdnForKey() throws Exception {
+ DistinguishedName dn = new DistinguishedName(
+ "cn=john doe, ou=Some company, c=SE");
+ LdapRdn ldapRdn = dn.getLdapRdn("ou");
+ assertEquals(new LdapRdn("ou=Some company"), ldapRdn);
+ }
+
+ public void testGetLdapRdnForKeyNoMatchingKeyThrowsException()
+ throws Exception {
+ DistinguishedName dn = new DistinguishedName(
+ "cn=john doe, ou=Some company, c=SE");
+ try {
+ dn.getLdapRdn("nosuchkey");
+ fail("IllegalArgumentException expected");
+ } catch (IllegalArgumentException expected) {
+ assertTrue(true);
+ }
+ }
+
+ public void testGetValue() throws Exception {
+ DistinguishedName dn = new DistinguishedName(
+ "cn=john doe, ou=Some company, c=SE");
+ String value = dn.getValue("ou");
+ assertEquals("Some company", value);
+ }
+
+ public void testGetValueNoMatchingKeyThrowsException() throws Exception {
+ DistinguishedName dn = new DistinguishedName(
+ "cn=john doe, ou=Some company, c=SE");
+ try {
+ dn.getValue("nosuchkey");
+ fail("IllegalArgumentException expected");
+ } catch (IllegalArgumentException expected) {
+ assertTrue(true);
+ }
+ }
+
public void test_longDN() throws InvalidNameException {
DistinguishedName name = new DistinguishedName("");
assertNotNull(name);
diff --git a/spring-ldap/src/test/java/org/springframework/ldap/core/LdapRdnTest.java b/spring-ldap/src/test/java/org/springframework/ldap/core/LdapRdnTest.java
index 7a4120e7..b228dc40 100644
--- a/spring-ldap/src/test/java/org/springframework/ldap/core/LdapRdnTest.java
+++ b/spring-ldap/src/test/java/org/springframework/ldap/core/LdapRdnTest.java
@@ -19,7 +19,6 @@ package org.springframework.ldap.core;
import junit.framework.TestCase;
import org.springframework.ldap.BadLdapGrammarException;
-import org.springframework.ldap.core.LdapRdn;
import com.gargoylesoftware.base.testing.EqualsTester;
@@ -37,6 +36,8 @@ public class LdapRdnTest extends TestCase {
assertEquals("foo", rdn.getComponent().getKey());
assertEquals("bar", rdn.getComponent().getValue());
assertEquals("foo=bar", rdn.getComponent().getLdapEncoded());
+ assertEquals("foo", rdn.getKey());
+ assertEquals("bar", rdn.getValue());
}
public void testLdapRdn_parse_spaces() {
@@ -144,6 +145,30 @@ public class LdapRdnTest extends TestCase {
assertEquals("cn=John Doe", rdn.getComponent(0).encodeLdap());
assertEquals("sn=Doe", rdn.getComponent(1).encodeLdap());
assertEquals("cn=John Doe+sn=Doe", rdn.getLdapEncoded());
+ assertEquals("cn", rdn.getKey());
+ assertEquals("John Doe", rdn.getValue());
+ assertEquals("John Doe", rdn.getValue("cn"));
+ assertEquals("Doe", rdn.getValue("sn"));
+ }
+
+ public void testGetValueNoKeyWithCorrectValue() {
+ LdapRdn tested = new LdapRdn("cn=john doe");
+ try {
+ tested.getValue("sn");
+ fail("IllegalArgumentException expected");
+ } catch (IllegalArgumentException expected) {
+ assertTrue(true);
+ }
+ }
+
+ public void testGetValueNoComponents() {
+ LdapRdn tested = new LdapRdn();
+ try {
+ tested.getValue("sn");
+ fail("IllegalArgumentException expected");
+ } catch (IllegalArgumentException expected) {
+ assertTrue(true);
+ }
}
public void testEquals() throws Exception {