/** * 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(); }
/** * Remove the supplied path from the beginning of this * <code>DistinguishedName</code> if this instance starts with * <code>path</code>. Useful for stripping base path suffix from a * <code>DistinguishedName</code>. * * @param path the path to remove from the beginning of this instance. */ public void removeFirst(Name path) { if (path != null && this.startsWith(path)) { for (int i = 0; i < path.size(); i++) { this.removeFirst(); } } }
/** * Remove the supplied path from the beginning of this * <code>DistinguishedName</code> if this instance starts with * <code>path</code>. Useful for stripping base path suffix from a * <code>DistinguishedName</code>. * * @param path the path to remove from the beginning of this instance. */ public void removeFirst(Name path) { if (path != null && this.startsWith(path)) { for (int i = 0; i < path.size(); i++) { this.removeFirst(); } } }
/** * Remove the supplied path from the beginning of this * <code>DistinguishedName</code> if this instance starts with * <code>path</code>. Useful for stripping base path suffix from a * <code>DistinguishedName</code>. * * @param path the path to remove from the beginning of this instance. */ public void removeFirst(Name path) { if (path != null && this.startsWith(path)) { for (int i = 0; i < path.size(); i++) { this.removeFirst(); } } }
/** * Remove the supplied path from the beginning of this * <code>DistinguishedName</code> if this instance starts with * <code>path</code>. Useful for stripping base path suffix from a * <code>DistinguishedName</code>. * * @param path the path to remove from the beginning of this instance. */ public void removeFirst(Name path) { if (path != null && this.startsWith(path)) { for (int i = 0; i < path.size(); i++) { this.removeFirst(); } } }
protected DistinguishedName getRelativeDN(String fullDN) { DistinguishedName principalDN = new DistinguishedName(fullDN); if (configuration.getBaseDN().size() > 0) { principalDN.removeFirst(configuration.getBaseDN()); } return principalDN; }
public void add(Entity entity, Entity parentEntity) throws SecurityException { if (parentEntity == null || parentEntity.getInternalId() == null) { throw new SecurityException(SecurityException.UNEXPECTED.create(getClass().getName(), "add(Entity entity, Entity parentEntity)", "Provided parent entity is null or has no internal ID.")); } DistinguishedName parentDn = new DistinguishedName(parentEntity.getInternalId()); parentDn.removeFirst(new DistinguishedName(configuration.getBaseDN())); internalAdd(entity, parentDn); }
/** * 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; } }
/** * 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(); }
name.removeFirst(new DistinguishedName(ctx.getNameInNamespace())); Attribute namingAttr = new BasicAttribute("userPassword", newPassword); ModificationItem[] items = new ModificationItem[1];
private static void loadLdif(DirContext context, Resource ldifFile) throws IOException { try { DistinguishedName baseDn = (DistinguishedName) context.getEnvironment() .get(DefaultDirObjectFactory.JNDI_ENV_BASE_PATH_KEY); LdifParser parser = new LdifParser(ldifFile); parser.open(); while (parser.hasMoreRecords()) { LdapAttributes record = parser.getRecord(); DistinguishedName dn = record.getDN(); if (baseDn != null) { dn.removeFirst(baseDn); } context.bind(dn, null, record); } } catch (NamingException e) { throw new RuntimeException("Failed to populate LDIF", e); } }