/** * Obtains the part of a DN relative to a supplied base context. * <p> * If the DN is "cn=bob,ou=people,dc=springframework,dc=org" and the base context name * is "ou=people,dc=springframework,dc=org" it would return "cn=bob". * </p> * * @param fullDn the DN * @param baseCtx the context to work out the name relative to. * * @return the * * @throws NamingException any exceptions thrown by the context are propagated. */ public static String getRelativeName(String fullDn, Context baseCtx) throws NamingException { String baseDn = baseCtx.getNameInNamespace(); if (baseDn.length() == 0) { return fullDn; } DistinguishedName base = new DistinguishedName(baseDn); DistinguishedName full = new DistinguishedName(fullDn); if (base.equals(full)) { return ""; } Assert.isTrue(full.startsWith(base), "Full DN does not start with base DN"); full.removeFirst(base); return full.toString(); }
/** * Calculate the relative DN based on the current base. For example, * if the base is "ou=example" and the fullDNString is * "cn=myuser,ou=example", then the returned DN will be "cn=myuser". * * Note: if constructor argument {@code base} was {@code null} then this will throw an exception. * * @param fullDNString the full distinguished name, not {@code null} * @return the corresponding relative distinguished name, not {@code null} */ public DistinguishedName relativeDN(String fullDNString) { DistinguishedName full = new DistinguishedName(fullDNString); DistinguishedName base = new DistinguishedName(this.base); if (this.base.trim().length() == 0) { return full; } else if (base.equals(full)) { return new DistinguishedName(""); } else if (!full.startsWith(base)) { throw new InternalException(String.format( "Full DN (%s) does not start with base DN (%s)", full, base)); } else { full.removeFirst(base); return full; } }
@Test public void testUnmodifiableDistinguishedNameEqualsIdenticalMutableOne() throws Exception { DistinguishedName immutable = DistinguishedName.immutableDistinguishedName("cn=john doe"); DistinguishedName mutable = new DistinguishedName("cn=john doe"); assertThat(immutable.equals(mutable)).isTrue(); }
/** * Obtains the part of a DN relative to a supplied base context. * <p>If the DN is "cn=bob,ou=people,dc=springframework,dc=org" and the base context name is * "ou=people,dc=springframework,dc=org" it would return "cn=bob". * </p> * * @param fullDn the DN * @param baseCtx the context to work out the name relative to. * * @return the * * @throws NamingException any exceptions thrown by the context are propagated. */ public static String getRelativeName(String fullDn, Context baseCtx) throws NamingException { String baseDn = baseCtx.getNameInNamespace(); if (baseDn.length() == 0) { return fullDn; } DistinguishedName base = new DistinguishedName(baseDn); DistinguishedName full = new DistinguishedName(fullDn); if(base.equals(full)) { return ""; } Assert.isTrue(full.startsWith(base), "Full DN does not start with base DN"); full.removeFirst(base); return full.toString(); }
/** * Obtains the part of a DN relative to a supplied base context. * <p> * If the DN is "cn=bob,ou=people,dc=springframework,dc=org" and the base context name * is "ou=people,dc=springframework,dc=org" it would return "cn=bob". * </p> * * @param fullDn the DN * @param baseCtx the context to work out the name relative to. * * @return the * * @throws NamingException any exceptions thrown by the context are propagated. */ public static String getRelativeName(String fullDn, Context baseCtx) throws NamingException { String baseDn = baseCtx.getNameInNamespace(); if (baseDn.length() == 0) { return fullDn; } DistinguishedName base = new DistinguishedName(baseDn); DistinguishedName full = new DistinguishedName(fullDn); if (base.equals(full)) { return ""; } Assert.isTrue(full.startsWith(base), "Full DN does not start with base DN"); full.removeFirst(base); return full.toString(); }
@Test public void testClone() { DistinguishedName path1 = new DistinguishedName("cn=john.doe, OU=Users,OU=Some company,C=SE"); DistinguishedName path2 = (DistinguishedName) path1.clone(); assertThat(path2).as("Should be equal").isEqualTo(path1); path2.removeFirst(); assertThat(path1.equals(path2)).isFalse(); }